github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/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 12:09:48</date> 10 //</624342674287104000> 11 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 // 25 // 26 // 27 28 package simulation 29 30 import ( 31 "github.com/ethereum/go-ethereum/node" 32 "github.com/ethereum/go-ethereum/p2p/discover" 33 "github.com/ethereum/go-ethereum/p2p/simulations/adapters" 34 ) 35 36 // 37 // 38 func (s *Simulation) Service(name string, id discover.NodeID) node.Service { 39 simNode, ok := s.Net.GetNode(id).Node.(*adapters.SimNode) 40 if !ok { 41 return nil 42 } 43 services := simNode.ServiceMap() 44 if len(services) == 0 { 45 return nil 46 } 47 return services[name] 48 } 49 50 // 51 // 52 func (s *Simulation) RandomService(name string) node.Service { 53 n := s.RandomUpNode() 54 if n == nil { 55 return nil 56 } 57 return n.Service(name) 58 } 59 60 // 61 // 62 func (s *Simulation) Services(name string) (services map[discover.NodeID]node.Service) { 63 nodes := s.Net.GetNodes() 64 services = make(map[discover.NodeID]node.Service) 65 for _, node := range nodes { 66 if !node.Up { 67 continue 68 } 69 simNode, ok := node.Node.(*adapters.SimNode) 70 if !ok { 71 continue 72 } 73 services[node.ID()] = simNode.Service(name) 74 } 75 return services 76 } 77