publish.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # Copyright (c) 2014, Creativa 77 SRL
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
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         # Call superclas constructor
00048         Capability.__init__(self, protocol)
00049 
00050         # Register the operations that this capability provides
00051         protocol.register_operation("publish", self.publish)
00052 
00053         # Save the topics that are published on for the purposes of unregistering
00054         self._published = {}
00055 
00056     def publish(self, message):
00057         # Do basic type checking
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         # Register as a publishing client, propagating any exceptions
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         # Get the message if one was provided
00083         msg = message.get("msg", {})
00084 
00085         # Publish the message
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()


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Thu Jun 6 2019 21:51:43