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_execution class description: this thread is responsible to inform the corresponding session object to execute functions from client and C/C++ marian server. 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_execution extends Thread { public final static int OK = 0; public final static int INVALID_PRIORITY = 1; public final static int NULL_PROTOCOL = 2; public final static int PROTOCOL_GREATER_THAN_1 = 1; private String protocol; private int client_priority; private int c_priority; private session s; /** this is just used for debugging */ Debug debug; /** method description: this constructor will create a session execution object from the specified directory, the directory contains all the configuration information about this object, this object is created by the specified session object. uses the services of class(es): input parameter(s): file_name -- this directory/file contains all the configuration information about session executation object s -- this is the session object which created this object debug -- used for debugging output parameter(s): none return value: none synchronization: none */ public session_execution(String file_name, session s, Debug debug) { BufferedReader in_file = null; String line = null; StringTokenizer token_line = null; String stoken = null; String stoken1 = null; init(); this.debug = debug; this.s = s; if (s == null) { debug.dumpTrace("Class:session_execution Method:constructor Session is null."); } if ((file_name == null) || file_name.equals("")) { debug.dumpTrace("Class:session_execution 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) //in case an empty line is encountered { stoken = token_line.nextToken(); //ignore empty lines and lines with only one token if (!stoken.startsWith("#")) //Ignore comments(begin with "#") { if (stoken.equals("client_priority")) { stoken1 = token_line.nextToken(); try { client_priority = Integer.parseInt(stoken1); if (client_priority <= 0) { client_priority = 1; debug.dumpTrace("Class:session_execution Method:constructor client_priority must be greater than 0."); } } catch (Exception e) { client_priority = 1; debug.dumpTrace("Class:session_execution Method:constructor client_priority must be an integer."); } } if (stoken.equals("c_priority")) { stoken1 = token_line.nextToken(); try { c_priority = Integer.parseInt(stoken1); if (c_priority <= 0) { c_priority = 1; debug.dumpTrace("Class:session_execution Method:constructor c_priority must be greater than 0."); } } catch (Exception e) { c_priority = 1; debug.dumpTrace("Class:session_execution Method:constructor c_priority must be an integer."); } } if (stoken.equals("protocol")) { protocol = token_line.nextToken(); if (!protocol.equals("1.0")) { protocol = new String("1.0"); debug.dumpTrace("Class:session_execution Method:constructor protocol must be 1.0."); } } } // end if (!stoken.startsWith("#")) } // end if (token_line.countTokens() > 1) line = in_file.readLine(); } // end while in_file.close(); } catch (IOException e) { debug.dumpTrace("Class:session_execution Method:constructor File open error or get parameters(client_priority,c_priority,protocol) error."); return; } } /** method description: this constructor will create a session execution object, this object is created by the specified session object. uses the services of class(es): input parameter(s): s -- this is the session object which created this object debug -- used for debugging output parameter(s): none return value: none synchronization: none */ public session_execution(session s, Debug debug) { init(); this.s = s; this.debug = debug; if ( s == null) { debug.dumpTrace("Class:session_execution Method:constructor Session is null."); } return; } /** method description: this method is the main part of the thread, it's responsible for inform corresponding session object to execute functions sent from client and C/C++ marian server uses the services of class(es): session input parameter(s): none output parameter(s): none return value: none synchronization: none */ public void run() { int i; boolean executed; for (;;) { do { executed = false; i = client_priority; do { if (s.execute_next_call_from_client() == s.OK) { executed = true; i--; } else { break; } } while (i != 0); i = c_priority; do { if (s.execute_next_call_from_c() == s.OK) { executed = true; i--; } else { break; } } while ( i != 0 ); } while (executed); // probably sleep some time here can increase the speed of the // whole system since most of the time the function queueswill // be empty try { suspend(); } catch (Exception e) { debug.dumpTrace("class session_execution, method run, error happened when suspend"); } } } /** method description: this method will return the priority of the functions sent by client. uses the services of class(es): none input parameter(s): none output parameter(s): none return value: the priority of functions sent by client as an integer synchronization: none */ public int get_client_priority() { return client_priority; } /** method description: this method will return the priority of functions sent by C/C++ marian server uses the services of class(es): input parameter(s): none output parameter(s): none return value: the priority of functions sent by C/C++ server as an integer synchronization: none */ public int get_c_priority() { return c_priority; } /** method description: this method will set the priority of the functions sent by client. uses the services of class(es): none input parameter(s): priority -- this will become the new priority of functions sent by client output parameter(s): none return value: OK -- the value has been set correctly INVALID_PRIORITY -- negative value which doesn't make sense has been identified other -- synchronization: none */ public int set_client_priority(int priority) { if (priority > 0) { client_priority = priority; return OK; } debug.dumpTrace("Class:session_execution Method:set_client_priority Priority less than 0."); return INVALID_PRIORITY; } /** method description: this method will set the priority of the functions sent by C/C++ server. uses the services of class(es): none input parameter(s): priority -- this will become the new priority of functions sent by C/C++ server output parameter(s): none return value: OK -- the value has been set correctly INVALID_PRIORITY -- negative value which doesn't make sense has been identified other -- synchronization: none */ public int set_c_priority(int priority) { if (priority > 0) { c_priority = priority; return OK; } debug.dumpTrace("Class:session_execution Method:set_c_priority Priority less than 0."); return INVALID_PRIORITY; } /** method description: this method will tell the protocol this thread currently used to inform corresponding session object to execute functions from client and C/C++ uses the services of class(es): none input parameter(s): none output parameter(s): none return value: the protocol currently used by this object to execute functions as a string synchronization: none */ public String get_execution_protocol() { return protocol; } /** method description: this method will set the protocol this thread will use to inform corresponding session object to execute functions sent by client and C/C++ marian server. 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 PROTOCOL_GREATER_THAN_1 -- the protocol is not supported NULL_PROTOCOL -- null protocol identified synchronization: none */ public int set_execution_protocol(String protocol) { if (protocol == null) { debug.dumpTrace("Class:session_execution Method:set_execution_protocol Protocol is null."); return NULL_PROTOCOL; } if (!protocol.equals("1.0")) { debug.dumpTrace("Class:session_execution Method:set_execution_protocol Protocol is greater than 1.0."); return PROTOCOL_GREATER_THAN_1; } this.protocol = protocol; return OK; } /** 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() { c_priority = 1; client_priority = 1; protocol = new String("1.0"); s = null; debug = null; return; } }