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 rosbridge_library.capabilities.advertise import Advertise
00007 from rosbridge_library.capabilities.publish import Publish
00008 from rosbridge_library.capabilities.subscribe import Subscribe
00009 from rosbridge_library.capabilities.advertise_service import AdvertiseService
00010 from rosbridge_library.capabilities.unadvertise_service import UnadvertiseService
00011 from rosbridge_library.capabilities.call_service import CallService
00012 
00013 from functools import partial
00014 from signal import signal, SIGINT, SIG_DFL
00015 
00016 import SocketServer
00017 import sys
00018 import time
00019 
00020 #TODO: take care of socket timeouts and make sure to close sockets after killing programm to release network ports
00021 
00022 #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
00023 
00024 
00025 def shutdown_hook(server):
00026         server.shutdown()
00027 
00028 if __name__ == "__main__":
00029     loaded = False
00030     retry_count = 0
00031     while not loaded:
00032         retry_count += 1
00033         print "trying to start rosbridge TCP server.."
00034         try:
00035             print ""
00036             init_node("rosbridge_tcp")
00037             signal(SIGINT, SIG_DFL)
00038 
00039             """
00040             Parameter handling:
00041              - try to get parameter from parameter server (..define those via launch-file)
00042              - overwrite value if given as commandline-parameter
00043 
00044             BEGIN...
00045             """
00046 
00047 #TODO: ensure types get cast correctly after getting from parameter server
00048 #TODO: check if ROS parameter server uses None string for 'None-value' or Null or something else, then change code accordingly
00049 
00050             # update parameters from parameter server or use default value ( second parameter of get_param )
00051             port = get_param('~port', 9090)
00052             host = get_param('~host', '')
00053             incoming_buffer = get_param('~incoming_buffer', RosbridgeTcpSocket.incoming_buffer)
00054             socket_timeout = get_param('~socket_timeout', RosbridgeTcpSocket.socket_timeout)
00055             retry_startup_delay = get_param('~retry_startup_delay', 5.0)  # seconds
00056             fragment_timeout = get_param('~fragment_timeout', RosbridgeTcpSocket.fragment_timeout)
00057             delay_between_messages = get_param('~delay_between_messages', RosbridgeTcpSocket.delay_between_messages)
00058             max_message_size = get_param('~max_message_size', RosbridgeTcpSocket.max_message_size)
00059             bson_only_mode = get_param('~bson_only_mode', False)
00060 
00061             if max_message_size == "None":
00062                 max_message_size = None
00063 
00064             # Get the glob strings and parse them as arrays.
00065             RosbridgeTcpSocket.topics_glob = [
00066                     element.strip().strip("'")
00067                     for element in get_param('~topics_glob', '')[1:-1].split(',')
00068                     if len(element.strip().strip("'")) > 0]
00069             RosbridgeTcpSocket.services_glob = [
00070                     element.strip().strip("'")
00071                     for element in get_param('~services_glob', '')[1:-1].split(',')
00072                     if len(element.strip().strip("'")) > 0]
00073             RosbridgeTcpSocket.params_glob = [
00074                     element.strip().strip("'")
00075                     for element in get_param('~params_glob', '')[1:-1].split(',')
00076                     if len(element.strip().strip("'")) > 0]
00077         
00078             # update parameters if provided via commandline
00079             # .. could implemented 'better' (value/type checking, etc.. )
00080             if "--port" in sys.argv:
00081                 idx = sys.argv.index("--port") + 1
00082                 if idx < len(sys.argv):
00083                     port = int(sys.argv[idx])
00084                 else:
00085                     print "--port argument provided without a value."
00086                     sys.exit(-1)
00087 
00088             if "--host" in sys.argv:
00089                 idx = sys.argv.index("--host") + 1
00090                 if idx < len(sys.argv):
00091                     host = str(sys.argv[idx])
00092                 else:
00093                     print "--host argument provided without a value."
00094                     sys.exit(-1)
00095 
00096             if "--incoming_buffer" in sys.argv:
00097                 idx = sys.argv.index("--incoming_buffer") + 1
00098                 if idx < len(sys.argv):
00099                     incoming_buffer = int(sys.argv[idx])
00100                 else:
00101                     print "--incoming_buffer argument provided without a value."
00102                     sys.exit(-1)
00103 
00104             if "--socket_timeout" in sys.argv:
00105                 idx = sys.argv.index("--socket_timeout") + 1
00106                 if idx < len(sys.argv):
00107                     socket_timeout = int(sys.argv[idx])
00108                 else:
00109                     print "--socket_timeout argument provided without a value."
00110                     sys.exit(-1)
00111 
00112             if "--retry_startup_delay" in sys.argv:
00113                 idx = sys.argv.index("--retry_startup_delay") + 1
00114                 if idx < len(sys.argv):
00115                     retry_startup_delay = int(sys.argv[idx])
00116                 else:
00117                     print "--retry_startup_delay argument provided without a value."
00118                     sys.exit(-1)
00119 
00120             if "--fragment_timeout" in sys.argv:
00121                 idx = sys.argv.index("--fragment_timeout") + 1
00122                 if idx < len(sys.argv):
00123                     fragment_timeout = int(sys.argv[idx])
00124                 else:
00125                     print "--fragment_timeout argument provided without a value."
00126                     sys.exit(-1)
00127 
00128             if "--delay_between_messages" in sys.argv:
00129                 idx = sys.argv.index("--delay_between_messages") + 1
00130                 if idx < len(sys.argv):
00131                     delay_between_messages = float(sys.argv[idx])
00132                 else:
00133                     print "--delay_between_messages argument provided without a value."
00134                     sys.exit(-1)
00135 
00136             if "--max_message_size" in sys.argv:
00137                 idx = sys.argv.index("--max_message_size") + 1
00138                 if idx < len(sys.argv):
00139                     value = sys.argv[idx]
00140                     if value == "None":
00141                         max_message_size = None
00142                     else:
00143                         max_message_size = int(value)
00144                 else:
00145                     print "--max_message_size argument provided without a value. (can be None or <Integer>)"
00146                     sys.exit(-1)
00147 
00148             # export parameters to handler class
00149             RosbridgeTcpSocket.incoming_buffer = incoming_buffer
00150             RosbridgeTcpSocket.socket_timeout = socket_timeout
00151             RosbridgeTcpSocket.fragment_timeout = fragment_timeout
00152             RosbridgeTcpSocket.delay_between_messages = delay_between_messages
00153             RosbridgeTcpSocket.max_message_size = max_message_size
00154             RosbridgeTcpSocket.bson_only_mode = bson_only_mode
00155 
00156 
00157             if "--topics_glob" in sys.argv:
00158                 idx = sys.argv.index("--topics_glob") + 1
00159                 if idx < len(sys.argv):
00160                     value = sys.argv[idx]
00161                     if value == "None":
00162                         RosbridgeTcpSocket.topics_glob = []
00163                     else:
00164                         RosbridgeTcpSocket.topics_glob = [element.strip().strip("'") for element in value[1:-1].split(',')]
00165                 else:
00166                     print "--topics_glob argument provided without a value. (can be None or a list)"
00167                     sys.exit(-1)
00168 
00169             if "--services_glob" in sys.argv:
00170                 idx = sys.argv.index("--services_glob") + 1
00171                 if idx < len(sys.argv):
00172                     value = sys.argv[idx]
00173                     if value == "None":
00174                         RosbridgeTcpSocket.services_glob = []
00175                     else:
00176                         RosbridgeTcpSocket.services_glob = [element.strip().strip("'") for element in value[1:-1].split(',')]
00177                 else:
00178                     print "--services_glob argument provided without a value. (can be None or a list)"
00179                     sys.exit(-1)
00180 
00181             if "--params_glob" in sys.argv:
00182                 idx = sys.argv.index("--params_glob") + 1
00183                 if idx < len(sys.argv):
00184                     value = sys.argv[idx]
00185                     if value == "None":
00186                         RosbridgeTcpSocket.params_glob = []
00187                     else:
00188                         RosbridgeTcpSocket.params_glob = [element.strip().strip("'") for element in value[1:-1].split(',')]
00189                 else:
00190                     print "--params_glob argument provided without a value. (can be None or a list)"
00191                     sys.exit(-1)
00192 
00193             if "--bson_only_mode" in sys.argv:
00194                 bson_only_mode = True
00195 
00196             # To be able to access the list of topics and services, you must be able to access the rosapi services.
00197             if RosbridgeTcpSocket.services_glob:
00198                 RosbridgeTcpSocket.services_glob.append("/rosapi/*")
00199 
00200             Subscribe.topics_glob = RosbridgeTcpSocket.topics_glob
00201             Advertise.topics_glob = RosbridgeTcpSocket.topics_glob
00202             Publish.topics_glob = RosbridgeTcpSocket.topics_glob
00203             AdvertiseService.services_glob = RosbridgeTcpSocket.services_glob
00204             UnadvertiseService.services_glob = RosbridgeTcpSocket.services_glob
00205             CallService.services_glob = RosbridgeTcpSocket.services_glob
00206 
00207             """
00208             ...END (parameter handling)
00209             """
00210 
00211 
00212             # Server host is a tuple ('host', port)
00213             # empty string for host makes server listen on all available interfaces
00214             SocketServer.ThreadingTCPServer.allow_reuse_address = True
00215             server = SocketServer.ThreadingTCPServer((host, port), RosbridgeTcpSocket)
00216             on_shutdown(partial(shutdown_hook, server))
00217 
00218             loginfo("Rosbridge TCP server started on port %d", port)
00219 
00220             server.serve_forever()
00221             loaded = True
00222         except Exception, e:
00223             time.sleep(retry_startup_delay)
00224     print "server loaded"


rosbridge_server
Author(s): Jonathan Mace
autogenerated on Wed Sep 13 2017 03:18:20