/** * Creates a new instance of DefaultNetMessageHandler. * * @param messageReceiverDelegate * the delegate message receiver that processes the BiDiB messages * @param address * the address of the master to connect to * @param port * the port of the master to connect to * @param connectionListener * the connection listener */ public DefaultNetMessageHandler(BidibMessageProcessor messageReceiverDelegate, InetAddress address, int port, final ConnectionListener connectionListener) { this.messageReceiverDelegate = messageReceiverDelegate; this.connectionListener = connectionListener; LOGGER.info("Set the remote address: {}, port: {}", address, port); remoteAddress = new BidibNetAddress(address, port); }
@Test public void equals() { BidibNetAddress address1 = new BidibNetAddress(InetAddress.getLoopbackAddress(), 1234); BidibNetAddress address2 = new BidibNetAddress(InetAddress.getLoopbackAddress(), 1234); Assert.assertEquals(address1, address2); BidibNetAddress address3 = new BidibNetAddress(InetAddress.getLoopbackAddress(), 2345); Assert.assertNotEquals(address1, address3); }
@Test public void getAddress() { BidibNetAddress address1 = new BidibNetAddress(InetAddress.getLoopbackAddress(), 1234); Assert.assertEquals(address1.getAddress(), InetAddress.getLoopbackAddress()); }
@Test public void getPortNumber() { BidibNetAddress address1 = new BidibNetAddress(InetAddress.getLoopbackAddress(), 1234); Assert.assertEquals(address1.getPortNumber(), 1234); } }
@Override public void receive(final DataPacket packet) { // a data packet was received ... process the envelope and extract the message // if (LOGGER.isTraceEnabled()) { LOGGER.warn("Received a packet from address: {}, port: {}, data: {}", packet.getAddress(), packet.getPort(), ByteUtils.bytesToHex(packet.getData())); // } BidibNetAddress current = new BidibNetAddress(packet.getAddress(), packet.getPort()); if (!knownBidibHosts.contains(current)) { LOGGER.info("Adding new known Bidib host: {}", current); knownBidibHosts.add(current); } // TODO for the first magic response we need special processing because we need to keep the session key // remove the UDP paket wrapper data and forward to the MessageReceiver ByteArrayOutputStream output = new ByteArrayOutputStream(); output.write(packet.getData(), 0, packet.getData().length); LOGGER.info("Forward received message to messageReceiverDelegate: {}, output: {}", messageReceiverDelegate, ByteUtils.bytesToHex(output)); try { messageReceiverDelegate.receive(output); } catch (Exception ex) { LOGGER.warn("Process messages failed.", ex); throw new RuntimeException(ex); } }
@Override public void receive(final DataPacket packet) { // a data packet was received ... process the envelope and extract the message // if (LOGGER.isTraceEnabled()) { LOGGER.info("Received a packet from address: {}, port: {}, data: {}", packet.getAddress(), packet.getPort(), ByteUtils.bytesToHex(packet.getData())); // } BidibNetAddress current = new BidibNetAddress(packet.getAddress(), packet.getPort()); if (!knownBidibHosts.contains(current)) { LOGGER.info("Adding new known Bidib host: {}", current); knownBidibHosts.add(current); } try { receiveBuffer.write(packet.getData(), 0, packet.getData().length); // forward to the MessageReceiver LOGGER.info("Forward received message to messageReceiverDelegate: {}, data: {}", messageReceiverDelegate, ByteUtils.bytesToHex(receiveBuffer)); messageReceiverDelegate.receive(receiveBuffer); } catch (Exception ex) { LOGGER.warn("Process messages failed.", ex); throw new RuntimeException(ex); } finally { receiveBuffer.reset(); } }