FreeWRL/FreeX3D  3.0.0
EAIAsyncThread.java
1 // copyright (c) 1997,1998 stephen f. white
2 // Modified for use with EAI and FreeWRL. John Stewart CRC Canada 1999
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2, or (at your option)
7 // any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, write to
16 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17 package vrml.external.FreeWRLEAI;
18 
19 
20 //JAS import java.util.*;
21 //JAS import java.awt.*;
22 //JAS import java.net.*;
23 //JAS import java.io.*;
24 import vrml.external.field.*;
25 import vrml.external.*;
26 
27 // John A. Stewart - john.stewart@crc.ca
28 //
29 // This sends "Registered Listeners" replies back to the EAI code. It
30 // is called by the EAIinThread; and it queues these messages, just in case
31 // some of the global tables are blocked by another process.
32 
33 
34 public class EAIAsyncThread extends Thread {
35  private EAIAsyncQueue EAIMessages = new EAIAsyncQueue();
36  private boolean running;
37  private boolean timerSet;
38  private long timeout;
39  private static final long TIMEOUT = 100;
40  private WriterThreadObserver observer;
41 
42  public void run()
43  {
44  running = true;
45  while (running) {
46  try {
47  synchronized (this) {
48 
49  wait ((long) 50);
50  }
51  // send all queued EAIMessages
52 
53  // this is outside the synchronized block so that new
54  // EAIMessages can come in even if sendEAIAsyncMessage() blocks
55  for(;;) {
56  EAIAsyncMessage msg = EAIMessages.dequeue();
57  if (msg == null) break;
58  sendEAIAsyncMessage(msg);
59  }
60  } catch (InterruptedException e) {
61  running = false;
62  }
63  }
64  //System.out.println("EAIAsyncThread exiting");
65  }
66 
67  // this is the main access point to this object -- it enqueues
68  // the given EAIMessage on the appropriate queue, and wakes up the
69  // sleeping thread
70 
71  public synchronized void send(String eaistring, int indx)
72  {
73  EAIAsyncMessage msg;
74  msg = new EAIAsyncMessage(eaistring,indx);
75 
76  EAIMessages.enqueue(msg);
77  notify();
78  }
79 
80  // secondary access point -- stop the writer thread
81 
82  public synchronized void stopThread()
83  {
84  System.out.println("stopping EAIAsyncThread");
85  running = false;
86  notify();
87  }
88 
89  // sendEAIAsyncMessage() actually sends a EAIAsyncMessage (woohoo)
90 
91  private void sendEAIAsyncMessage(EAIAsyncMessage msg)
92  {
93  float[] fvals = new float[4];
94  int count = 0;
95  EventOut me;
96 
97  //System.out.println ("EAIAsyncThread.callback - value " + msg.value +
98  // " EventType " + msg.EventNumber );
99 
100  if (BrowserGlobals.EVtype[msg.EventNumber]==19) {
101  me = new EventOutMFVec3f();
102  } else if (BrowserGlobals.EVtype[msg.EventNumber]==18) {
103  me = new EventOutSFVec3f();
104  } else if (BrowserGlobals.EVtype[msg.EventNumber]==17) {
105  me = new EventOutMFVec2f();
106  } else if (BrowserGlobals.EVtype[msg.EventNumber]==16) {
107  me = new EventOutSFVec2f();
108  } else if (BrowserGlobals.EVtype[msg.EventNumber]==15) {
109  me = new EventOutMFString();
110  } else if (BrowserGlobals.EVtype[msg.EventNumber]==14) {
111  me = new EventOutSFString();
112  } else if (BrowserGlobals.EVtype[msg.EventNumber]==13) {
113  me = new EventOutMFRotation();
114  } else if (BrowserGlobals.EVtype[msg.EventNumber]==12) {
115  me = new EventOutSFRotation();
116  } else if (BrowserGlobals.EVtype[msg.EventNumber]==11) {
117  me = new EventOutMFNode();
118  } else if (BrowserGlobals.EVtype[msg.EventNumber]==10) {
119  me = new EventOutSFNode ();
120  } else if (BrowserGlobals.EVtype[msg.EventNumber]==9) {
121  me = new EventOutMFInt32();
122  } else if (BrowserGlobals.EVtype[msg.EventNumber]==8) {
123  me = new EventOutSFInt32();
124  } else if (BrowserGlobals.EVtype[msg.EventNumber]==7) {
125  me = new EventOutMFFloat();
126  } else if (BrowserGlobals.EVtype[msg.EventNumber]==6) {
127  me = new EventOutSFFloat();
128  } else if (BrowserGlobals.EVtype[msg.EventNumber]==5) {
129  me = new EventOutMFColor();
130  } else if (BrowserGlobals.EVtype[msg.EventNumber]==4) {
131  me = new EventOutSFColor();
132  } else if (BrowserGlobals.EVtype[msg.EventNumber]==3) {
133  me = new EventOutSFTime();
134  } else if (BrowserGlobals.EVtype[msg.EventNumber]==2) {
135  me = new EventOutSFImage();
136  } else if (BrowserGlobals.EVtype[msg.EventNumber]==1) {
137  me = new EventOutSFBool();
138 
139 
140  } else {
141  System.out.println (" EAIASyncThread: handling something funny here, " +
142  BrowserGlobals.EVtype[msg.EventNumber]);
143  me = new EventOut();
144  }
145  me.RLreturn = msg.value;
146 
147  if (BrowserGlobals.EVObserver[msg.EventNumber] != null) {
148  BrowserGlobals.EVObserver[msg.EventNumber].callback (me,
149  BrowserGlobals.TickTime, BrowserGlobals.EVObject[msg.EventNumber]);
150  } else {
151  System.out.println ("WARNING - EAIAsyncThread.callback - thread callback null, discarding");
152  }
153  }
154 }