/** * 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; }
private MessageReceiver getNetMessageReceiver() { return (MessageReceiver) getMessageReceiver(); }
@Test public void openTcp() throws PortNotFoundException, PortNotOpenedException { NetBidib netBidib = (NetBidib) NetBidib.createInstance(); Assert.assertNotNull(netBidib); netBidib.setResponseTimeout(BidibInterface.DEFAULT_TIMEOUT * 100); try { netBidib.open(protocol + ":localhost:" + NetBidib.BIDIB_UDP_PORT_NUMBER, new ConnectionListener() { Collections.<TransferListener> emptySet(), null); Assert.assertNotNull(netBidib.getRootNode()); Assert.assertEquals(netBidib.getRootNode().getNodeMagic(), Integer.valueOf(BidibLibrary.BIDIB_SYS_MAGIC)); netBidib.close();
setConnectionListener(connectionListener); registerListeners(nodeListeners, messageListeners, transferListeners); port = internalOpen(portName, context); connectedPortName = portName; sendMagic();
@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."); }
final BidibMessageProcessor messageReceiver = getMessageReceiver(); getNetMessageReceiver().enable(); startReceiverAndQueues(messageReceiver, context);
@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; } } };
/** * 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; }
@Test public void openUdp() throws PortNotFoundException, PortNotOpenedException, ProtocolException { NetBidib netBidib = (NetBidib) NetBidib.createInstance(); Assert.assertNotNull(netBidib); netBidib.setResponseTimeout(BidibInterface.DEFAULT_TIMEOUT * 100); try { netBidib.open("udp:localhost:" + NetBidib.BIDIB_UDP_PORT_NUMBER, new ConnectionListener() { Collections.<TransferListener> emptySet(), null); Assert.assertNotNull(netBidib.getRootNode()); Assert.assertEquals(netBidib.getRootNode().getNodeMagic(), Integer.valueOf(BidibLibrary.BIDIB_SYS_MAGIC)); long uniqueId = netBidib.getRootNode().getUniqueId(); uniqueId = netBidib.getRootNode().getUniqueId(true); netBidib.getRootNode().getNextFeature(); netBidib.getRootNode().getNextFeature(); netBidib.getRootNode().getNextFeature(); netBidib.getRootNode().getNextFeature(); netBidib.close();