org.kurento.jsonrpc.client.JsonRpcClient Java Examples

The following examples show how to use org.kurento.jsonrpc.client.JsonRpcClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KurentoClient.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
protected KurentoClient(JsonRpcClient client) {
  this.client = client;
  this.manager = new RomManager(new RomClientJsonRpcClient(client));
  client.setRequestTimeout(requesTimeout);
  client.setConnectionTimeout((int) connectionTimeout);
  if (client instanceof AbstractJsonRpcClientWebSocket) {
    ((AbstractJsonRpcClientWebSocket) client).enableHeartbeat(KEEPALIVE_TIME);
  }
  try {
    long start = System.currentTimeMillis();
    client.connect();
    long duration = System.currentTimeMillis() - start;

    if (duration > WARN_CONNECTION_TIME) {
      log.warn("Connected to KMS in {} millis (> {} millis)", duration, WARN_CONNECTION_TIME);
    }

  } catch (Exception e) {
    throw new KurentoException("Exception connecting to KMS", e);
  }
}
 
Example #2
Source File: BidirectionalMultiTest.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  log.debug("Client started");

  JsonRpcClient client = createJsonRpcClient("/BidirectionalMultiTest");

  client.setServerRequestHandler(new DefaultJsonRpcHandler<Integer>() {

    @Override
    public void handleRequest(Transaction transaction, Request<Integer> request)
        throws Exception {

      log.debug("Reverse request: " + request);
      transaction.sendResponse(request.getParams() + 1);
    }
  });

  for (int i = 0; i < 60; i++) {
    client.sendRequest("echo", i, Integer.class);
  }

  client.close();

  log.debug("Client finished");
}
 
Example #3
Source File: BasicAsyncClientEchoTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  log.debug("Client started");

  JsonRpcClient client = createJsonRpcClient("/jsonrpc");

  final JsonObject params = new JsonObject();
  params.addProperty("param1", "Value1");
  params.addProperty("param2", "Value2");

  CountDownLatch finishTestLatch = new CountDownLatch(1);

  client.sendRequest("echo", params, new Continuation<JsonElement>() {

    @Override
    public void onSuccess(JsonElement result) {
      log.debug("Response:" + result);

      Assert.assertEquals(params.get("param1").getAsString(), "Value1");
      Assert.assertEquals(params.get("param2").getAsString(), "Value2");
    }

    @Override
    public void onError(Throwable cause) {
      cause.printStackTrace();
    }
  });

  finishTestLatch.await(5, TimeUnit.SECONDS);

  client.close();

  log.debug("Client finished");
}
 
Example #4
Source File: KurentoClient.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
private static void updateLabel(JsonRpcClient client, String label) {
  String clientLabel = "KurentoClient";
  if (label != null) {
    clientLabel += ":" + label;
  }
  client.setLabel(clientLabel);
}
 
Example #5
Source File: WebSocketRomTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
protected JsonRpcClient createJsonRpcClient() {

  String uri = "ws://localhost:" + getPort() + "/handler";
  log.debug("Creating client in URI: " + uri);
  return new JsonRpcClientNettyWebSocket(uri);
}
 
Example #6
Source File: OpenViduClient.java    From openvidu with Apache License 2.0 4 votes vote down vote up
public OpenViduClient(JsonRpcClient client, ServerJsonRpcHandler handler) {
  this.client = client;
  this.handler = handler;
  this.client.setServerRequestHandler(this.handler);
}
 
Example #7
Source File: JsonRpcClientLocalTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Test
public void echoTest() throws Exception {

  LOG.info("Client started");

  JsonRpcClient client = new JsonRpcClientLocal(new EchoJsonRpcHandler());

  Params params = new Params();
  params.param1 = "Value1";
  params.param2 = "Value2";

  Params result = client.sendRequest("echo", params, Params.class);

  LOG.info("Response:" + result);

  Assert.assertEquals(params.param1, result.param1);
  Assert.assertEquals(params.param2, result.param2);

  client.close();

  LOG.info("Client finished");

}
 
Example #8
Source File: ClientSession.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
  if (requestSender instanceof JsonRpcClient) {
    ((JsonRpcClient) requestSender).close();
  }
}
 
Example #9
Source File: MultipleSessionsTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
private void counterSession() {

    JsonRpcClient client = createJsonRpcClient("/jsonrpc_multiple");

    try {

      for (int i = 0; i < 5; i++) {

        int counter = client.sendRequest("count", null, Integer.class);
        Assert.assertEquals(i, counter);
      }

      client.close();

    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
 
Example #10
Source File: BasicEchoTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws IOException {

  log.debug("Client started");

  JsonRpcClient client = createJsonRpcClient("/jsonrpc");
  Params params = new Params();
  params.param1 = "Value1";
  params.param2 = "Value2";

  Params result = client.sendRequest("echo", params, Params.class);

  log.debug("Response:" + result);

  Assert.assertEquals(params.param1, result.param1);
  Assert.assertEquals(params.param2, result.param2);

  client.close();

  log.debug("Client finished");

}
 
Example #11
Source File: CloseMessageTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  JsonRpcClientWebSocket client = (JsonRpcClientWebSocket) createJsonRpcClient("/reconnection");
  client.setSendCloseMessage(true);

  Assert.assertEquals("new", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));

  String sessionId = client.getSession().getSessionId();

  client.close();

  JsonRpcClient client2 = createJsonRpcClient("/reconnection");
  client2.connect();
  client2.setSessionId(sessionId);

  Assert.assertEquals("new", client2.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client2.sendRequest("sessiontest", String.class));

}
 
Example #12
Source File: CloseSessionTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  JsonRpcClient client = createJsonRpcClient("/close_session_handler");

  Assert.assertEquals("new", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));

  client = createJsonRpcClient("/close_session_handler");

  Assert.assertEquals("new", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));

  client.close();

}
 
Example #13
Source File: JsonRpcConnectorBaseTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
protected JsonRpcClient createJsonRpcClient(String servicePath) {
  return createJsonRpcClient(servicePath, null);
}
 
Example #14
Source File: LocalRomTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Override
protected JsonRpcClient createJsonRpcClient() {
  return new JsonRpcClientLocal(handler);
}
 
Example #15
Source File: OpenViduClient.java    From openvidu with Apache License 2.0 4 votes vote down vote up
public OpenViduClient(JsonRpcClient client) {
  this.client = client;
  this.handler = new ServerJsonRpcHandler();
  this.client.setServerRequestHandler(this.handler);
}
 
Example #16
Source File: AbstractRomTest.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws IOException {

  LOG.info("Starting server");

  RomServerJsonRpcHandler jsonRpcHandler = new RomServerJsonRpcHandler(
      "org.kurento.client.internal.test.model.server", "Impl");

  startJsonRpcServer(jsonRpcHandler);

  LOG.info("Server started");

  LOG.info("Starting client");

  JsonRpcClient client = createJsonRpcClient();
  useRom(client);

  client.close();

  destroyJsonRpcServer();
}
 
Example #17
Source File: RomClientJsonRpcClient.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
public RomClientJsonRpcClient(JsonRpcClient client) {
  this.client = client;
}
 
Example #18
Source File: OpenViduClientTest.java    From openvidu with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
  jsonRpcClient = mock(JsonRpcClient.class);
  serverHandler = new ServerJsonRpcHandler();
  client = new OpenViduClient(jsonRpcClient, serverHandler);
}
 
Example #19
Source File: KurentoClient.java    From kurento-java with Apache License 2.0 4 votes vote down vote up
public static KurentoClient createFromJsonRpcClient(JsonRpcClient jsonRpcClient) {
  return new KurentoClient(jsonRpcClient);
}
 
Example #20
Source File: RoomClientTest.java    From kurento-room with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
  jsonRpcClient = mock(JsonRpcClient.class);
  serverHandler = new ServerJsonRpcHandler();
  client = new KurentoRoomClient(jsonRpcClient, serverHandler);
}
 
Example #21
Source File: KurentoRoomClient.java    From kurento-room with Apache License 2.0 4 votes vote down vote up
public KurentoRoomClient(JsonRpcClient client, ServerJsonRpcHandler handler) {
  this.client = client;
  this.handler = handler;
  this.client.setServerRequestHandler(this.handler);
}
 
Example #22
Source File: KurentoRoomClient.java    From kurento-room with Apache License 2.0 4 votes vote down vote up
public KurentoRoomClient(JsonRpcClient client) {
  this.client = client;
  this.handler = new ServerJsonRpcHandler();
  this.client.setServerRequestHandler(this.handler);
}
 
Example #23
Source File: ServerEventsTest.java    From kurento-java with Apache License 2.0 3 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  log.debug("Client started");

  JsonRpcClient client = createJsonRpcClient("/serverevents");

  String result = client.sendRequest("echo", "params", String.class);

  Assert.assertTrue("The method 'afterConnectionEstablished' is not invoked",
      afterConnectionEstablishedLatch.await(TIMEOUT, TimeUnit.MILLISECONDS));

  log.debug("Response:" + result);

  Assert.assertEquals("params", result);

  Assert.assertTrue("The method 'handleRequest' is not invoked",
      requestLatch.await(TIMEOUT, TimeUnit.MILLISECONDS));

  client.close();

  Assert.assertTrue("The method 'afterConnectionClosed' is not invoked",
      afterConnectionClosedLatch.await(TIMEOUT, TimeUnit.MILLISECONDS));

  log.debug("Client finished");

}
 
Example #24
Source File: NotificationTest.java    From kurento-java with Apache License 2.0 3 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  serverRequestLatch = new CountDownLatch(3);

  JsonRpcClient client = createJsonRpcClient("/notification");

  client.setServerRequestHandler(new DefaultJsonRpcHandler<Integer>() {

    @Override
    public void handleRequest(Transaction transaction, Request<Integer> request)
        throws Exception {

      serverRequestLatch.countDown();
    }
  });

  client.sendNotification("echo", 1);
  client.sendNotification("echo", 2);
  client.sendNotification("echo", 3);

  Assert.assertTrue("The server has not invoked requests",
      serverRequestLatch.await(5000, TimeUnit.MILLISECONDS));

  client.close();

}
 
Example #25
Source File: NewSessionTest.java    From kurento-java with Apache License 2.0 3 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  JsonRpcClient client = createJsonRpcClient("/new_session_handler");

  Assert.assertEquals("new", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));
  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));

  client.close();

}
 
Example #26
Source File: LargePackageTest.java    From kurento-java with Apache License 2.0 3 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  JsonRpcClient client = createJsonRpcClient("/largepackage");

  String largePackage = client.sendRequest("echo", String.class);

  System.out.println(largePackage);

  Thread.sleep(2000);

  client.close();

}
 
Example #27
Source File: AsyncServerTest.java    From kurento-java with Apache License 2.0 3 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  JsonRpcClient client = createJsonRpcClient("/async_handler");

  String response = client.sendRequest("count", "fakeparams", String.class);

  Assert.assertEquals("AsyncHello", response);

  client.close();

}
 
Example #28
Source File: ReconnectionFromServerTest.java    From kurento-java with Apache License 2.0 2 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  JsonRpcClient client = new JsonRpcClientWebSocket(
      "ws://localhost:" + getPort() + "/reconnection2");
  client.setServerRequestHandler(new DefaultJsonRpcHandler<JsonElement>() {

    @Override
    public void handleRequest(Transaction transaction, Request<JsonElement> request)
        throws Exception {

      log.debug("Receive request in client: " + request);
      transaction.sendResponse("world");
      log.debug("Response sent from client");
    }
  });

  Assert.assertEquals("new", client.sendRequest("sessiontest", String.class));

  waitForServer();

  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));

  waitForServer();

  log.debug("SessionId: " + client.getSession().getSessionId());

  JsonRpcClientWebSocket webSocketClient = (JsonRpcClientWebSocket) client;

  webSocketClient.closeNativeClient();

  Thread.sleep(100);

  Assert.assertEquals("old", client.sendRequest("sessiontest", String.class));

  waitForServer();

  log.debug("Acquired");

  client.close();

}
 
Example #29
Source File: PingPongTest.java    From kurento-java with Apache License 2.0 2 votes vote down vote up
@Test
public void test() throws IOException, InterruptedException {

  log.debug("Client started");

  JsonRpcClient client = createJsonRpcClient("/pingpong", new JsonRpcWSConnectionAdapter() {

    @Override
    public void connectionFailed() {
      System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

    }

    @Override
    public void disconnected() {
      System.out.println("#######################################");

    }
  });

  client.setHeartbeatInterval(500);
  client.enableHeartbeat();

  String result = client.sendRequest("echo", "Params", String.class);

  log.debug("Response:" + result);

  Assert.assertEquals(result, "OK");

  Thread.sleep(20000);

  log.debug("----------------- Disabling heartbeat in client ----------------");

  client.disableHeartbeat();

  // This should lead to reconnect clients

  Thread.sleep(30000);

  log.debug("----------------- Enabling heartbeat in client ----------------");
  client.enableHeartbeat();

  Thread.sleep(30000);

  log.debug("Client finished");

}
 
Example #30
Source File: AbstractRomTest.java    From kurento-java with Apache License 2.0 2 votes vote down vote up
public void useRom(JsonRpcClient client) {

    RomManager manager = new RomManager(new RomClientJsonRpcClient(client));

    SampleClass obj = new SampleClass.Builder("XXX", false, manager).withAtt3(0.5f).withAtt4(22)
        .build();

    for (int i = 0; i < 5; i++) {

      assertEquals(obj.getAtt1(), "XXX");
      assertFalse(obj.getAtt2());
      assertEquals(obj.getAtt3(), 0.5f, 0.01);
      assertEquals(obj.getAtt4(), 22);

      assertEquals(SampleEnum.CONSTANT_1, obj.echoEnum(SampleEnum.CONSTANT_1));

      ComplexParam returnValue = obj.echoRegister(new ComplexParam("prop1", 33));

      assertEquals(returnValue.getProp1(), "prop1");
      assertEquals(returnValue.getProp2(), 33);

      List<SampleEnum> result = obj
          .echoListEnum(Arrays.asList(SampleEnum.CONSTANT_1, SampleEnum.CONSTANT_2));

      assertEquals(SampleEnum.CONSTANT_1, result.get(0));
      assertEquals(SampleEnum.CONSTANT_2, result.get(1));

      List<ComplexParam> params = new ArrayList<>();
      params.add(new ComplexParam("prop1_1", 33));
      params.add(new ComplexParam("prop1_2", 44));

      List<ComplexParam> returnParams = obj.echoListRegister(params);

      ComplexParam value1 = returnParams.get(0);
      ComplexParam value2 = returnParams.get(1);

      assertEquals(value1.getProp1(), "prop1_1");
      assertEquals(value1.getProp2(), 33);

      assertEquals(value2.getProp1(), "prop1_2");
      assertEquals(value2.getProp2(), 44);

    }
  }