View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package org.xnap.plugin.festival;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import org.apache.log4j.Logger;
25  import org.xnap.gui.StatusBar;
26  
27  public class FestivalRunner implements Runnable
28  {
29  	//--- Constant(s) ---
30  	
31  	//--- Data field(s) ---
32  	
33  	private boolean die = false;
34  	private OutputStream out;
35  	private FestivalMessageQueue queue;
36  	private Thread t = null;
37  	
38      private static Logger logger = Logger.getLogger(FestivalRunner.class);
39  	
40  	//--- Constructor(s) ---
41  	
42      public FestivalRunner(FestivalMessageQueue queue, OutputStream out)
43  	{
44  		this.out = out;
45  		this.queue = queue;
46  	}
47  
48      //--- Method(s) ---
49  	
50  	public void start()
51  	{
52  		die = false;
53  		t = new Thread(this, "FestivalRunner");
54  		t.start();
55  	}
56  
57  	public void stop()
58  	{
59  		die = true;
60  		if (t != null) {
61  			t.interrupt();
62  		}
63  	}
64  
65  	public void run()
66  	{
67  		while (!die) {
68  			String msg = null;
69  			try {
70  				msg = queue.getMessage();
71  			}
72  			catch (InterruptedException ie) {
73  				continue;
74  			}
75  			try {
76  				out.write(msg.getBytes());
77  				out.flush();
78  			}
79  			catch (IOException ie) {
80  				StatusBar.setText("Error writing to festival");
81  				logger.debug("Error writing to festival", ie);
82  				return;
83  			}
84  		}
85  	}
86  }