home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap05 / MuClient.java < prev    next >
Encoding:
Java Source  |  1996-10-07  |  3.8 KB  |  118 lines

  1. // MuClient.java
  2. //
  3. import vrml.*;
  4. import vrml.field.*;
  5. import vrml.node.*;
  6.  
  7. import java.net.*;
  8. import java.io.*;
  9. import java.util.*;
  10.  
  11. public class MuClient extends Script{
  12.    public final static String HOST = "localhost"; // server host name
  13.    public final static int AVATARS = 3;
  14.  
  15.    Socket socket = null;
  16.    DataInputStream in = null;     // input stream from server to client
  17.    DataOutputStream out = null;   // output sream from client to server
  18.    Browser b;                     // for displaying error message to user
  19.    SFVec3f avatarPosition[] = new SFVec3f[AVATARS];         
  20.    SFRotation avatarRotation[] = new SFRotation[AVATARS];
  21.    float[] coord;
  22.    float[] rotation;
  23.    int id;
  24.    boolean connected = false;
  25.  
  26.    public void initialize(){
  27.       b = getBrowser();
  28.       Node avatar = null;
  29.  
  30.       // get the reference to 'translation' and 'rotation' field of 'avatar' node
  31.       for (int i=0; i < AVATARS; i++){
  32.          try {
  33.             avatar = (Node)((SFNode)getField("avatar" + i)).getValue();
  34.             avatarPosition[i] = (SFVec3f)avatar.getExposedField("translation");
  35.             avatarRotation[i] = (SFRotation)avatar.getExposedField("rotation");
  36.          } catch (Exception e) {
  37.             b.setDescription("can not get avatar");
  38.             return;
  39.          }
  40.       }
  41.       coord = new float[3];
  42.       rotation = new float[4];
  43.    }
  44.  
  45.    public void processEvent(Event ev) {
  46.       if(ev.getName().equals("entered")){
  47.          ConstSFBool v = (ConstSFBool)ev.getValue();
  48.  
  49.          if(v.getValue() && (!connected)){
  50.             // open network and input/output stream
  51.             try { 
  52.                socket = new Socket(HOST, MuProtocol.PORT);
  53.                in = new DataInputStream(socket.getInputStream());
  54.                out = new DataOutputStream(socket.getOutputStream());
  55.                id = in.readInt();
  56.  
  57.                // create a thread to wait for data from server
  58.                new MuReceiver(in, this); 
  59.                connected = true;
  60.             } catch (UnknownHostException e) {
  61.                b.setDescription("Unknown host: " + HOST);
  62.                return;
  63.             } catch (Exception e) {
  64.                b.setDescription("Connection error");
  65.                return;
  66.             }
  67.          }
  68.       } else if(ev.getName().equals("myPosition")){
  69.          ((ConstSFVec3f)ev.getValue()).getValue(coord);
  70.       
  71.          try {
  72.             // send my id and my current position to server
  73.             out.writeInt(id);
  74.         out.writeInt(MuProtocol.MOVE);
  75.             out.writeFloat(coord[0]);
  76.             out.writeFloat(coord[1]);
  77.             out.writeFloat(coord[2]);
  78.          } catch (IOException e) {
  79.             b.setDescription("Fail to send position to server:  " + e);
  80.             return;
  81.          }
  82.       } else if(ev.getName().equals("myOrientation")){
  83.     ((ConstSFRotation)ev.getValue()).getValue(rotation);
  84.  
  85.          try {
  86.             // send my id and my current position to server
  87.             out.writeInt(id);
  88.         out.writeInt(MuProtocol.ROTATION);
  89.             out.writeFloat(rotation[0]);
  90.             out.writeFloat(rotation[1]);
  91.             out.writeFloat(rotation[2]);
  92.             out.writeFloat(rotation[3]);
  93.          } catch (IOException e) {
  94.             b.setDescription("Fail to send orientation to server:  " + e);
  95.             return;
  96.          }
  97.       }    
  98.    }
  99.  
  100.    public void updatePosition(int id, float x, float y, float z){
  101.       avatarPosition[id].setValue(x, y, z);
  102.    }
  103.  
  104.    public void updateOrientation(int id, float x, float y, float z, float r){
  105.       avatarRotation[id].setValue(x, y, z, r);
  106.    }
  107.  
  108.    public void shutdown() {
  109.       try {
  110.          out.close();
  111.          in.close();
  112.          socket.close();
  113.       } catch (Exception e) {
  114.          b.setDescription("Connection close error");
  115.       }
  116.    }
  117. }
  118.