Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* (C) Copyright 2013 Kurento (http://kurento.org/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.kurento.jsonrpc.internal.server;
import static org.kurento.jsonrpc.internal.JsonRpcConstants.METHOD_CLOSE;
import static org.kurento.jsonrpc.internal.JsonRpcConstants.METHOD_CONNECT;
import static org.kurento.jsonrpc.internal.JsonRpcConstants.METHOD_PING;
import static org.kurento.jsonrpc.internal.JsonRpcConstants.PONG;
import static org.kurento.jsonrpc.internal.JsonRpcConstants.PONG_PAYLOAD;
import static org.kurento.jsonrpc.internal.JsonRpcConstants.RECONNECTION_ERROR;
import static org.kurento.jsonrpc.internal.JsonRpcConstants.RECONNECTION_SUCCESSFUL;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import javax.annotation.PostConstruct;
import org.kurento.commons.SecretGenerator;
import org.kurento.jsonrpc.JsonRpcHandler;
import org.kurento.jsonrpc.JsonUtils;
import org.kurento.jsonrpc.internal.JsonRpcHandlerManager;
import org.kurento.jsonrpc.internal.client.AbstractSession;
import org.kurento.jsonrpc.internal.client.TransactionImpl.ResponseSender;
import org.kurento.jsonrpc.internal.server.PingWatchdogManager.NativeSessionCloser;
import org.kurento.jsonrpc.message.Request;
import org.kurento.jsonrpc.message.Response;
import org.kurento.jsonrpc.message.ResponseError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskRejectedException;
import org.springframework.scheduling.TaskScheduler;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
public class ProtocolManager {
public static final String CLIENT_CLOSED_CLOSE_REASON = "Client sent close message";
private static final String INTERVAL_PROPERTY = "interval";
public interface ServerSessionFactory {
ServerSession createSession(String sessionId, Object registerInfo,
SessionsManager sessionsManager);
void updateSessionOnReconnection(ServerSession session);
}
private static final Logger log = LoggerFactory.getLogger(ProtocolManager.class);
private static final SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss,S");
protected SecretGenerator secretGenerator = new SecretGenerator();
@Autowired
private SessionsManager sessionsManager;
@Autowired
@Qualifier("jsonrpcTaskScheduler")
private TaskScheduler taskScheduler;
private final JsonRpcHandlerManager handlerManager;
private String label = "";
private int maxHeartbeats = 0;
private int heartbeats = 0;
private PingWatchdogManager pingWachdogManager;
public ProtocolManager(JsonRpcHandler> handler) {
this.handlerManager = new JsonRpcHandlerManager(handler);
}
public ProtocolManager(JsonRpcHandler> handler, SessionsManager sessionsManager,
TaskScheduler taskScheduler) {
this.handlerManager = new JsonRpcHandlerManager(handler);
this.sessionsManager = sessionsManager;
this.taskScheduler = taskScheduler;
postConstruct();
}
@PostConstruct
private void postConstruct() {
NativeSessionCloser nativeSessionCloser = new NativeSessionCloser() {
@Override
public void closeSession(String transportId) {
ServerSession serverSession = sessionsManager.getByTransportId(transportId);
if (serverSession != null) {
serverSession.closeNativeSession("Close for not receive ping from client");
} else {
log.warn("Ping wachdog trying to close a non-registered ServerSession");
}
}
};
this.pingWachdogManager = new PingWatchdogManager(taskScheduler, nativeSessionCloser);
}
public void setLabel(String label) {
this.label = "[" + label + "] ";
}
public void processMessage(String messageJson, ServerSessionFactory factory,
ResponseSender responseSender, String internalSessionId) throws IOException {
JsonObject messagetJsonObject = JsonUtils.fromJson(messageJson, JsonObject.class);
processMessage(messagetJsonObject, factory, responseSender, internalSessionId);
}
/**
* Process incoming message. The response is sent using responseSender. If null, the session will
* be used.
*
* @param messagetJsonObject
* @param factory
* @param responseSender
* @param internalSessionId
* @throws IOException
*/
public void processMessage(JsonObject messagetJsonObject, ServerSessionFactory factory,
ResponseSender responseSender, String internalSessionId) throws IOException {
if (messagetJsonObject.has(Request.METHOD_FIELD_NAME)) {
processRequestMessage(factory, messagetJsonObject, responseSender, internalSessionId);
} else {
processResponseMessage(messagetJsonObject, internalSessionId);
}
}
// TODO Unify ServerSessionFactory, ResponseSender and transportId in a
// entity "RequestContext" or similar. In this way, there are less
// parameters
// and the implementation is easier
private void processRequestMessage(ServerSessionFactory factory, JsonObject requestJsonObject,
final ResponseSender responseSender, String transportId) throws IOException {
final Request request = JsonUtils.fromJsonRequest(requestJsonObject,
JsonElement.class);
switch (request.getMethod()) {
case METHOD_CONNECT:
log.debug("{} Req-> {} (transportId={})", label, request, transportId);
processReconnectMessage(factory, request, responseSender, transportId);
break;
case METHOD_PING:
log.trace("{} Req-> {} (transportId={})", label, request, transportId);
processPingMessage(factory, request, responseSender, transportId);
break;
case METHOD_CLOSE:
log.trace("{} Req-> {} (transportId={})", label, request, transportId);
processCloseMessage(factory, request, responseSender, transportId);
break;
default:
final ServerSession session = getOrCreateSession(factory, transportId, request);
log.debug("{} Req-> {} [jsonRpcSessionId={}, transportId={}]", label, request,
session.getSessionId(), transportId);
// TODO, Take out this an put in Http specific handler. The main
// reason is to wait for request before responding to the client.
// And for no contaminate the ProtocolManager.
if (request.getMethod().equals(Request.POLL_METHOD_NAME)) {
Type collectionType = new TypeToken>>() {
}.getType();
List> responseList = JsonUtils.fromJson(request.getParams(),
collectionType);
for (Response response : responseList) {
session.handleResponse(response);
}
// Wait for some time if there is a request from server to
// client
// TODO Allow send empty responses. Now you have to send at
// least an
// empty string
responseSender.sendResponse(new Response