Search in sources :

Example 31 with RoboContext

use of com.robo4j.RoboContext in project robo4j by Robo4J.

the class RoboHttpPingPongTest method runPongServer.

public void runPongServer() throws Exception {
    RoboContext system = configurePongSystem(0);
    system.start();
    System.out.println("systemPong: State after start:");
    System.out.println(SystemUtil.printStateReport(system));
    System.out.println("Press <Enter>...");
    System.in.read();
    system.shutdown();
}
Also used : RoboContext(com.robo4j.RoboContext)

Example 32 with RoboContext

use of com.robo4j.RoboContext in project robo4j by Robo4J.

the class HttpServerUnitTests method httpServerUnitNoCodecsPackageTest.

@Test
void httpServerUnitNoCodecsPackageTest() throws Exception {
    Throwable exception = assertThrows(RoboBuilderException.class, () -> {
        RoboBuilder builder = new RoboBuilder();
        Configuration config = new ConfigurationBuilder().addInteger(PROPERTY_SOCKET_PORT, PORT).build();
        builder.add(HttpServerUnit.class, config, ID_HTTP_SERVER);
        RoboContext system = builder.build();
        system.start();
        System.out.println("system: State after start:");
        System.out.println(SystemUtil.printStateReport(system));
        RoboReference<HttpServerUnit> systemReference = system.getReference(ID_HTTP_SERVER);
        system.shutdown();
        System.out.println("system: State after shutdown:");
        System.out.println(SystemUtil.printStateReport(system));
        assertEquals(LifecycleState.SHUTDOWN, systemReference.getState());
    });
    assertEquals("Error initializing RoboUnit", exception.getMessage());
}
Also used : ConfigurationBuilder(com.robo4j.configuration.ConfigurationBuilder) Configuration(com.robo4j.configuration.Configuration) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 33 with RoboContext

use of com.robo4j.RoboContext in project robo4j by Robo4J.

the class RemoteContextTests method messageToDiscoveredContextAndReferenceToDiscoveredContextTest.

@Test
@SuppressWarnings("unchecked")
void messageToDiscoveredContextAndReferenceToDiscoveredContextTest() throws Exception {
    RoboContext receiverSystem = buildRemoteReceiverContext(ACK_CONSUMER);
    receiverSystem.start();
    RoboReference<TestMessageType> ackConsumer = receiverSystem.getReference(ACK_CONSUMER);
    // Note that all this cludging about with local lookup service
    // implementations etc would normally not be needed.
    // This is just to isolate this test from other tests.
    final LocalLookupServiceImpl localLookup = new LocalLookupServiceImpl();
    final LookupService service = LookupServiceTests.getLookupService(localLookup);
    LookupServiceProvider.setDefaultLookupService(service);
    service.start();
    // context has been discovered
    RoboContextDescriptor contextDescriptor = getRoboContextDescriptor(service, CONTEXT_ID_REMOTE_RECEIVER);
    assertTrue(service.getDiscoveredContexts().size() > 0);
    assertNotNull(contextDescriptor);
    // build the producer system
    RoboContext producerEmitterSystem = buildEmitterContext(TestMessageType.class, ACK_CONSUMER, REMOTE_UNIT_EMITTER);
    producerEmitterSystem.start();
    localLookup.addContext(producerEmitterSystem);
    RoboReference<String> remoteTestMessageProducer = producerEmitterSystem.getReference(REMOTE_UNIT_EMITTER);
    remoteTestMessageProducer.sendMessage(RemoteTestMessageProducer.ATTR_SEND_MESSAGE);
    CountDownLatch ackConsumerCountDownLatch = ackConsumer.getAttribute(AckingStringConsumer.DESCRIPTOR_ACK_LATCH).get();
    ackConsumerCountDownLatch.await();
    List<TestMessageType> receivedMessages = (List<TestMessageType>) ackConsumer.getAttribute(AckingStringConsumer.DESCRIPTOR_MESSAGES).get();
    assertTrue(receivedMessages.size() > 0);
    CountDownLatch producerCountDownLatch = remoteTestMessageProducer.getAttribute(RemoteTestMessageProducer.DESCRIPTOR_COUNT_DOWN_LATCH).get();
    producerCountDownLatch.await();
    CountDownLatch producerAckLatch = remoteTestMessageProducer.getAttribute(RemoteTestMessageProducer.DESCRIPTOR_ACK_LATCH).get();
    producerAckLatch.await();
    Integer producerAcknowledge = remoteTestMessageProducer.getAttribute(RemoteTestMessageProducer.DESCRIPTOR_TOTAL_ACK).get();
    assertTrue(producerAcknowledge > 0);
}
Also used : List(java.util.List) RoboContext(com.robo4j.RoboContext) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 34 with RoboContext

use of com.robo4j.RoboContext in project robo4j by Robo4J.

the class RemoteContextTests method messageIncludingReferenceToDiscoveredContextTest.

@Test
void messageIncludingReferenceToDiscoveredContextTest() throws RoboBuilderException, IOException, ConfigurationException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteMessageReceiverAckSystem.xml"));
    AckingStringConsumer consumer = new AckingStringConsumer(builder.getContext(), ACK_CONSUMER);
    builder.add(consumer);
    RoboContext receiverCtx = builder.build();
    receiverCtx.start();
    final LocalLookupServiceImpl localLookup = new LocalLookupServiceImpl();
    final LookupService service = LookupServiceTests.getLookupService(localLookup);
    LookupServiceProvider.setDefaultLookupService(service);
    service.start();
    for (int i = 0; i < NUMBER_ITERATIONS && (service.getDescriptor("9") == null); i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(service.getDiscoveredContexts().size() > 0);
    RoboContextDescriptor descriptor = service.getDescriptor("9");
    assertNotNull(descriptor);
    builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testMessageEmitterSystem_8.xml"));
    RemoteTestMessageProducer remoteTestMessageProducer = new RemoteTestMessageProducer(builder.getContext(), REMOTE_UNIT_EMITTER);
    remoteTestMessageProducer.initialize(getEmitterConfiguration("9", ACK_CONSUMER));
    builder.add(remoteTestMessageProducer);
    RoboContext emitterContext = builder.build();
    localLookup.addContext(emitterContext);
    emitterContext.start();
    remoteTestMessageProducer.sendMessage("sendMessage");
    for (int i = 0; i < NUMBER_ITERATIONS && consumer.getReceivedMessages().size() == 0; i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(consumer.getReceivedMessages().size() > 0);
    System.out.println("Got messages: " + consumer.getReceivedMessages());
    assertTrue(remoteTestMessageProducer.getAckCount() > 0);
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 35 with RoboContext

use of com.robo4j.RoboContext in project robo4j by Robo4J.

the class RemoteContextTests method messageToDiscoveredContextTest.

@Test
void messageToDiscoveredContextTest() throws RoboBuilderException, IOException, ConfigurationException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteMessageReceiverSystem.xml"));
    StringConsumer consumer = new StringConsumer(builder.getContext(), ACK_CONSUMER);
    builder.add(consumer);
    RoboContext receiverCtx = builder.build();
    receiverCtx.start();
    // Note that all this cludging about with local lookup service
    // implementations etc would normally not be needed.
    // This is just to isolate this test from other tests.
    final LocalLookupServiceImpl localLookup = new LocalLookupServiceImpl();
    final LookupService service = LookupServiceTests.getLookupService(localLookup);
    LookupServiceProvider.setDefaultLookupService(service);
    service.start();
    for (int i = 0; i < NUMBER_ITERATIONS && (service.getDescriptor("7") == null); i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(service.getDiscoveredContexts().size() > 0);
    RoboContextDescriptor descriptor = service.getDescriptor("7");
    assertNotNull(descriptor);
    builder = new RoboBuilder(RemoteContextTests.class.getClassLoader().getResourceAsStream("testMessageEmitterSystem_10.xml"));
    RemoteStringProducer remoteStringProducer = new RemoteStringProducer(builder.getContext(), REMOTE_UNIT_EMITTER);
    remoteStringProducer.initialize(getEmitterConfiguration("7", ACK_CONSUMER));
    builder.add(remoteStringProducer);
    RoboContext emitterContext = builder.build();
    localLookup.addContext(emitterContext);
    emitterContext.start();
    remoteStringProducer.sendMessage("sendRandomMessage");
    for (int i = 0; i < NUMBER_ITERATIONS && consumer.getReceivedMessages().size() == 0; i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(consumer.getReceivedMessages().size() > 0);
    System.out.println("Got messages: " + consumer.getReceivedMessages());
    emitterContext.shutdown();
    receiverCtx.shutdown();
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) StringConsumer(com.robo4j.StringConsumer) Test(org.junit.jupiter.api.Test)

Aggregations

RoboContext (com.robo4j.RoboContext)50 RoboBuilder (com.robo4j.RoboBuilder)33 Test (org.junit.jupiter.api.Test)20 InputStream (java.io.InputStream)17 Configuration (com.robo4j.configuration.Configuration)10 ConfigurationBuilder (com.robo4j.configuration.ConfigurationBuilder)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 FileInputStream (java.io.FileInputStream)5 RoboReference (com.robo4j.RoboReference)4 RoboBuilderException (com.robo4j.RoboBuilderException)3 ServerPathConfig (com.robo4j.socket.http.units.ServerPathConfig)3 List (java.util.List)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 StringConsumer (com.robo4j.StringConsumer)2 BiColor (com.robo4j.hw.rpi.i2c.adafruitbackpack.BiColor)2 DataEvent3f (com.robo4j.hw.rpi.imu.bno.DataEvent3f)2 HttpMethod (com.robo4j.socket.http.HttpMethod)2 CameraMessage (com.robo4j.socket.http.codec.CameraMessage)2 HttpPathMethodDTO (com.robo4j.socket.http.dto.HttpPathMethodDTO)2