private synchronized RequestFactory getRequestFactory() { if (requestFactory == null) { requestFactory = new RequestFactory(); } return requestFactory; }
public void createFromRaw(byte[] messageContent, final List<BidibCommand> bidibMessages) throws ProtocolException { // read the first byte that is the length of the message byte len = messageContent[0]; if ((len + 1) < messageContent.length - 1) { // we have multiple messages int index = 0; while (index < messageContent.length - 1) { len = messageContent[index]; byte[] part = new byte[len + 1]; System.arraycopy(messageContent, index, part, 0, len + 1); BidibCommand bidibMessage = createConcreteMessage(part); bidibMessages.add(bidibMessage); LOGGER.trace("Added new bidibMessage: {}, index: {}", bidibMessage, index); index += len + 1; } } else { // we have a single messages BidibCommand bidibMessage = createConcreteMessage(messageContent); bidibMessages.add(bidibMessage); LOGGER.trace("Added new bidibMessage: {}", bidibMessage); } }
/** * 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 void processMessages(ByteArrayOutputStream output) { try { if (interfacePort != null) { try { List<BidibCommand> bidibMessages = requestFactory.create(output.toByteArray()); for (BidibCommand message : bidibMessages) { MSG_TX_LOGGER.info(">> {} : {}", message, ByteUtils.bytesToHex(message.getContent())); } } catch (Exception e1) { LOGGER.warn("Prepare logging outgoing messages to interface failed.", e1); } interfacePort.send(output.toByteArray()); } } catch (Exception ex) { LOGGER.warn("Send message from proxy to interface failed.", ex); } finally { output.reset(); } } };
getRequestFactory().createFromRaw(output.toByteArray(), commands);
send(requestFactory.createFeatureGetNext(), null, true, FeatureResponse.TYPE, FeatureNotAvailableResponse.TYPE); if (response instanceof FeatureResponse) {
/** * Get the accessory state of the specified accessory. * * @param accessoryNumber * the number of the accessory * @throws ProtocolException */ public void getAccessoryState(int... accessoryNumber) throws ProtocolException { // response is signaled asynchronously if (accessoryNumber.length > 1) { int windowSize = BidibNode.BULK_WINDOW_SIZE; List<BidibCommand> messages = new LinkedList<>(); for (int i = 0; i < accessoryNumber.length; i++) { messages.add(delegate.getRequestFactory().createAccessoryGet(accessoryNumber[i])); } LOGGER .info("Get the accessory states with bulk messages, windowSize: {}, accessoryNumber: {}", new Object[] { windowSize, accessoryNumber }); delegate.sendBulk(windowSize, messages, true, ProcessSendQueue.enabled); } else { LOGGER.info("Get the accessory states with single message, accessoryNumber: {}", accessoryNumber[0]); delegate.sendNoWait(new AccessoryGetMessage(accessoryNumber[0])); } }
delegate.getRequestFactory().createCommandStationAccessory(address, addressType, timingControl, activateCoil, aspect, timeBaseUnit, time); delegate.sendNoWait(message);
/** * Request the number of features of the node. This call will reset the internal counter for the next * <code>getNextFeature()</code> request. * * @return number of features on the node * @throws IOException * @throws ProtocolException * @throws InterruptedException */ public Integer getFeatureCount() throws ProtocolException { if (isBootloaderNode()) { LOGGER.warn("The current node is a bootloader node and does not support feature requests."); throw createNotSupportedByBootloaderNode("MSG_FEATURE_GETALL"); } BidibMessage response = send(requestFactory.createFeatureGetAll(), null, true, FeatureCountResponse.TYPE); if (response instanceof FeatureCountResponse) { Integer result = ((FeatureCountResponse) response).getCount(); return result; } if (ignoreWaitTimeout) { LOGGER.warn("No response received but ignoreWaitTimeout ist set! Current node: {}", this); return Integer.valueOf(0); } throw createNoResponseAvailable("get feature count"); }
send(requestFactory.createFeatureGet(featureNumber), null, true, FeatureResponse.TYPE, FeatureNotAvailableResponse.TYPE);
@Override public void sendData(ByteArrayOutputStream data) { try { byte[] bytes = data.toByteArray(); LOGGER.info("Send is called with bytes: {}", ByteUtils.bytesToHex(bytes)); // encode the message SerialMessageEncoder.encodeMessage(data, output); List<BidibCommand> bidibMessages = requestFactory.create(output.toByteArray()); for (BidibCommand bidibMessage : bidibMessages) { byte[] address = ((BidibMessage) bidibMessage).getAddr(); String nodeAddress = NodeUtils.formatAddress(address); SimulatorNode simulator = SimulatorRegistry.getInstance().getSimulator(nodeAddress); if (simulator == null) { LOGGER.warn("No simulator found for nodeAddress: {}", nodeAddress); } else { simulator.processRequest(bidibMessage); } LOGGER.debug("Forwarded message to simulator: {}", bidibMessage); } } catch (Exception ex) { LOGGER.warn("Process request failed.", ex); } finally { output.reset(); } }
createFromRaw(messageContent, bidibMessages);
messages.add(requestFactory.createFeatureGetNext());
requestFactory = new RequestFactory();
requestFactory = new RequestFactory();