/** * 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); }
@Override public boolean equals(Object other) { if (other instanceof BidibNetAddress) { BidibNetAddress bidibHost = (BidibNetAddress) other; if (bidibHost.getAddress().equals(address) && bidibHost.getPortNumber() == portNumber) { return true; } } return false; }
@Test public void getPortNumber() { BidibNetAddress address1 = new BidibNetAddress(InetAddress.getLoopbackAddress(), 1234); Assert.assertEquals(address1.getPortNumber(), 1234); } }
@Test public void getAddress() { BidibNetAddress address1 = new BidibNetAddress(InetAddress.getLoopbackAddress(), 1234); Assert.assertEquals(address1.getAddress(), InetAddress.getLoopbackAddress()); }
@Override public void send(NetBidibPort port, byte[] message) { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Send message to port: {}, message: {}", port, ByteUtils.bytesToHex(message)); } if (port != null) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); // send the message to every known host for (BidibNetAddress host : knownBidibHosts) { bos.reset(); bos.write(message); LOGGER.info("Send message to address: {}, port: {}", host.getAddress(), host.getPortNumber()); // send the data to the host port.send(bos.toByteArray(), host.getAddress(), host.getPortNumber()); } } catch (IOException ex) { LOGGER.warn("Send message to port failed.", ex); throw new RuntimeException("Send message to datagram socket failed.", ex); } } else { LOGGER.warn("Send not possible, the port is closed."); } }
@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); }
@Override public void send(NetBidibPort port, byte[] bytes) { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Send message to port: {}, message: {}", port, ByteUtils.bytesToHex(bytes)); } if (remoteAddress == null) { LOGGER.warn("### No remote addresses available. The message will not be sent!"); return; } if (port != null) { try { // add the bidib message payload sendBuffer.write(bytes); LOGGER.info("Send message to remote address, address: {}, port: {}", remoteAddress.getAddress(), remoteAddress.getPortNumber()); // send the message to the port port.send(sendBuffer.toByteArray(), remoteAddress.getAddress(), remoteAddress.getPortNumber()); } catch (IOException ex) { LOGGER.warn("Send message to port failed.", ex); throw new RuntimeException("Send message to datagram socket failed.", ex); } finally { sendBuffer.reset(); } } else { LOGGER.warn("Send not possible, the port is closed."); } }
@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); } }
addDelimiter(bos); LOGGER.info("Send message to address: {}, port: {}", host.getAddress(), host.getPortNumber()); port.send(bos.toByteArray(), host.getAddress(), host.getPortNumber());
@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(); } }