@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."); } }
@Override public void stop() { LOGGER.info("Stop the simulator."); if (netBidibPortSimulator != null) { LOGGER.info("Stop the port."); netBidibPortSimulator.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; } } isStarted.set(false); netBidibPortSimulator = null; } LOGGER.info("Stop the simulator finished."); }
@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 void stop() { LOGGER.info("Stop the simulator."); if (netBidibPortSimulator != null) { LOGGER.info("Stop the port."); netBidibPortSimulator.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; } } isStarted.set(false); netBidibPortSimulator = null; } LOGGER.info("Stop the simulator finished."); }
@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 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."); }
@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 sendPacketBuffer.write(bytes); LOGGER.info("Send message to remote address, address: {}, port: {}", remoteAddress.getAddress(), remoteAddress.getPortNumber()); // send the message to the port port.send(sendPacketBuffer.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 { sendPacketBuffer.reset(); } } else { LOGGER.warn("Send not possible, the port is closed."); } }
port.send(bos.toByteArray(), host.getAddress(), host.getPortNumber());