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


rosbridge_server
Author(s): Jonathan Mace
autogenerated on Thu Jun 6 2019 21:51:50