home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 6.8 KB | 190 lines |
- // A world, as it appears at the client API end
-
- // Written by Bernie Roehl, December 1996
-
- package multi;
-
- import java.util.*;
- import java.net.*;
- import java.io.*;
-
- public class World {
- Socket registry_socket; // tcp socket of registry
- DataInputStream registry_input; // input stream for that socket
- PrintStream registry_output; // output stream for that socket
- String banner; // received from registry on WELCOME
-
- DatagramSocket incoming_socket; // used for receiving updates
- int packets_received = 0; // total number of packets received
-
- long connected_time; // date and time at which we connected
-
- boolean identified_to_registry = false;
-
- Vec3 location = new Vec3(); // location of our viewpoint
- float acuity = 1.0f; // size over distance
- int horizon = 0; // maximum number of visible entities
- int[] visRegions; // list of visible regions
- boolean suspended = false; // true to supress updates
- boolean request_refresh = false; // true when we want all new updates
- boolean gone = false; // set when we're leaving the world
-
- Vector entities = new Vector(); // vector of entities
- Vector local_entities = new Vector(); // vector of locally-created entities
-
- int text_port = 0; // port on which client listens for text
-
- FilterSender filter_sender; // filter sender thread
- UpdateReceiver update_receiver; // update receiver thread
- Reaper reaper; // reaper thread
- RegistryUpdater registry_updater; // registry updater thread
- ChangeMonitor change_monitor; // change monitor thread
-
- public String getBanner() { return banner; }
-
- public long getConnectedTime() { return connected_time; }
-
- public synchronized int getPacketCount() { return packets_received; }
-
- public synchronized void setViewpoint(Vec3 loc) {
- location = loc;
- synchronized(filter_sender) { filter_sender.notify(); }
- }
-
- public synchronized void setVisualAcuity(float acu) {
- acuity = acu;
- synchronized(filter_sender) { filter_sender.notify(); }
- }
-
- public synchronized void setVisualHorizonCount(int n) {
- horizon = n;
- synchronized(filter_sender) { filter_sender.notify(); }
- }
-
- public synchronized void setVisibleRegions(int[] regions) {
- // visRegions = regions.clone();
- synchronized(filter_sender) { filter_sender.notify(); }
- }
-
- public synchronized void suspend(boolean flag) {
- suspended = flag;
- synchronized(filter_sender) { filter_sender.notify(); }
- }
-
- public synchronized void refresh() {
- request_refresh = true;
- synchronized(filter_sender) { filter_sender.notify(); }
- }
-
- public synchronized int getTextPort() { return text_port; }
-
- public synchronized void setTextPort(int port) {
- text_port = port;
- synchronized(filter_sender) { filter_sender.notify(); }
- }
-
- // method to enumerate entities -- both global and local
- public Enumeration getEntities() { return entities.elements(); }
- public Enumeration getLocalEntities() { return local_entities.elements(); }
-
- public World(String hostname, int port)
- throws IOException, ConnectionRefusedException
- {
- // connect up to registry host
- InetAddress host = InetAddress.getByName(hostname);
- registry_socket = new Socket(host, port, true);
- registry_input = new DataInputStream(registry_socket.getInputStream());
- registry_output = new PrintStream(registry_socket.getOutputStream(), true);
- registry_output.println("HELLO");
- String response = registry_input.readLine();
- if (response.length() > 7)
- banner = response.substring(8);
- else
- banner = "";
- if (response.startsWith("REFUSED"))
- throw new ConnectionRefusedException(banner);
- connected_time = System.currentTimeMillis();
- // create datagram socket and initialize LocalEntity outgoing info
- incoming_socket = new DatagramSocket();
- LocalEntity.setHost(host, port + 2); // data port
- // start threads
- update_receiver = new UpdateReceiver(this);
- filter_sender = new FilterSender(this, host, port);
- reaper = new Reaper(this);
- registry_updater = new RegistryUpdater(this);
- change_monitor = new ChangeMonitor(this);
- }
-
- public void finalize() {
- // delete all the local entities
- // ...
- // tell the filter host to stop sending us updates
- gone = true;
- synchronized(filter_sender) { filter_sender.notify(); }
- // shut down the various threads
- change_monitor.stop();
- registry_updater.stop();
- reaper.stop();
- // tell the registry host that we're out of here
- synchronized(registry_socket) {
- registry_output.println("GOODBYE");
- try { registry_socket.close(); } catch(IOException e) { }
- }
- update_receiver.stop();
- filter_sender.stop();
- }
-
- public void identity(String username, String password)
- throws IOException, BadIdentityException {
- String response;
- synchronized(registry_socket) {
- registry_output.println("IDENT " + username + " " + password);
- try { response = registry_input.readLine(); }
- catch(IOException e) {
- System.out.println("error reading response to IDENTITY: " + e);
- throw new BadIdentityException("bad response to IDENTITY");
- }
- }
- if (response.startsWith("OKAY"))
- identified_to_registry = true;
- else
- throw new BadIdentityException("no such user or bad password");
- }
-
- public Entity addEntity(int entid) {
- Entity e = new Entity(entid);
- int i;
- for (i = 0; i < entities.size(); ++i)
- if (entities.elementAt(i) == null) {
- entities.setElementAt(e, i);
- return e;
- }
- entities.addElement(e);
- return e;
- }
-
- public Entity getEntity(int entid) {
- int i;
- for (i = 0; i < entities.size(); ++i) {
- Entity e = (Entity) entities.elementAt(i);
- if (e.getId() == entid)
- return e;
- }
- return null;
- }
-
- public Entity getEntityByTextId(int tssrc) {
- int i;
- for (i = 0; i < entities.size(); ++i) {
- Entity e = (Entity) entities.elementAt(i);
- if (e.getTextSSRC() == tssrc)
- return e;
- }
- return null;
- }
-
-
- }
-
-
-