home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-07 | 3.8 KB | 118 lines |
- // MuClient.java
- //
- import vrml.*;
- import vrml.field.*;
- import vrml.node.*;
-
- import java.net.*;
- import java.io.*;
- import java.util.*;
-
- public class MuClient extends Script{
- public final static String HOST = "localhost"; // server host name
- public final static int AVATARS = 3;
-
- Socket socket = null;
- DataInputStream in = null; // input stream from server to client
- DataOutputStream out = null; // output sream from client to server
- Browser b; // for displaying error message to user
- SFVec3f avatarPosition[] = new SFVec3f[AVATARS];
- SFRotation avatarRotation[] = new SFRotation[AVATARS];
- float[] coord;
- float[] rotation;
- int id;
- boolean connected = false;
-
- public void initialize(){
- b = getBrowser();
- Node avatar = null;
-
- // get the reference to 'translation' and 'rotation' field of 'avatar' node
- for (int i=0; i < AVATARS; i++){
- try {
- avatar = (Node)((SFNode)getField("avatar" + i)).getValue();
- avatarPosition[i] = (SFVec3f)avatar.getExposedField("translation");
- avatarRotation[i] = (SFRotation)avatar.getExposedField("rotation");
- } catch (Exception e) {
- b.setDescription("can not get avatar");
- return;
- }
- }
- coord = new float[3];
- rotation = new float[4];
- }
-
- public void processEvent(Event ev) {
- if(ev.getName().equals("entered")){
- ConstSFBool v = (ConstSFBool)ev.getValue();
-
- if(v.getValue() && (!connected)){
- // open network and input/output stream
- try {
- socket = new Socket(HOST, MuProtocol.PORT);
- in = new DataInputStream(socket.getInputStream());
- out = new DataOutputStream(socket.getOutputStream());
- id = in.readInt();
-
- // create a thread to wait for data from server
- new MuReceiver(in, this);
- connected = true;
- } catch (UnknownHostException e) {
- b.setDescription("Unknown host: " + HOST);
- return;
- } catch (Exception e) {
- b.setDescription("Connection error");
- return;
- }
- }
- } else if(ev.getName().equals("myPosition")){
- ((ConstSFVec3f)ev.getValue()).getValue(coord);
-
- try {
- // send my id and my current position to server
- out.writeInt(id);
- out.writeInt(MuProtocol.MOVE);
- out.writeFloat(coord[0]);
- out.writeFloat(coord[1]);
- out.writeFloat(coord[2]);
- } catch (IOException e) {
- b.setDescription("Fail to send position to server: " + e);
- return;
- }
- } else if(ev.getName().equals("myOrientation")){
- ((ConstSFRotation)ev.getValue()).getValue(rotation);
-
- try {
- // send my id and my current position to server
- out.writeInt(id);
- out.writeInt(MuProtocol.ROTATION);
- out.writeFloat(rotation[0]);
- out.writeFloat(rotation[1]);
- out.writeFloat(rotation[2]);
- out.writeFloat(rotation[3]);
- } catch (IOException e) {
- b.setDescription("Fail to send orientation to server: " + e);
- return;
- }
- }
- }
-
- public void updatePosition(int id, float x, float y, float z){
- avatarPosition[id].setValue(x, y, z);
- }
-
- public void updateOrientation(int id, float x, float y, float z, float r){
- avatarRotation[id].setValue(x, y, z, r);
- }
-
- public void shutdown() {
- try {
- out.close();
- in.close();
- socket.close();
- } catch (Exception e) {
- b.setDescription("Connection close error");
- }
- }
- }
-