Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 from rosbridge_library.capability import Capability
00035 from rosbridge_library.internal.publishers import manager
00036
00037
00038 class Registration():
00039 """ Keeps track of how many times a client has requested to advertise
00040 a publisher.
00041
00042 A client could advertise and unadvertise a topic multiple times, and we
00043 must make sure that the underlying publisher is only created and destroyed
00044 at the appropriate moments
00045
00046 """
00047
00048 def __init__(self, client_id, topic):
00049
00050 self.client_id = client_id
00051 self.topic = topic
00052 self.clients = {}
00053
00054 def unregister(self):
00055 manager.unregister(self.client_id, self.topic)
00056
00057 def register_advertisement(self, msg_type, adv_id=None, latch=False, queue_size=100):
00058
00059 manager.register(self.client_id, self.topic, msg_type, latch=latch, queue_size=queue_size)
00060
00061 self.clients[adv_id] = True
00062
00063 def unregister_advertisement(self, adv_id=None):
00064 if adv_id is None:
00065 self.clients.clear()
00066 elif adv_id in self.clients:
00067 del self.clients[adv_id]
00068
00069 def is_empty(self):
00070 return len(self.clients) == 0
00071
00072
00073 class Advertise(Capability):
00074
00075 advertise_msg_fields = [(True, "topic", (str, unicode)), (True, "type", (str, unicode))]
00076 unadvertise_msg_fields = [(True, "topic", (str, unicode))]
00077
00078 def __init__(self, protocol):
00079
00080 Capability.__init__(self, protocol)
00081
00082
00083 protocol.register_operation("advertise", self.advertise)
00084 protocol.register_operation("unadvertise", self.unadvertise)
00085
00086
00087 self._registrations = {}
00088
00089 def advertise(self, message):
00090
00091 aid = message.get("id", None)
00092
00093 self.basic_type_check(message, self.advertise_msg_fields)
00094 topic = message["topic"]
00095 msg_type = message["type"]
00096 latch = message.get("latch", False)
00097 queue_size = message.get("queue_size", 100)
00098
00099
00100 if not topic in self._registrations:
00101 client_id = self.protocol.client_id
00102 self._registrations[topic] = Registration(client_id, topic)
00103
00104
00105 self._registrations[topic].register_advertisement(msg_type, aid, latch, queue_size)
00106
00107 def unadvertise(self, message):
00108
00109 aid = message.get("id", None)
00110
00111 self.basic_type_check(message, self.unadvertise_msg_fields)
00112 topic = message["topic"]
00113
00114
00115 if topic not in self._registrations:
00116 return
00117 self._registrations[topic].unregister_advertisement(aid)
00118
00119
00120 if self._registrations[topic].is_empty():
00121 self._registrations[topic].unregister()
00122 del self._registrations[topic]
00123
00124 def finish(self):
00125 for registration in self._registrations.values():
00126 registration.unregister()
00127 self._registrations.clear()
00128 self.protocol.unregister_operation("advertise")
00129 self.protocol.unregister_operation("unadvertise")