advertise.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 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         # Initialise variables
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         # Register with the publisher manager, propagating any exception
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         # Call superclas constructor
00080         Capability.__init__(self, protocol)
00081 
00082         # Register the operations that this capability provides
00083         protocol.register_operation("advertise", self.advertise)
00084         protocol.register_operation("unadvertise", self.unadvertise)
00085 
00086         # Initialize class variables
00087         self._registrations = {}
00088 
00089     def advertise(self, message):
00090         # Pull out the ID
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         # Create the Registration if one doesn't yet exist
00100         if not topic in self._registrations:
00101             client_id = self.protocol.client_id
00102             self._registrations[topic] = Registration(client_id, topic)
00103 
00104         # Register, propagating any exceptions
00105         self._registrations[topic].register_advertisement(msg_type, aid, latch, queue_size)
00106 
00107     def unadvertise(self, message):
00108         # Pull out the ID
00109         aid = message.get("id", None)
00110 
00111         self.basic_type_check(message, self.unadvertise_msg_fields)
00112         topic = message["topic"]
00113 
00114         # Now unadvertise the topic
00115         if topic not in self._registrations:
00116             return
00117         self._registrations[topic].unregister_advertisement(aid)
00118 
00119         # Check if the registration is now finished with
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")


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Thu Aug 27 2015 14:50:35