advertise.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
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         # Initialise variables
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         # Register with the publisher manager, propagating any exception
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         # Call superclas constructor
00079         Capability.__init__(self, protocol)
00080 
00081         # Register the operations that this capability provides
00082         protocol.register_operation("advertise", self.advertise)
00083         protocol.register_operation("unadvertise", self.unadvertise)
00084 
00085         # Initialize class variables
00086         self._registrations = {}
00087 
00088     def advertise(self, message):
00089         # Pull out the ID
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         # Create the Registration if one doesn't yet exist
00097         if not topic in self._registrations:
00098             client_id = self.protocol.client_id
00099             self._registrations[topic] = Registration(client_id, topic)
00100 
00101         # Register, propagating any exceptions
00102         self._registrations[topic].register_advertisement(msg_type, aid)
00103 
00104     def unadvertise(self, message):
00105         # Pull out the ID
00106         aid = message.get("id", None)
00107 
00108         self.basic_type_check(message, self.unadvertise_msg_fields)
00109         topic = message["topic"]
00110 
00111         # Now unadvertise the topic
00112         if topic not in self._registrations:
00113             return
00114         self._registrations[topic].unregister_advertisement(aid)
00115 
00116         # Check if the registration is now finished with
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")


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Thu Jan 2 2014 11:53:35