github.com/turingchain2020/turingchain@v1.1.21/system/dapp/commands/net.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package commands 6 7 import ( 8 "github.com/turingchain2020/turingchain/types" 9 "github.com/spf13/cobra" 10 11 "github.com/turingchain2020/turingchain/rpc/jsonclient" 12 rpctypes "github.com/turingchain2020/turingchain/rpc/types" 13 ) 14 15 // NetCmd net command 16 func NetCmd() *cobra.Command { 17 cmd := &cobra.Command{ 18 Use: "net", 19 Short: "Net operation", 20 Args: cobra.MinimumNArgs(1), 21 } 22 23 cmd.AddCommand( 24 GetPeerInfoCmd(), 25 IsClockSyncCmd(), 26 IsSyncCmd(), 27 GetNetInfoCmd(), 28 GetFatalFailureCmd(), 29 GetTimeStausCmd(), 30 NetProtocolsCmd(), 31 ) 32 33 return cmd 34 } 35 36 // GetPeerInfoCmd get peers connected info 37 func GetPeerInfoCmd() *cobra.Command { 38 cmd := &cobra.Command{ 39 Use: "peer", 40 Short: "Get remote peer nodes", 41 Run: peerInfo, 42 } 43 cmd.Flags().StringP("type", "t", "", "p2p type, gossip or dht") 44 return cmd 45 } 46 47 func peerInfo(cmd *cobra.Command, args []string) { 48 rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") 49 var res rpctypes.PeerList 50 p2pty, _ := cmd.Flags().GetString("type") 51 req := types.P2PGetPeerReq{P2PType: p2pty} 52 ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetPeerInfo", req, &res) 53 ctx.Run() 54 } 55 56 // IsClockSyncCmd get ntp clock sync status 57 func IsClockSyncCmd() *cobra.Command { 58 cmd := &cobra.Command{ 59 Use: "is_clock_sync", 60 Short: "Get ntp clock synchronization status", 61 Run: isClockSync, 62 } 63 return cmd 64 } 65 66 func isClockSync(cmd *cobra.Command, args []string) { 67 rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") 68 var res bool 69 ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.IsNtpClockSync", nil, &res) 70 ctx.Run() 71 } 72 73 // IsSyncCmd get local db sync status 74 func IsSyncCmd() *cobra.Command { 75 cmd := &cobra.Command{ 76 Use: "is_sync", 77 Short: "Get blockchain synchronization status", 78 Run: isSync, 79 } 80 return cmd 81 } 82 83 func isSync(cmd *cobra.Command, args []string) { 84 rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") 85 var res bool 86 ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.IsSync", nil, &res) 87 ctx.Run() 88 } 89 90 // GetNetInfoCmd get net info 91 func GetNetInfoCmd() *cobra.Command { 92 cmd := &cobra.Command{ 93 Use: "info", 94 Short: "Get net information", 95 Run: netInfo, 96 } 97 cmd.Flags().StringP("type", "t", "", "p2p type, gossip or dht") 98 return cmd 99 } 100 101 func netInfo(cmd *cobra.Command, args []string) { 102 rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") 103 p2pty, _ := cmd.Flags().GetString("type") 104 req := types.P2PGetNetInfoReq{P2PType: p2pty} 105 var res rpctypes.NodeNetinfo 106 ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetNetInfo", req, &res) 107 ctx.Run() 108 } 109 110 //NetProtocolsCmd get all prototols netinfo 111 func NetProtocolsCmd() *cobra.Command { 112 cmd := &cobra.Command{ 113 Use: "protocols", 114 Short: "Get netprotocols information", 115 Run: netProtocols, 116 } 117 cmd.Flags().StringP("type", "t", "", "p2p type, gossip or dht") 118 return cmd 119 } 120 121 func netProtocols(cmd *cobra.Command, args []string) { 122 rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") 123 var res types.NetProtocolInfos 124 ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.NetProtocols", &types.ReqNil{}, &res) 125 ctx.Run() 126 } 127 128 // GetFatalFailureCmd get FatalFailure 129 func GetFatalFailureCmd() *cobra.Command { 130 cmd := &cobra.Command{ 131 Use: "fault", 132 Short: "Get system fault", 133 Run: fatalFailure, 134 } 135 return cmd 136 } 137 138 func fatalFailure(cmd *cobra.Command, args []string) { 139 rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") 140 var res int64 141 ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetFatalFailure", nil, &res) 142 ctx.Run() 143 } 144 145 // GetTimeStausCmd get time status 146 func GetTimeStausCmd() *cobra.Command { 147 cmd := &cobra.Command{ 148 Use: "time", 149 Short: "Get time status", 150 Run: timestatus, 151 } 152 return cmd 153 } 154 155 func timestatus(cmd *cobra.Command, args []string) { 156 rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") 157 var res rpctypes.TimeStatus 158 ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetTimeStatus", nil, &res) 159 ctx.Run() 160 }