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 
00038 
00039 class Publish(Capability):
00040 
00041     publish_msg_fields = [(True, "topic", (str, unicode))]
00042 
00043     topics_glob = None
00044 
00045     def __init__(self, protocol):
00046         # Call superclas constructor
00047         Capability.__init__(self, protocol)
00048 
00049         # Register the operations that this capability provides
00050         protocol.register_operation("publish", self.publish)
00051 
00052         # Save the topics that are published on for the purposes of unregistering
00053         self._published = {}
00054 
00055     def publish(self, message):
00056         # Do basic type checking
00057         self.basic_type_check(message, self.publish_msg_fields)
00058         topic = message["topic"]
00059         latch = message.get("latch", False)
00060         queue_size = message.get("queue_size", 100)
00061 
00062         if Publish.topics_glob is not None and Publish.topics_glob:
00063             self.protocol.log("debug", "Topic security glob enabled, checking topic: " + topic)
00064             match = False
00065             for glob in Publish.topics_glob:
00066                 if (fnmatch.fnmatch(topic, glob)):
00067                     self.protocol.log("debug", "Found match with glob " + glob + ", continuing publish...")
00068                     match = True
00069                     break
00070             if not match:
00071                 self.protocol.log("warn", "No match found for topic, cancelling publish to: " + topic)
00072                 return
00073         else:
00074             self.protocol.log("debug", "No topic security glob, not checking publish.")
00075 
00076         # Register as a publishing client, propagating any exceptions
00077         client_id = self.protocol.client_id
00078         manager.register(client_id, topic, latch=latch, queue_size=queue_size)
00079         self._published[topic] = True
00080 
00081         # Get the message if one was provided
00082         msg = message.get("msg", {})
00083 
00084         # Publish the message
00085         manager.publish(client_id, topic, msg, latch=latch, queue_size=queue_size)
00086  
00087     def finish(self):
00088         client_id = self.protocol.client_id
00089         for topic in self._published:
00090             manager.unregister(client_id, topic)
00091         self._published.clear()


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Wed Sep 13 2017 03:18:07