Package rospy :: Package impl :: Module transport

Source Code for Module rospy.impl.transport

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2008, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id$ 
 34  """ 
 35  Base classes for rospy transports 
 36   
 37  These are the base underlying transport implementations, i.e. 
 38  TCP/IP connections, etc... 
 39   
 40  For topic implementations, see L{topics} 
 41  """ 
 42   
 43  import threading 
 44   
 45  # we need ids for the transports so we can send the IDs instead of 
 46  # full connection details 
 47  _transport_id = 0 
 48  _id_lock = threading.Lock() 
49 -def _nextId():
50 global _transport_id 51 try: 52 _id_lock.acquire() 53 _transport_id += 1 54 return _transport_id 55 finally: 56 _id_lock.release()
57 58 INBOUND = 'i' 59 OUTBOUND = 'o' 60 BIDIRECTIONAL = 'b' 61 62 ## Base API of Transport implementations
63 -class Transport(object):
64 transport_type = 'UNKNOWN' 65 66 ## @param self 67 ## @param direction str: INBOUND | OUTBOUND | BIDIRECTIONAL 68 ## @param name str
69 - def __init__(self, direction, name='unnamed'):
70 self.name = name 71 self.direction = direction 72 self.done = False 73 self.cleanup_cb = None 74 self.endpoint_id = '' 75 76 #STATS 77 self.id = _nextId() 78 self.stat_bytes = 0 79 # Number of messages that have passed through this transport 80 self.stat_num_msg = 0
81
82 - def fileno(self):
83 """ 84 Get a file descriptor for select() if available 85 """ 86 return None
87 88 ## callback function to invoke when this connection is 89 ## closed. Function will be passed this transport as an argument. 90 ## @param self 91 ## @param cleanup_callback fn(Transport): callback for when connection is closed
92 - def set_cleanup_callback(self, cleanup_callback):
93 self.cleanup_cb = cleanup_callback
94 95 ## terminate i/o. Leaf subclasses should override and call this implementation 96 ## @param self
97 - def close(self):
98 self.done = True 99 if self.cleanup_cb: 100 self.cleanup_cb(self)
101 102 ## Write raw data to transport 103 ## @throws TransportInitialiationError could not be initialized 104 ## @throws TransportTerminated no longer open for publishing
105 - def write_data(self, data):
106 raise Exception("not implemented")
107 108 ## Shell class to hold stats about transport that is being killed off. 109 ## This allows the information to stick around but the original Tranport to be gc'd
110 -class DeadTransport(Transport):
111 112 ## @param self 113 ## @param transport str: transport name
114 - def __init__(self, transport):
115 super(DeadTransport, self).__init__( 116 transport.direction, transport.name) 117 self.transport_type = transport.transport_type #class property 118 self.id = transport.id 119 self.stat_bytes = transport.stat_bytes 120 self.stat_num_msg = transport.stat_num_msg 121 self.done = True 122 self.endpoint_id = transport.endpoint_id
123 124 ## ProtocolHandler interface: implements topic communication for a 125 ## particular protocol(s). In order to understand the methods of this 126 ## API, it is important to understand how topic communication is 127 ## established: 128 ## 129 ## When a subscriber is notified of a new topic publisher, it contacts 130 ## the publisher to establish a connection. The subscriber gathers a 131 ## list of supported protocols (e.g. [['TCPROS'], ['MEMMAP']]) from 132 ## its protocol handlers (L{get_supported}) and then passes these to 133 ## the publisher. Each of these protocols is actual a list, 134 ## e.g. ['MPI', LaneWidth, BusSpeed], since a protocol may have 135 ## associated parameters. This is considered the start of the 136 ## 'negotiation phase'. 137 ## 138 ## subscriber -> pub.requestTopic(protocols) 139 ## 140 ## The Publisher selects a protocol from the lists and tells the 141 ## appropriate protocol handler to prepare the outbound connection: 142 ## 143 ## pub.requestTopic() -> pub.protocol_handler.init_publisher(selected_protocol) 144 ## 145 ## The protocol handler will return a new set of parameters 146 ## representing connection parameters, e.g. [TCPROS, address, 147 ## port]. These new parameters are passed back to the subscriber, 148 ## which tells its protocol handler to establish the connection. 149 ## 150 ## subscriber -> subscriber.protocol_handler.create_transport(protocolParams)
151 -class ProtocolHandler(object): #interface
152 153 ## shutdown any resources associated with handling this protocol 154 ## @param self
155 - def shutdown(self):
156 pass
157 158 ## Create a new Transport using the specified \a protocol_params 159 ## returned from the Publisher \a pub_uri. 160 ## @param self 161 ## @param protocol_params [[str, val*]]: parameter list from Publisher. Actual 162 ## contents are protocol-specified. 163 ## @param pub_uri str: publisher API URI 164 ## @param topic str: topic name 165 ## @return int, str, int: code, message, debug
166 - def create_transport(self, topic, pub_uri, protocol_params):
167 raise Exception("interface impl")
168 169 ## @param self 170 ## @param protocol str: protocol name. Must match string identifier used in 171 ## negotiation. 172 ## @return bool: True if this handler supports the specified protocol"""
173 - def supports(self, protocol):
174 return False
175 176 ## This method is called on subscribers and returns the protocol list 177 ## @param self 178 ## @return [[str, val*]]: list of supported protocol params. Each set of protocol params is a 179 ## list where the first element is the string identifier for the protocol.
180 - def get_supported(self):
181 return []
182 183 ## Prepare a transport based on one of the supported protocols 184 ## declared by a Subscriber. Subscribers supply a list of 185 ## supported protocols, of which one is selected by the Publisher 186 ## and passed to init_publisher(). init_publisher is responsible 187 ## for initializing the publisher based on the selection. 188 ## @param self 189 ## @param topic str: name of topic 190 ## @param protocol: selected protocol parameters from the Subscriber. 191 ## @return (int, str, list): (code, statusMessage, params). params 192 ## is protocol specific. These params will be sent to the Subscriber 193 ## so that it can create_transport().
194 - def init_publisher(self, topic, protocol):
195 raise Exception("interface impl") 196