github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/swarm/network/simulation/service.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package simulation
    18  
    19  import (
    20  	"github.com/ShyftNetwork/go-empyrean/node"
    21  	"github.com/ShyftNetwork/go-empyrean/p2p/enode"
    22  	"github.com/ShyftNetwork/go-empyrean/p2p/simulations/adapters"
    23  )
    24  
    25  // Service returns a single Service by name on a particular node
    26  // with provided id.
    27  func (s *Simulation) Service(name string, id enode.ID) node.Service {
    28  	simNode, ok := s.Net.GetNode(id).Node.(*adapters.SimNode)
    29  	if !ok {
    30  		return nil
    31  	}
    32  	services := simNode.ServiceMap()
    33  	if len(services) == 0 {
    34  		return nil
    35  	}
    36  	return services[name]
    37  }
    38  
    39  // RandomService returns a single Service by name on a
    40  // randomly chosen node that is up.
    41  func (s *Simulation) RandomService(name string) node.Service {
    42  	n := s.Net.GetRandomUpNode().Node.(*adapters.SimNode)
    43  	if n == nil {
    44  		return nil
    45  	}
    46  	return n.Service(name)
    47  }
    48  
    49  // Services returns all services with a provided name
    50  // from nodes that are up.
    51  func (s *Simulation) Services(name string) (services map[enode.ID]node.Service) {
    52  	nodes := s.Net.GetNodes()
    53  	services = make(map[enode.ID]node.Service)
    54  	for _, node := range nodes {
    55  		if !node.Up {
    56  			continue
    57  		}
    58  		simNode, ok := node.Node.(*adapters.SimNode)
    59  		if !ok {
    60  			continue
    61  		}
    62  		services[node.ID()] = simNode.Service(name)
    63  	}
    64  	return services
    65  }