コード例 #1
0
 def test_endpoint(self):
     endpoint = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                     '192.168.0.1', 12345)
     endpoint_equal = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                           '192.168.0.1', 12345)
     self.assertEqual(
         endpoint, endpoint_equal,
         "equal AddressBook.Endpoint are not equal, expected: True, got: False"
     )
     endpoint_nequal = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                            '192.168.0.1', 54321)
     self.assertNotEqual(
         endpoint, endpoint_nequal,
         "not equal AddressBook.Endpoint are equal, expected: False, got: True"
     )
     self.assertEqual(
         'UDP', endpoint.etype_str(),
         "wrong etype as str in AddressBook.Endpoint, expected: %s, got: %s"
         % ('UDP', endpoint.etype_str()))
     self.assertEqual(
         '192.168.0.1:12345', endpoint.address_str(),
         "wrong address as str in AddressBook.Endpoint, expected: %s, got: %s"
         % ('192.168.0.1:12345', endpoint.address_str()))
     try:
         '%s' % endpoint
     except Exception as err:
         self.fail(
             "AddressBook.Endpoint.__repr__() raised Exception unexpectedly: %s"
             % err)
コード例 #2
0
 def test_add_jaus_address(self):
     addrbook = AddressBook(default_port=1234, addrbook_udp={})
     addrbook.add_jaus_address(JausAddress.from_string('1.1.1'),
                               '192.168.0.1', 12345,
                               AddressBook.Endpoint.UDP)
     self.assertEqual(
         1, len(addrbook._map),
         "wrong count of discovered UDP addresses after add address, expected: %d, got: %d"
         % (1, len(addrbook._map)))
     addrbook.add_jaus_address(JausAddress.from_string('1.1.1'),
                               '192.168.0.2', 12345,
                               AddressBook.Endpoint.UDP)
     self.assertEqual(
         1, len(addrbook._map),
         "wrong count of discovered UDP addresses after add same address, expected: %d, got: %d"
         % (1, len(addrbook._map)))
     addrbook.remove(JausAddress.from_string('1.1.2'))
     self.assertEqual(
         1, len(addrbook._map),
         "wrong count of discovered UDP addresses after remove wrong jaus address, expected: %d, got: %d"
         % (1, len(addrbook._map)))
     addrbook.remove(JausAddress.from_string('1.1.1'))
     self.assertEqual(
         0, len(addrbook._map),
         "wrong count of discovered UDP addresses after remove, expected: %d, got: %d"
         % (0, len(addrbook._map)))
コード例 #3
0
 def test_apply_destination(self):
     addrbook = AddressBook(default_port=1234,
                            addrbook_udp={'192.168.0.2:12345': '2.255.255'})
     addrbook.add_jaus_address(JausAddress.from_string('1.1.1'),
                               '192.168.0.1', 12345,
                               AddressBook.Endpoint.UDP)
     # from dynamic address
     msg = Message()
     msg.dst_id = JausAddress.from_string('1.1.1')
     apply_res = addrbook.apply_destination(msg)
     self.assertEqual(True, apply_res, "can not appy destination")
     dst_endpoint = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                         '192.168.0.1', 12345)
     self.assertEqual(
         dst_endpoint, msg.tinfo_dst,
         "wrong endpoint applied, expected: %s, got: %s" %
         (dst_endpoint, msg.tinfo_dst))
     # from static udp
     msg = Message()
     msg.dst_id = JausAddress.from_string('2.1.1')
     apply_res = addrbook.apply_destination(msg)
     self.assertEqual(True, apply_res,
                      "can not appy destination from static udp")
     dst_endpoint2 = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                          '192.168.0.2', 12345)
     self.assertEqual(
         dst_endpoint2, msg.tinfo_dst,
         "wrong endpoint applied, should be from static udp, expected: %s, got: %s"
         % (dst_endpoint, msg.tinfo_dst))
     # test for not exiting jaus id
     msg = Message()
     msg.dst_id = JausAddress.from_string('3.1.1')
     apply_res = addrbook.apply_destination(msg)
     self.assertEqual(False, apply_res,
                      "applied destination for not existing jaus id")
コード例 #4
0
ファイル: udp_mc.py プロジェクト: fkie/iop_node_manager
 def _loop_recv(self):
     '''
     This method handles the received multicast messages.
     '''
     while not self._closed:
         try:
             (data, address) = self.recvfrom(self._recv_buffer)
             if data and not self._closed and address[
                     0] not in self._locals:  # skip messages received from self
                 msgs = self._parser_mcast.unpack(data)
                 for msg in msgs:
                     try:
                         msg.tinfo_src = self._sender_endpoints[address]
                     except KeyError:
                         endpoint = AddressBook.Endpoint(
                             AddressBook.Endpoint.UDP, address[0],
                             address[1])
                         msg.tinfo_src = endpoint
                         self._sender_endpoints[address] = endpoint
                     # self.logger.debug("Received from %s" % (msg.tinfo_src))
                     self._router.route_udp_msg(msg)
         except socket.timeout:
             pass
         except queue.Full as full_error:
             self.logger.warning(
                 "Error while process received multicast message: %s" %
                 full_error)
         except socket.error:
             if not self._closed:
                 self.logger.warning("socket error: %s" %
                                     traceback.format_exc())
コード例 #5
0
ファイル: uds.py プロジェクト: fkie/iop_node_manager
 def __init__(self,
              name,
              remove_on_close=False,
              force_bind=False,
              root_path='/tmp',
              recv_buffer=5000,
              loglevel='info'):
     self._closed = False
     self.logger = NMLogger('uds[%s]' % name, loglevel)
     self._remove_on_close = remove_on_close
     self._recv_buffer = recv_buffer
     socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_DGRAM)
     self.setblocking(True)
     self._socket_path = os.path.join(root_path, name)
     self._parser = MessageParser(AddressBook.Endpoint(
         AddressBook.Endpoint.UDS, self._socket_path),
                                  loglevel=loglevel)
     if os.path.exists(self._socket_path) and not force_bind:
         self.logger.debug("Connect to local socket %s" % self._socket_path)
         self.connect(self._socket_path)
     else:
         self.logger.debug("Create local socket connection %s" %
                           self._socket_path)
         if os.path.exists(self._socket_path):
             os.unlink(self._socket_path)
         self.bind(self._socket_path)
         os.chmod(self._socket_path,
                  stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
コード例 #6
0
 def __init__(self, host='', port=0, router=None, interface='', logger_name='tcp_client', recv_buffer=5000, queue_length=0, loglevel='info'):
     '''
     :param str host: destination host.
     :param int port: destination port.
     :param router: class which provides `route_tcp_msg(fkie_iop_node_manager.message.Message)` method. If `None` receive will be disabled.
     '''
     self._closed = False
     self._connected = False
     self._connection_error_printed = False
     self.logger = NMLogger('%s[%s:%d]' % (logger_name, host, port), loglevel)
     self._router = router
     self._recv_buffer = recv_buffer
     self._queue_length = queue_length
     self._socket_type = socket.AF_INET
     self._queue_send = queue.PQueue(queue_length, 'queue_%s_send_%s:%d' % (logger_name, host, port), loglevel=loglevel)
     self._raddr = (host, port)
     self.interface = interface
     self._first_send_msg = True
     if self.interface:
         addrinfo = getaddrinfo(self.interface)
         self._socket_type = addrinfo[0]
     self._endpoint_client = AddressBook.Endpoint(AddressBook.Endpoint.TCP, host, port)
     self._message_parser = MessageParser(self._endpoint_client, stream=True, loglevel=loglevel)
     self._thread_connect = threading.Thread(target=self._connect, args=(self._raddr,))
     self._thread_connect.start()
     self._thread_send = threading.Thread(target=self._loop_send)
     self._thread_send.start()
コード例 #7
0
ファイル: test_queue.py プロジェクト: fkie/iop_node_manager
 def test_queue_full(self):
     queue = PQueue(maxsize=2)
     msg = Message()
     first_src = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                      '192.168.0.1', 12345)
     msg.tinfo_src = first_src
     queue.put(msg)
     queue.put(msg)
     self.assertRaises(Full, queue.put, msg)
コード例 #8
0
 def test_create(self):
     addrbook = AddressBook(default_port=1234, addrbook_udp={})
     self.assertEqual(
         1234, addrbook._default_port,
         "wrong default_port after creation AddressBook, expected: %d, got: %d"
         % (1234, addrbook._default_port))
     self.assertEqual(
         0, len(addrbook._static_udp),
         "wrong count of static UDP addresses after creation AddressBook with empty address book, expected: %d, got: %d"
         % (0, len(addrbook._static_udp)))
     addrbook = AddressBook(default_port=1234,
                            addrbook_udp={'192.168.0.1:2345': '1.1.1'})
     self.assertEqual(
         1, len(addrbook._static_udp),
         "wrong count of static UDP addresses after creation AddressBook, expected: %d, got: %d"
         % (1, len(addrbook._static_udp)))
     addrbook = AddressBook(default_port=1234,
                            addrbook_udp={'192.168.0.1': '1.1.1'})
     self.assertEqual(
         1, len(addrbook._static_udp),
         "wrong count of static UDP addresses after creation AddressBook without port, expected: %d, got: %d"
         % (1, len(addrbook._static_udp)))
     addrbook = AddressBook(
         default_port=1234,
         addrbook_udp={'192.168.0.1:2345': ['1.1.1', '1.1.2']})
     self.assertEqual(
         2, len(addrbook._static_udp),
         "wrong count of static UDP addresses after creation AddressBook with list, expected: %d, got: %d"
         % (2, len(addrbook._static_udp)))
     try:
         '%s' % addrbook
     except Exception as err:
         self.fail(
             "AddressBook.__repr__() raised Exception unexpectedly: %s" %
             err)
     # create with invalid port
     try:
         addrbook = AddressBook(
             default_port=1234,
             addrbook_udp={'192.168.0.1:2345s': ['1.1.1', '1.1.2']})
         self.fail(
             "AddressBook raises no Exception on parse not valid port!")
     except Exception:
         pass
コード例 #9
0
ファイル: test_queue.py プロジェクト: fkie/iop_node_manager
 def test_put_with_priority(self):
     queue = PQueue()
     msg = Message()
     self.assertRaises(Exception, queue.put, msg)
     first_src = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                      '192.168.0.1', 12345)
     msg.tinfo_src = first_src
     msg.priority = 1
     queue.put(msg)
     self.assertEqual(
         1, queue.size(),
         "PQueue with prio: wrong count of messages, expected: %d, got: %d"
         % (1, queue.size()))
     msg2 = Message()
     second_src = AddressBook.Endpoint(AddressBook.Endpoint.UDS, '/name')
     msg2.tinfo_src = second_src
     msg2.priority = 3
     queue.put(msg2)
     self.assertEqual(
         2, queue.size(),
         "PQueue with prio: wrong count of messages after second insert, expected: %d, got: %d"
         % (2, queue.size()))
     msgr = queue.get()
     self.assertEqual(
         1, queue.size(),
         "PQueue with prio: wrong count of messages after get, expected: %d, got: %d"
         % (1, queue.size()))
     self.assertEqual(
         second_src, msgr.tinfo_src,
         "PQueue with prio: got wrong first message, expected: %s, got: %s"
         % (second_src, msgr.tinfo_src))
     msgr = queue.get()
     self.assertEqual(
         0, queue.size(),
         "PQueue with prio: wrong count of messages after second get, expected: %d, got: %d"
         % (0, queue.size()))
     self.assertEqual(
         first_src, msgr.tinfo_src,
         "PQueue with prio: got wrong second message, expected: %s, got: %s"
         % (first_src, msgr.tinfo_src))
コード例 #10
0
 def _loop_send(self):
     while not self._closed:
         # Waits for next available Message. This method cancel waiting on clear() of PQueue and return None.
         msg = self._queue_send.get()
         if msg is not None:
             dst = msg.tinfo_dst
             if self._default_dst is not None:
                 # it is a loopback socket, send to fictive debug destination
                 dst = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                            self._default_dst[0],
                                            self._default_dst[1])
             if dst is not None:
                 # send to given addresses
                 self._sendto(msg.bytes(), dst.address, dst.port)
コード例 #11
0
ファイル: udp_mc.py プロジェクト: fkie/iop_node_manager
 def _loop_send(self):
     while not self._closed:
         # Wait for next available Message. This method cancel waiting on clear() of PQueue and return None.
         msg = self._queue_send.get()
         if msg is not None:
             dst = msg.tinfo_dst
             if dst is None:  # or msg.dst_id.has_wildcards():
                 dst = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                            self.mgroup,
                                            self.getsockname()[1])
             if dst is not None:
                 self._sendto(msg, dst)
             else:
                 self.logger.warning(
                     "Can't send message to %s, destination not found!" %
                     (dst))
コード例 #12
0
ファイル: test_queue.py プロジェクト: fkie/iop_node_manager
 def test_clear(self):
     queue = PQueue()
     msg = Message()
     first_src = AddressBook.Endpoint(AddressBook.Endpoint.UDP,
                                      '192.168.0.1', 12345)
     msg.tinfo_src = first_src
     queue.put(msg)
     queue.put(msg)
     queue.put(msg)
     self.assertEqual(
         3, queue.size(),
         "PQueue: wrong count of messages, expected: %d, got: %d" %
         (3, queue.size()))
     queue.clear()
     self.assertEqual(
         0, queue.size(),
         "PQueue: wrong count of messages after clear, expected: %d, got: %d"
         % (0, queue.size()))
コード例 #13
0
ファイル: tcp_input.py プロジェクト: fkie/iop_node_manager
 def __init__(self,
              connection,
              router=None,
              logger_name='tcp_input',
              recv_buffer=5000,
              queue_length=0,
              close_callback=None,
              loglevel='info'):
     '''
     :param (str,int) connection: client address.
     :param router: class which provides `route_tcp_msg(fkie_iop_node_manager.message.Message)` method. If `None` receive will be disabled.
     '''
     self._closed = False
     self._send_error_printed = False
     self._connection = connection
     self._raddr = connection.getpeername()
     self.logger = NMLogger(
         '%s[%s:%d]' % (logger_name, self._raddr[0], self._raddr[1]),
         loglevel)
     self._router = router
     self._recv_buffer = recv_buffer
     self._queue_length = queue_length
     self._close_callback = close_callback
     self._first_send_msg = True
     self._queue_send = queue.PQueue(
         queue_length,
         'queue_%s_send_%s:%d' %
         (logger_name, self._raddr[0], self._raddr[1]),
         loglevel=loglevel)
     self._endpoint_client = AddressBook.Endpoint(AddressBook.Endpoint.TCP,
                                                  self._raddr[0],
                                                  self._raddr[1])
     self._message_parser = MessageParser(self._endpoint_client,
                                          stream=True,
                                          loglevel=loglevel)
     self._thread_send = threading.Thread(target=self._loop_send)
     self._thread_send.start()
     if self._router is not None:
         self._thread_recv = threading.Thread(target=self._loop_recv)
         self._thread_recv.start()
コード例 #14
0
 def _handle_msg(self, msg):
     try:
         if msg is None:
             return
         if msg.dst_id.zero or msg.cmd_code > 0:
             # handle connection requests/closing
             try:
                 self._statistics.add(msg)
                 if msg.cmd_code == Message.CODE_CONNECT:
                     # Connection request from client.
                     self.logger.debug("Connection request from %s" %
                                       msg.src_id)
                     resp = Message()
                     resp.version = Message.AS5669
                     resp.dst_id = msg.src_id
                     resp.cmd_code = Message.CODE_ACCEPT
                     dest_sock = self.create_local_socket(msg.src_id)
                     if dest_sock is not None:
                         dest_sock.send_msg(resp)
                     resp.ts_receive = time.time()
                     resp.tinfo_src = AddressBook.Endpoint(
                         AddressBook.Endpoint.UDS,
                         self._local_socket.socket_path)
                     if dest_sock is not None:
                         resp.tinfo_dst = AddressBook.Endpoint(
                             AddressBook.Endpoint.UDS,
                             dest_sock.socket_path)
                     self._statistics.add(resp)
                 elif msg.cmd_code == Message.CODE_CANCEL:
                     # Disconnect client.
                     self.logger.debug("Disconnect request from %s" %
                                       msg.src_id)
                     self.remove_local_socket(msg.src_id)
             except Exception as e:
                 import traceback
                 print(traceback.format_exc())
                 self.logger.warning(
                     "Error while handle connection management message: %s"
                     % e)
         else:
             # all other message put in priority queue
             try:
                 # override priority
                 if self._priority_map:
                     try:
                         msg_id = int(msg.msg_id)
                         if msg_id in self._priority_map:
                             prio = self._priority_map[msg_id]
                             # self.logger.debug("Override priority for msg ID: 0x%x, current: %d, new: %d" % (msg_id, msg.priority, prio))
                             msg.priority = prio
                     except Exception as err:
                         import traceback
                         print(traceback.format_exc())
                         self.logger.warning(
                             "can not changed priority: %s" % (err))
                 self._router.route_local_msg(msg)
                 if msg.src_id not in self._local_sockets:
                     self.create_local_socket(msg.src_id)
             except Exception as e:
                 import traceback
                 print(traceback.format_exc())
                 self.logger.warning(
                     "Error while put local message to global queue: %s" %
                     e)
     except Exception as e:
         import traceback
         print(traceback.format_exc())
         self.logger.warning("Error while get send item from queue: %s" % e)