home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 6.3 KB | 163 lines |
- // A local entity
-
- // Written by Bernie Roehl, December 1996
-
- package multi;
-
- import java.io.*;
- import java.net.*;
-
- public class LocalEntity {
-
- static InetAddress address;
- static int port;
-
- static ByteArrayOutputStream bout = new ByteArrayOutputStream();
- static DataOutputStream dout = new DataOutputStream(bout);
- static DatagramSocket socket;
-
- static void setHost(InetAddress addr, int prt)
- throws SocketException {
- address = addr;
- port = prt;
- socket = new DatagramSocket();
- }
-
- World world;
- int id;
- String cname;
- String url;
- String[] info;
- boolean is_avatar = true;
- boolean is_persistent = false;
- int text_id;
- String nickname;
-
- int seqnum = 0;
- Vec3 location = new Vec3();
- Rotation orientation = new Rotation();
- long timestamp = System.currentTimeMillis();
- int region = 0;
- float size = 2f;
- boolean gone = false;
-
- public int getId() { return id; }
- public String getName() { return cname; }
- public synchronized String getURL() { return url; }
- public synchronized void setURL(String s) { url = s; }
- public synchronized String getNickName() { return nickname; }
- public synchronized void setNickName(String name) { nickname = new String(name); }
- public synchronized String[] getInfo() { return info; }
- public synchronized void setInfo(String[] inf) { info = inf; }
- public synchronized boolean isAvatar() { return is_avatar; }
- public synchronized void setAvatar(boolean flag) { is_avatar = flag; }
- public synchronized boolean isPersistent() { return is_persistent; }
- public synchronized void setPersistent(boolean per) { is_persistent = per; }
- public synchronized int getTextId() { return text_id; }
- public synchronized void setTextId(int id) { text_id = id; }
-
- public synchronized Vec3 getLocation() { return location; }
- public synchronized void setLocation(Vec3 loc) { location = loc; }
- public synchronized Rotation getOrientation() { return orientation; }
- public synchronized void setOrientation(Rotation ori) { orientation = ori; }
- public synchronized long getTimestamp() { return timestamp; }
- public synchronized void setTimestamp(long time) { timestamp = time; }
- public synchronized int getRegion() { return region; }
- public synchronized void setRegion(int r) { region = r; }
- public synchronized float getSize() { return size; }
- public synchronized void setSize(float siz) { size = siz; }
-
- public LocalEntity(World wrld, String name)
- throws PermissionDeniedException {
- world = wrld;
- cname = new String(name);
- synchronized(world.registry_socket) {
- String response;
- world.registry_output.println("ALLOC " + cname);
- try { response = world.registry_input.readLine(); }
- catch (IOException ex) {
- throw new PermissionDeniedException("bad response");
- }
- if (response.startsWith("REFUSED"))
- throw new PermissionDeniedException(response.substring(8));
- id = Integer.parseInt(response);
- }
- world.local_entities.addElement(this);
- }
-
- public void finalize() {
- gone = true;
- try { sendUpdate(); } catch(IOException ex) { }
- if (is_persistent == false) {
- synchronized(world.registry_socket) {
- world.registry_output.println("RELEASE " + id);
- try { world.registry_input.readLine(); }
- catch (IOException ex) { }
- }
- }
- }
-
- public synchronized void sendUpdate() throws IOException {
- synchronized (bout) {
- bout.reset();
- dout.writeByte(0x80); // version = 2, padding = 0, extension = 0, CSRC count = 0
- dout.writeByte(79); // mark = 0, payload type = 79
- dout.writeShort(seqnum);
- dout.writeInt((int) (timestamp >> 16));
- dout.writeInt(id);
- dout.writeFloat(location.getX());
- dout.writeFloat(location.getY());
- dout.writeFloat(location.getZ());
- dout.writeFloat(orientation.getX());
- dout.writeFloat(orientation.getY());
- dout.writeFloat(orientation.getZ());
- dout.writeFloat(orientation.getAngle());
- dout.writeInt(region);
- dout.writeFloat(size);
- dout.writeInt(gone ? 0x0001 : 0x0000); // flags
- DatagramPacket packet = new DatagramPacket(bout.toByteArray(),
- bout.size(), address, port);
- ++seqnum; // move up
- synchronized(socket) {
- socket.send(packet);
- }
- }
- }
-
- private void validate_response()
- throws IOException, PermissionDeniedException {
- String response = world.registry_input.readLine();
- String msg = "";
- if (response.length() > 7)
- msg = response.substring(8);
- if (response.startsWith("REFUSED"))
- throw new PermissionDeniedException(msg);
- }
-
- public synchronized void updateRegistry()
- throws IOException, PermissionDeniedException {
- synchronized(world.registry_socket) {
- world.registry_output.println("URL " + id + " " + ((url == null) ? "no_url" : url));
- validate_response();
- world.registry_output.println("PERSISTENT " + id + " " + is_persistent);
- validate_response();
- world.registry_output.println("AVATAR " + id + " " + is_avatar);
- validate_response();
- world.registry_output.println("TEXTID " + id + " " + text_id);
- validate_response();
- world.registry_output.println("NICKNAME " + id + " " + ((nickname == null) ? "guest" : nickname));
- validate_response();
- if (info != null) {
- world.registry_output.println("INFO " + id);
- validate_response();
- for (int i = 0; i < info.length; ++i)
- if (info[i] != null)
- world.registry_output.println(info[i]);
- world.registry_output.println(".");
- }
- }
- }
-
- }
-
-