protected void parsePackets( final ByteArrayOutputStream output, NetMessageHandler messageReceiver, InetAddress address, int portNumber) throws ProtocolException { // call utils NetBidibPacketUtils.parsePackets(output, messageReceiver, address, portNumber); } }
@Override protected BidibMessageProcessor createMessageReceiver(NodeRegistry nodeFactory) { return new NetMessageReceiver(nodeFactory, false); }
private MessageReceiver getNetMessageReceiver() { return (MessageReceiver) getMessageReceiver(); }
@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."); } }
@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 close() { LOGGER.info("Close the port."); if (port != null) { LOGGER.info("Stop the port."); port.stop(); if (portWorker != null) { synchronized (portWorker) { try { portWorker.join(5000L); } catch (InterruptedException ex) { LOGGER.warn("Wait for termination of port worker failed.", ex); } portWorker = null; } } port = null; } stopReceiverAndQueues(null); if (getConnectionListener() != null) { getConnectionListener().closed(connectedPortName); } // clear the connectedPortName connectedPortName = null; cleanupAfterClose(null); LOGGER.info("Close the port finished."); }
@Test public void parsePacketsStream2() throws IOException, ProtocolException { LOGGER.info("Parse packets from received data."); // The message begins with the BIDIB_WIZARD_NET_PREFIX and contains 1 message byte[] receiveData = new byte[] { 0x0C, 0x00, 0x02, (byte) 0x89, 0x01, 0x00, (byte) 0xDA, 0x00, 0x0D, 0x68, 0x00, 0x64, (byte) 0xEA, /**/ 0x05, 0x00, 0x05, (byte) 0x90, 0x01, 0x01 }; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); buffer.write(receiveData); NetMessageHandler messageHandler = Mockito.mock(NetMessageHandler.class); InetAddress address = InetAddress.getLoopbackAddress(); int portNumber = 12345; NetBidibTcpServerSocketHandler handler = Mockito.mock(NetBidibTcpServerSocketHandler.class); Mockito.doCallRealMethod().when(handler).parsePackets(Mockito.any(), Mockito.any(NetMessageHandler.class), Mockito.any(InetAddress.class), Mockito.anyInt()); handler.parsePackets(buffer, messageHandler, address, portNumber); Mockito.verify(messageHandler, Mockito.times(2)).receive(Mockito.any()); }
/** * 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 void publishResponse(ByteArrayOutputStream output) throws ProtocolException { // Publish the responses to the host LOGGER.info( "Publish the response. Prepare message to send to host using netSimulationMessageHandler: {}", netSimulationMessageHandler); try { // send to handler netSimulationMessageHandler.send(netBidibPortSimulator, output.toByteArray()); } catch (Exception ex) { LOGGER.warn("Process messages failed.", ex); } }
/** * Get a new initialized instance of NetBidib. * * @return the instance of NetBidib */ public static BidibInterface createInstance() { LOGGER.info("Create new instance of NetBidib."); NetBidib instance = new NetBidib(); instance.initialize(); return instance; }
@Override public void closed(String port) { // TODO Auto-generated method stub // connectionListener.closed(port); if (active) { connectionListener.closed(port); return; } try { active = true; close(); } finally { active = false; } } };
@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 { port.send(message, null, 0); } 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."); } }
/** * Get the magic from the root node * * @return the magic provided by the root node * @throws ProtocolException */ private int sendMagic() throws ProtocolException { BidibNode rootNode = getRootNode(); // Ignore the first exception ... int magic = -1; try { magic = rootNode.getMagic(15000); } catch (Exception e) { LOGGER.warn("Get magic from node failed. Retry get magic from node."); magic = rootNode.getMagic(15000); } LOGGER.info("The node returned magic: {}", magic); return magic; }
/** * Parse the received data to process the received bidib packets. * * @param input * the received data * @throws ProtocolException */ protected void parseInput(final ByteArrayOutputStream input) throws ProtocolException { MSG_RAW_LOGGER.info("<<<< len: {}, data: {}", input.size(), ByteUtils.bytesToHex(input)); // if a CRC error is detected in splitMessages the reading loop will terminate ... try { processMessages(input); } catch (ProtocolException ex) { LOGGER.warn("Process messages failed.", ex); } } }
@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."); } }
@Test public void parsePacketsStream() throws IOException, ProtocolException { LOGGER.info("Parse packets from received data."); // The message begins with the BIDIB_WIZARD_NET_PREFIX and contains 3 messages byte[] receiveData = new byte[] { 0x0C, 0x00, 0x02, (byte) 0x89, 0x01, 0x00, (byte) 0xDA, 0x00, 0x0D, 0x68, 0x00, 0x64, (byte) 0xEA, /**/ 0x05, 0x00, 0x05, (byte) 0x90, 0x01, 0x01, /**/ 0x05, 0x00, 0x06, (byte) 0x90, 0x02, 0x01, /**/ 0x05, 0x00, 0x07, (byte) 0x90, 0x03, 0x64, }; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); buffer.write(receiveData); NetMessageHandler messageReceiver = Mockito.mock(NetMessageHandler.class); InetAddress address = InetAddress.getLoopbackAddress(); int portNumber = 12345; NetBidibTcpServerSocketHandler handler = Mockito.mock(NetBidibTcpServerSocketHandler.class); Mockito.doCallRealMethod().when(handler).parsePackets(Mockito.any(), Mockito.any(NetMessageHandler.class), Mockito.any(InetAddress.class), Mockito.anyInt()); handler.parsePackets(buffer, messageReceiver, address, portNumber); Mockito.verify(messageReceiver, Mockito.times(4)).receive(Mockito.any()); }
@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 publishResponse(ByteArrayOutputStream output) throws ProtocolException { // Publish the responses to the host LOGGER.info( "Publish the response. Prepare message to send to host using netSimulationMessageHandler: {}", netSimulationMessageHandler); try { // send to handler netSimulationMessageHandler.send(netBidibPortSimulator, output.toByteArray()); } catch (Exception ex) { LOGGER.warn("Process messages failed.", ex); } }