graph.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # License: BSD
4 # https://raw.github.com/robotics-in-concert/rocon_multimaster/license/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 import rocon_gateway_utils
18 import rosgraph
19 from rosgraph.impl.graph import Edge, EdgeList
20 import rocon_python_comms
21 
22 ##############################################################################
23 # Graph
24 ##############################################################################
25 
26 
27 class Graph(object):
28 
29  '''
30  Utility class for polling statistics from a running gateway-hub network.
31  '''
32 
33  def __init__(self):
34  '''
35  Creates the polling topics necessary for updating statistics
36  about the running gateway-hub network.
37  '''
38  self._last_update = 0
39  self._gateway_namespace = None
40  self._local_gateway = None
41  self._remote_gateways = None
42  self.gateway_nodes = [] # Gateway nodes
43  self.flipped_nodes = [] # Flip connection nodes (i.e. topic name)
44  self.pulled_nodes = []
45  self.pulled_edges = [] # Gateway-Topic edges
46  self.gateway_edges = [] # Gateway-Gateway edges
47  self.flipped_edges = [] # All unconnected node-topics or topic-nodes
48 
49  # Rubbish to clear out once rocon_gateway_graph is integrated
50  self.bad_nodes = []
51 
52  try:
53  self._gateway_namespace = rocon_gateway_utils.resolve_local_gateway(timeout=rospy.rostime.Duration(2.0))
54  self._gateway_info = rocon_python_comms.SubscriberProxy(
55  self._gateway_namespace + '/gateway_info', gateway_msgs.GatewayInfo)
56  self._remote_gateway_info = rospy.ServiceProxy(
57  self._gateway_namespace + '/remote_gateway_info', gateway_srvs.RemoteGatewayInfo)
58  except rocon_python_comms.NotFoundException as e:
59  rospy.logerr("Gateway Graph: %s" % str(e))
60  self._gateway_namespace = None
61 
62  def local_gateway_name(self):
63  if self._local_gateway:
64  return self._local_gateway.name
65  else:
66  return ''
67 
68  def update(self):
69  if not self._gateway_namespace:
70  return
71  self._local_gateway = self._gateway_info()
72  req = gateway_srvs.RemoteGatewayInfoRequest()
73  req.gateways = []
74  self._remote_gateways = self._remote_gateway_info(req).gateways
75  self._last_update = rospy.get_rostime()
76  # Gateways
77  self.gateway_nodes.append(self._local_gateway.name)
78  self.gateway_nodes.extend([remote_gateway.name for remote_gateway in self._remote_gateways])
79  # Edges
80  self.pulled_edges = EdgeList()
81  self.gateway_edges = EdgeList()
82  self.pulled_edges = EdgeList()
83  self.flipped_edges = EdgeList()
84  # Check local gateway
85  for remote_rule in self._local_gateway.flipped_connections:
86  self.gateway_edges.add(Edge(self._local_gateway.name, remote_rule.remote_rule.gateway))
87  # this adds a bloody magic space, to help disambiguate node names from topic names
88  connection_id = rosgraph.impl.graph.topic_node(
89  remote_rule.remote_rule.rule.name + '-' + remote_rule.remote_rule.rule.type)
90  self.flipped_nodes.append(connection_id)
91  self.flipped_edges.add(Edge(self._local_gateway.name, connection_id))
92  self.flipped_edges.add(Edge(connection_id, remote_rule.remote_rule.gateway))
93  for remote_rule in self._local_gateway.pulled_connections:
94  connection_id = rosgraph.impl.graph.topic_node(remote_rule.rule.name + '-' + remote_rule.rule.type)
95  self.pulled_nodes.append(connection_id)
96  self.pulled_edges.add(Edge(self._local_gateway.name, connection_id))
97  self.pulled_edges.add(Edge(connection_id, remote_rule.gateway))
98  for rule in self._local_gateway.public_interface:
99  connection_id = rosgraph.impl.graph.topic_node(rule.name + '-' + rule.type)
100  # print "pulled edge: %s->%s" % (self._local_gateway.name, connection_id)
101  self.pulled_nodes.append(connection_id)
102  self.pulled_edges.add(Edge(self._local_gateway.name, connection_id))
103  # Check remote gateways
104  for remote_gateway in self._remote_gateways:
105  for remote_rule in remote_gateway.flipped_interface:
106  connection_id = rosgraph.impl.graph.topic_node(remote_rule.rule.name + '-' + remote_rule.rule.type)
107  self.flipped_nodes.append(connection_id)
108  self.flipped_edges.add(Edge(remote_gateway.name, connection_id))
109  self.flipped_edges.add(Edge(connection_id, remote_rule.gateway))
110  self.gateway_edges.add(Edge(remote_gateway.name, remote_rule.gateway))
111  for remote_rule in remote_gateway.pulled_interface:
112  connection_id = rosgraph.impl.graph.topic_node(remote_rule.rule.name + '-' + remote_rule.rule.type)
113  self.pulled_nodes.append(connection_id)
114  self.pulled_edges.add(Edge(remote_rule.gateway, connection_id))
115  self.pulled_edges.add(Edge(connection_id, remote_gateway.name))
116  self.gateway_edges.add(Edge(remote_gateway.name, remote_rule.gateway))
def __init__(self)
Definition: graph.py:33
def local_gateway_name(self)
Definition: graph.py:62


rocon_gateway
Author(s): Daniel Stonier , Jihoon Lee , Piyush Khandelwal
autogenerated on Mon Jun 10 2019 14:40:10