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