udp_handler.py
Go to the documentation of this file.
1 import rospy
2 from rosauth.srv import Authentication
3 from rosbridge_library.rosbridge_protocol import RosbridgeProtocol
4 from rosbridge_library.util import json, bson
5 
6 from twisted.internet.protocol import DatagramProtocol,Factory
7 
8 class RosbridgeUdpFactory(DatagramProtocol):
9  def startProtocol(self):
10  self.socks = dict()
11  def datagramReceived(self, message, source_addr):
12  (host, port) = source_addr
13  endpoint = host.__str__() + port.__str__()
14  if endpoint in self.socks:
15  self.socks[endpoint].datagramReceived(message)
16  else:
17  writefunc = lambda msg: self.transport.write(msg, (host,port))
18  self.socks[endpoint] = RosbridgeUdpSocket(writefunc)
19  self.socks[endpoint].startProtocol()
20  self.socks[endpoint].datagramReceived(message)
21 
23  client_id_seed = 0
24  clients_connected = 0
25  client_count_pub = None
26  authenticate = False
27 
28  # The following parameters are passed on to RosbridgeProtocol
29  # defragmentation.py:
30  fragment_timeout = 600 # seconds
31  # protocol.py:
32  delay_between_messages = 0 # seconds
33  max_message_size = None # bytes
34  unregister_timeout = 10.0 # seconds
35 
36  def __init__(self,write):
37  self.write = write
38  self.authenticated = False
39 
40  def startProtocol(self):
41  cls = self.__class__
42  parameters = {
43  "fragment_timeout": cls.fragment_timeout,
44  "delay_between_messages": cls.delay_between_messages,
45  "max_message_size": cls.max_message_size,
46  "unregister_timeout": cls.unregister_timeout
47  }
48  try:
49  self.protocol = RosbridgeProtocol(cls.client_id_seed, parameters=parameters)
50  self.protocol.outgoing = self.send_message
51  self.authenticated = False
52  cls.client_id_seed += 1
53  cls.clients_connected += 1
54  if cls.client_count_pub:
55  cls.client_count_pub.publish(cls.clients_connected)
56  except Exception as exc:
57  rospy.logerr("Unable to accept incoming connection. Reason: %s", str(exc))
58  rospy.loginfo("Client connected. %d clients total.", cls.clients_connected)
59  if cls.authenticate:
60  rospy.loginfo("Awaiting proper authentication...")
61 
62  def datagramReceived(self, message):
63  cls = self.__class__
64  # check if we need to authenticate
65  if cls.authenticate and not self.authenticated:
66  try:
67  msg = json.loads(message)
68  if msg['op'] == 'auth':
69  # check the authorization information
70  auth_srv = rospy.ServiceProxy('authenticate', Authentication)
71  resp = auth_srv(msg['mac'], msg['client'], msg['dest'],
72  msg['rand'], rospy.Time(msg['t']), msg['level'],
73  rospy.Time(msg['end']))
74  self.authenticated = resp.authenticated
75  if self.authenticated:
76  rospy.loginfo("Client %d has authenticated.", self.protocol.client_id)
77  return
78  # if we are here, no valid authentication was given
79  rospy.logwarn("Client %d did not authenticate. Closing connection.",
80  self.protocol.client_id)
81  self.close()
82  except:
83  # proper error will be handled in the protocol class
84  self.protocol.incoming(message)
85  else:
86  # no authentication required
87  self.protocol.incoming(message)
88 
89  def stopProtocol(self):
90  cls = self.__class__
91  cls.clients_connected -= 1
92  self.protocol.finish()
93  if cls.client_count_pub:
94  cls.client_count_pub.publish(cls.clients_connected)
95  rospy.loginfo("Client disconnected. %d clients total.", cls.clients_connected)
96  def send_message(self, message):
97  binary = type(message)==bson.BSON
98  self.write(message)
99  def check_origin(self, origin):
100  return False
def datagramReceived(self, message, source_addr)
Definition: udp_handler.py:11


rosbridge_server
Author(s): Jonathan Mace
autogenerated on Wed Jun 3 2020 03:55:18