publish.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 
41 
42  publish_msg_fields = [(True, "topic", string_types)]
43 
44  topics_glob = None
45 
46  def __init__(self, protocol):
47  # Call superclas constructor
48  Capability.__init__(self, protocol)
49 
50  # Register the operations that this capability provides
51  protocol.register_operation("publish", self.publish)
52 
53  # Save the topics that are published on for the purposes of unregistering
54  self._published = {}
55 
56  if protocol.parameters and "unregister_timeout" in protocol.parameters:
57  manager.unregister_timeout = protocol.parameters.get("unregister_timeout")
58 
59  def publish(self, message):
60  # Do basic type checking
61  self.basic_type_check(message, self.publish_msg_fields)
62  topic = message["topic"]
63  latch = message.get("latch", False)
64  queue_size = message.get("queue_size", 100)
65 
66  if Publish.topics_glob is not None and Publish.topics_glob:
67  self.protocol.log("debug", "Topic security glob enabled, checking topic: " + topic)
68  match = False
69  for glob in Publish.topics_glob:
70  if (fnmatch.fnmatch(topic, glob)):
71  self.protocol.log("debug", "Found match with glob " + glob + ", continuing publish...")
72  match = True
73  break
74  if not match:
75  self.protocol.log("warn", "No match found for topic, cancelling publish to: " + topic)
76  return
77  else:
78  self.protocol.log("debug", "No topic security glob, not checking publish.")
79 
80  # Register as a publishing client, propagating any exceptions
81  client_id = self.protocol.client_id
82  manager.register(client_id, topic, latch=latch, queue_size=queue_size)
83  self._published[topic] = True
84 
85  # Get the message if one was provided
86  msg = message.get("msg", {})
87 
88  # Publish the message
89  manager.publish(client_id, topic, msg, latch=latch, queue_size=queue_size)
90 
91  def finish(self):
92  client_id = self.protocol.client_id
93  for topic in self._published:
94  manager.unregister(client_id, topic)
95  self._published.clear()
rosbridge_library.internal.publishers
Definition: publishers.py:1
rosbridge_library.capabilities.publish.Publish
Definition: publish.py:40
rosbridge_library.capabilities.publish.Publish.publish_msg_fields
list publish_msg_fields
Definition: publish.py:42
rosbridge_library.capabilities.publish.Publish.__init__
def __init__(self, protocol)
Definition: publish.py:46
rosbridge_library.util
Definition: src/rosbridge_library/util/__init__.py:1
rosbridge_library.capability.Capability.protocol
protocol
Definition: capability.py:58
rosbridge_library.capability.Capability.basic_type_check
def basic_type_check(self, msg, types_info)
Definition: capability.py:76
rosbridge_library.capability
Definition: capability.py:1
rosbridge_library.capability.Capability
Definition: capability.py:37
rosbridge_library.capabilities.publish.Publish.finish
def finish(self)
Definition: publish.py:91
rosbridge_library.capabilities.publish.Publish.publish
def publish(self, message)
Definition: publish.py:59
rosbridge_library.capabilities.publish.Publish._published
_published
Definition: publish.py:54


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Tue Oct 3 2023 02:12:45