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 from rosbridge_library.capability import Capability
00034 from rosbridge_library.internal.publishers import manager
00035
00036
00037 class Registration():
00038 """ Keeps track of how many times a client has requested to advertise
00039 a publisher.
00040
00041 A client could advertise and unadvertise a topic multiple times, and we
00042 must make sure that the underlying publisher is only created and destroyed
00043 at the appropriate moments
00044
00045 """
00046
00047 def __init__(self, client_id, topic):
00048
00049 self.client_id = client_id
00050 self.topic = topic
00051 self.clients = {}
00052
00053 def unregister(self):
00054 manager.unregister(self.client_id, self.topic)
00055
00056 def register_advertisement(self, msg_type, adv_id=None):
00057
00058 manager.register(self.client_id, self.topic, msg_type)
00059
00060 self.clients[adv_id] = True
00061
00062 def unregister_advertisement(self, adv_id=None):
00063 if adv_id is None:
00064 self.clients.clear()
00065 elif adv_id in self.clients:
00066 del self.clients[adv_id]
00067
00068 def is_empty(self):
00069 return len(self.clients) == 0
00070
00071
00072 class Advertise(Capability):
00073
00074 advertise_msg_fields = [(True, "topic", (str, unicode)), (True, "type", (str, unicode))]
00075 unadvertise_msg_fields = [(True, "topic", (str, unicode))]
00076
00077 def __init__(self, protocol):
00078
00079 Capability.__init__(self, protocol)
00080
00081
00082 protocol.register_operation("advertise", self.advertise)
00083 protocol.register_operation("unadvertise", self.unadvertise)
00084
00085
00086 self._registrations = {}
00087
00088 def advertise(self, message):
00089
00090 aid = message.get("id", None)
00091
00092 self.basic_type_check(message, self.advertise_msg_fields)
00093 topic = message["topic"]
00094 msg_type = message["type"]
00095
00096
00097 if not topic in self._registrations:
00098 client_id = self.protocol.client_id
00099 self._registrations[topic] = Registration(client_id, topic)
00100
00101
00102 self._registrations[topic].register_advertisement(msg_type, aid)
00103
00104 def unadvertise(self, message):
00105
00106 aid = message.get("id", None)
00107
00108 self.basic_type_check(message, self.unadvertise_msg_fields)
00109 topic = message["topic"]
00110
00111
00112 if topic not in self._registrations:
00113 return
00114 self._registrations[topic].unregister_advertisement(aid)
00115
00116
00117 if self._registrations[topic].is_empty():
00118 self._registrations[topic].unregister()
00119 del self._registrations[topic]
00120
00121 def finish(self):
00122 for registration in self._registrations.values():
00123 registration.unregister()
00124 self._registrations.clear()
00125 self.protocol.unregister_operation("advertise")
00126 self.protocol.unregister_operation("unadvertise")