package edu.vt.marian.server; import java.io.*; import java.net.*; import java.lang.*; import java.util.*; import edu.vt.marian.common.*; /** class name: session_manage class description: this thread is responsible for check session "database" from time to time and inform corresponding session manager object to delete old sessions. uses the services of class(es): designer(s): Jianxin Zhao (jxzhao@csgrad.cs.vt.edu) implementator(s): Ning Chai (nchai@csgrad.cs.vt.edu) finished time: Nov 26th, 1998 known bugs: JDK version: 1.1.5 side effects: */ public class session_manage extends Thread { public final static int OK = 0; public final static int INVALID_TIME = 1; public final static int NULL_PROTOCOL = 2; public final static int INVALID_PROTOCOL = 3; private session_table st; private long check_interval; private long delete_interval; private String protocol; /** this is just used for debugging */ Debug debug; /** method description: this constructor will create a session manage thread, the directory/file contains all the configuration information about this object, this object is created by the specified session table object. 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 manage st --- the session table which created this object debug -- used for debugging output parameter(s): none return value: none synchronization: none */ public session_manage(String file_name, session_table st, Debug debug) { BufferedReader in_file = null; String line = null; StringTokenizer token_line = null; String s = null; String s1 = null; init(); this.debug = debug; if ((file_name == null) || file_name.equals("")) { debug.dumpTrace("Class: session_manage Method:constructor File name is null or empty."); } try { in_file = new BufferedReader(new FileReader(file_name)); line = in_file.readLine(); while (line != null) { token_line = new StringTokenizer(line," ",false); if (token_line.countTokens() > 1) // ignore empty lines and lines with only one token { s = token_line.nextToken(); if (!s.startsWith("#")) //Ignore comments(begin with "#") { if (s.equals("check_interval")) { s1 = token_line.nextToken(); try { check_interval = Long.parseLong(s1); if (check_interval < 0) { check_interval = 420000; // set it to default value -- 7 minutes debug.dumpTrace("Class:session_manage Method:constructor check_interval must be a positive long integer."); } } catch (Exception e) { check_interval = 420000; // set it to default value -- 7 minutes debug.dumpTrace("Class:session_manage Method:constructor check_interval must be a long integer."); } } if (s.equals("delete_interval")) { s1 = token_line.nextToken(); try { delete_interval = Long.parseLong(s1); if (delete_interval < 0) { delete_interval = 420000; // set it to default value -- 7 minutes debug.dumpTrace("Class:session_manage Method:constructor delete_interval must be a positive long integer."); } } catch (Exception e) { delete_interval = 420000; // set it to default value -- 7 minutes debug.dumpTrace("Class:session_manage Method:constructor delete_interval must be a long integer."); } } if (s.equals("protocol")) { protocol = token_line.nextToken(); if (!protocol.equals("1.0")) { protocol = new String("1.0"); debug.dumpTrace("Class:session_manage Method:constructor protocol must be 1.0."); } } } // end -- if (!s.startsWith("#")) } // end -- if (token_line.countTokens() > 1) line = in_file.readLine(); } // end -- while (line != null) in_file.close(); } catch (IOException e) { debug.dumpTrace("Class:session_manage Method:constructor File open error."); } this.st = st; if (st == null) { debug.dumpTrace("Class: session_manage Method:constructor Session_table is null."); } return; } /** method description: this method is responsible for check session "database" and inform session manager to delete old sessions from time to time. uses the services of class(es): session_table input parameter(s): none output parameter(s): none return value: none synchronization: none */ public void run() { for (;;) { try { sleep(get_check_interval()); } catch (Exception e) { debug.dumpTrace("Class: session_manage Method:run Sleep failed."); } st.delete_old_sessions(get_delete_interval()); } } /** method description: this method will set the time interval under which this thread will check session "database". uses the services of class(es): none input parameter(s): time -- this will become the new interval, the unit is ms output parameter(s): none return value: OK -- the value has been set correctly INVALID_TIME -- the value is negative and doesn't make sense other -- synchronization: none */ public int set_check_interval(long time) { if (time < 0) { debug.dumpTrace("Class:session_manage Method:set_check_interval Invalid time."); return INVALID_TIME; } check_interval = time; return OK; } /** method description: this method will set the time value to delete a session, a session will be considered old if it has no activity longer than the specified time. uses the services of class(es): none input parameter(s): time -- this will become the new interval output parameter(s): none return value: OK -- the value has been set correctly INVALID_TIME -- the value is negative and doesn't make sense other -- synchronization: none */ public int set_delete_interval(long time) { if (time < 0) { debug.dumpTrace("Class:session_manage Method:set_check_interval Invalid time."); return INVALID_TIME; } delete_interval = time; return OK; } /** method description: this method will return the time interval under which this thread check session "database". uses the services of class(es): none input parameter(s): none output parameter(s): none return value: the interval as a long integer, the unit is ms synchronization: none */ public long get_check_interval() { return check_interval; } /** method description: this method will return the time value used to judge whether or not a session is old. uses the services of class(es): none input parameter(s): none output parameter(s): none return value: the interval as a long integer, the unit is ms synchronization: none */ public long get_delete_interval() { return delete_interval; } /** method description: this method will set the protocol the thread will use to manage the session "database" checking and session deleting uses the services of class(es): none input parameter(s): protocol -- this will become the new protocol output parameter(s): none return value: OK -- the new protocol has been set correctly INVALID_PROTOCOL -- the new protocol is not supported currently NULL_PROTOCOL -- the protocol is null synchronization: none */ public int set_protocol(String protocol) { if (protocol == null) { debug.dumpTrace("Class:session_manage Method:set_protocol Protocol is null."); return NULL_PROTOCOL; } if (!protocol.equals("1.0")) { debug.dumpTrace("Class:session_manage Method:set_protocol Invalid protocol."); return INVALID_PROTOCOL; } this.protocol = protocol; return OK; } /** method description: this method will return the protocol this thread currently use to manage the session "database" checking and session deleting uses the services of class(es): none input parameter(s): none output parameter(s): none return value: the protocol currently used by this object as a string synchronization: none */ public String get_protocol() { return protocol; } /** method description: this method will initialize the data members of this object uses the services of class(es): none input parameter(s): none output parameter(s): none return value: none synchronization: none */ private void init() { check_interval = 420000; // default value -- 7 minutes delete_interval = 420000; // default value -- 7 minutes st = null; debug = null; protocol = new String("1.0"); return; } }