github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/simulation/service.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package simulation
    26  
    27  import (
    28  	"github.com/ethereum/go-ethereum/node"
    29  	"github.com/ethereum/go-ethereum/p2p/discover"
    30  	"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
    31  )
    32  
    33  //
    34  //
    35  func (s *Simulation) Service(name string, id discover.NodeID) node.Service {
    36  	simNode, ok := s.Net.GetNode(id).Node.(*adapters.SimNode)
    37  	if !ok {
    38  		return nil
    39  	}
    40  	services := simNode.ServiceMap()
    41  	if len(services) == 0 {
    42  		return nil
    43  	}
    44  	return services[name]
    45  }
    46  
    47  //
    48  //
    49  func (s *Simulation) RandomService(name string) node.Service {
    50  	n := s.RandomUpNode()
    51  	if n == nil {
    52  		return nil
    53  	}
    54  	return n.Service(name)
    55  }
    56  
    57  //
    58  //
    59  func (s *Simulation) Services(name string) (services map[discover.NodeID]node.Service) {
    60  	nodes := s.Net.GetNodes()
    61  	services = make(map[discover.NodeID]node.Service)
    62  	for _, node := range nodes {
    63  		if !node.Up {
    64  			continue
    65  		}
    66  		simNode, ok := node.Node.(*adapters.SimNode)
    67  		if !ok {
    68  			continue
    69  		}
    70  		services[node.ID()] = simNode.Service(name)
    71  	}
    72  	return services
    73  }