Search in sources :

Example 1 with RoboContext

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

the class LookupServiceTests method testLookup.

@Test
void testLookup() throws IOException, InterruptedException {
    final LookupService service = getLookupService(new LocalLookupServiceImpl());
    service.start();
    RoboContextDescriptor descriptor = createRoboContextDescriptor();
    ContextEmitter emitter = new ContextEmitter(descriptor, InetAddress.getByName(LookupServiceProvider.DEFAULT_MULTICAST_ADDRESS), LookupServiceProvider.DEFAULT_PORT, 250);
    for (int i = 0; i < 10; i++) {
        emitter.emit();
        Thread.sleep(250);
    }
    Map<String, RoboContextDescriptor> discoveredContexts = service.getDiscoveredContexts();
    System.out.println(discoveredContexts);
    assertEquals(1, discoveredContexts.size());
    RoboContext context = service.getContext(descriptor.getId());
    assertNotNull(context);
    ClientRemoteRoboContext remoteContext = (ClientRemoteRoboContext) context;
    assertNotNull(remoteContext.getAddress());
    System.out.println("Address: " + remoteContext.getAddress());
}
Also used : RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 2 with RoboContext

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

the class RemoteContextTests method discoveryOfDiscoveryEnabledRoboContextTest.

@Test
void discoveryOfDiscoveryEnabledRoboContextTest() throws RoboBuilderException, IOException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testDiscoverableSystem.xml"));
    RoboContext ctx = builder.build();
    ctx.start();
    final LookupService service = LookupServiceTests.getLookupService(new LocalLookupServiceImpl());
    service.start();
    for (int i = 0; i < NUMBER_ITERATIONS && (service.getDescriptor("6") == null); i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(service.getDiscoveredContexts().size() > 0);
    RoboContextDescriptor descriptor = service.getDescriptor("6");
    assertEquals(descriptor.getMetadata().get("name"), "Caprica");
    assertEquals(descriptor.getMetadata().get("class"), "Cylon");
    ctx.shutdown();
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 3 with RoboContext

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

the class RemoteContextTests method buildReceiverSystemStringConsumer.

/*
	 * Builds a system ready to receive strings.
	 */
private RoboContext buildReceiverSystemStringConsumer() throws RoboBuilderException, ConfigurationException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteReceiver.xml"));
    Configuration configuration = new ConfigurationBuilder().addInteger("totalNumberMessages", 1).build();
    StringConsumer stringConsumer = new StringConsumer(builder.getContext(), UNIT_STRING_CONSUMER);
    stringConsumer.initialize(configuration);
    builder.add(stringConsumer);
    RoboContext receiverSystem = builder.build();
    receiverSystem.start();
    return receiverSystem;
}
Also used : ConfigurationBuilder(com.robo4j.configuration.ConfigurationBuilder) Configuration(com.robo4j.configuration.Configuration) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) StringConsumer(com.robo4j.StringConsumer)

Example 4 with RoboContext

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

the class RemoteStringProducer method sendRandomMessage.

public void sendRandomMessage() {
    final String message = StringToolkit.getRandomMessage(10);
    RoboContext ctx = LookupServiceProvider.getDefaultLookupService().getContext(targetContext);
    ctx.getReference(target).sendMessage(message);
}
Also used : RoboContext(com.robo4j.RoboContext)

Example 5 with RoboContext

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

the class CounterUnitTests method test.

@Test
void test() throws RoboBuilderException, InterruptedException, ExecutionException {
    // FIXME(Marcus/Aug 20, 2017): We really should get rid of the sleeps
    // here and use waits with timeouts...
    RoboBuilder builder = new RoboBuilder();
    builder.add(IntegerConsumer.class, ID_CONSUMER);
    builder.add(CounterUnit.class, getCounterConfiguration(ID_CONSUMER, 1000), ID_COUNTER);
    RoboContext context = builder.build();
    context.start();
    assertEquals(LifecycleState.STARTED, context.getState());
    RoboReference<CounterCommand> counter = context.getReference(ID_COUNTER);
    RoboReference<Integer> consumer = context.getReference(ID_CONSUMER);
    counter.sendMessage(CounterCommand.START);
    Thread.sleep(2500);
    assertTrue(consumer.getAttribute(NUMBER_OF_MESSAGES).get() > 2);
    counter.sendMessage(CounterCommand.STOP);
    Thread.sleep(200);
    Integer count = consumer.getAttribute(NUMBER_OF_MESSAGES).get();
    Thread.sleep(2500);
    assertEquals(count, consumer.getAttribute(NUMBER_OF_MESSAGES).get());
    ArrayList<Integer> messages = consumer.getAttribute(MESSAGES).get();
    assertNotEquals(0, messages.size());
    assertNotEquals(0, (int) messages.get(messages.size() - 1));
    counter.sendMessage(CounterCommand.RESET);
    Thread.sleep(1000);
    assertEquals(0, (int) counter.getAttribute(COUNTER).get());
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) 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