@Override public BidibNode findNode(byte[] nodeAddress) { return nodeRegistry.findNode(nodeAddress); }
/** * Initialize the instance. This must only be called from this class */ protected void initialize() { LOGGER.info("Initialize AbstractBidib, create a NodeRegistry."); nodeRegistry = new NodeRegistry(); LOGGER.info("Created nodeRegistry: {}", nodeRegistry); nodeRegistry.setBidib(this); // create the request factory requestFactory = new RequestFactory(); nodeRegistry.setRequestFactory(requestFactory); requestFactory.initialize(); // warmup for (int i = 0; i < 100; i++) { ByteArrayOutputStream item = new ByteArrayOutputStream(64); freeBufferQueue.add(item); } // create the message receiver messageReceiver = createMessageReceiver(nodeRegistry); }
@Override public AccessoryNode getAccessoryNode(Node node) { return nodeRegistry.getAccessoryNode(node.getAddr()); }
/** * Create a new {@code BiDiBNode} instance from the provided node data. * * @param node * the node data * @return the new {@code BiDiBNode} */ public BidibNode createNode(Node node) { LOGGER.info("Create the new bidibNode of node: {}", node); BidibNode bidibNode = null; // check if the node is already in the system bidibNode = findNode(node.getAddr()); if (bidibNode != null) { LOGGER.warn("The new node is already registered in the system: {}", bidibNode); messageReceiver.removeOrphanNode(node); LOGGER.warn("Removed orphan node: {}", node); } // removeNode(node); bidibNode = getNode(node); LOGGER.info("createNode returns new bidibNode: {}", bidibNode); return bidibNode; }
nodeRegistry.triggerPendingAcknowledge(bidibNode); nodeRegistry.triggerPendingAcknowledge(bidibNode); nodeRegistry.triggerPendingAcknowledge(bidibNode); .acknowledgePosition(feedbackPositionResponse.getDecoderAddress(), feedbackPositionResponse.getLocationType(), feedbackPositionResponse.getLocationAddress()); nodeRegistry.triggerPendingAcknowledge(bidibNode); case BidibLibrary.MSG_ACCESSORY_NOTIFY: AccessoryNode accessoryNode = nodeRegistry.getAccessoryNode(message.getAddr()); nodeRegistry.triggerPendingAcknowledge(bidibNode); BidibNode newNode = nodeRegistry.createNode(node); LOGGER.info("The node factory has registered the new node: {}", newNode); fireNodeNew = true; nodeRegistry.triggerPendingAcknowledge(bidibNode); LOGGER.info("Acknowledge node lost on node: {}", bidibNode); bidibNode.acknowledgeNodeChanged(nodeLostResponse.getNodeTabVersion()); nodeRegistry.triggerPendingAcknowledge(bidibNode);
@Override public BidibNode getNode(Node node) { return nodeRegistry.getNode(node); }
@Override public BoosterNode getBoosterNode(Node node) { return nodeRegistry.getBoosterNode(node); }
@Override public RootNode getRootNode() { return nodeRegistry.getRootNode(); }
@Override public CommandStationNode getCommandStationNode(Node node) { return nodeRegistry.getCommandStationNode(node); }
/** * Get a bidib node from the registered nodes or create a new bidib node. * * @param node * the node * @return the bidib node */ public BidibNode getNode(final Node node) { LOGGER.debug("Get the bidibNode of node: {}", node); int address = NodeUtils.convertAddress(node.getAddr()); LOGGER.info("Fetch bidibNode from nodes, address: {}", address); BidibNode bidibNode = null; synchronized (nodes) { bidibNode = nodes.get(address); LOGGER.debug("Get the bidibNode from nodesSet with address: {}, bidibNode: {}", address, bidibNode); if (bidibNode == null) { LOGGER.info("No registered node found with address: {}", address); // get the classId of the new node int classId = ByteUtils.getClassIdFromUniqueId(node.getUniqueId()); LOGGER.info("Create new bidibNode with classId: {}", classId); // create the new bidib node bidibNode = createBidibNode(node); LOGGER.info("Created new bidibNode: {}, address: {}", bidibNode, address); nodes.put(address, bidibNode); } } return bidibNode; }
/** * Returns the provided node as CommandStationNode instance of null if the node is not an CommandStationNode. * * @param node * the node * @return the CommandStationNode instance */ public CommandStationNode getCommandStationNode(Node node) { BidibNode bidibNode = getNode(node); if (NodeUtils.hasCommandStationFunctions(node.getUniqueId())) { CommandStationNode commandStationNode = new CommandStationNode(bidibNode); LOGGER.debug("prepared command station node: {}", commandStationNode); return commandStationNode; } LOGGER.debug("The requested node is not a CommandStationNode."); throw new InvalidConfigurationException("The requested node is not a CommandStationNode."); }
private void notifyBulkSender(byte[] address, Integer answerType) { // notify the bulk sender BidibNode node = nodeRegistry.findNode(address); LOGGER.trace("Notify bulk answer to node: {}", node); if (node != null) { try { node.notifyBulkAnswer(answerType); } catch (Exception ex) { LOGGER.error("Notify bulk answer to node failed.", ex); } } }
/** * Returns the provided node as BoosterNode instance of null if the node is not an BoosterNode. * * @param node * the node * @return the BoosterNode instance */ public BoosterNode getBoosterNode(Node node) { BidibNode bidibNode = getNode(node); if (NodeUtils.hasBoosterFunctions(node.getUniqueId())) { BoosterNode boosterNode = new BoosterNode(bidibNode); LOGGER.debug("prepared booster node: {}", boosterNode); return boosterNode; } LOGGER.debug("The requested node is not a BoosterNode."); throw new InvalidConfigurationException("The requested node is not a BoosterNode."); }
/** * Returns the provided node as AccessoryNode instance of null if the node is not an AccessoryNode. * * @param address * the node address * @return the AccessoryNode instance */ public AccessoryNode getAccessoryNode(byte[] address) { BidibNode bidibNode = findNode(address); try { if (NodeUtils.hasAccessoryFunctions(bidibNode.getUniqueId())) { AccessoryNode accessoryNode = new AccessoryNode(bidibNode); LOGGER.debug("prepared accessory node: {}", accessoryNode); return accessoryNode; } } catch (Exception ex) { LOGGER.warn("Get accessory node failed.", ex); } LOGGER .warn("The requested node is not an AccessoryNode, node: {}, address: {}", bidibNode, NodeUtils.formatAddress(address)); return null; }
bidibNode = nodeRegistry.findNode(message.getAddr());
@Test public void receiveBoostStatResponseTest() throws IOException { byte[] address = new byte[] { 0 }; NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue byte[] data = new byte[] { 0x04, 0x00, 0x3a, (byte) 0xb0, (byte) 0x06, (byte) 0x74, (byte) 0xFE }; final byte[] nodeAddress = new byte[] { 0 }; Mockito.when(nodeFactory.findNode(nodeAddress)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(58)); receiver.receive(pack(data)); // verify that booster state is on Mockito.verify(messageListener, Mockito.never()).boosterState(address, BoosterState.ON); }
@Test public void receiveMessageTest() throws IOException { byte[] address = new byte[] { 0 }; NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); // prepare the object to test SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue byte[] data = new byte[] { 0x05, 0x00, 0x01, (byte) 0x86, (byte) 0x02, (byte) 0x00, (byte) 0x46, (byte) 0xFE }; Mockito.when(nodeFactory.findNode(address)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(1)); receiver.receive(pack(data)); // verify that error was called once Mockito.verify(messageListener, Mockito.times(1)).error(address, 2, address); }
@Test public void receiveMessageTest() throws IOException { byte[] address = new byte[] { 0 }; NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); // prepare the object to test NetMessageReceiver receiver = new NetMessageReceiver(nodeFactory, false); receiver.addMessageListener(messageListener); // set the receive queue byte[] data = new byte[] { 0x05, 0x00, 0x01, (byte) 0x86, (byte) 0x02, (byte) 0x00, 0x06, 0x00, 0x01, (byte) 0xc1, 0x00, 0x01, 0x02 }; Mockito.when(nodeFactory.findNode(address)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(1)); receiver.receive(pack(data)); // verify that error was called once Mockito.verify(messageListener, Mockito.times(1)).error(address, 2, address); Mockito.verify(messageListener, Mockito.times(1)).lcNa(Mockito.eq(address), Mockito.any(), Mockito.eq(2)); } }
@Test public void receiveMessageLeadingMagicTest() throws IOException { byte[] address = new byte[] { 0 }; NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); // prepare the object to test SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue byte[] data = new byte[] { (byte) 0xFE, 0x05, 0x00, 0x01, (byte) 0x86, (byte) 0x02, (byte) 0x00, (byte) 0x46, (byte) 0xFE }; Mockito.when(nodeFactory.findNode(address)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(1)); receiver.receive(pack(data)); // verify that error was called once Mockito.verify(messageListener, Mockito.times(1)).error(address, 2, address); }
@Test public void receive4Test() throws IOException, ProtocolException { NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue byte[] rawmessage = { 0x08, 0x03, 0x00, 0x1E, (byte) 0xC6, 0x00, 0x07, 0x00, 0x00, 0x08, 0x03, 0x00, 0x1F, (byte) 0xC6, 0x00, 0x08, 0x00, 0x00, 0x08, 0x03, 0x00, 0x20, (byte) 0xC6, 0x00, 0x09, 0x00, 0x00, 0x08, 0x03, 0x00, 0x21, (byte) 0xC6, 0x00, 0x0A, 0x00, 0x00, 0x08, 0x03, 0x00, 0x22, (byte) 0xC6, 0x00, 0x0B, 0x00, 0x00, 0x08, 0x03, 0x00, 0x23, (byte) 0xC6, 0x00, 0x0C, 0x00, 0x00, 0x08, 0x03, 0x00, 0x24, (byte) 0xC6, 0x00, 0x0D, 0x00, 0x00, (byte) 0xF5, (byte) 0xFE }; byte[] message = new byte[] { 0x08, 0x03, 0x00, 0x17, (byte) 0xC6, 0x00, 0x00, 0x00, 0x00 }; final byte[] nodeAddress = new byte[] { 3 }; Mockito.when(nodeFactory.findNode(nodeAddress)).thenReturn(bidibNode); Mockito .when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(0x1E), Integer.valueOf(0x1F), Integer.valueOf(0x20), Integer.valueOf(0x21), Integer.valueOf(0x22), Integer.valueOf(0x23), Integer.valueOf(0x24)); receiver.receive(pack(rawmessage)); BidibMessage response = new BidibResponseFactory().create(message); // verify that message was signaled Mockito.verify(messageListener, Mockito.times(7)).lcConfigX(Mockito.eq(nodeAddress), Mockito.any()); Assert.assertEquals(response.getType(), ByteUtils.getLowByte(BidibLibrary.MSG_LC_CONFIGX)); }