tcp_handler.py
Go to the documentation of this file.
00001 import rospy
00002 from rosbridge_library.rosbridge_protocol import RosbridgeProtocol
00003 
00004 import SocketServer
00005 
00006 
00007 class RosbridgeTcpSocket(SocketServer.BaseRequestHandler):
00008     """
00009     TCP Socket server for rosbridge
00010     """
00011 
00012     busy = False
00013     queue = []
00014     client_id_seed = 0
00015     clients_connected = 0
00016 
00017     # list of possible parameters ( with internal default values
00018     port = 9090                             # integer (portnumber)
00019     host = ""                               # hostname / IP as string
00020     incoming_buffer = 65536                 # bytes
00021     socket_timeout = 10                     # seconds
00022     retry_startup_delay = 5                 # seconds
00023     # advertise_service.py:
00024     service_request_timeout = 600           # seconds
00025     check_response_delay = 0.01             # seconds
00026     wait_for_busy_service_provider = 0.01   # seconds
00027     max_service_requests = 1000000          # requests
00028     # defragmentation.py:
00029     fragment_timeout = 600                  # seconds
00030     # protocol.py:
00031     delay_between_messages = 0.01           # seconds
00032     max_message_size = None                 # bytes
00033 
00034     def setup(self):
00035         cls = self.__class__
00036         parameters = {
00037             "port": cls.port,
00038             "host": cls.host,
00039             "incoming_buffer": cls.incoming_buffer,
00040             "socket_timeout": cls.socket_timeout,
00041             "retry_startup_delay": cls.retry_startup_delay,
00042             "service_request_timeout": cls.service_request_timeout,
00043             "check_response_delay": cls.check_response_delay,
00044             "wait_for_busy_service_provider": cls.wait_for_busy_service_provider,
00045             "max_service_requests": cls.max_service_requests,
00046             "fragment_timeout": cls.fragment_timeout,
00047             "delay_between_messages": cls.delay_between_messages,
00048             "max_message_size": cls.max_message_size
00049         }
00050 
00051         try:
00052             self.protocol = RosbridgeProtocol(cls.client_id_seed, parameters = parameters)
00053             self.protocol.outgoing = self.send_message
00054             cls.client_id_seed += 1
00055             cls.clients_connected += 1
00056             self.protocol.log("info", "connected. " + str(cls.clients_connected) + " client total.")
00057         except Exception as exc:
00058             rospy.logerr("Unable to accept incoming connection.  Reason: %s", str(exc))
00059 
00060     def handle(self):
00061         """
00062         Listen for TCP messages
00063         """
00064         cls = self.__class__
00065         self.request.settimeout(cls.socket_timeout)
00066         while 1:
00067             try:
00068               data = self.request.recv(cls.incoming_buffer)
00069               # Exit on empty string
00070               if data.strip() == '':
00071                   break
00072               elif len(data.strip()) > 0:
00073                   self.protocol.incoming(data.strip(''))
00074               else:
00075                   pass
00076             except Exception, e:
00077                 pass
00078                 self.protocol.log("debug", "socket connection timed out! (ignore warning if client is only listening..)")
00079 
00080     def finish(self):
00081         """
00082         Called when TCP connection finishes
00083         """
00084         cls = self.__class__
00085         cls.clients_connected -= 1
00086         self.protocol.finish()
00087         self.protocol.log("info", "disconnected. " + str(cls.clients_connected) + " client total." )
00088 
00089     def send_message(self, message=None):
00090         """
00091         Callback from rosbridge
00092         """
00093 
00094         self.request.send(message)


rosbridge_server
Author(s): Jonathan Mace
autogenerated on Thu Aug 27 2015 14:50:40