FreeWRL/FreeX3D  3.0.0
EAIoutThread.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 
18 package vrml.external.FreeWRLEAI;
19 
20 //JAS import java.util.*;
21 //JAS import java.applet.*;
22 //JAS import java.awt.*;
23 //JAS import java.net.*;
24 import java.io.*;
25 
26 
27 
28 //import java.io.IOException;
29 //import java.io.EOFException;
30 //import java.util.Vector;
31 //import java.util.Enumeration;
32 
33 public class EAIoutThread extends Thread {
34  private PrintWriter output;
35  private EAIoutQueue transientEAIMessages = new EAIoutQueue();
36  private EAIoutQueue EAIMessages = new EAIoutQueue();
37  private boolean running;
38  private boolean timerSet;
39  private long timeout;
40  private static final long TIMEOUT = 100;
41  private WriterThreadObserver observer;
42 
43  public EAIoutThread(PrintWriter output) {
44  this.output = output;
45  }
46 
47  public void run()
48  {
49  running = true;
50  while (running) {
51  try {
52  synchronized (this) {
53  wait((long) 50);
54  }
55  // send all queued EAIMessages
56 
57  for(;;) {
58  EAIMessage msg = EAIMessages.dequeue();
59  if (msg == null) break;
60  sendEAIMessage(msg);
61  }
62  } catch (InterruptedException e) {
63  running = false;
64  }
65  }
66  }
67 
68  // this is the main access point to this object -- it enqueues
69  // the given EAIMessage on the appropriate queue, and wakes up the
70  // sleeping thread
71 
72  public synchronized void send(String eaistring)
73  {
74  EAIMessage msg;
75 
76  msg = new EAIMessage(eaistring);
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  running = false;
87  notify();
88  }
89 
90  // sendEAIMessage() actually sends a EAIMessage (woohoo)
91 
92  private void sendEAIMessage(EAIMessage msg)
93  {
94  if (msg == null) { stopThread(); return; }
95  output.println(msg.mmm);
96  output.flush();
97  }
98 }