rosbridge_tcp.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from rospy import init_node, get_param, loginfo, logerr, on_shutdown
00004 from rosbridge_server import RosbridgeTcpSocket
00005 
00006 from functools import partial
00007 from signal import signal, SIGINT, SIG_DFL
00008 
00009 import SocketServer
00010 import sys
00011 import time
00012 
00013 #TODO: take care of socket timeouts and make sure to close sockets after killing programm to release network ports
00014 
00015 #TODO: add new parameters to websocket version! those of rosbridge_tcp.py might not be needed, but the others should work well when adding them to .._websocket.py
00016 
00017 
00018 def shutdown_hook(server):
00019         server.shutdown()
00020 
00021 if __name__ == "__main__":
00022     loaded = False
00023     retry_count = 0
00024     while not loaded:
00025         retry_count += 1
00026         print "trying to start rosbridge TCP server.."
00027         try:
00028             print ""
00029             init_node("rosbridge_tcp")
00030             signal(SIGINT, SIG_DFL)
00031 
00032             """
00033             Parameter handling:
00034              - try to get parameter from parameter server (..define those via launch-file)
00035              - overwrite value if given as commandline-parameter
00036 
00037             BEGIN...
00038             """
00039 
00040 #TODO: ensure types get cast correctly after getting from parameter server
00041 #TODO: check if ROS parameter server uses None string for 'None-value' or Null or something else, then change code accordingly
00042 
00043             # update parameters from parameter server or use default value ( second parameter of get_param )
00044             port = get_param('~port', RosbridgeTcpSocket.port)
00045             host = get_param('~host', RosbridgeTcpSocket.host)
00046             incoming_buffer = get_param('~incoming_buffer', RosbridgeTcpSocket.incoming_buffer)
00047             socket_timeout = get_param('~socket_timeout', RosbridgeTcpSocket.socket_timeout)
00048             retry_startup_delay = get_param('~retry_startup_delay', RosbridgeTcpSocket.retry_startup_delay)
00049             service_request_timeout = get_param('~service_request_timeout', RosbridgeTcpSocket.service_request_timeout)
00050             check_response_delay = get_param('~check_response_delay', RosbridgeTcpSocket.check_response_delay)
00051             wait_for_busy_service_provider = get_param('~wait_for_busy_service_provider', RosbridgeTcpSocket.wait_for_busy_service_provider)
00052             max_service_requests = get_param('~max_service_requests', RosbridgeTcpSocket.max_service_requests)
00053             fragment_timeout = get_param('~fragment_timeout', RosbridgeTcpSocket.fragment_timeout)
00054             delay_between_messages = get_param('~delay_between_messages', RosbridgeTcpSocket.delay_between_messages)
00055             max_message_size = get_param('~max_message_size', RosbridgeTcpSocket.max_message_size)
00056             if max_message_size == "None":
00057                 max_message_size = None
00058 
00059 
00060             # update parameters if provided via commandline
00061             # .. could implemented 'better' (value/type checking, etc.. )
00062             if "--port" in sys.argv:
00063                 idx = sys.argv.index("--port") + 1
00064                 if idx < len(sys.argv):
00065                     port = int(sys.argv[idx])
00066                 else:
00067                     print "--port argument provided without a value."
00068                     sys.exit(-1)
00069 
00070             if "--host" in sys.argv:
00071                 idx = sys.argv.index("--host") + 1
00072                 if idx < len(sys.argv):
00073                     host = str(sys.argv[idx])
00074                 else:
00075                     print "--host argument provided without a value."
00076                     sys.exit(-1)
00077 
00078             if "--incoming_buffer" in sys.argv:
00079                 idx = sys.argv.index("--incoming_buffer") + 1
00080                 if idx < len(sys.argv):
00081                     incoming_buffer = int(sys.argv[idx])
00082                 else:
00083                     print "--incoming_buffer argument provided without a value."
00084                     sys.exit(-1)
00085 
00086             if "--socket_timeout" in sys.argv:
00087                 idx = sys.argv.index("--socket_timeout") + 1
00088                 if idx < len(sys.argv):
00089                     socket_timeout = int(sys.argv[idx])
00090                 else:
00091                     print "--socket_timeout argument provided without a value."
00092                     sys.exit(-1)
00093 
00094             if "--retry_startup_delay" in sys.argv:
00095                 idx = sys.argv.index("--retry_startup_delay") + 1
00096                 if idx < len(sys.argv):
00097                     retry_startup_delay = int(sys.argv[idx])
00098                 else:
00099                     print "--retry_startup_delay argument provided without a value."
00100                     sys.exit(-1)
00101 
00102             if "--service_request_timeout" in sys.argv:
00103                 idx = sys.argv.index("--service_request_timeout") + 1
00104                 if idx < len(sys.argv):
00105                     service_request_timeout = int(sys.argv[idx])
00106                 else:
00107                     print "--service_request_timeout argument provided without a value."
00108                     sys.exit(-1)
00109 
00110             if "--check_response_delay" in sys.argv:
00111                 idx = sys.argv.index("--check_response_delay") + 1
00112                 if idx < len(sys.argv):
00113                     check_response_delay = float(sys.argv[idx])
00114                 else:
00115                     print "--check_response_delay argument provided without a value."
00116                     sys.exit(-1)
00117 
00118             if "--wait_for_busy_service_provider" in sys.argv:
00119                 idx = sys.argv.index("--wait_for_busy_service_provider") + 1
00120                 if idx < len(sys.argv):
00121                     wait_for_busy_service_provider = float(sys.argv[idx])
00122                 else:
00123                     print "--wait_for_busy_service_provider argument provided without a value."
00124                     sys.exit(-1)
00125 
00126             if "--max_service_requests" in sys.argv:
00127                 idx = sys.argv.index("--max_service_requests") + 1
00128                 if idx < len(sys.argv):
00129                     max_service_requests = int(sys.argv[idx])
00130                 else:
00131                     print "--max_service_requests argument provided without a value."
00132                     sys.exit(-1)
00133 
00134             if "--fragment_timeout" in sys.argv:
00135                 idx = sys.argv.index("--fragment_timeout") + 1
00136                 if idx < len(sys.argv):
00137                     fragment_timeout = int(sys.argv[idx])
00138                 else:
00139                     print "--fragment_timeout argument provided without a value."
00140                     sys.exit(-1)
00141 
00142             if "--delay_between_messages" in sys.argv:
00143                 idx = sys.argv.index("--delay_between_messages") + 1
00144                 if idx < len(sys.argv):
00145                     delay_between_messages = float(sys.argv[idx])
00146                 else:
00147                     print "--delay_between_messages argument provided without a value."
00148                     sys.exit(-1)
00149 
00150             if "--max_message_size" in sys.argv:
00151                 idx = sys.argv.index("--max_message_size") + 1
00152                 if idx < len(sys.argv):
00153                     value = sys.argv[idx]
00154                     if value == "None":
00155                         max_message_size = None
00156                     else:
00157                         max_message_size = int(value)
00158                 else:
00159                     print "--max_message_size argument provided without a value. (can be None or <Integer>)"
00160                     sys.exit(-1)
00161 
00162             # export parameters to handler class
00163             RosbridgeTcpSocket.port = port
00164             RosbridgeTcpSocket.host = host
00165             RosbridgeTcpSocket.incoming_buffer = incoming_buffer
00166             RosbridgeTcpSocket.socket_timeout = socket_timeout
00167             RosbridgeTcpSocket.retry_startup_delay = retry_startup_delay
00168             RosbridgeTcpSocket.service_request_timeout = service_request_timeout
00169             RosbridgeTcpSocket.check_response_delay = check_response_delay
00170             RosbridgeTcpSocket.wait_for_busy_service_provider = wait_for_busy_service_provider
00171             RosbridgeTcpSocket.max_service_requests = max_service_requests
00172             RosbridgeTcpSocket.fragment_timeout = fragment_timeout
00173             RosbridgeTcpSocket.delay_between_messages = delay_between_messages
00174             RosbridgeTcpSocket.max_message_size = max_message_size
00175 
00176             """
00177             ...END (parameter handling)
00178             """
00179 
00180 
00181             # Server host is a tuple ('host', port)
00182             # empty string for host makes server listen on all available interfaces
00183             server = SocketServer.ThreadingTCPServer((host, port), RosbridgeTcpSocket)
00184             on_shutdown(partial(shutdown_hook, server))
00185 
00186             loginfo("Rosbridge TCP server started on port %d", port)
00187 
00188             server.serve_forever()
00189             loaded = True
00190         except Exception, e:
00191             time.sleep(retry_startup_delay)
00192     print "server loaded"


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