rosbridge.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 from roslib import load_manifest
00003 load_manifest('rosbridge_server')
00004 from rospy import init_node, get_param, loginfo, logerr
00005 
00006 from signal import signal, SIGINT, SIG_DFL
00007 from functools import partial
00008 
00009 from tornado.ioloop import IOLoop
00010 from tornado.web import Application
00011 from tornado.websocket import WebSocketHandler
00012 
00013 from rosbridge_library.rosbridge_protocol import RosbridgeProtocol
00014 
00015 
00016 import sys
00017 
00018 # Global ID seed for clients
00019 client_id_seed = 0
00020 clients_connected = 0
00021 
00022 
00023 class RosbridgeWebSocket(WebSocketHandler):
00024 
00025     def open(self):
00026         global client_id_seed, clients_connected
00027         try:
00028             self.protocol = RosbridgeProtocol(client_id_seed)
00029             self.protocol.outgoing = self.send_message
00030             client_id_seed = client_id_seed + 1
00031             clients_connected = clients_connected + 1
00032         except Exception as exc:
00033             logerr("Unable to accept incoming connection.  Reason: %s", str(exc))
00034 #            raise
00035         loginfo("Client connected.  %d clients total.", clients_connected)
00036 
00037     def on_message(self, message):
00038         self.protocol.incoming(message)
00039 
00040     def on_close(self):
00041         global clients_connected
00042         clients_connected = clients_connected - 1
00043         self.protocol.finish()
00044         loginfo("Client disconnected.  %d clients total.", clients_connected)
00045 
00046     def send_message(self, message):
00047         IOLoop.instance().add_callback(partial(self.write_message, message))
00048 
00049     def allow_draft76(self):
00050         """Override to enable support for the older "draft76" protocol.
00051 
00052         The draft76 version of the websocket protocol is disabled by
00053         default due to security concerns, but it can be enabled by
00054         overriding this method to return True.
00055 
00056         Connections using the draft76 protocol do not support the
00057         ``binary=True`` flag to `write_message`.
00058 
00059         Support for the draft76 protocol is deprecated and will be
00060         removed in a future version of Tornado.
00061         """
00062         return True
00063 
00064 
00065 if __name__ == "__main__":
00066     init_node("rosbridge_server")
00067     signal(SIGINT, SIG_DFL)
00068 
00069     certfile = get_param('~certfile', None)
00070     keyfile = get_param('~keyfile', None)
00071     port = get_param('/rosbridge/port', 9090)
00072     if "--port" in sys.argv:
00073         idx = sys.argv.index("--port")+1
00074         if idx < len(sys.argv):
00075             port = int(sys.argv[idx])
00076         else:
00077             print "--port argument provided without a value."
00078             sys.exit(-1)
00079     
00080     
00081     application = Application([(r"/", RosbridgeWebSocket), (r"", RosbridgeWebSocket)])
00082     if certfile is not None and keyfile is not None:
00083         application.listen(port, ssl_options={ "certfile": certfile, "keyfile": keyfile})
00084     else:
00085         application.listen(port)
00086     loginfo("Rosbridge server started on port %d", port)
00087 
00088     IOLoop.instance().start()


rosbridge_server
Author(s): Jonathan Mace
autogenerated on Thu Jan 2 2014 11:53:55