Initial import to Gerrit.
[profile/ivi/festival.git] / src / modules / java / cstr / festival / Client.java
1
2  //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
3  //                                                                        \\
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        \\
20  //       permission.                                                      \\
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       \\
29  //   THIS SOFTWARE.                                                       \\
30  //                                                                        \\
31  //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
32  //                                                                        \\
33  //                  Author: Richard Caley (rjc@cstr.ed.ac.uk)             \\
34  //  --------------------------------------------------------------------  \\
35  //  Mainline for Java festival client.                                    \\
36  //                                                                        \\
37  //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
38
39 package cstr.festival ;
40
41 import java.lang.*;
42 import java.util.*;
43 import java.awt.*;
44 import java.io.*;
45 import java.net.*;
46
47 import cstr.festival.client.*;
48 import cstr.festival.scheme.*;
49
50 class RequestHandler implements RequestListener
51 {
52   Thread mainline;
53
54   public RequestHandler(Thread main)
55     {
56       mainline=main;
57     }
58
59   public void requestRunning(Request r)
60     {
61       System.out.println("Request "+r.command+" running");
62     }
63
64   public void requestResult(Request r, Object res)
65     {
66       System.out.println("Request "+r.command+" result="+res);
67     }
68
69   public void requestError(Request r, String mes)
70     {
71       System.out.println("Request "+r.command+" error "+mes);
72     }
73
74   public void requestFinished(Request r)
75     {
76       System.out.println("Request "+r.command+" finished");
77     }
78 }
79
80 public class Client {
81
82   public static void useage(String error)
83     {
84       if (error != null)
85         {
86           System.out.println("");
87           System.out.println("  "+error);
88         }
89       System.out.println("");
90       System.out.println("Useage: festival_client_java [options] file...");
91       System.out.println("");
92       System.out.println("      --help                  Show this message.");
93       System.out.println("      --server <host>         Connect to named host.");
94       System.out.println("      --port <portnum>        Use the given port.");
95       System.out.println("      --sync                  Wait for each expression to finish");
96       System.out.println("                                  executing before reading next.");
97       System.out.println("      --wait                  Wait for all expressions to finish");
98       System.out.println("                                  executing before exiting.");
99       
100       System.exit(error!=null?1:0);
101     }
102
103   public static void main (String[] args)
104     {
105       String server="localhost";
106       int port=1314;
107       int i;
108       boolean waitAtEnd=false;
109       boolean sync=false;
110
111       for(i=0; i<args.length; i++)
112         {
113           if (args[i].equals("--help"))
114               useage(null);
115           else if (args[i].equals("--server"))
116             {
117               i++;
118               if (i>=args.length)
119                 useage("Need name after --server");
120
121               server = args[i];
122             }
123           else if (args[i].equals("--port"))
124             {
125               i++;
126               if (i>=args.length)
127                 useage("Need name after --server");
128
129               try {
130                 port = Integer.parseInt(args[i]);
131               } catch (NumberFormatException ex) {
132                 useage("Not a valid port number '" + args[i] + "'");
133               }
134
135             }
136           else if (args[i].equals("--wait"))
137             waitAtEnd=true;
138           else if (args[i].equals("--sync"))
139             sync=true;
140           else if (args[i].startsWith("--"))
141             useage("Unknown argument "+args[i]);
142           else
143             break;
144         }
145
146       System.out.println("Server='"+server+":"+port+"'");
147
148       Session s=null;
149
150       try {
151         s = new Session(server, port);
152       } catch (UnknownHostException ex) {
153         useage("Unknown host '"+ex.getMessage()+"'");
154       } catch (IOException ex) {
155         useage("Can't connect '"+ex.getMessage()+"'");
156       }
157
158       s.initialise();
159
160       System.out.println("Connected");
161
162       if (i == args.length)
163         {
164           args = new String[] { "-" };
165           i=0;
166         }
167
168     RequestListener handler = new RequestHandler(Thread.currentThread());
169
170     file:
171       for(; i<args.length; i++)
172         {
173           System.out.println("  '"+args[i]+"'");
174
175           String filename = args[i];
176
177           Reader reader = null;
178
179           try {
180             if (filename.equals("-"))
181               reader = new InputStreamReader(System.in);
182             else
183               reader =  new InputStreamReader(new FileInputStream(filename));
184           } catch (FileNotFoundException ex) {
185             useage("Can't open '"+ ex.getMessage() + "'");
186           }
187
188           ReflectingSchemeReader sreader = new ReflectingSchemeReader(reader);
189
190           try {
191             String exp;
192             while (true)
193               {
194                 if (filename.equals("-"))
195                   {
196                     System.out.print("> ");
197                     System.out.flush();
198                   }
199
200                 exp=sreader.nextExprString();
201
202                 if (exp==null)
203                   break;
204
205                 System.out.println(": "+exp);
206                 Request r = s.request(exp, handler);
207                 if (sync)
208                   while (!r.isFinished())
209                     r.waitForUpdate();
210               }
211             } catch (IOException ioex) {
212               System.out.println("      Error "+ioex.getMessage());
213               break file;
214             }
215           System.out.println("--EOF--");
216         }
217
218       System.out.println("call terminate");
219       s.terminate(waitAtEnd);
220     }
221 };