org.kurento.jsonrpc.DefaultJsonRpcHandler Java Examples

The following examples show how to use org.kurento.jsonrpc.DefaultJsonRpcHandler. 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: 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 #2
Source File: RomClientJsonRpcClient.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void addRomEventHandler(final RomEventHandler eventHandler) {

  this.client.setServerRequestHandler(new DefaultJsonRpcHandler<JsonObject>() {

    @Override
    public void handleRequest(Transaction transaction, Request<JsonObject> request)
        throws Exception {
      processEvent(eventHandler, request);
    }
  });
}
 
Example #3
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 #4
Source File: JsonRpcHandlerManager.java    From kurento-java with Apache License 2.0 3 votes vote down vote up
public static Class<?> getParamsType(Class<?> handlerClass) {

    Type[] genericInterfaces = handlerClass.getGenericInterfaces();

    for (Type type : genericInterfaces) {

      if (type instanceof ParameterizedType) {
        ParameterizedType parameterized = (ParameterizedType) type;

        if (parameterized.getRawType() == JsonRpcHandler.class) {
          return (Class<?>) parameterized.getActualTypeArguments()[0];
        }
      }
    }

    Type genericSuperclass = handlerClass.getGenericSuperclass();
    if (genericSuperclass != null) {

      if (genericSuperclass instanceof Class) {
        return getParamsType((Class<?>) genericSuperclass);
      }

      ParameterizedType paramClass = (ParameterizedType) genericSuperclass;

      if (paramClass.getRawType() == DefaultJsonRpcHandler.class) {
        return (Class<?>) paramClass.getActualTypeArguments()[0];
      }

      return getParamsType((Class<?>) paramClass.getRawType());

    }

    throw new JsonRpcException("Unable to obtain the type paramter of JsonRpcHandler");
  }
 
Example #5
Source File: BidirectionalTest.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("/jsonrpcreverse");

  final CountDownLatch inverseRequestLatch = new CountDownLatch(2);
  final Params[] inverseRequestParams = new Params[1];

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

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

      log.debug("Reverse request: " + request);

      transaction.sendResponse(request.getParams());
      inverseRequestParams[0] = request.getParams();

      inverseRequestLatch.countDown();
    }
  });

  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);

  inverseRequestLatch.await();

  Params newResult = inverseRequestParams[0];

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

  client.close();

  log.debug("Client finished");

}
 
Example #6
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();

}