home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch18 / Server.java < prev    next >
Text File  |  1997-02-21  |  14KB  |  368 lines

  1. // Simple server, just for testing the client
  2.  
  3. // Written by Bernie Roehl, January 1997
  4.  
  5. import java.net.*;
  6. import java.io.*;
  7. import java.util.*;
  8.  
  9. public class Server {
  10.     public static void main(String[] args) throws SocketException, IOException {
  11.         int filter_port, update_port, text_port;
  12.  
  13.         if (args.length > 0)
  14.             filter_port = Integer.parseInt(args[0]);
  15.         else
  16.             filter_port = 3000;
  17.         System.out.println("filter port is " + filter_port);
  18.  
  19.         if (args.length > 1)
  20.             update_port = Integer.parseInt(args[1]);
  21.         else
  22.             update_port = filter_port + 2;
  23.         System.out.println("update port is " + update_port);
  24.  
  25.         if (args.length > 2)
  26.             text_port = Integer.parseInt(args[2]);
  27.         else
  28.             text_port = filter_port + 4;
  29.         System.out.println("text port is " + text_port);
  30.  
  31.         Vector connections = new Vector();  // vector of HostInfo
  32.         new ListenThread(filter_port);
  33.         new ControlThread(filter_port, connections);
  34.         new EchoThread(update_port, connections);
  35.         new TextEchoThread(text_port, connections);
  36.     }
  37. }
  38.  
  39. class HostInfo {
  40.     InetAddress address;
  41.     int update_port;
  42.     int text_port;
  43. }
  44.  
  45. class ControlThread extends Thread {
  46.     DatagramSocket socket;
  47.     Vector connections;
  48.     public ControlThread(int controlPort, Vector conn)
  49.         throws SocketException {
  50.         connections = conn;
  51.         socket = new DatagramSocket(controlPort);
  52.         start();
  53.     }
  54.     public void run() {
  55.         byte[] buffer = new byte[1024];
  56.         DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  57.         ByteArrayInputStream bin = new ByteArrayInputStream(buffer);
  58.         DataInputStream sin = new DataInputStream(bin);
  59.         while (true) {
  60.             try { socket.receive(packet); }
  61.             catch (IOException e) {
  62.                 System.out.println("could not read from control socket: " + e);
  63.                 break;
  64.             }
  65.             bin.reset();
  66.             int update_port = 0, text_port = 0;
  67.             try {
  68.                 update_port = sin.readUnsignedShort();
  69.                 sin.readUnsignedShort();  // flags
  70.                 sin.readFloat();  sin.readFloat();  sin.readFloat();  // XYZ
  71.                 sin.readFloat();  // acuity
  72.                 sin.readInt();    // horizon
  73.                 int nregions = sin.readUnsignedShort();
  74.                 while (nregions-- > 0) sin.readUnsignedShort();
  75.                 int nports = sin.readUnsignedShort();
  76.                 if (nports > 0) {
  77.                     text_port = sin.readUnsignedShort();
  78.                 }
  79.             }
  80.             catch (IOException e) {
  81.                 System.out.println("could not read filter message: " + e);
  82.                 break;
  83.             }
  84.             add(connections, packet.getAddress(), update_port, text_port);
  85.         }
  86.     }
  87.  
  88.     private void add(Vector connections, InetAddress addr, int port, int text_port) {
  89.         synchronized(connections) { 
  90.             Enumeration enum = connections.elements();
  91.             while (enum.hasMoreElements()) {
  92.                 HostInfo h = (HostInfo) enum.nextElement();
  93.                 if (h.address.equals(addr) && h.update_port == port) {
  94.                     h.text_port = text_port;
  95.                     return;  // already there
  96.                 }
  97.             }
  98.             HostInfo h = new HostInfo();
  99.             h.address = addr; 
  100.             h.update_port = port;
  101.             h.text_port = text_port;
  102.             connections.addElement(h);
  103.         }
  104.     }
  105.  
  106. }
  107.  
  108. class EchoThread extends Thread { 
  109.     DatagramSocket insocket, outsocket;
  110.     Vector connections;
  111.     public EchoThread(int dataport, Vector conn) throws SocketException {
  112.         insocket = new DatagramSocket(dataport);
  113.         outsocket = new DatagramSocket();
  114.         connections = conn;
  115.         start();
  116.     }
  117.     public void run() {
  118.         byte[] buffer = new byte[1024];
  119.         DatagramPacket inpacket = new DatagramPacket(buffer, buffer.length);
  120.         int rcount = 0, scount = 0;
  121.         while (true) {
  122.             try { insocket.receive(inpacket); }
  123.             catch (IOException e) {
  124.                 System.out.println("could not read from data socket: " + e);
  125.                 break;
  126.             }
  127.             DatagramPacket outpacket;
  128.             synchronized(connections) {
  129.                 Enumeration enum = connections.elements();
  130.                 while (enum.hasMoreElements()) {
  131.                     HostInfo h = (HostInfo) enum.nextElement();
  132.                     outpacket = new DatagramPacket(
  133.                         inpacket.getData(), inpacket.getLength(),
  134.                         h.address, h.update_port);
  135.                     try { outsocket.send(outpacket); }
  136.                     catch (IOException e) {
  137.                         System.out.println("could not send packet: " + e);
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.     }
  143. }
  144.  
  145. class TextEchoThread extends Thread { 
  146.     DatagramSocket insocket, outsocket;
  147.     Vector connections;
  148.     public TextEchoThread(int dataport, Vector conn) throws SocketException {
  149.         insocket = new DatagramSocket(dataport);
  150.         outsocket = new DatagramSocket();
  151.         connections = conn;
  152.         start();
  153.     }
  154.     public void run() {
  155.         byte[] buffer = new byte[1024];
  156.         DatagramPacket inpacket = new DatagramPacket(buffer, buffer.length);
  157.         int rcount = 0, scount = 0;
  158.         while (true) {
  159.             try { insocket.receive(inpacket); }
  160.             catch (IOException e) {
  161.                 System.out.println("could not read from text socket: " + e);
  162.                 break;
  163.             }
  164. System.out.println("Server received text packet of size " + inpacket.getLength());
  165.             DatagramPacket outpacket;
  166.             synchronized(connections) {
  167.                 Enumeration enum = connections.elements();
  168.                 while (enum.hasMoreElements()) {
  169.                     HostInfo h = (HostInfo) enum.nextElement();
  170.                     outpacket = new DatagramPacket(
  171.                         inpacket.getData(), inpacket.getLength(),
  172.                         h.address, h.text_port);
  173.                     try {
  174.                         outsocket.send(outpacket);
  175.                     }
  176.                     catch (IOException e) {
  177.                         System.out.println("could not send packet: " + e);
  178.                     }
  179. System.out.println("Server sent text packet of size " + outpacket.getLength());
  180.                 }
  181.             }
  182.         }
  183.     }
  184. }
  185.  
  186. class ListenThread extends Thread {
  187.     ServerSocket listen_socket;
  188.     Vector entities;
  189.     public ListenThread(int port) throws IOException {
  190.         listen_socket = new ServerSocket(port);
  191.         entities = new Vector();
  192.         start(); 
  193.     }
  194.     public void run() {
  195.         try {
  196.             while (true) {
  197.                 Socket client_socket = listen_socket.accept();
  198.                 Connection c = new Connection(client_socket, entities);
  199.             }
  200.         }
  201.         catch (IOException e) {
  202.             System.out.println("Error while listening: " + e);
  203.             return;
  204.         }
  205.     }
  206.  
  207. }
  208.  
  209. class Entity {
  210.     int id;
  211.     String cname = null;
  212.     String nickname = null;
  213.     String url = null;
  214.     String owner = null;
  215.     boolean isAvatar = false;
  216.     boolean isPersistent = false;
  217.     int textid;
  218.     long creationTime;
  219.     public Entity(String cn, int i) {
  220.         cname = new String(cn);
  221.         id = i;
  222.         creationTime = System.currentTimeMillis();
  223.     }
  224. }
  225.  
  226. class Connection extends Thread {
  227.  
  228.     protected Socket client;
  229.     protected DataInputStream in;
  230.     protected PrintStream out;
  231.     protected String ident;
  232.     protected Vector entities;
  233.  
  234.     public Connection(Socket client_socket, Vector ents) {
  235.         entities = ents; 
  236.         client = client_socket;
  237.         try {
  238.             in = new DataInputStream(client.getInputStream());
  239.             out = new PrintStream(client.getOutputStream());
  240.         }
  241.         catch (IOException e) {
  242.             try client.close(); catch (IOException e2);
  243.             System.out.println("error getting socket streams: " + e);
  244.             return;
  245.         }
  246.         start();
  247.     }
  248.  
  249.     public void run() {
  250.         try {
  251.             while (true) {
  252.                 String inputLine = in.readLine(); 
  253.                 if (inputLine == null) {
  254.                     break;
  255.                 }
  256.                 StringTokenizer tokens = new StringTokenizer(inputLine);
  257.                 if (tokens.hasMoreTokens() == false)  // empty line
  258.                     continue;
  259.                 String command = tokens.nextToken();
  260.                 if (command.equals("HELLO")) {
  261.                     out.println("WELCOME 0.1 Stub Server");
  262.                 }
  263.                 else if (command.equals("GOODBYE"))
  264.                     break;
  265.                 else if (command.equals("IDENT")) {
  266.                     ident = new String(tokens.nextToken());
  267.                     out.println("OKAY");
  268.                 }
  269.                 else if (command.equals("LIST") || command.equals("LISTNEW")) {
  270.                     synchronized (entities) {
  271.                         Enumeration enum = entities.elements();
  272.                         while (enum.hasMoreElements()) {
  273.                             Entity ent = (Entity) enum.nextElement();
  274.                             out.println(ent.id);
  275.                         }
  276.                     }
  277.                     out.println(".");
  278.                 }
  279.                 else if (command.equals("GETENTITY")) {
  280.                     synchronized (entities) {
  281.                         Entity ent = find(Integer.parseInt(tokens.nextToken()));
  282.                         if (ent == null)
  283.                             out.println("REFUSED no such entity");
  284.                         else {
  285.                             out.println(ent.cname + " " + ent.url + " "
  286.                                 + ent.owner + " "
  287.                                 + ent.isAvatar + " " + ent.isPersistent + " "
  288.                                 + ent.creationTime);
  289.                             out.println(ent.nickname);
  290.                             out.println(ent.textid);
  291.                             out.println(".");
  292.                         }
  293.                     }
  294.                 }
  295.                 else if (command.equals("GETINFO")) {
  296.                     int entid = Integer.parseInt(tokens.nextToken());
  297.                     out.println("Some User");
  298.                     out.println("<not implemented>");
  299.                     out.println(".");
  300.                 }
  301.                 else if (command.equals("ALLOC")) {
  302.                     if (ident == null)
  303.                         out.println("REFUSED no ident");
  304.                     else {
  305.                         synchronized(entities) {
  306.                             int allocid = entities.size();
  307.                             out.println(allocid);
  308.                             Entity ent = new Entity(tokens.nextToken(), allocid);
  309.                             ent.owner = new String(ident);
  310.                             entities.addElement(ent);
  311.                         }
  312.                     }
  313.                 }
  314.                 else if (command.equals("RELEASE")) {
  315.                     out.println("OKAY");
  316.                 } 
  317.                 else if (command.equals("INFO")) {
  318.                     out.println("OKAY");
  319.                     while (in.readLine().equals(".") == false);
  320.                 } 
  321.                 else if (command.equals("URL")) {
  322.                     Entity ent = find(Integer.parseInt(tokens.nextToken()));
  323.                     ent.url = new String(tokens.nextToken());
  324.                     out.println("OKAY");
  325.                 } 
  326.                 else if (command.equals("NICKNAME")) {
  327.                     Entity ent = find(Integer.parseInt(tokens.nextToken()));
  328.                     ent.nickname = tokens.nextToken();
  329.                     while (tokens.hasMoreTokens())
  330.                         ent.nickname += " " + tokens.nextToken();
  331.                     out.println("OKAY");
  332.                 } 
  333.                 else if (command.equals("AVATAR")) {
  334.                     Entity ent = find(Integer.parseInt(tokens.nextToken()));
  335.                     ent.isAvatar = tokens.nextElement().equals("true");
  336.                     out.println("OKAY");
  337.                 } 
  338.                 else if (command.equals("PERSISTENT")) {
  339.                     Entity ent = find(Integer.parseInt(tokens.nextToken()));
  340.                     ent.isPersistent = tokens.nextElement().equals("true");
  341.                     out.println("OKAY");
  342.                 } 
  343.                 else if (command.equals("TEXTID")) {
  344.                     Entity ent = find(Integer.parseInt(tokens.nextToken()));
  345.                     ent.textid = Integer.parseInt(tokens.nextToken());
  346.                     out.println("OKAY");
  347.                 } 
  348.                 else  // catch-all
  349.                     out.println("REFUSED no such command");
  350.             }
  351.             out.flush();
  352.         }
  353.         catch (IOException e);
  354.         finally try client.close(); catch (IOException e2);
  355.     }
  356.  
  357.     private Entity find(int id) {
  358.         Enumeration enum = entities.elements();
  359.         while (enum.hasMoreElements()) {
  360.             Entity ent = (Entity) enum.nextElement();
  361.             if (ent.id == id)
  362.                 return ent;
  363.         }
  364.         return null;
  365.     }
  366. }
  367.  
  368.