home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / PR8ADPL7.TAR / productivity_tools / PR8ADPL7 / ServerDevice.java < prev    next >
Encoding:
Java Source  |  1996-05-23  |  2.6 KB  |  90 lines

  1. // ServerDevice.java
  2. // A device driver for getting information about and controlling the server.
  3. import java.util.Vector;
  4. import java.io.IOException;
  5.  
  6. public class ServerDevice extends DeviceDriver
  7. {
  8.     // read
  9.     // Reading from the Server device returns server information, based
  10.     // on the Command header. Valid commands are:
  11.     //  'Netstat'        // get active connections
  12.     //  'Properties'    // get all server properties
  13.     Message read(Message msg, ServerClient s) throws IOException
  14.     {
  15.     String command = msg.find("Command");
  16.     if (command == null)
  17.         throw new IOException("No Command given");
  18.  
  19.     Message r = new Message();
  20.     r.add("Reply","Data");
  21.     if (command.equals("Netstat")) {
  22.         // Send back a list of active connections
  23.         BufferOutputStream buf = new BufferOutputStream();
  24.         Vector cons = JFSserver.cons;
  25.         synchronized(cons) {
  26.             for(int i=0; i<cons.size(); i++) {
  27.                 ServerClient c=(ServerClient)cons.elementAt(i);
  28.                 StringJoiner j = new StringJoiner(':');
  29.                 j.add(String.valueOf(c.cnum));
  30.                 j.add(c.host);
  31.                 j.add(c.name==null ? "" : c.name);
  32.                 j.add(String.valueOf(c.last));
  33.                 j.add(String.valueOf(c.sent));
  34.                 j.add(String.valueOf(c.recv));
  35.                 buf.puts(j.toString());
  36.                 }
  37.             }
  38.         r.setdata(buf.getarray());
  39.         }
  40.     else if (command.equals("Properties")) {
  41.         // Send a list of all server properties
  42.         ServerProperty allp[] = JFSserver.allprops();
  43.         BufferOutputStream buf = new BufferOutputStream();
  44.         for(int i=0; i<allp.length; i++)
  45.             buf.puts(allp[i].output());
  46.         r.setdata(buf.getarray());
  47.         }
  48.     else
  49.         throw new IOException("Unknown command : "+command);
  50.     return r;
  51.     }
  52.  
  53.     // write
  54.     // Writing to the Server device performs some action based on the
  55.     // Command header, and other headers specific to certain commands.
  56.     // Valid commands are:
  57.     //  'Kill'    // Disconnect a client. Required the Id header
  58.     void write(Message msg, ServerClient s) throws IOException
  59.     {
  60.     String cmd = msg.find("Command");
  61.     if (cmd == null)
  62.         throw new IOException("No Command given");
  63.  
  64.     // Carry out command
  65.     if (cmd.equals("Kill")) {
  66.         // Kill an existing connection
  67.         String idstr = msg.find("Id");
  68.         if (idstr == null)
  69.             throw new IOException("No Id given");
  70.         int id = Integer.parseInt(idstr);
  71.         ServerClient idc = null;
  72.         for(int i=0; i<JFSserver.cons.size(); i++) {
  73.             ServerClient c =
  74.                 (ServerClient)JFSserver.cons.elementAt(i);
  75.             if (c.cnum == id) idc = c;
  76.             }
  77.         if (idc == null)
  78.             throw new IOException("Connection Id not found");
  79.         if (idc == s)
  80.             throw new IOException("Cannot kill own connection");
  81.         idc.shutdown();
  82.         }
  83.     else {
  84.         // Unknown command
  85.         throw new IOException("Command "+cmd+" not found");
  86.         }
  87.     }
  88. }
  89.  
  90.