Go to the documentation of this file.00001 import rospy
00002 from rosbridge_library.rosbridge_protocol import RosbridgeProtocol
00003 from rosbridge_library.util import json
00004 import bson
00005
00006 from twisted.internet.protocol import DatagramProtocol,Factory
00007
00008 class RosbridgeUdpFactory(DatagramProtocol):
00009 def startProtocol(self):
00010 self.socks = dict()
00011 def datagramReceived(self, message, (host, port)):
00012 endpoint = host.__str__() + port.__str__()
00013 if endpoint in self.socks:
00014 self.socks[endpoint].datagramReceived(message)
00015 else:
00016 writefunc = lambda msg: self.transport.write(msg, (host,port))
00017 self.socks[endpoint] = RosbridgeUdpSocket(writefunc)
00018 self.socks[endpoint].startProtocol()
00019 self.socks[endpoint].datagramReceived(message)
00020
00021 class RosbridgeUdpSocket:
00022 client_id_seed = 0
00023 clients_connected = 0
00024 authenticate = False
00025
00026
00027
00028 fragment_timeout = 600
00029
00030 delay_between_messages = 0
00031 max_message_size = None
00032
00033 def __init__(self,write):
00034 self.write = write
00035 self.authenticated = False
00036
00037 def startProtocol(self):
00038 cls = self.__class__
00039 parameters = {
00040 "fragment_timeout": cls.fragment_timeout,
00041 "delay_between_messages": cls.delay_between_messages,
00042 "max_message_size": cls.max_message_size
00043 }
00044 try:
00045 self.protocol = RosbridgeProtocol(cls.client_id_seed, parameters=parameters)
00046 self.protocol.outgoing = self.send_message
00047 self.authenticated = False
00048 cls.client_id_seed += 1
00049 cls.clients_connected += 1
00050 except Exception as exc:
00051 rospy.logerr("Unable to accept incoming connection. Reason: %s", str(exc))
00052 rospy.loginfo("Client connected. %d clients total.", cls.clients_connected)
00053 if cls.authenticate:
00054 rospy.loginfo("Awaiting proper authentication...")
00055
00056 def datagramReceived(self, message):
00057 cls = self.__class__
00058
00059 if cls.authenticate and not self.authenticated:
00060 try:
00061 msg = json.loads(message)
00062 if msg['op'] == 'auth':
00063
00064 auth_srv = rospy.ServiceProxy('authenticate', Authentication)
00065 resp = auth_srv(msg['mac'], msg['client'], msg['dest'],
00066 msg['rand'], rospy.Time(msg['t']), msg['level'],
00067 rospy.Time(msg['end']))
00068 self.authenticated = resp.authenticated
00069 if self.authenticated:
00070 rospy.loginfo("Client %d has authenticated.", self.protocol.client_id)
00071 return
00072
00073 rospy.logwarn("Client %d did not authenticate. Closing connection.",
00074 self.protocol.client_id)
00075 self.close()
00076 except:
00077
00078 self.protocol.incoming(message)
00079 else:
00080
00081 self.protocol.incoming(message)
00082
00083 def stopProtocol(self):
00084 cls = self.__class__
00085 cls.clients_connected -= 1
00086 self.protocol.finish()
00087 rospy.loginfo("Client disconnected. %d clients total.", cls.clients_connected)
00088 def send_message(self, message):
00089 binary = type(message)==bson.BSON
00090 self.write(message)
00091 def check_origin(self, origin):
00092 return False