SCMPMessage reqMessage = request.getMessage();
String serviceName = reqMessage.getServiceName();
// check service is present and enabled
Service abstractService = this.getService(serviceName);
if (abstractService.isEnabled() == false) {
SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.SERVICE_DISABLED, "service="
+ abstractService.getName() + " is disabled");
scmpCommandException.setMessageType(getKey());
throw scmpCommandException;
}
// enhance ipAddressList
String ipAddressList = reqMessage.getHeader(SCMPHeaderAttributeKey.IP_ADDRESS_LIST);
ipAddressList = ipAddressList + Constants.SLASH + request.getRemoteSocketAddress().getAddress().getHostAddress();
reqMessage.setHeader(SCMPHeaderAttributeKey.IP_ADDRESS_LIST, ipAddressList);
String sessionInfo = reqMessage.getHeader(SCMPHeaderAttributeKey.SESSION_INFO);
int eciInSeconds = reqMessage.getHeaderInt(SCMPHeaderAttributeKey.ECHO_INTERVAL);
int eciInMillis = eciInSeconds * Constants.SEC_TO_MILLISEC_FACTOR;
int oti = reqMessage.getHeaderInt(SCMPHeaderAttributeKey.OPERATION_TIMEOUT);
switch (abstractService.getType()) {
case CASCADED_SESSION_SERVICE:
CascadedSC cascadedSC = ((CascadedSessionService) abstractService).getCascadedSC();
CommandCascCallback callback = new CommandCascCallback(request, response, responderCallback);
cascadedSC.createSession(reqMessage, callback, oti);
return;
case CASCADED_FILE_SERVICE:
cascadedSC = ((CascadedFileService) abstractService).getCascadedSC();
callback = new CommandCascCallback(request, response, responderCallback);
cascadedSC.createSession(reqMessage, callback, oti);
return;
case SESSION_SERVICE:
// code for type session service is below switch statement
break;
case FILE_SERVICE:
FileService fileService = (FileService) abstractService;
// create file session
FileSession fileSession = new FileSession(sessionInfo, ipAddressList, fileService.getPath(),
fileService.getUploadFileScriptName(), fileService.getGetFileListScriptName());
fileSession.setService(fileService);
FileServer fileServer = fileService.allocateFileServerAndCreateSession(fileSession);
// add server to session
fileSession.setServer(fileServer);
fileSession.setSessionTimeoutMillis(eciInMillis * basicConf.getEchoIntervalMultiplier());
// finally add file session to the registry
this.sessionRegistry.addSession(fileSession.getId(), fileSession);
// reply to client - SCMP Version request
SCMPMessage reply = new SCMPMessage(reqMessage.getSCMPVersion());
reply.setIsReply(true);
reply.setMessageType(getKey());
reply.setSessionId(fileSession.getId());
response.setSCMP(reply);
responderCallback.responseCallback(request, response);
return;
default:
// code for other types of services is below
break;
}
// create session
Session session = new Session(sessionInfo, ipAddressList);
session.setService(abstractService);
session.setSessionTimeoutMillis(eciInMillis * basicConf.getEchoIntervalMultiplier());
reqMessage.setSessionId(session.getId());
// no need to forward echo attributes
reqMessage.removeHeader(SCMPHeaderAttributeKey.ECHO_INTERVAL);
// tries allocating a server for this session
CreateSessionCommandCallback callback = null;
int otiOnSCMillis = (int) (oti * basicConf.getOperationTimeoutMultiplier());
int tries = (otiOnSCMillis / Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
// Following loop implements the wait mechanism in case of a busy connection pool
int i = 0;
do {
// reset ipList&msgType, might have been modified in below create session try
reqMessage.setHeader(SCMPHeaderAttributeKey.IP_ADDRESS_LIST, ipAddressList);
reqMessage.setMessageType(this.getKey());
callback = new CreateSessionCommandCallback(request, response, responderCallback, session);
try {
((SessionService) abstractService).allocateServerAndCreateSession(reqMessage, callback, session, otiOnSCMillis
- (i * Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS));
// no exception has been thrown - get out of wait loop
break;
} catch (NoFreeServerException ex) {
LOGGER.debug("NoFreeServerException caught in wait mec of csc create session, tries left=" + tries);
if (i >= (tries - 1)) {
// only one loop outstanding - don't continue throw current exception
throw ex;
}
} catch (ConnectionPoolBusyException ex) {
LOGGER.debug("ConnectionPoolBusyException caught in wait mec of csc create session, tries left=" + tries);
if (i >= (tries - 1)) {
// only one loop outstanding - don't continue throw current exception
LOGGER.warn(SCMPError.NO_FREE_CONNECTION.getErrorText("service=" + reqMessage.getServiceName()));
SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.NO_FREE_CONNECTION, "service="
+ reqMessage.getServiceName());
scmpCommandException.setMessageType(this.getKey());
throw scmpCommandException;
}
}
// sleep for a while and then try again
Thread.sleep(Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);