github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/network/simulation/service.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 19:16:43</date> 10 //</624450114710147072> 11 12 13 package simulation 14 15 import ( 16 "github.com/ethereum/go-ethereum/node" 17 "github.com/ethereum/go-ethereum/p2p/enode" 18 "github.com/ethereum/go-ethereum/p2p/simulations/adapters" 19 ) 20 21 //服务在特定节点上按名称返回单个服务 22 //提供ID。 23 func (s *Simulation) Service(name string, id enode.ID) node.Service { 24 simNode, ok := s.Net.GetNode(id).Node.(*adapters.SimNode) 25 if !ok { 26 return nil 27 } 28 services := simNode.ServiceMap() 29 if len(services) == 0 { 30 return nil 31 } 32 return services[name] 33 } 34 35 //RandomService按名称返回 36 //随机选择的向上的节点。 37 func (s *Simulation) RandomService(name string) node.Service { 38 n := s.Net.GetRandomUpNode().Node.(*adapters.SimNode) 39 if n == nil { 40 return nil 41 } 42 return n.Service(name) 43 } 44 45 //服务返回具有所提供名称的所有服务 46 //从向上的节点。 47 func (s *Simulation) Services(name string) (services map[enode.ID]node.Service) { 48 nodes := s.Net.GetNodes() 49 services = make(map[enode.ID]node.Service) 50 for _, node := range nodes { 51 if !node.Up { 52 continue 53 } 54 simNode, ok := node.Node.(*adapters.SimNode) 55 if !ok { 56 continue 57 } 58 services[node.ID()] = simNode.Service(name) 59 } 60 return services 61 } 62