OSDN Git Service

Unify net.go's json response (#136)
authorLiu-Cheng Xu <xuliuchengxlc@gmail.com>
Tue, 28 Nov 2017 02:10:37 +0000 (10:10 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 28 Nov 2017 02:10:37 +0000 (10:10 +0800)
* Unify net.go's json response

* Add TODO

blockchain/reactor.go
cmd/cobra/commands/net.go

index 32e35ed..6af761e 100755 (executable)
@@ -50,12 +50,14 @@ const (
        ERROR   = "error"
 )
 
+// Response describes the response standard.
 type Response struct {
        Status string
        Msg    string
        Data   []string
 }
 
+// DefaultRawResponse is used as the default response when fail to get data.
 var DefaultRawResponse = []byte(`{"Status":"error","Msg":"Unable to get data","Data":null}`)
 
 //BlockchainReactor handles long-term catchup syncing.
@@ -78,22 +80,6 @@ type BlockchainReactor struct {
        evsw          types.EventSwitch
 }
 
-const (
-       SUCCESS = "success"
-       FAIL    = "fail"
-       ERROR   = "error"
-)
-
-// DefaultRawResponse is used as the default response when fail to get data
-var DefaultRawResponse = []byte(`{"Status":"error","Msg":"Unable to get data","Data":null}`)
-
-// Response describes the response standard.
-type Response struct {
-       Status string   `json:"status"`
-       Msg    string   `json:"msg"`
-       Data   []string `json:"data"`
-}
-
 func batchRecover(ctx context.Context, v *interface{}) {
        if r := recover(); r != nil {
                var err error
@@ -534,16 +520,20 @@ func (bcR *BlockchainReactor) BroadcastTransaction(tx *legacy.Tx) error {
        return nil
 }
 
-func (bcr *BlockchainReactor) isNetListening() bool {
-       return bcr.sw.IsListening()
+func (bcr *BlockchainReactor) isNetListening() []byte {
+       data := []string{strconv.FormatBool(bcr.sw.IsListening())}
+       return resWrapper(data)
 }
 
-func (bcr *BlockchainReactor) peerCount() int {
-       return len(bcr.sw.Peers().List())
+func (bcr *BlockchainReactor) peerCount() []byte {
+       // TODO: use key-value instead of bare value
+       data := []string{strconv.FormatInt(int64(len(bcr.sw.Peers().List())), 16)}
+       return resWrapper(data)
 }
 
-func (bcr *BlockchainReactor) isNetSyncing() bool {
-       return bcr.blockKeeper.IsCaughtUp()
+func (bcr *BlockchainReactor) isNetSyncing() []byte {
+       data := []string{strconv.FormatBool(bcr.blockKeeper.IsCaughtUp())}
+       return resWrapper(data)
 }
 
 func (bcr *BlockchainReactor) getBlockTransactionsCountByHeight(height uint64) []byte {
index 4e198b2..7865b09 100644 (file)
@@ -2,9 +2,13 @@ package commands
 
 import (
        "context"
+       "encoding/json"
+       "strconv"
 
        "github.com/spf13/cobra"
        jww "github.com/spf13/jwalterweatherman"
+
+       "github.com/bytom/blockchain"
 )
 
 var netInfoCmd = &cobra.Command{
@@ -17,15 +21,34 @@ var netInfoCmd = &cobra.Command{
                jww.FEEDBACK.Printf("net info: %v\n", response)
        },
 }
-
 var netListeningCmd = &cobra.Command{
        Use:   "net-listening",
        Short: "If client is actively listening for network connections",
        Run: func(cmd *cobra.Command, args []string) {
-               var response interface{}
+               var rawResponse []byte
+               var response blockchain.Response
+
                client := mustRPCClient()
-               client.Call(context.Background(), "/net-listening", nil, &response)
-               jww.FEEDBACK.Printf("net listening: %v\n", response)
+               client.Call(context.Background(), "/net-listening", nil, &rawResponse)
+
+               if err := json.Unmarshal(rawResponse, &response); err != nil {
+                       jww.ERROR.Println(err)
+                       return
+               }
+
+               // TODO: code reuse
+               if response.Status == blockchain.SUCCESS {
+                       data := response.Data
+                       res, err := strconv.ParseBool(data[0])
+                       if err != nil {
+                               jww.ERROR.Println("Fail to parse response data")
+                               return
+                       }
+                       jww.FEEDBACK.Printf("net listening: %v\n", res)
+                       return
+               }
+               jww.ERROR.Println(response.Msg)
+
        },
 }
 
@@ -33,10 +56,29 @@ var peerCountCmd = &cobra.Command{
        Use:   "peer-count",
        Short: "Number of peers currently connected to the client",
        Run: func(cmd *cobra.Command, args []string) {
-               var response interface{}
+               var rawResponse []byte
+               var response blockchain.Response
+
                client := mustRPCClient()
-               client.Call(context.Background(), "/peer-count", nil, &response)
-               jww.FEEDBACK.Printf("peer count: %v\n", response)
+               client.Call(context.Background(), "/peer-count", nil, &rawResponse)
+
+               if err := json.Unmarshal(rawResponse, &response); err != nil {
+                       jww.ERROR.Println(err)
+                       return
+               }
+
+               if response.Status == blockchain.SUCCESS {
+                       data := response.Data
+                       i, err := strconv.ParseInt(data[0], 16, 64)
+                       if err != nil {
+                               jww.ERROR.Println("Fail to parse response data")
+                               return
+                       }
+                       jww.FEEDBACK.Printf("peer count: %v\n", i)
+                       return
+               }
+               jww.ERROR.Println(response.Msg)
+
        },
 }
 
@@ -44,9 +86,27 @@ var netSyncingCmd = &cobra.Command{
        Use:   "net-syncing",
        Short: "If the network is still syncing",
        Run: func(cmd *cobra.Command, args []string) {
-               var response interface{}
+               var rawResponse []byte
+               var response blockchain.Response
+
                client := mustRPCClient()
-               client.Call(context.Background(), "/net-syncing", nil, &response)
-               jww.FEEDBACK.Printf("net syncing: %v\n", response)
+               client.Call(context.Background(), "/net-syncing", nil, &rawResponse)
+
+               if err := json.Unmarshal(rawResponse, &response); err != nil {
+                       jww.ERROR.Println(err)
+                       return
+               }
+
+               if response.Status == blockchain.SUCCESS {
+                       data := response.Data
+                       res, err := strconv.ParseBool(data[0])
+                       if err != nil {
+                               jww.ERROR.Println("Fail to parse response data")
+                               return
+                       }
+                       jww.FEEDBACK.Printf("net syncing: %v\n", res)
+                       return
+               }
+               jww.ERROR.Println(response.Msg)
        },
 }