home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 2.6 KB | 78 lines |
- // Thread that periodically sends filter messages
-
- // Written by Bernie Roehl, December 1996
-
- package multi;
-
- import java.net.*;
- import java.io.*;
-
- class FilterSender extends Thread {
- World world;
- InetAddress address;
- int port;
-
- public FilterSender(World wrld, InetAddress addr, int prt) {
- world = wrld;
- address = addr;
- port = prt;
- start();
- }
-
- public synchronized void run() {
- DatagramSocket out_socket;
- try { out_socket = new DatagramSocket(); }
- catch (IOException e) {
- System.out.println("Could not create datagram socket");
- return;
- }
- ByteArrayOutputStream bout = new ByteArrayOutputStream();
- DataOutputStream out = new DataOutputStream(bout);
- while (true) {
- bout.reset();
- try {
- short flags = 0;
- if (world.suspended)
- flags |= 0x0001;
- if (world.request_refresh) {
- flags |= 0x0002;
- world.request_refresh = false;
- }
- if (world.gone)
- flags |= 0x0004;
- out.writeShort(world.incoming_socket.getLocalPort());
- out.writeShort(flags);
- out.writeFloat(world.location.getX());
- out.writeFloat(world.location.getY());
- out.writeFloat(world.location.getZ());
- out.writeFloat(world.acuity);
- out.writeInt(world.horizon);
- out.writeShort(0); // no regions
- if (world.text_port == 0)
- out.writeShort(0); // no additional ports
- else {
- out.writeShort(1); // one additional port
- out.writeShort(world.text_port); // the text port
- }
- out.writeShort(0); // padding byte (required because of apparent bug in java.net)
- }
- catch (IOException e) {
- System.out.println("Error while building filter message: " + e);
- return;
- }
- DatagramPacket packet =
- new DatagramPacket(bout.toByteArray(), bout.size(),
- address, port);
- try { out_socket.send(packet); }
- catch (IOException e) {
- System.out.println("Could not send packet: " + e);
- return;
- }
- try { wait(5000); }
- catch (InterruptedException e) { }
- }
- }
-
- }
-
-