github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/mobile/p2p.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:43</date> 10 //</624342654309634048> 11 12 13 //包含p2p包的包装。 14 15 package geth 16 17 import ( 18 "errors" 19 20 "github.com/ethereum/go-ethereum/p2p" 21 ) 22 23 //nodeinfo表示已知主机信息的pi简短摘要。 24 type NodeInfo struct { 25 info *p2p.NodeInfo 26 } 27 28 func (ni *NodeInfo) GetID() string { return ni.info.ID } 29 func (ni *NodeInfo) GetName() string { return ni.info.Name } 30 func (ni *NodeInfo) GetEnode() string { return ni.info.Enode } 31 func (ni *NodeInfo) GetIP() string { return ni.info.IP } 32 func (ni *NodeInfo) GetDiscoveryPort() int { return ni.info.Ports.Discovery } 33 func (ni *NodeInfo) GetListenerPort() int { return ni.info.Ports.Listener } 34 func (ni *NodeInfo) GetListenerAddress() string { return ni.info.ListenAddr } 35 func (ni *NodeInfo) GetProtocols() *Strings { 36 protos := []string{} 37 for proto := range ni.info.Protocols { 38 protos = append(protos, proto) 39 } 40 return &Strings{protos} 41 } 42 43 //peerinfo表示关于pi-connected peer已知信息的pi简短摘要。 44 type PeerInfo struct { 45 info *p2p.PeerInfo 46 } 47 48 func (pi *PeerInfo) GetID() string { return pi.info.ID } 49 func (pi *PeerInfo) GetName() string { return pi.info.Name } 50 func (pi *PeerInfo) GetCaps() *Strings { return &Strings{pi.info.Caps} } 51 func (pi *PeerInfo) GetLocalAddress() string { return pi.info.Network.LocalAddress } 52 func (pi *PeerInfo) GetRemoteAddress() string { return pi.info.Network.RemoteAddress } 53 54 //PeerInfos表示关于远程对等机的一部分信息。 55 type PeerInfos struct { 56 infos []*p2p.PeerInfo 57 } 58 59 //SIZE返回切片中的对等信息条目数。 60 func (pi *PeerInfos) Size() int { 61 return len(pi.infos) 62 } 63 64 //GET从切片返回给定索引处的对等信息。 65 func (pi *PeerInfos) Get(index int) (info *PeerInfo, _ error) { 66 if index < 0 || index >= len(pi.infos) { 67 return nil, errors.New("index out of bounds") 68 } 69 return &PeerInfo{pi.infos[index]}, nil 70 } 71