@SuppressWarnings("unchecked") private <T> void addTopicListener( String topic, Object inner, ByteArrayConverter<T> converter, TypedListener<T> listener) throws ConnectorException { synchronized (messageDispatcher) { MessageDispatcher<T> dispatcher = messageDispatcher.get(topic); if (dispatcher == null) { ConnectorSession session = connection.createSession(); ConnectorConsumer consumer = session.createConsumer(session.createTopic(topic)); messageDispatcher.put(topic, dispatcher = new MessageDispatcher<T>(consumer, converter)); } dispatcher.addListener(inner, listener); } }
@Override public void sendBlock(Block block) throws BCSAPIException { try (ConnectorSession session = connection.createSession()) { log.trace("send block " + block.getID()); ConnectorProducer blockProducer = session.createProducer(session.createTopic("newBlock")); ConnectorMessage m = session.createMessage(); m.setPayload(block.toBCSAPIMessage().toByteArray()); byte[] reply = synchronousRequest(session, blockProducer, m); if (reply != null) { try { BCSAPIMessage.ExceptionMessage em = BCSAPIMessage.ExceptionMessage.parseFrom(reply); throw new BCSAPIException(em.getMessage(0)); } catch (InvalidProtocolBufferException e) { throw new BCSAPIException("Invalid response", e); } } } catch (ConnectorException e) { throw new BCSAPIException(e); } }
@Override public APIHeader mine(Address address) throws BCSAPIException { try (ConnectorSession session = connection.createSession()) { log.trace("mine to " + address); ConnectorProducer producer = session.createProducer(session.createTopic("mine")); ConnectorMessage m = session.createMessage(); BCSAPIMessage.Script.Builder builder = BCSAPIMessage.Script.newBuilder() .setScript(ByteString.copyFrom(address.getAddressScript().toByteArray())); m.setPayload(builder.build().toByteArray()); byte[] reply = synchronousRequest(session, producer, m); if (reply != null) { return APIHeader.fromProtobuf(BCSAPIMessage.BLK.parseFrom(reply)); } } catch (ConnectorException | InvalidProtocolBufferException | HyperLedgerException e) { throw new BCSAPIException(e); } return null; }