2 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
4 // Centre for Speech Technology Research \\
5 // University of Edinburgh, UK \\
6 // Copyright (c) 1996,1997 \\
7 // All Rights Reserved. \\
8 // Permission is hereby granted, free of charge, to use and distribute \\
9 // this software and its documentation without restriction, including \\
10 // without limitation the rights to use, copy, modify, merge, publish, \\
11 // distribute, sublicense, and/or sell copies of this work, and to \\
12 // permit persons to whom this work is furnished to do so, subject to \\
13 // the following conditions: \\
14 // 1. The code must retain the above copyright notice, this list of \\
15 // conditions and the following disclaimer. \\
16 // 2. Any modifications must be clearly marked as such. \\
17 // 3. Original authors' names are not deleted. \\
18 // 4. The authors' names are not used to endorse or promote products \\
19 // derived from this software without specific prior written \\
21 // THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK \\
22 // DISCLAIM ALL WARRANTIES With REGARD TO THIS SOFTWARE, INCLUDING \\
23 // ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT \\
24 // SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE \\
25 // FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES \\
26 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN \\
27 // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, \\
28 // ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF \\
31 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
33 // Author: Richard Caley (rjc@cstr.ed.ac.uk) \\
34 // -------------------------------------------------------------------- \\
35 // The thread which actually communicates with festival. \\
37 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
40 package cstr.festival.client ;
56 public Job(Integer i, String c, Session s)
64 class Festival extends Thread
66 public static final int FT_SCHEME = 1;
67 public static final int FT_WAVE = 2;
69 protected static byte [] endm;
71 protected String hostname=null;
72 protected InetAddress address = null;
73 protected int port=-1;
74 protected boolean closeOnExit;
76 protected PrintWriter out;
77 protected InputStream in;
81 protected JobQueue jobs;
83 private byte buffer[];
84 private int buffered_p;
85 private int buffered_e;
88 String end="ft_StUfF_key";
89 endm = new byte[end.length()];
91 for(int i=0;i<endm.length; i++)
93 endm[i] = (byte)end.charAt(i);
98 protected Festival(Socket sock, boolean close)
102 jobs = new JobQueue();
103 buffer = new byte[8096];
109 public Festival(Socket sock)
114 public Festival(InetAddress addr, int p)
117 this(new Socket(addr, p), true);
122 public Festival(String host, int p)
123 throws IOException, UnknownHostException
125 this(InetAddress.getByName(host), p);
132 setPriority(getPriority()+1);
133 out = new PrintWriter(s.getOutputStream());
134 in = s.getInputStream();
146 Job job = (Job)jobs.get();
147 // System.out.println("sending "+job.command);
148 out.println(job.command);
153 job.session.notifyRunning(job.id);
157 status = getStatus();
158 if (status.startsWith("ER"))
160 job.session.notifyError(job.id, "");
163 else if (status.startsWith("LP"))
165 byte [] res=getResult();
166 job.session.notifyResult(job.id, FT_SCHEME, res);
168 else if (status.startsWith("WV"))
170 byte[] res=getResult();
171 job.session.notifyResult(job.id, FT_WAVE, res);
173 else if (status.startsWith("OK"))
179 byte [] res=getResult();
180 job.session.notifyError(job.id, "unknown type");
184 job.session.notifyFinished(job.id);
187 } catch (IOException ex) {
188 System.err.println("IO Error '"+ex.getMessage()+"'");
192 } catch (IOException ex) {
193 System.err.println("Error closing festival socket '"+ex.getMessage()+"'");
195 while (!jobs.isEmpty())
197 System.out.println("Left Job '"+((Job)jobs.get()).command+"'");
202 public void connect()
204 // System.err.println("Connecting");
208 public void disconnect(boolean carefully)
210 // System.err.println("Disconnecting");
217 } catch (InterruptedException ex) {
225 private void closeSocket() throws IOException
229 // System.err.println("Closing festival socket");
234 public void newJob(Integer id, String c, Session s)
236 jobs.add(new Job(id,c,s));
240 // finally! To the things which talk to festival.
242 private byte[] readTo(InputStream s, char end) throws IOException
244 if (buffered_e == buffered_p)
247 buffered_e=s.read(buffer, 0, 8096);
251 for(int i=buffered_p; i<buffered_e; i++)
252 if (buffer[i] == (byte)end)
254 res = new byte[i-buffered_p+1];
259 res = new byte[buffered_e-buffered_p];
261 for(int i=0; i< res.length; i++)
262 res[i] = buffer[buffered_p+i];
264 buffered_p = buffered_p+res.length;
269 protected String getLine() throws IOException
271 StringBuffer line= new StringBuffer(20);
275 byte [] bit = readTo(in, '\n');
276 line.append(new String(bit));
278 //System.out.println("got '"+line.toString()+"'");
280 if (line.charAt(line.length()-1) == '\n')
284 //System.out.println("result '"+line.toString()+"'");
285 return line.toString();
289 protected String getStatus() throws IOException
291 String line = getLine();
293 // System.out.println(" Status = '"+line+"'");
294 return line==null?"EOF":line;
297 protected byte[] getResult() throws IOException
299 byte [] res = new byte[0];
303 byte [] bit = readTo(in, 'y');
305 int end = bit.length-endm.length;
311 for(int i=0; i< endm.length; i++)
312 if (bit[i+end] != endm[i])
319 byte [] new_res = new byte[res.length+end];
321 for (int i=0; i< res.length; i++)
323 for (int i=0; i< end; i++)
324 new_res[res.length+i] = bit[i];
328 if (end < bit.length)
337 StringBuffer val = new StringBuffer();
345 line = in.readLine();
349 int endm = line.indexOf("ft_StUfF_key");
353 val.append(line.substring(0, endm));
354 pending = line.substring(endm+12);
363 line = in.readLine();
365 return val.toString();