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
00018 port = 9090
00019 host = ""
00020 incoming_buffer = 65536
00021 socket_timeout = 10
00022 retry_startup_delay = 5
00023
00024 service_request_timeout = 600
00025 check_response_delay = 0.01
00026 wait_for_busy_service_provider = 0.01
00027 max_service_requests = 1000000
00028
00029 fragment_timeout = 600
00030
00031 delay_between_messages = 0.01
00032 max_message_size = None
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
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)