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 import fnmatch
00035 from rosbridge_library.capability import Capability
00036 from rosbridge_library.internal.publishers import manager
00037 from rosbridge_library.util import string_types
00038
00039
00040 class Publish(Capability):
00041
00042 publish_msg_fields = [(True, "topic", string_types)]
00043
00044 topics_glob = None
00045
00046 def __init__(self, protocol):
00047
00048 Capability.__init__(self, protocol)
00049
00050
00051 protocol.register_operation("publish", self.publish)
00052
00053
00054 self._published = {}
00055
00056 def publish(self, message):
00057
00058 self.basic_type_check(message, self.publish_msg_fields)
00059 topic = message["topic"]
00060 latch = message.get("latch", False)
00061 queue_size = message.get("queue_size", 100)
00062
00063 if Publish.topics_glob is not None and Publish.topics_glob:
00064 self.protocol.log("debug", "Topic security glob enabled, checking topic: " + topic)
00065 match = False
00066 for glob in Publish.topics_glob:
00067 if (fnmatch.fnmatch(topic, glob)):
00068 self.protocol.log("debug", "Found match with glob " + glob + ", continuing publish...")
00069 match = True
00070 break
00071 if not match:
00072 self.protocol.log("warn", "No match found for topic, cancelling publish to: " + topic)
00073 return
00074 else:
00075 self.protocol.log("debug", "No topic security glob, not checking publish.")
00076
00077
00078 client_id = self.protocol.client_id
00079 manager.register(client_id, topic, latch=latch, queue_size=queue_size)
00080 self._published[topic] = True
00081
00082
00083 msg = message.get("msg", {})
00084
00085
00086 manager.publish(client_id, topic, msg, latch=latch, queue_size=queue_size)
00087
00088 def finish(self):
00089 client_id = self.protocol.client_id
00090 for topic in self._published:
00091 manager.unregister(client_id, topic)
00092 self._published.clear()