Package rocon_gateway :: Module graph
[frames] | no frames]

Source Code for Module rocon_gateway.graph

  1  #!/usr/bin/env python 
  2  # 
  3  # License: BSD 
  4  #   https://raw.github.com/robotics-in-concert/rocon_multimaster/master/rocon_gateway_graph/LICENSE 
  5  # 
  6  ''' 
  7    This code does not effect the runtime of gateways at all - it is used for 
  8    debugging and monitoring purposes only. 
  9  ''' 
 10  ############################################################################## 
 11  # Imports 
 12  ############################################################################## 
 13   
 14  import rospy 
 15  import gateway_msgs.srv as gateway_srvs 
 16  import gateway_msgs.msg as gateway_msgs 
 17  from master_api import LocalMaster 
 18  import rosgraph 
 19  from rosgraph.impl.graph import Edge, EdgeList 
 20  import rocon_utilities 
 21   
 22  ############################################################################## 
 23  # Graph 
 24  ############################################################################## 
 25   
 26   
27 -class Graph(object):
28 ''' 29 Utility class for polling statistics from a running gateway-hub network. 30 ''' 31
32 - def __init__(self):
33 ''' 34 Creates the polling topics necessary for updating statistics 35 about the running gateway-hub network. 36 ''' 37 self._last_update = 0 38 self._gateway_namespace = None 39 self._local_gateway = None 40 self._remote_gateways = None 41 self.gateway_nodes = [] # Gateway nodes 42 self.flipped_nodes = [] # Flip connection nodes (i.e. topic name) 43 self.pulled_nodes = [] 44 self.pulled_edges = [] # Gateway-Topic edges 45 self.gateway_edges = [] # Gateway-Gateway edges 46 self.flipped_edges = [] # All unconnected node-topics or topic-nodes 47 48 # Rubbish to clear out once rocon_gateway_graph is integrated 49 self.bad_nodes = [] 50 51 if self._resolve_gateway_namespace(): 52 self.configure()
53
54 - def configure(self):
55 self._gateway_info = rocon_utilities.SubscriberProxy(self.gateway_namespace + '/gateway_info', gateway_msgs.GatewayInfo) 56 self._remote_gateway_info = rospy.ServiceProxy(self.gateway_namespace + '/remote_gateway_info', gateway_srvs.RemoteGatewayInfo)
57
58 - def local_gateway_name(self):
59 if self._local_gateway: 60 return self._local_gateway.name 61 else: 62 return ''
63
64 - def update(self):
65 if not self._resolve_gateway_namespace(): 66 return 67 self._local_gateway = self._gateway_info() 68 req = gateway_srvs.RemoteGatewayInfoRequest() 69 req.gateways = [] 70 self._remote_gateways = self._remote_gateway_info(req).gateways 71 self._last_update = rospy.get_rostime() 72 # Gateways 73 self.gateway_nodes.append(self._local_gateway.name) 74 self.gateway_nodes.extend([remote_gateway.name for remote_gateway in self._remote_gateways]) 75 # Edges 76 self.pulled_edges = EdgeList() 77 self.gateway_edges = EdgeList() 78 self.pulled_edges = EdgeList() 79 self.flipped_edges = EdgeList() 80 # Check local gateway 81 for remote_rule in self._local_gateway.flipped_connections: 82 self.gateway_edges.add(Edge(self._local_gateway.name, remote_rule.gateway)) 83 # this adds a bloody magic space, to help disambiguate node names from topic names 84 connection_id = rosgraph.impl.graph.topic_node(remote_rule.rule.name + '-' + remote_rule.rule.type) 85 self.flipped_nodes.append(connection_id) 86 self.flipped_edges.add(Edge(self._local_gateway.name, connection_id)) 87 self.flipped_edges.add(Edge(connection_id, remote_rule.gateway)) 88 for remote_rule in self._local_gateway.pulled_connections: 89 connection_id = rosgraph.impl.graph.topic_node(remote_rule.rule.name + '-' + remote_rule.rule.type) 90 self.pulled_nodes.append(connection_id) 91 self.pulled_edges.add(Edge(self._local_gateway.name, connection_id)) 92 self.pulled_edges.add(Edge(connection_id, remote_rule.gateway)) 93 for rule in self._local_gateway.public_interface: 94 connection_id = rosgraph.impl.graph.topic_node(rule.name + '-' + rule.type) 95 #print "pulled edge: %s->%s" % (self._local_gateway.name, connection_id) 96 self.pulled_nodes.append(connection_id) 97 self.pulled_edges.add(Edge(self._local_gateway.name, connection_id)) 98 # Check remote gateways 99 for remote_gateway in self._remote_gateways: 100 for remote_rule in remote_gateway.flipped_interface: 101 connection_id = rosgraph.impl.graph.topic_node(remote_rule.rule.name + '-' + remote_rule.rule.type) 102 self.flipped_nodes.append(connection_id) 103 self.flipped_edges.add(Edge(remote_gateway.name, connection_id)) 104 self.flipped_edges.add(Edge(connection_id, remote_rule.gateway)) 105 self.gateway_edges.add(Edge(remote_gateway.name, remote_rule.gateway)) 106 for remote_rule in remote_gateway.pulled_interface: 107 connection_id = rosgraph.impl.graph.topic_node(remote_rule.rule.name + '-' + remote_rule.rule.type) 108 self.pulled_nodes.append(connection_id) 109 self.pulled_edges.add(Edge(remote_rule.gateway, connection_id)) 110 self.pulled_edges.add(Edge(connection_id, remote_gateway.name)) 111 self.gateway_edges.add(Edge(remote_gateway.name, remote_rule.gateway))
112
114 ''' 115 Checks if the gateway namespace was found and if not 116 attempts to resolve it. 117 ''' 118 if self._gateway_namespace: 119 return 120 master = LocalMaster() 121 self.gateway_namespace = master.find_gateway_namespace() 122 if not self.gateway_namespace: 123 rospy.logerr("Gateway Graph: could not find a local gateway - did you start it?") 124 return self.gateway_namespace
125