org.kurento.jsonrpc.Transaction Java Examples

The following examples show how to use org.kurento.jsonrpc.Transaction. 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: JsonRpcUserControl.java    From kurento-room with Apache License 2.0 6 votes vote down vote up
public void joinRoom(Transaction transaction, Request<JsonObject> request,
    ParticipantRequest participantRequest) throws IOException, InterruptedException,
    ExecutionException {
  String roomName = getStringParam(request, ProtocolElements.JOINROOM_ROOM_PARAM);
  String userName = getStringParam(request, ProtocolElements.JOINROOM_USER_PARAM);

  boolean dataChannels = false;
  if (request.getParams().has(ProtocolElements.JOINROOM_DATACHANNELS_PARAM)) {
    dataChannels = request.getParams().get(ProtocolElements.JOINROOM_DATACHANNELS_PARAM)
        .getAsBoolean();
  }

  ParticipantSession participantSession = getParticipantSession(transaction);
  participantSession.setParticipantName(userName);
  participantSession.setRoomName(roomName);
  participantSession.setDataChannels(dataChannels);

  roomManager.joinRoom(userName, roomName, dataChannels, true, participantRequest);
}
 
Example #2
Source File: JsonRpcNotificationService.java    From kurento-room with Apache License 2.0 6 votes vote down vote up
private Transaction getAndRemoveTransaction(ParticipantRequest participantRequest) {
  Integer tid = null;
  if (participantRequest == null) {
    log.warn("Unable to obtain a transaction for a null ParticipantRequest object");
    return null;
  }
  String tidVal = participantRequest.getRequestId();
  try {
    tid = Integer.parseInt(tidVal);
  } catch (NumberFormatException e) {
    log.error("Invalid transaction id, a number was expected but recv: {}", tidVal, e);
    return null;
  }
  String sessionId = participantRequest.getParticipantId();
  SessionWrapper sw = sessions.get(sessionId);
  if (sw == null) {
    log.warn("Invalid session id {}", sessionId);
    return null;
  }
  log.trace("#{} - {} transactions", sessionId, sw.getTransactions().size());
  Transaction t = sw.getTransaction(tid);
  sw.removeTransaction(tid);
  return t;
}
 
Example #3
Source File: DemoJsonRpcUserControl.java    From kurento-room with Apache License 2.0 6 votes vote down vote up
private void handleHatRequest(Transaction transaction, Request<JsonObject> request,
    ParticipantRequest participantRequest) throws IOException {
  boolean filterOn = request.getParams().get(filterType.getCustomRequestParam()).getAsBoolean();
  String pid = participantRequest.getParticipantId();
  if (filterOn) {
    if (transaction.getSession().getAttributes().containsKey(SESSION_ATTRIBUTE_FILTER)) {
      throw new RuntimeException(filterType + " filter already on");
    }
    log.info("Applying {} filter to session {}", filterType, pid);

    FaceOverlayFilter filter =
        new FaceOverlayFilter.Builder(roomManager.getPipeline(pid)).build();
    filter.setOverlayedImage(this.hatUrl, this.offsetXPercent, this.offsetYPercent,
        this.widthPercent, this.heightPercent);

    addFilter(transaction, pid, filter);
  } else {
    removeFilter(transaction, pid);
  }
  transaction.sendResponse(new JsonObject());
}
 
Example #4
Source File: AsyncServerTest.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final Transaction transaction, Request<String> request)
    throws Exception {

  transaction.startAsync();

  // Poor man method scheduling
  new Thread() {
    @Override
    public void run() {
      try {
        Thread.sleep(1000);
        transaction.sendResponse("AsyncHello");
      } catch (Exception e) {
      }
    }
  }.start();
}
 
Example #5
Source File: DemoJsonRpcUserControl.java    From kurento-room with Apache License 2.0 6 votes vote down vote up
@Override
public void customRequest(Transaction transaction, Request<JsonObject> request,
    ParticipantRequest participantRequest) {
  try {
    if (request.getParams() == null
        || request.getParams().get(filterType.getCustomRequestParam()) == null) {
      throw new RuntimeException(
          "Request element '" + filterType.getCustomRequestParam() + "' is missing");
    }
    switch (filterType) {
      case MARKER:
        handleMarkerRequest(transaction, request, participantRequest);
        break;
      case HAT:
      default:
        handleHatRequest(transaction, request, participantRequest);
    }
  } catch (Exception e) {
    log.error("Unable to handle custom request", e);
    try {
      transaction.sendError(e);
    } catch (IOException e1) {
      log.warn("Unable to send error response", e1);
    }
  }
}
 
Example #6
Source File: RpcNotificationService.java    From openvidu with Apache License 2.0 6 votes vote down vote up
public void sendErrorResponse(String participantPrivateId, Integer transactionId, Object data,
		OpenViduException error) {
	Transaction t = getAndRemoveTransaction(participantPrivateId, transactionId);
	if (t == null) {
		if (!isIpcamParticipant(participantPrivateId)) {
			log.error("No transaction {} found for paticipant with private id {}, unable to send result {}",
					transactionId, participantPrivateId, data);
		}
		return;
	}
	try {
		String dataVal = data != null ? data.toString() : null;
		t.sendError(error.getCodeValue(), error.getMessage(), dataVal);
	} catch (Exception e) {
		log.error("Exception sending error response to user ({})", transactionId, e);
	}
}
 
Example #7
Source File: BidirectionalTest.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(Transaction transaction, Request<Object> request) throws Exception {

  log.debug("Request id:" + request.getId());
  log.debug("Request method:" + request.getMethod());
  log.debug("Request params:" + request.getParams());

  transaction.sendResponse(request.getParams());

  final Session session = transaction.getSession();
  final Object params = request.getParams();

  new Thread() {
    @Override
    public void run() {
      asyncReverseSend(session, params);
    }
  }.start();
}
 
Example #8
Source File: CloseSessionTest.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final Transaction transaction, Request<String> request)
    throws Exception {

  Session session = transaction.getSession();

  if (session.isNew()) {
    transaction.sendResponse("new");
  } else {
    transaction.sendResponse("old");
  }

  if (counter == 2) {
    session.close();
  }
  counter++;
}
 
Example #9
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 #10
Source File: BidirectionalMultiTest.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(Transaction transaction, Request<Integer> request) throws Exception {

  log.debug("Request id:" + request.getId());
  log.debug("Request method:" + request.getMethod());
  log.debug("Request params:" + request.getParams());

  transaction.sendResponse(request.getParams());

  final Session session = transaction.getSession();
  final Object params = request.getParams();

  new Thread() {
    @Override
    public void run() {
      asyncReverseSend(session, params);
    }
  }.start();
}
 
Example #11
Source File: JsonRpcClientLocalTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(Transaction transaction, Request<JsonObject> request)
    throws Exception {

  LOG.info("Request id:" + request.getId());
  LOG.info("Request method:" + request.getMethod());
  LOG.info("Request params:" + request.getParams());

  transaction.sendResponse(request.getParams());
}
 
Example #12
Source File: NotificationTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final Transaction transaction, Request<Integer> request)
    throws Exception {

  if (!transaction.isNotification()) {
    throw new RuntimeException("Notification expected");
  }

  Thread.sleep(1000);

  transaction.getSession().sendNotification("response", request.getParams());
}
 
Example #13
Source File: PerSessionJsonRpcHandler.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(Transaction transaction, Request<T> request) throws Exception {

  JsonRpcHandler<T> handler = getHandler(transaction.getSession());

  Assert.isTrue(handler != null, "Handler of class " + provider.getClass()
      + " can't be created. Be sure that there" + " is a bean registered of this type");

  try {
    handler.handleRequest(transaction, request);
  } catch (Exception e) {
    handler.handleUncaughtException(transaction.getSession(), e);
  }
}
 
Example #14
Source File: JsonRpcNotificationService.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
@Override
public void sendErrorResponse(ParticipantRequest participantRequest, Object data,
    RoomException error) {
  Transaction t = getAndRemoveTransaction(participantRequest);
  if (t == null) {
    log.error("No transaction found for {}, unable to send result {}", participantRequest, data);
    return;
  }
  try {
    String dataVal = data != null ? data.toString() : null;
    t.sendError(error.getCodeValue(), error.getMessage(), dataVal);
  } catch (Exception e) {
    log.error("Exception sending error response to user ({})", participantRequest, e);
  }
}
 
Example #15
Source File: MultipleSessionsTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(Transaction transaction, Request<String> request) throws Exception {

  if (demoBean == null) {
    throw new RuntimeException("Not autowired dependencies");
  }

  transaction.sendResponse(counter);
  counter++;
}
 
Example #16
Source File: ReconnectionFromServerTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final Transaction transaction, Request<String> request)
    throws Exception {

  log.debug("Receive request in server: " + request);

  if (session == null) {
    session = transaction.getSession();
  }

  if (session.isNew()) {
    transaction.sendResponse("new");
  } else {
    transaction.sendResponse("old");
  }

  log.debug("Response sent from server");

  executor.schedule(new Runnable() {
    @Override
    public void run() {
      try {
        log.debug("Request send from server");
        JsonElement result = session.sendRequest("hello");
        log.debug("Response received in server");
        log.debug("Result: " + result);
        s.release();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }, 500, TimeUnit.MILLISECONDS);
}
 
Example #17
Source File: NewSessionTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final Transaction transaction, Request<String> request)
    throws Exception {

  if (transaction.getSession().isNew()) {
    transaction.sendResponse("new");
  } else {
    transaction.sendResponse("old");
  }
}
 
Example #18
Source File: ServerEventsTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(Transaction transaction, Request<String> request) throws Exception {

  log.debug("Request: " + request);
  transaction.sendResponse(request.getParams());
  requestLatch.countDown();
}
 
Example #19
Source File: RomServerJsonRpcHandler.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
private void handleInvokeCommand(Transaction transaction, String objectRef, String operationName,
    JsonObject operationParams) throws IOException {

  Object result = server.invoke(objectRef, operationName,
      JsonUtils.fromJson(operationParams, Props.class), Object.class);

  transaction.sendResponse(result);
}
 
Example #20
Source File: ReconnectionTest.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final Transaction transaction, Request<String> request)
    throws Exception {

  Session session = transaction.getSession();

  if (session.isNew()) {
    transaction.sendResponse("new");
  } else {
    transaction.sendResponse("old");
  }
}
 
Example #21
Source File: ServerJsonRpcHandler.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
private Notification mediaError(Transaction transaction, Request<JsonObject> request) {
  String description = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.MEDIAERROR_ERROR_PARAM, String.class);
  MediaErrorInfo eventInfo = new MediaErrorInfo(description);
  log.debug("Recvd media error event {}", eventInfo);
  return eventInfo;
}
 
Example #22
Source File: ServerJsonRpcHandler.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
private Notification participantJoined(Transaction transaction, Request<JsonObject> request) {
  String id = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTJOINED_USER_PARAM, String.class);
  ParticipantJoinedInfo eventInfo = new ParticipantJoinedInfo(id);
  log.debug("Recvd participant joined event {}", eventInfo);
  return eventInfo;
}
 
Example #23
Source File: ServerJsonRpcHandler.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
private Notification participantLeft(Transaction transaction, Request<JsonObject> request) {
  String name = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTLEFT_NAME_PARAM, String.class);
  ParticipantLeftInfo eventInfo = new ParticipantLeftInfo(name);
  log.debug("Recvd participant left event {}", eventInfo);
  return eventInfo;
}
 
Example #24
Source File: ServerJsonRpcHandler.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
private Notification participantPublished(Transaction transaction, Request<JsonObject> request) {
  String id = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTPUBLISHED_USER_PARAM, String.class);
  JsonArray jsonStreams = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTPUBLISHED_STREAMS_PARAM, JsonArray.class);
  Iterator<JsonElement> streamIt = jsonStreams.iterator();
  List<String> streams = new ArrayList<String>();
  while (streamIt.hasNext()) {
    streams.add(JsonRoomUtils.getResponseProperty(streamIt.next(),
        ProtocolElements.PARTICIPANTPUBLISHED_STREAMID_PARAM, String.class));
  }
  ParticipantPublishedInfo eventInfo = new ParticipantPublishedInfo(id, streams);
  log.debug("Recvd published event {}", eventInfo);
  return eventInfo;
}
 
Example #25
Source File: ServerJsonRpcHandler.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
private Notification participantUnpublished(Transaction transaction, Request<JsonObject> request) {
  String name = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTUNPUBLISHED_NAME_PARAM, String.class);
  ParticipantUnpublishedInfo eventInfo = new ParticipantUnpublishedInfo(name);
  log.debug("Recvd participant unpublished event {}", eventInfo);
  return eventInfo;
}
 
Example #26
Source File: ServerJsonRpcHandler.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
private Notification roomClosed(Transaction transaction, Request<JsonObject> request) {
  String room = JsonRoomUtils.getRequestParam(request, ProtocolElements.ROOMCLOSED_ROOM_PARAM,
      String.class);
  RoomClosedInfo eventInfo = new RoomClosedInfo(room);
  log.debug("Recvd room closed event {}", eventInfo);
  return eventInfo;
}
 
Example #27
Source File: ServerJsonRpcHandler.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
private Notification participantSendMessage(Transaction transaction, Request<JsonObject> request) {
  String room = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTSENDMESSAGE_ROOM_PARAM, String.class);
  String user = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTSENDMESSAGE_USER_PARAM, String.class);
  String message = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTSENDMESSAGE_MESSAGE_PARAM, String.class);
  SendMessageInfo eventInfo = new SendMessageInfo(room, user, message);
  log.debug("Recvd send message event {}", eventInfo);
  return eventInfo;
}
 
Example #28
Source File: ServerJsonRpcHandler.java    From openvidu with Apache License 2.0 5 votes vote down vote up
private Notification participantSendMessage(Transaction transaction,
    Request<JsonObject> request) {
  String data = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTSENDMESSAGE_DATA_PARAM, String.class);
  String from = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTSENDMESSAGE_FROM_PARAM, String.class);
  String type = JsonRoomUtils.getRequestParam(request,
      ProtocolElements.PARTICIPANTSENDMESSAGE_TYPE_PARAM, String.class);
  SendMessageInfo eventInfo = new SendMessageInfo(data, from, type);
  log.debug("Recvd send message event {}", eventInfo);
  return eventInfo;
}
 
Example #29
Source File: JsonRpcNotificationService.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
@Override
public void sendResponse(ParticipantRequest participantRequest, Object result) {
  Transaction t = getAndRemoveTransaction(participantRequest);
  if (t == null) {
    log.error("No transaction found for {}, unable to send result {}", participantRequest, result);
    return;
  }
  try {
    t.sendResponse(result);
  } catch (Exception e) {
    log.error("Exception responding to user ({})", participantRequest, e);
  }
}
 
Example #30
Source File: JsonRpcNotificationService.java    From kurento-room with Apache License 2.0 5 votes vote down vote up
public SessionWrapper addTransaction(Transaction t, Request<JsonObject> request) {
  String sessionId = t.getSession().getSessionId();
  SessionWrapper sw = sessions.get(sessionId);
  if (sw == null) {
    sw = new SessionWrapper(t.getSession());
    SessionWrapper oldSw = sessions.putIfAbsent(sessionId, sw);
    if (oldSw != null) {
      log.warn("Concurrent initialization of session wrapper #{}", sessionId);
      sw = oldSw;
    }
  }
  sw.addTransaction(request.getId(), t);
  return sw;
}