FreeWRL/FreeX3D  3.0.0
EAIinThread.java
1 package sai.eai;
2 
3 import org.web3d.x3d.sai.*;
4 import sai.*;
5 
6 import java.applet.*;
7 import java.net.*;
8 import java.io.*;
9 
10 
11 // The Thread that reads input from the FreeWRL browser...
12 public class EAIinThread implements Runnable {
13 
14  // DataInputStream EAIin;
15  BufferedReader EAIin;
16  Socket sock;
17  Applet FreeWLRSceneInterface;
18  BrowserInterface mybrowser;
19 
20  boolean debug = false;
21 
22  // The following are used to send from the event thread to the
23  // browser thread. The event thread gets stuff from the EAI port
24  // from the FreeWRL Browser, and sends Replies back to the
25  // browser thread.
26 
27  private PrintWriter EAItoBrowserPrintWriter = null;
28 
29  // Initialization - get the socket and the FreeWLRSceneInterfaces thread
30  public EAIinThread (Socket s, Applet d, PrintWriter pwtoBrowserjava, BrowserInterface me) {
31 
32  sock = s;
33  FreeWLRSceneInterface=d;
34  mybrowser=me;
35  EAItoBrowserPrintWriter = pwtoBrowserjava;
36  }
37 
38  public void run() {
39  // Open the socket, and wait for the first reply....
40 
41  String reply;
42  String EVentno;
43  String EVentreply;
44  String REreply;
45  String Stemp;
46  String EVTime;
47 
48  try {
49  EAIin = new BufferedReader( new InputStreamReader(sock.getInputStream()));
50  } catch (IOException e) {
51  System.out.print ("error reiniting data input stream");
52  }
53 
54  // Now, this is the loop that loops to end all loops....
55 
56  try {
57  // wait for FreeWRL to send us the correct number of lines...
58  // rep 1, 2, 3 should be "RE" "2" "0" , with maybe another
59  // parameter at the end.
60  // EVs are events, and have two following lines.
61 
62  reply = new String ("");
63 
64  while (reply != null) {
65  // Loop here, processing incoming events
66  reply = EAIin.readLine();
67 
68  if (reply.equals("EV")) {
69  EVTime = EAIin.readLine();
70  BrowserGlobals.TickTime = Double.parseDouble(EVTime);
71 
72  EVentno = EAIin.readLine();
73  int eventno = Integer.parseInt(EVentno);
74  if (debug) System.out.println ("EAIinThread 3 reply is " + EVentno);
75 
76  EVentreply = new String ("");
77  reply = EAIin.readLine();
78  if (debug) System.out.println ("EAIinThread 5 reply is " + reply);
79 
80  // Now, read the reply, until the string "EV_EOT is read in ???
81  while (!reply.equals("EV_EOT")) {
82  EVentreply = EVentreply + reply;
83  reply = EAIin.readLine();
84  }
85 
86  if (debug)
87  System.out.println ("EAIinThread sending EVentno: " +
88  EVentno + " EventReply " + EVentreply + " reply " + reply);
89 
90  mybrowser.Browser_RL_Async_send(EVentreply,eventno);
91  } else if (reply.equals("RE")) {
92  EVTime = EAIin.readLine();
93  BrowserGlobals.TickTime = Double.parseDouble(EVTime);
94 
95  // This is the integer reply to the command... number...
96  EAItoBrowserPrintWriter.println(EAIin.readLine());
97 
98  // and the response
99  EVentreply = new String ("");
100  //EAItoBrowserPrintWriter.println(EAIin.readLine());
101  reply = EAIin.readLine();
102  if (debug) System.out.println ("EAIinThread 5xx reply is " + reply);
103 
104  // Now, read the reply, until the string "RE_EOT is read in ???
105  while (!reply.equals("RE_EOT")) {
106  EVentreply = EVentreply + reply;
107  reply = EAIin.readLine();
108  }
109 
110  EAItoBrowserPrintWriter.println(EVentreply);
111 
112  EAItoBrowserPrintWriter.flush();
113 
114  } else if (reply.equals ("QUIT")) {
115  if (debug) System.out.println ("EAIinThread, got the quit signal");
116  System.exit(0);
117  } else {
118  System.out.println ("expecting REor EV, got " + reply);
119  if (debug) System.out.println ("EAIinThread 9 reply is " + reply);
120  }
121  }
122  } catch (IOException e) {
123  //System.out.print ("error reiniting data input stream\n");
124  }
125 }
126 }