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 sai.eai;
18 
19 
20 //JAS import java.util.*;
21 //JAS import java.awt.*;
22 //JAS import java.net.*;
23 //JAS import java.io.*;
24 import org.web3d.x3d.sai.*;
25 import vrml.external.field.*;
26 import sai.*;
27 import java.util.*;
28 
29 // John A. Stewart - john.stewart@crc.ca
30 //
31 // This sends "Registered Listeners" replies back to the EAI code. It
32 // is called by the EAIinThread; and it queues these messages, just in case
33 // some of the global tables are blocked by another process.
34 
35 
36 public class EAIAsyncThread extends Thread {
37  private EAIAsyncQueue EAIMessages = new EAIAsyncQueue();
38  private boolean running;
39  private boolean timerSet;
40  private long timeout;
41  private static final long TIMEOUT = 100;
42  private WriterThreadObserver observer;
43 
44  public void run()
45  {
46  running = true;
47  while (running) {
48  try {
49  synchronized (this) {
50 
51  wait ((long) 50);
52  }
53  // send all queued EAIMessages
54 
55  // this is outside the synchronized block so that new
56  // EAIMessages can come in even if sendEAIAsyncMessage() blocks
57  for(;;) {
58  EAIAsyncMessage msg = EAIMessages.dequeue();
59  if (msg == null) break;
60  sendEAIAsyncMessage(msg);
61  }
62  } catch (InterruptedException e) {
63  running = false;
64  }
65  }
66  //System.out.println("EAIAsyncThread exiting");
67  }
68 
69  // this is the main access point to this object -- it enqueues
70  // the given EAIMessage on the appropriate queue, and wakes up the
71  // sleeping thread
72 
73  public synchronized void send(String eaistring, int indx)
74  {
75  EAIAsyncMessage msg;
76  msg = new EAIAsyncMessage(eaistring,indx);
77 
78  EAIMessages.enqueue(msg);
79  notify();
80  }
81 
82  // secondary access point -- stop the writer thread
83 
84  public synchronized void stopThread()
85  {
86  System.out.println("stopping EAIAsyncThread");
87  running = false;
88  notify();
89  }
90 
91  // sendEAIAsyncMessage() actually sends a EAIAsyncMessage (woohoo)
92 
93  private void sendEAIAsyncMessage(EAIAsyncMessage msg)
94  {
95  float[] fvals = new float[4];
96  int count = 0;
97  EventOut me;
98 
99  //System.out.println ("EAIAsyncThread.callback - value " + msg.value +
100  // " EventType " + msg.EventNumber );
101 
102  if (BrowserGlobals.EVtype[msg.EventNumber]==18) {
103  me = new EventOutSFVec3f();
104  } else if (BrowserGlobals.EVtype[msg.EventNumber]==12) {
105  me = new EventOutSFRotation();
106  } else if (BrowserGlobals.EVtype[msg.EventNumber]==11) {
107  me = new EventOutMFNode();
108  } else if (BrowserGlobals.EVtype[msg.EventNumber]==3) {
109  me = new EventOutSFTime();
110  } else if (BrowserGlobals.EVtype[msg.EventNumber]==1) {
111  me = new EventOutSFBool();
112 
113  // These are not yet properly handled...
114  // public final static int SFIMAGE = 2;
115  // public final static int SFTIME = 3;
116  // public final static int SFCOLOR = 4;
117  // public final static int MFCOLOR = 5;
118  // public final static int SFFLOAT = 6;
119  // public final static int MFFLOAT = 7;
120  // public final static int SFINT32 = 8;
121  // public final static int MFINT32 = 9;
122  // public final static int SFNODE = 10;
123  // public final static int MFROTATION = 13;
124  // public final static int SFSTRING = 14;
125  // public final static int MFSTRING = 15;
126  // public final static int SFVEC2F = 16;
127  // public final static int MFVEC2F = 17;
128  // public final static int MFVEC3F = 19;
129 
130 
131  } else {
132  System.out.println (" EAIASyncThread: handling something funny here, " +
133  BrowserGlobals.EVtype[msg.EventNumber]);
134  me = new EventOut();
135  }
136  me.RLreturn = msg.value;
137 
138  if (BrowserGlobals.EVObserver[msg.EventNumber] != null) {
139  X3DFieldEvent event = new X3DFieldEvent(me, BrowserGlobals.TickTime, BrowserGlobals.EVObject[msg.EventNumber]);
140  BrowserGlobals.EVObserver[msg.EventNumber].readableFieldChanged (event);
141  } else {
142  System.out.println ("WARNING - EAIAsyncThread.callback - thread callback null, discarding");
143  }
144  }
145 }