package edu.vt.marian.server; import java.io.*; import java.net.*; import java.util.*; import edu.vt.marian.common.*; import edu.vt.marian.uip.*; /** class name: session_manager class description: this class is mainly a wrapper of the class session_table but it knows client uip and server uip through which it communicate with client(s) and C/C++ server, it also pass system resource manager to session table.

uses the services of class(es): session_table, client_uip, server_uip designer(s): Jianxin Zhao (jxzhao@csgrad.cs.vt.edu) implementator(s): finished time: known bugs: JDK version: 1.1.5 side effects: */ public class session_manager implements call_back_processor { /** this object is used to communicate with webgate(s) */ private server_uip my_su = null; /** this object is used to communicate with C/C++ server */ private client_uip my_cu = null; /** this object is responsible to manage a number of sessions */ private session_table my_st = null; /** this object is used to log data */ private log_manager my_lm = null; /** this object contains all the resources of the system */ private resource_manager my_rm = null; /** this directory contains all the configuration information of this object */ private String my_dir = null; /** this is just used for debugging */ Debug debug; /** possible methods return values */ public final static int NULL_FUNCTION = 1; public final static int PROCESS_CALL_FROM_SERVER_UIP_FAILED = 2; public final static int PROCESS_CALL_FROM_CLIENT_UIP_FAILED = 3; public final static int NULL_FUNCTION_NAME = 4; public final static int INVALID_PARAMETER = 5; public final static int PROCESS_CALL_FROM_SESSION_TABLE_TO_CLIENT_FAILED = 6; public final static int PROCESS_CALL_FROM_SESSION_TABLE_TO_C_FAILED = 7; public final static int NULL_PARAMETER = 8; /** method description: this constructor will create a session manager object from the dpecified directory, the directory/file contains all the configuration information about this object, this object will use the server uip object to communicate with webgate(s), use the client uip object to communicate with C/C++ marian server, resource manager object will be passed to the session table created by it.

uses the services of class(es): input parameter(s): file_name --- the name of the file/directory which contain the configuration information about the session manager su --- the session manager communicate with webgate through this object cu -- the session manager communicate with C/C++ server through this object rm -- this object contain all the resource of the system, pack them all together into one object will make adding and deleting one or several of them easier in the future debug -- used for debugging output parameter(s): none return value: none */ public session_manager(String file_name, resource_manager rm, Debug debug) { this.debug = debug; if (rm == null) { debug.dumpTrace("class session_manager, constructor, parameter resource manager is null"); return; } my_rm = rm; my_lm = my_rm.get_log_manager(); if (my_lm == null) { debug.dumpTrace("class session_manager, constructor, log manager is null"); return; } if (file_name == null) { debug.dumpTrace("class session_manager, constructor, parameter file name is null"); return; } my_dir = file_name; // create the session table my_st = new session_table(this, my_dir + "session_table" + File.separator, rm, debug); } /** method description: this method will process the function recieved from webgate, the parameter client_id specified which webgate sent this function

uses the services of class(es): session_table input parameter(s): client_id --- used to identify which client_uip sent the function, currently each client_uip represent a webgate rf -- the function sent to this object by the client_uip output parameter(s): none return value: 0 -- the function has been processed successfully 1 -- error happens when the session table process the function */ public int process_call_back(int client_id, rpc_function rf) { if (rf == null) { debug.dumpTrace("class session manager, method process_call_back from server uip, parameter function is null"); return NULL_FUNCTION; } if (my_st.process_call_from_client(client_id, rf) != my_st.OK) { return PROCESS_CALL_FROM_SERVER_UIP_FAILED; } return OK; } /** method description: this method will process the function sent from the C/C++ MARIAN server.

uses the services of class(es): session_table input parameter(s): cu --- this is the client uip object which sent the function rf --- the function sent by the C/C++ MARIAN server output parameter(s): none return value: 0 -- the function has been processed correctly 1 -- error happens when the session table process the function */ public int process_call_back(client_uip cu, rpc_function rf) { if (rf == null) { debug.dumpTrace("class session manager, process call back from client uip, parameter function is null"); return NULL_FUNCTION; } if (rf.get_name() == null) { debug.dumpTrace("class session manager, process call back from client uip, function name is null"); return NULL_FUNCTION_NAME; } if ((rf.get_name()).equals("startup_notify")) { // some part in the C/C++ server has just been restarted // we need to reset all the sessions we have parameter p = rf.get_parameter(0); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function startup_notify"); return INVALID_PARAMETER; } int module_id = ((Integer) p.get_value()).intValue(); String message = "startup_notify received from C/C++ server, module_id = " + Integer.toString(module_id); my_st.exit(message); my_st = new session_table(this, my_dir + "session_table" + File.separator, my_rm, debug); return OK; } if ((rf.get_name()).equals("start_processing")) { // do nothing except log it parameter p = rf.get_parameter(0); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function start_processing"); return INVALID_PARAMETER; } int session_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(1); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function start_processing"); return INVALID_PARAMETER; } int query_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(2); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function start_processing"); return INVALID_PARAMETER; } int caller_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(3); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function start_processing"); return INVALID_PARAMETER; } int function_code = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(4); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function start_processing"); return INVALID_PARAMETER; } int host_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(5); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function start_processing"); return INVALID_PARAMETER; } int thread_id = ((Integer) p.get_value()).intValue(); String message = "start_processing received, session_id = " + Integer.toString(session_id) + " query_id = " + Integer.toString(query_id) + " caller_id = " + Integer.toString(caller_id) + " function_code = " + Integer.toString(function_code) + " host_id = " + Integer.toString(host_id) + " thread_id = " + Integer.toString(thread_id); my_lm.write_log(1, "session_manager", message); return OK; } if ((rf.get_name()).equals("end_processing")) { // do nothing except log it parameter p = rf.get_parameter(0); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function end_processing"); return INVALID_PARAMETER; } int session_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(1); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function end_processing"); return INVALID_PARAMETER; } int query_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(2); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function end_processing"); return INVALID_PARAMETER; } int caller_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(3); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function end_processing"); return INVALID_PARAMETER; } int function_code = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(4); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function end_processing"); return INVALID_PARAMETER; } int host_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(5); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function end_processing"); return INVALID_PARAMETER; } int thread_id = ((Integer) p.get_value()).intValue(); String message = "end_processing received, session_id = " + Integer.toString(session_id) + " query_id = " + Integer.toString(query_id) + " caller_id = " + Integer.toString(caller_id) + " function_code = " + Integer.toString(function_code) + " host_id = " + Integer.toString(host_id) + " thread_id = " + Integer.toString(thread_id); my_lm.write_log(1, "session_manager", message); return OK; } if ((rf.get_name()).equals("error_for_session_manager")) { // do nothing except log it parameter p = rf.get_parameter(0); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function error_for_session_manager"); return INVALID_PARAMETER; } int session_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(1); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function error_for_session_manager"); return INVALID_PARAMETER; } int query_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(2); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function error_for_session_manager"); return INVALID_PARAMETER; } int error_code = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(3); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("STRING"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function error_for_session_manager"); return INVALID_PARAMETER; } String error_message = (String)p.get_value(); p = rf.get_parameter(4); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function error_for_session_manager"); return INVALID_PARAMETER; } int host_id = ((Integer) p.get_value()).intValue(); p = rf.get_parameter(5); if ((p == null) || (! p.is_valid()) || (!(p.get_type()).equals("INTEGER"))) { debug.dumpTrace("class session manager, method process call back from client uip, parameter invalid for function error_for_session_manager"); return INVALID_PARAMETER; } int thread_id = ((Integer) p.get_value()).intValue(); String message = "error_for_session_manager received, session_id = " + Integer.toString(session_id) + " query_id = " + Integer.toString(query_id) + " error_code = " + Integer.toString(error_code) + " error_message = " + error_message + " host_id = " + Integer.toString(host_id) + " thread_id = " + Integer.toString(thread_id); my_lm.write_log(1, "session_manager", message); return OK; } // all other functions pass to session table to process if (my_st.process_call_from_c(rf) != my_st.OK) { return PROCESS_CALL_FROM_CLIENT_UIP_FAILED; } return OK; } /** method description: this method will process the function sent from it's session table to client (currently webgate)

uses the services of class(es): server_uip input parameter(s): client_id -- specifies which client (webgate) to send this function rf -- the function which need to be sent output parameter(s): none return value: 0 -- the function has been processed correctly 1 -- error happens when server uip send the function other -- */ public int process_call_from_session_table_to_client(int client_id, rpc_function rf) { if ((rf == null) || (my_su == null)) { debug.dumpTrace("class session_manager, method process_call_from_session_table_to_client, parameter function or server_uip is null"); return NULL_FUNCTION; } // pass the client_id and function to server uip to pass to corresponding // webgate if (my_su.rpc_call(client_id, rf) != my_su.OK) { return PROCESS_CALL_FROM_SESSION_TABLE_TO_CLIENT_FAILED; } return OK; } /** method description: this method will process the function sent from it's session table to C/C++ marian server

uses the services of class(es): client_uip input parameter(s): rf -- the rpc function need to be sent output parameter(s): none return value: 0 -- the function has been processed correctly 1 -- error happens when client uip send the function */ public int process_call_from_session_table_to_c(rpc_function rf) { if ((rf == null) || (my_cu == null)) { debug.dumpTrace("class session_manager, method process_call_from_session_table_to_c, parameter function or client_uip is null"); return NULL_FUNCTION; } // pass the client_id and function to server uip to pass to corresponding // webgate if (my_cu.rpc_call(rf) != my_cu.OK) { return PROCESS_CALL_FROM_SESSION_TABLE_TO_C_FAILED; } return OK; } /** method description: this method will let this object exit smoothly, release all the resources it occupies and kill all the threads it created

uses the services of class(es): sesssion_table input parameter(s): condition -- specify the condition under which this method is called output parameter(s): none return value: 0 -- the exit is smooth other -- */ public int exit(String condition) { my_st.exit(condition); return OK; } /** method description: this method will inform the session_manager of the communication classes it will use to communicate with webgate(s) and C/C++ marian server.

uses the services of class(es): none input parameter(s): cu -- this will be used by the session_manager to communicate with the C/C++ marian server su -- this will be used by the session_manager to communicate with webgate(s) output parameter(s): none return value: 0 -- the communication classes has been set correctly other -- */ public int set_communication_classes(client_uip cu, server_uip su) { if ((cu == null) || (su == null)) { debug.dumpTrace("class session_manager, method set_communication_classes, parameter client_uip or server_uip is null"); return NULL_PARAMETER; } my_su = su; my_cu = cu; return OK; } /** method description: this method will log he message sent from the session_table of this class

uses the services of class(es): none input parameter(s): log_level -- the depth of the message generator relative to the session_table, 1 means it's the session_table itself message -- the message the session_table want the session_manager to log output parameter(s): none return value: 0 -- the message has been loged correctly */ public int log_data_from_session_table(int log_level, String message) { my_lm.write_log(log_level, "session_manager", message); return OK; } }