package edu.vt.marian.uip; import java.io.*; import java.net.*; import java.util.*; import edu.vt.marian.common.*; /** class name: server_uip_thread class description: this class is responsible to pass rpc_functions to and receive rpc_functions from a number of client_uips, it use the service of server_uip_receiver_table to implement these functionsm it's also responsible for informing the server_uip_receiver_table to add a new receiver when a client connection is detected
uses the services of class(es): designer(s): Jianxin Zhao (jxzhao@csgrad.cs.vt.edu) implementator(s): finished time: known bugs: JDK version: 1.1.5 side effects: */ public class server_uip_thread extends Thread { /** just used for debugging */ Debug debug; /** this is the onject which created this object */ private server_uip su; /** this object is used here to manage a batch of server uip receivers */ private server_uip_receiver_table surt; /** this socket is used to get new connection from client uips */ private ServerSocket listen_socket; /** this is the version of this server uip part */ private int version; /** this number specifies at most how many client uips are supported at the same time */ private int max_client_number; /** this variable specifies the current status of this this object */ private int status; /** this is the default max client number */ private final static int DEFAULT_MAX_CLIENT_NUMBER = 100; /** here are the possible status of this object */ public final static int OK = 0; public final static int EXIT = 1; public final static int CONNECTION_ERROR = 2; /** possible method return values */ public final static int PASSING_FUNCTION_FAILED = 1; public final static int PROCESS_CALL_BACK_FAILED = 2; /** method description: this constructor will create a client uip object based on the information contained in the file/directory, it assume the parameter call back processor can process functions it received from server. uses the services of class(es): input parameter(s): file_name --- the name of the file/directory which contain the configuration information cbp --- this parameter is supposed to be able to process the functions received from server using the method process_call_back(client_uip cu, rpc_function, rf) debug -- used for debugging output parameter(s): none return value: none */ public server_uip_thread(server_uip su, int version, int port_number, int max_client_number, Debug debug) { // initialize data members init(); this.debug = debug; if (su == null) { debug.dumpTrace("server_uip_thread.[constructor]: parameter server_uip is null."); return; } this.su = su; this.version = version; this.max_client_number = max_client_number; surt = new server_uip_receiver_table(this, max_client_number, debug); try { listen_socket = new ServerSocket(port_number); } catch (Exception e) { debug.dumpTrace("server_uip_thread.[constructor]: can't create server socket " + port_number + ": " + e.toString()); status = CONNECTION_ERROR; return; } // the socket is successfully created status = OK; this.start(); return; } /** method description: this method will let the object exit smoothly, release all the resources it occupied and kill all the threads it created. uses the services of class(es): client_uip_reciever input parameter(s): condition --- under what condition is this method called, not used currently output parameter(s): none return value: 0 -- every thing is fine other -- */ public void run() { Socket s = null; while (true) { try { s = listen_socket.accept(); } catch (Exception e) { debug.dumpTrace("class server_uip_thread, method run, error in accepting client connections"); status = CONNECTION_ERROR; return; } // a client uip connection has been detected, inform the // server uip receiver table to add a receiver surt.add_receiver(s, version); } } /** method description: this method will return the current status of this object uses the services of class(es): none input parameter(s): none output parameter(s): none return value: 0 -- everything is fine and the server has been connected corrected other -- this value specify the reasons why can not connect to server. */ public int get_status() { return status; } /** method description: this method will send the function to the server this object corresponds to uses the services of class(es): rpc_function input parameter(s): function --- the function which need to be sent to the server output parameter(s): none return value: 0 -- the function has been passed to the server correctly other -- */ public int rpc_call(int client_id, rpc_function function) { if (surt.rpc_call(client_id, function) == surt.OK) { return OK; } return PASSING_FUNCTION_FAILED; } /** method description: this method will send the function to the server this object corresponds to uses the services of class(es): rpc_function input parameter(s): function --- the function which need to be sent to the server output parameter(s): none return value: 0 -- the function has been passed to the server correctly other -- */ public int exit(String condition) { this.stop(); status = EXIT; try { listen_socket.close(); } catch (Exception e) {} // exit the server uip receiver table surt.exit(condition); return OK; } /** method description: this method will send the function to the server this object corresponds to uses the services of class(es): rpc_function input parameter(s): function --- the function which need to be sent to the server output parameter(s): none return value: 0 -- the function has been passed to the server correctly other -- */ public int process_call_back_from_server_uip_receiver_table( int client_id, String client_desc, rpc_function function) { //debug.dumpTrace("class server_uip_thread, method process_call_back_from_server_uip_receiver_table, at the beginning"); if (su.process_call_back_from_server_uip_thread(client_id, client_desc, function) == su.OK) { return OK; } return PROCESS_CALL_BACK_FAILED; } /** method description: this method will process the log data sent from the server_uip_receiver_table
uses the services of class(es): input parameter(s): message -- the message sent from the server_uip_receiver_table output parameter(s): none return value: 0 -- the message has been processed successfully other -- synchronization consideration: none */ public int log_data_from_server_uip_receiver_table(String message) { su.log_data_from_server_uip_thread(message); return OK; } /** method description: this method will initialize the data members of this object
uses the services of class(es): input parameter(s): none output parameter(s): none return value: none synchronization consideration: none */ private void init() { debug = null; su = null; surt = null; listen_socket = null; version = 1; max_client_number = DEFAULT_MAX_CLIENT_NUMBER; } }