package edu.vt.marian.uip; import java.io.*; import java.net.*; import java.util.*; import edu.vt.marian.common.*; /** Class name: uip_server Class description: this class contains information about a uip server Author: Jianxin Zhao Finished time: ????, 1998 Known bugs: none Platform: jdk1.1.5 under UNIX */ public class uip_server { /** this is just for debugging */ Debug debug; /** this is the name of the host on which the server is running */ private String hostname; /** this is the port number the server is listening to */ private int port; /** this is the short description of the server */ private String short_des; /** this is the long description of the server */ private String long_des; /** this constructor will create an uip_server object based on the hostname, port short description and lng description provided, note long description maybe a link to the server site */ public uip_server(String hostname, int port, String short_des, String long_des, Debug debug) { // first initialize data members init(); this.debug = debug; set_hostname(hostname); set_port(port); set_short_description(short_des); set_long_description(long_des); } /** this constructor will create a uip server object from the specified file */ public uip_server(String filename, Debug debug) { // first initialize data members init(); this.debug = debug; File uip_server_config = new File(filename); BufferedReader br; try { FileReader fr = new FileReader(uip_server_config); br = new BufferedReader(fr); // process hostname and port number, it's unlikely that one of them // contains more than one lines if (br.readLine().equals("1")) { // hostname not empty set_hostname(br.readLine()); } set_port(Integer.parseInt(br.readLine())); // process server short description, multiple lines allowed int number_lines = Integer.parseInt(br.readLine()); int i; String s = new String(""); for (i = 0; i < number_lines; i++) { if (i == 0) { // this is the first line s += br.readLine(); } else { // this is not the first line, add "\n" between them s += "\n" + br.readLine(); } } if (i != 0) { set_short_description(s); } // process server long description, multiple lines allowed number_lines = Integer.parseInt(br.readLine()); s = new String(""); for (i = 0; i < number_lines; i++) { if (i == 0) { // this is the first line s += br.readLine(); } else { // this is not the first line, add "\n" between them s += "\n" + br.readLine(); } } if (i != 0) { set_long_description(s); } br.close(); } catch (IOException e3) { debug.dumpTrace("error reading data from " + uip_server_config. getAbsolutePath()); } } /** this constructor will create an uip_server object from a stream, this might be helpful for remote management in the future */ public uip_server(BufferedReader br, Debug debug) { // first initialize data members init(); this.debug = debug; // process hostname and port number, it's unlikely that one of them // contains more than one lines try { if (br.readLine().equals("1")) { set_hostname(br.readLine()); } set_port(Integer.parseInt(br.readLine())); // process server short description, multiple lines allowed int number_lines = Integer.parseInt(br.readLine()); int i; String s = new String(""); for (i = 0; i < number_lines; i++) { if (i == 0) { // this is the first line s += br.readLine(); } else { // this is not the first line, add "\n" between them s += "\n" + br.readLine(); } } set_short_description(s); // process server long description, multiple lines allowed number_lines = Integer.parseInt(br.readLine()); s = new String(""); for (i = 0; i < number_lines; i++) { if (i == 0) { // this is the first line s += br.readLine(); } else { // this is not the first line, add "\n" between them s += "\n" + br.readLine(); } } set_long_description(s); } catch (IOException e) { debug.dumpTrace("error reading uip server from stream"); } } /** this method will set the hostname of this uip server */ private String set_hostname(String name) { if (name == null) { hostname = null; return "ok"; } hostname = new String(name); return "ok"; } /** this method will return the hostmname of this uip server */ public String get_hostname() { if (hostname == null) { return null; } return new String(hostname); } /** this method will set the port for the uip server */ private String set_port(int port) { this.port = port; return "ok"; } /** this method will return the port number of this uip server */ public int get_port() { return port; } /** this method will set the ip address for the uip server (not implemented yet) */ public String set_ip_address(String ip_address) { return null; // not implemented yet } /** this method will return the ip address of this uip server (not implemented yet) */ public String get_ip_address() { return null; // not implemented yet } /** this method will set the short description for the uip server */ public String set_short_description(String short_des) { if (short_des != null) { this.short_des = new String(short_des); return "ok"; } this.short_des = null; return "ok"; } /** this method will return the short description of this uip server */ public String get_short_description() { if (short_des != null) { return new String(short_des); } return null; } /** this method will set the long description (may be a link to serve site) for the uip server */ public String set_long_description(String long_des) { if (long_des != null) { this.long_des = new String(long_des); return "ok"; } this.long_des = null; return "ok"; } /** this method will return the long description of this uip server note that long description maybe a link to the server site since it's most likely that the full description about the serve can be found there */ public String get_long_description() { if (long_des != null) { return new String(long_des); } return null; } /** this method will tell whether or not the specified uip server and this one are pointing to the same server */ public String equals(uip_server us) { // hostname/port pair is uniq for a server if (get_hostname().equals(us.get_hostname()) && get_port() == us.get_port()) { return "yes"; } // not match return "no"; } /** this method will print the content of this object to a stream, this might be helpful for remote management in the future */ public String to_stream(PrintWriter pw) { // write hostname and port number, it's unlikely that one of them // will contain more than one lines if (get_hostname() == null) { pw.println("0"); } else { pw.println("1"); pw.println(get_hostname()); } pw.println(get_port()); // used for count lines LinedString ms = new LinedString(debug); // write server short description, multiple lines allowed String s = get_short_description(); if (s == null) { pw.println("0"); } else { pw.println(ms.count_lines(s)); pw.println(s); } // write server long description, multiple lines allowed s = get_long_description(); if (s == null) { pw.println("0"); } else { pw.println(ms.count_lines(s)); pw.println(s); } pw.flush(); return "ok"; } /** this method will save the content of this object to the specified file */ public String save(String filename) { FileOutputStream fos = null; try { fos = new FileOutputStream(filename, false); } catch (IOException e3) { debug.dumpTrace("error opening uip server config file to write"); } PrintWriter pw = new PrintWriter(fos); // write hostname and port number, it's unlikely that one of them // will contain more than one lines if (get_hostname() == null) { pw.println("0"); } else { pw.println("1"); pw.println(get_hostname()); } pw.println(get_port()); // used for count lines LinedString ms = new LinedString(debug); // write server short description, multiple lines allowed String s = get_short_description(); if (s == null) { pw.println("0"); } else { pw.println(ms.count_lines(s)); pw.println(s); } // write server long description, multiple lines allowed s = get_long_description(); if (s == null) { pw.println("0"); } else { pw.println(ms.count_lines(s)); pw.println(s); } pw.flush(); pw.close(); return "OK"; } /** this method will initialize the data members of the class */ private void init() { debug = null; hostname = null; port = 0; short_des = null; long_des = null; } }