advertise.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # Copyright (c) 2014, Creativa 77 SRL
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 import fnmatch
35 from rosbridge_library.capability import Capability
36 from rosbridge_library.internal.publishers import manager
37 from rosbridge_library.util import string_types
38 
39 
40 class Registration():
41  """ Keeps track of how many times a client has requested to advertise
42  a publisher.
43 
44  A client could advertise and unadvertise a topic multiple times, and we
45  must make sure that the underlying publisher is only created and destroyed
46  at the appropriate moments
47 
48  """
49 
50  def __init__(self, client_id, topic):
51  # Initialise variables
52  self.client_id = client_id
53  self.topic = topic
54  self.clients = {}
55 
56  def unregister(self):
57  manager.unregister(self.client_id, self.topic)
58 
59  def register_advertisement(self, msg_type, adv_id=None, latch=False, queue_size=100):
60  # Register with the publisher manager, propagating any exception
61  manager.register(self.client_id, self.topic, msg_type, latch=latch, queue_size=queue_size)
62 
63  self.clients[adv_id] = True
64 
65  def unregister_advertisement(self, adv_id=None):
66  if adv_id is None:
67  self.clients.clear()
68  elif adv_id in self.clients:
69  del self.clients[adv_id]
70 
71  def is_empty(self):
72  return len(self.clients) == 0
73 
74 
76 
77  advertise_msg_fields = [(True, "topic", string_types), (True, "type", string_types)]
78  unadvertise_msg_fields = [(True, "topic", string_types)]
79 
80  topics_glob = None
81 
82  def __init__(self, protocol):
83  # Call superclas constructor
84  Capability.__init__(self, protocol)
85 
86  # Register the operations that this capability provides
87  protocol.register_operation("advertise", self.advertise)
88  protocol.register_operation("unadvertise", self.unadvertise)
89 
90  # Initialize class variables
91  self._registrations = {}
92 
93  if protocol.parameters and "unregister_timeout" in protocol.parameters:
94  manager.unregister_timeout = protocol.parameters.get("unregister_timeout")
95 
96  def advertise(self, message):
97  # Pull out the ID
98  aid = message.get("id", None)
99 
100  self.basic_type_check(message, self.advertise_msg_fields)
101  topic = message["topic"]
102  msg_type = message["type"]
103  latch = message.get("latch", False)
104  queue_size = message.get("queue_size", 100)
105 
106  if Advertise.topics_glob is not None and Advertise.topics_glob:
107  self.protocol.log("debug", "Topic security glob enabled, checking topic: " + topic)
108  match = False
109  for glob in Advertise.topics_glob:
110  if (fnmatch.fnmatch(topic, glob)):
111  self.protocol.log("debug", "Found match with glob " + glob + ", continuing advertisement...")
112  match = True
113  break
114  if not match:
115  self.protocol.log("warn", "No match found for topic, cancelling advertisement of: " + topic)
116  return
117  else:
118  self.protocol.log("debug", "No topic security glob, not checking advertisement.")
119 
120  # Create the Registration if one doesn't yet exist
121  if not topic in self._registrations:
122  client_id = self.protocol.client_id
123  self._registrations[topic] = Registration(client_id, topic)
124 
125  # Register, propagating any exceptions
126  self._registrations[topic].register_advertisement(msg_type, aid, latch, queue_size)
127 
128  def unadvertise(self, message):
129  # Pull out the ID
130  aid = message.get("id", None)
131 
132  self.basic_type_check(message, self.unadvertise_msg_fields)
133  topic = message["topic"]
134 
135  if Advertise.topics_glob is not None and Advertise.topics_glob:
136  self.protocol.log("debug", "Topic security glob enabled, checking topic: " + topic)
137  match = False
138  for glob in Advertise.topics_glob:
139  if (fnmatch.fnmatch(topic, glob)):
140  self.protocol.log("debug", "Found match with glob " + glob + ", continuing unadvertisement...")
141  match = True
142  break
143  if not match:
144  self.protocol.log("warn", "No match found for topic, cancelling unadvertisement of: " + topic)
145  return
146  else:
147  self.protocol.log("debug", "No topic security glob, not checking unadvertisement.")
148 
149  # Now unadvertise the topic
150  if topic not in self._registrations:
151  return
152  self._registrations[topic].unregister_advertisement(aid)
153 
154  # Check if the registration is now finished with
155  if self._registrations[topic].is_empty():
156  self._registrations[topic].unregister()
157  del self._registrations[topic]
158 
159  def finish(self):
160  for registration in self._registrations.values():
161  registration.unregister()
162  self._registrations.clear()
163  self.protocol.unregister_operation("advertise")
164  self.protocol.unregister_operation("unadvertise")
def register_advertisement(self, msg_type, adv_id=None, latch=False, queue_size=100)
Definition: advertise.py:59
def basic_type_check(self, msg, types_info)
Definition: capability.py:76


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Fri May 10 2019 02:17:02