Go to the documentation of this file.00001
00002
00003 from rospy import init_node, get_param, loginfo, logerr
00004 from rosbridge_library.rosbridge_protocol import RosbridgeProtocol
00005
00006 from signal import signal, SIGINT, SIG_DFL
00007
00008 import SocketServer
00009 import sys
00010 import time
00011
00012
00013
00014
00015
00016
00017 client_id_seed = 0
00018 clients_connected = 0
00019
00020
00021
00022 port = 9090
00023 host = ""
00024 incoming_buffer = 65536
00025 socket_timeout = 10
00026 retry_startup_delay = 5
00027
00028 service_request_timeout = 600
00029 check_response_delay = 0.01
00030 wait_for_busy_service_provider = 0.01
00031 max_service_requests = 1000000
00032
00033 fragment_timeout = 600
00034
00035 delay_between_messages = 0.01
00036 max_message_size = None
00037
00038
00039 class RosbridgeTcpSocket(SocketServer.BaseRequestHandler):
00040 """
00041 TCP Socket server for rosbridge
00042 """
00043
00044 busy = False
00045 queue = []
00046
00047 def setup(self):
00048 global client_id_seed, clients_connected
00049 parameters = {
00050 "port": port,
00051 "host": host,
00052 "incoming_buffer": incoming_buffer,
00053 "socket_timeout": socket_timeout,
00054 "retry_startup_delay": retry_startup_delay,
00055 "service_request_timeout": service_request_timeout,
00056 "check_response_delay": check_response_delay,
00057 "wait_for_busy_service_provider": wait_for_busy_service_provider,
00058 "max_service_requests": max_service_requests,
00059 "fragment_timeout": fragment_timeout,
00060 "delay_between_messages": delay_between_messages,
00061 "max_message_size": max_message_size
00062 }
00063
00064 try:
00065 self.protocol = RosbridgeProtocol(client_id_seed, parameters = parameters)
00066 self.protocol.outgoing = self.send_message
00067 client_id_seed = client_id_seed + 1
00068 clients_connected = clients_connected + 1
00069 self.protocol.log("info", "connected. " + str(clients_connected) + " client total.")
00070 except Exception as exc:
00071 logerr("Unable to accept incoming connection. Reason: %s", str(exc))
00072
00073
00074 def handle(self):
00075 """
00076 Listen for TCP messages
00077 """
00078 self.request.settimeout(socket_timeout)
00079 while 1:
00080 try:
00081 data = self.request.recv(incoming_buffer)
00082
00083 if data.strip() == '':
00084 break
00085 elif len(data.strip()) > 0:
00086
00087 self.protocol.incoming(data.strip(''))
00088 else:
00089 pass
00090 except Exception, e:
00091 pass
00092 self.protocol.log("debug", "socket connection timed out! (ignore warning if client is only listening..)")
00093
00094 def finish(self):
00095 """
00096 Called when TCP connection finishes
00097 """
00098 global clients_connected
00099 clients_connected = clients_connected - 1
00100 self.protocol.finish()
00101 self.protocol.log("info", "disconnected. " + str(clients_connected) + " client total." )
00102
00103 def send_message(self, message=None):
00104 """
00105 Callback from rosbridge
00106 """
00107
00108 self.request.send(message)
00109
00110
00111 if __name__ == "__main__":
00112 loaded = False
00113 retry_count = 0
00114 while not loaded:
00115 retry_count += 1
00116 print "trying to start rosbridge TCP server.."
00117 try:
00118 print ""
00119 init_node("rosbridge_tcp")
00120 signal(SIGINT, SIG_DFL)
00121
00122 """
00123 Parameter handling:
00124 - try to get parameter from parameter server (..define those via launch-file)
00125 - overwrite value if given as commandline-parameter
00126
00127 BEGIN...
00128 """
00129
00130
00131
00132
00133
00134 port = get_param('~port', port)
00135 host = get_param('~host', host)
00136 incoming_buffer = get_param('~incoming_buffer', incoming_buffer)
00137 socket_timeout = get_param('~socket_timeout', socket_timeout)
00138 retry_startup_delay = get_param('~retry_startup_delay', retry_startup_delay)
00139 service_request_timeout = get_param('~service_request_timeout', service_request_timeout)
00140 check_response_delay = get_param('~check_response_delay', check_response_delay)
00141 wait_for_busy_service_provider = get_param('~wait_for_busy_service_provider', wait_for_busy_service_provider)
00142 max_service_requests = get_param('~max_service_requests', max_service_requests)
00143 fragment_timeout = get_param('~fragment_timeout', fragment_timeout)
00144 delay_between_messages = get_param('~delay_between_messages', delay_between_messages)
00145 max_message_size = get_param('~max_message_size', max_message_size)
00146 if max_message_size == "None":
00147 max_message_size = None
00148
00149
00150
00151
00152 if "--port" in sys.argv:
00153 idx = sys.argv.index("--port") + 1
00154 if idx < len(sys.argv):
00155 port = int(sys.argv[idx])
00156 else:
00157 print "--port argument provided without a value."
00158 sys.exit(-1)
00159
00160 if "--host" in sys.argv:
00161 idx = sys.argv.index("--host") + 1
00162 if idx < len(sys.argv):
00163 host = str(sys.argv[idx])
00164 else:
00165 print "--host argument provided without a value."
00166 sys.exit(-1)
00167
00168 if "--incoming_buffer" in sys.argv:
00169 idx = sys.argv.index("--incoming_buffer") + 1
00170 if idx < len(sys.argv):
00171 incoming_buffer = int(sys.argv[idx])
00172 else:
00173 print "--incoming_buffer argument provided without a value."
00174 sys.exit(-1)
00175
00176 if "--socket_timeout" in sys.argv:
00177 idx = sys.argv.index("--socket_timeout") + 1
00178 if idx < len(sys.argv):
00179 socket_timeout = int(sys.argv[idx])
00180 else:
00181 print "--socket_timeout argument provided without a value."
00182 sys.exit(-1)
00183
00184 if "--retry_startup_delay" in sys.argv:
00185 idx = sys.argv.index("--retry_startup_delay") + 1
00186 if idx < len(sys.argv):
00187 retry_startup_delay = int(sys.argv[idx])
00188 else:
00189 print "--retry_startup_delay argument provided without a value."
00190 sys.exit(-1)
00191
00192 if "--service_request_timeout" in sys.argv:
00193 idx = sys.argv.index("--service_request_timeout") + 1
00194 if idx < len(sys.argv):
00195 service_request_timeout = int(sys.argv[idx])
00196 else:
00197 print "--service_request_timeout argument provided without a value."
00198 sys.exit(-1)
00199
00200 if "--check_response_delay" in sys.argv:
00201 idx = sys.argv.index("--check_response_delay") + 1
00202 if idx < len(sys.argv):
00203 check_response_delay = float(sys.argv[idx])
00204 else:
00205 print "--check_response_delay argument provided without a value."
00206 sys.exit(-1)
00207
00208 if "--wait_for_busy_service_provider" in sys.argv:
00209 idx = sys.argv.index("--wait_for_busy_service_provider") + 1
00210 if idx < len(sys.argv):
00211 wait_for_busy_service_provider = float(sys.argv[idx])
00212 else:
00213 print "--wait_for_busy_service_provider argument provided without a value."
00214 sys.exit(-1)
00215
00216 if "--max_service_requests" in sys.argv:
00217 idx = sys.argv.index("--max_service_requests") + 1
00218 if idx < len(sys.argv):
00219 max_service_requests = int(sys.argv[idx])
00220 else:
00221 print "--max_service_requests argument provided without a value."
00222 sys.exit(-1)
00223
00224 if "--fragment_timeout" in sys.argv:
00225 idx = sys.argv.index("--fragment_timeout") + 1
00226 if idx < len(sys.argv):
00227 fragment_timeout = int(sys.argv[idx])
00228 else:
00229 print "--fragment_timeout argument provided without a value."
00230 sys.exit(-1)
00231
00232 if "--delay_between_messages" in sys.argv:
00233 idx = sys.argv.index("--delay_between_messages") + 1
00234 if idx < len(sys.argv):
00235 delay_between_messages = float(sys.argv[idx])
00236 else:
00237 print "--delay_between_messages argument provided without a value."
00238 sys.exit(-1)
00239
00240 if "--max_message_size" in sys.argv:
00241 idx = sys.argv.index("--max_message_size") + 1
00242 if idx < len(sys.argv):
00243 value = sys.argv[idx]
00244 if value == "None":
00245 max_message_size = None
00246 else:
00247 max_message_size = int(value)
00248 else:
00249 print "--max_message_size argument provided without a value. (can be None or <Integer>)"
00250 sys.exit(-1)
00251
00252 """
00253 ...END (parameter handling)
00254 """
00255
00256
00257
00258
00259 server = SocketServer.ThreadingTCPServer((host, port), RosbridgeTcpSocket)
00260
00261 loginfo("Rosbridge TCP server started on port %d", port)
00262
00263 server.serve_forever()
00264 loaded = True
00265 except Exception, e:
00266 time.sleep(retry_startup_delay)
00267 print "server loaded"