home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch18 / multi / FilterSender.java < prev    next >
Encoding:
Java Source  |  1997-02-21  |  2.6 KB  |  78 lines

  1. // Thread that periodically sends filter messages
  2.  
  3. // Written by Bernie Roehl, December 1996
  4.  
  5. package multi;
  6.  
  7. import java.net.*;
  8. import java.io.*;
  9.  
  10. class FilterSender extends Thread {
  11.     World world;
  12.     InetAddress address;
  13.     int port;
  14.  
  15.     public FilterSender(World wrld, InetAddress addr, int prt) {
  16.         world = wrld;
  17.         address = addr;
  18.         port = prt;
  19.         start();
  20.     }
  21.  
  22.     public synchronized void run() {
  23.         DatagramSocket out_socket;
  24.         try { out_socket = new DatagramSocket(); }
  25.         catch (IOException e) {
  26.             System.out.println("Could not create datagram socket");
  27.             return;
  28.         }
  29.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  30.         DataOutputStream out = new DataOutputStream(bout);
  31.         while (true) {
  32.             bout.reset();
  33.             try {
  34.                 short flags = 0;
  35.                 if (world.suspended)
  36.                     flags |= 0x0001;
  37.                 if (world.request_refresh) {
  38.                     flags |= 0x0002;
  39.                     world.request_refresh = false;
  40.                 }
  41.                 if (world.gone)
  42.                     flags |= 0x0004;
  43.                 out.writeShort(world.incoming_socket.getLocalPort());
  44.                 out.writeShort(flags);
  45.                 out.writeFloat(world.location.getX());
  46.                 out.writeFloat(world.location.getY());
  47.                 out.writeFloat(world.location.getZ());
  48.                 out.writeFloat(world.acuity);
  49.                 out.writeInt(world.horizon);
  50.                 out.writeShort(0);       // no regions
  51.                 if (world.text_port == 0)
  52.                     out.writeShort(0);   // no additional ports
  53.                 else {
  54.                     out.writeShort(1);       // one additional port
  55.                     out.writeShort(world.text_port);  // the text port
  56.                 }
  57.                 out.writeShort(0);   // padding byte (required because of apparent bug in java.net)
  58.             }
  59.             catch (IOException e) {
  60.                 System.out.println("Error while building filter message: " + e);
  61.                 return;
  62.             }
  63.             DatagramPacket packet =
  64.                  new DatagramPacket(bout.toByteArray(), bout.size(),
  65.                                     address, port);
  66.             try { out_socket.send(packet); }
  67.             catch (IOException e) {
  68.                 System.out.println("Could not send packet: " + e);
  69.                 return;
  70.             }
  71.             try { wait(5000); }
  72.             catch (InterruptedException e) { }
  73.         }
  74.     }
  75.  
  76. }
  77.  
  78.