1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
package replica.textui;
|
6
|
|
|
7
|
|
import java.io.BufferedReader;
|
8
|
|
import java.io.BufferedWriter;
|
9
|
|
import java.io.FileNotFoundException;
|
10
|
|
import java.io.FileReader;
|
11
|
|
import java.io.IOException;
|
12
|
|
import java.io.InputStreamReader;
|
13
|
|
import java.io.OutputStreamWriter;
|
14
|
|
import java.net.InetAddress;
|
15
|
|
import java.net.Socket;
|
16
|
|
import java.util.Hashtable;
|
17
|
|
import java.util.StringTokenizer;
|
18
|
|
|
19
|
|
import replica.utils.BeanUtils;
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
|
public class SimpleInterface implements Runnable{
|
27
|
|
|
28
|
|
final static String CONNECT = "connect";
|
29
|
|
final static String DISCONNECT = "disconnect";
|
30
|
|
final static String QUIT = "quit";
|
31
|
|
final static String RUN = "run";
|
32
|
|
|
33
|
|
int port = 7654;
|
34
|
|
String address = "localhost";
|
35
|
|
|
36
|
|
Socket socket;
|
37
|
|
BufferedReader reader;
|
38
|
|
BufferedWriter writer;
|
39
|
|
|
40
|
|
static Hashtable initialOptions;
|
41
|
|
static{
|
42
|
1
|
initialOptions = new Hashtable();
|
43
|
1
|
initialOptions.put("-p", "port");
|
44
|
1
|
initialOptions.put("-a", "address");
|
45
|
|
}
|
46
|
|
|
47
|
0
|
public static void main(String[] args) {
|
48
|
|
|
49
|
0
|
SimpleInterface si = new SimpleInterface();
|
50
|
|
|
51
|
0
|
if( args != null ){
|
52
|
0
|
for (int i = 0; i < args.length; i=i+2) {
|
53
|
0
|
if( i < args.length - 1 )
|
54
|
0
|
BeanUtils.writeProperty(si, args[i], args[i+1]);
|
55
|
|
else{
|
56
|
0
|
System.out.println("Invalid program option. Usage" + usage());
|
57
|
0
|
System.exit(-1);
|
58
|
|
}
|
59
|
|
}
|
60
|
|
}
|
61
|
|
|
62
|
0
|
new Thread(si).start();
|
63
|
|
}
|
64
|
|
|
65
|
0
|
static String usage(){
|
66
|
0
|
return "java replica.textui.SimpleTextInterface <options>\n"+
|
67
|
|
"\n"+
|
68
|
|
" Where <options> can be any of the following:\n"+
|
69
|
|
" -p <portNumber> - changes the default port number to use.\n"+
|
70
|
|
" -a <host> - changes the default host address.";
|
71
|
|
|
72
|
|
}
|
73
|
|
|
74
|
|
|
75
|
1
|
public SimpleInterface (){
|
76
|
1
|
super();
|
77
|
|
}
|
78
|
|
|
79
|
0
|
public void run(){
|
80
|
|
|
81
|
0
|
BufferedReader userReader = null;
|
82
|
|
|
83
|
0
|
try{
|
84
|
0
|
userReader = new BufferedReader( new InputStreamReader( System.in ) );
|
85
|
|
|
86
|
0
|
System.out.print(helloMessage());
|
87
|
|
|
88
|
0
|
System.out.print(" > ");
|
89
|
|
|
90
|
0
|
String line = null;
|
91
|
0
|
StringBuffer command = new StringBuffer();
|
92
|
0
|
int lineNumber = 1;
|
93
|
0
|
while ( ( line = userReader.readLine() ) != null ){
|
94
|
0
|
command.append(line + "\n");
|
95
|
0
|
if( isApplicationCommand(line) ||
|
96
|
|
line.indexOf(';') >= 0 ||
|
97
|
|
line.trim().length() == 0){
|
98
|
0
|
String result = processLine(command.toString());
|
99
|
0
|
command = new StringBuffer();
|
100
|
0
|
lineNumber = 1;
|
101
|
0
|
if( result != null && result.length() > 0 )
|
102
|
0
|
System.out.print(result);
|
103
|
0
|
System.out.print("\n > ");
|
104
|
|
}
|
105
|
|
else{
|
106
|
0
|
System.out.print(" " + (++lineNumber) + " ");
|
107
|
|
}
|
108
|
|
}
|
109
|
|
}
|
110
|
|
catch(IOException e){
|
111
|
0
|
System.out.println("Problem connecting to ["+getAddress()+":"+
|
112
|
|
getPort()+"].");
|
113
|
0
|
e.printStackTrace();
|
114
|
|
}
|
115
|
|
finally{
|
116
|
0
|
close();
|
117
|
|
}
|
118
|
|
}
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
|
|
126
|
|
|
127
|
0
|
boolean isApplicationCommand(String line){
|
128
|
0
|
line = line.trim();
|
129
|
|
|
130
|
0
|
return line.startsWith(CONNECT) ||
|
131
|
|
line.startsWith(DISCONNECT) ||
|
132
|
|
line.startsWith(QUIT);
|
133
|
|
}
|
134
|
|
|
135
|
0
|
String processLine(String line){
|
136
|
0
|
if(line == null)
|
137
|
0
|
return null;
|
138
|
|
|
139
|
0
|
line = line.trim();
|
140
|
|
|
141
|
0
|
if( line.length() == 0 )
|
142
|
0
|
return null;
|
143
|
|
|
144
|
0
|
StringTokenizer tokenizer = new StringTokenizer( line);
|
145
|
|
|
146
|
0
|
if( tokenizer.countTokens() == 0)
|
147
|
0
|
return null;
|
148
|
|
|
149
|
0
|
String command = tokenizer.nextToken();
|
150
|
|
|
151
|
0
|
if( tokenizer.countTokens() == 0)
|
152
|
0
|
return processCommand(command, null);
|
153
|
|
|
154
|
0
|
String args = line.substring(command.length());
|
155
|
|
|
156
|
0
|
return processCommand( command, args);
|
157
|
|
}
|
158
|
|
|
159
|
0
|
String helloMessage(){
|
160
|
0
|
return "\n" +
|
161
|
|
" +----------------------------------------------+\n"+
|
162
|
|
" | T H E R E P L I C A P R O J E C T |\n"+
|
163
|
|
" | |\n"+
|
164
|
|
" | S i m p l e I n t e r f a c e. |\n"+
|
165
|
|
" +----------------------------------------------+\n"+
|
166
|
|
"\n"+
|
167
|
|
" by: Replica Inc.:)\n"+
|
168
|
|
"\n"+
|
169
|
|
"\n";
|
170
|
|
}
|
171
|
|
|
172
|
0
|
String processCommand(String command, String args){
|
173
|
|
|
174
|
0
|
if( command.equalsIgnoreCase(CONNECT) )
|
175
|
0
|
return connect(args);
|
176
|
|
|
177
|
0
|
if( command.equalsIgnoreCase(DISCONNECT) )
|
178
|
0
|
return disconnect();
|
179
|
|
|
180
|
0
|
if( command.equalsIgnoreCase(QUIT) ){
|
181
|
0
|
quit();
|
182
|
0
|
return "YOU WILL NEVER SEE THIS.";
|
183
|
|
}
|
184
|
|
|
185
|
0
|
if( command.equalsIgnoreCase(RUN) )
|
186
|
0
|
return run(args);
|
187
|
|
|
188
|
0
|
if( !isConnected() )
|
189
|
0
|
return "Not connected. You must connect first.";
|
190
|
|
|
191
|
0
|
return sendToServer( command +
|
192
|
|
(args==null?"":(" " + args)) );
|
193
|
|
}
|
194
|
|
|
195
|
0
|
String run(String args){
|
196
|
|
|
197
|
0
|
if( args == null || args.trim().length() == 0 )
|
198
|
0
|
return "Usage: run <filename>;";
|
199
|
|
|
200
|
0
|
args = args.trim();
|
201
|
0
|
if(args.endsWith(";"))
|
202
|
0
|
args = args.substring(0, args.length()-1);
|
203
|
|
|
204
|
0
|
BufferedReader reader = null;
|
205
|
0
|
int i = 0;
|
206
|
0
|
try{
|
207
|
0
|
reader = new BufferedReader( new FileReader(args));
|
208
|
|
|
209
|
0
|
String line = null;
|
210
|
0
|
while( (line = reader.readLine() ) != null ){
|
211
|
0
|
processLine(line);i++;
|
212
|
|
}
|
213
|
|
}
|
214
|
|
catch(FileNotFoundException e){
|
215
|
0
|
return "File no found: [" + args + "].";
|
216
|
|
}
|
217
|
|
catch(IOException e){
|
218
|
0
|
e.printStackTrace();
|
219
|
0
|
return e.getMessage();
|
220
|
|
}
|
221
|
|
finally{
|
222
|
0
|
try{ reader.close(); }catch(Throwable t){}
|
223
|
|
}
|
224
|
|
|
225
|
0
|
return "Processed " + i + " lines from file [" + args + "].";
|
226
|
|
}
|
227
|
|
|
228
|
0
|
String connect(String args){
|
229
|
|
|
230
|
0
|
Object[] conArgs = null;
|
231
|
0
|
try{
|
232
|
0
|
conArgs = getConnectArgs(args);
|
233
|
|
}
|
234
|
|
catch(IllegalArgumentException e){
|
235
|
0
|
return e.getMessage();
|
236
|
|
}
|
237
|
|
|
238
|
0
|
try{
|
239
|
0
|
connect( conArgs[0].toString(), ((Integer)conArgs[1]).intValue(), conArgs[2].toString() );
|
240
|
|
}
|
241
|
|
catch(IOException e){
|
242
|
0
|
e.printStackTrace();
|
243
|
0
|
return "Unable to connect to [" + conArgs[0] + ":" + conArgs[1]
|
244
|
|
+ "] as [" + conArgs[2] + "].";
|
245
|
|
}
|
246
|
|
|
247
|
0
|
return null;
|
248
|
|
}
|
249
|
|
|
250
|
|
|
251
|
|
|
252
|
|
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
|
|
258
|
|
|
259
|
|
|
260
|
|
|
261
|
|
|
262
|
|
|
263
|
0
|
Object[] getConnectArgs(String args) throws IllegalArgumentException{
|
264
|
|
|
265
|
0
|
if( args == null )
|
266
|
0
|
throw new IllegalArgumentException(
|
267
|
|
"Error in connect command. Usage: " + connectUsage() );
|
268
|
|
|
269
|
0
|
StringTokenizer tokenizer = new StringTokenizer(args, " ");
|
270
|
|
|
271
|
0
|
if( tokenizer.countTokens() < 1 )
|
272
|
0
|
throw new IllegalArgumentException(
|
273
|
|
"Error in connect command. Usage: " + connectUsage() );
|
274
|
|
|
275
|
0
|
String host = null;
|
276
|
0
|
int port = -1;
|
277
|
0
|
if( tokenizer.countTokens() > 1){
|
278
|
0
|
String fullAddress = tokenizer.nextToken();
|
279
|
0
|
StringTokenizer tok2 = new StringTokenizer(fullAddress,":");
|
280
|
0
|
host = tok2.nextToken();
|
281
|
0
|
if( tok2.countTokens() > 0 ){
|
282
|
0
|
String portStr = tok2.nextToken();
|
283
|
0
|
try{
|
284
|
0
|
port = Integer.parseInt(portStr);
|
285
|
|
}
|
286
|
|
catch(NumberFormatException e){
|
287
|
0
|
throw new IllegalArgumentException(
|
288
|
|
"Invalid port number [" + portStr + "]." );
|
289
|
|
}
|
290
|
|
}
|
291
|
|
}
|
292
|
|
|
293
|
0
|
String user = tokenizer.nextToken();
|
294
|
|
|
295
|
0
|
if( host == null || host.length() == 0 )
|
296
|
0
|
host = getAddress();
|
297
|
|
|
298
|
0
|
if( port < 0 )
|
299
|
0
|
port = getPort();
|
300
|
|
|
301
|
0
|
return new Object[]{ host, new Integer(port), user};
|
302
|
|
}
|
303
|
|
|
304
|
0
|
void connect(String address, int port, String serverArgs) throws IOException{
|
305
|
|
|
306
|
0
|
if( address == null || address.length() == 0)
|
307
|
0
|
address = getAddress();
|
308
|
|
|
309
|
0
|
if( port < 0 )
|
310
|
0
|
port = getPort();
|
311
|
|
|
312
|
0
|
socket = new Socket(InetAddress.getByName(address), port);
|
313
|
|
|
314
|
0
|
new Thread(new ReturnListener(new BufferedReader(
|
315
|
|
new InputStreamReader( socket.getInputStream() ) ) ) ).start();
|
316
|
|
|
317
|
0
|
writer = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream() ) );
|
318
|
|
|
319
|
0
|
writer.write( "connect " + serverArgs +
|
320
|
|
(serverArgs.trim().endsWith(";")?"\n":";\n") );
|
321
|
0
|
writer.flush();
|
322
|
|
}
|
323
|
|
|
324
|
0
|
String sendToServer(String msg){
|
325
|
0
|
try{
|
326
|
0
|
writer.write(msg + "\n");
|
327
|
0
|
writer.flush();
|
328
|
0
|
return "Sent [" + msg + "].";
|
329
|
|
}
|
330
|
|
catch(IOException e){
|
331
|
0
|
e.printStackTrace();
|
332
|
0
|
return "Problem sending message to server.";
|
333
|
|
}
|
334
|
|
}
|
335
|
|
|
336
|
0
|
String connectUsage(){
|
337
|
0
|
return "connect <host>:<port> <user>[/<password>][@<databasename>]";
|
338
|
|
}
|
339
|
|
|
340
|
0
|
String disconnect(){
|
341
|
0
|
close();
|
342
|
0
|
return "Disconnected from remote.";
|
343
|
|
}
|
344
|
|
|
345
|
0
|
void quit(){
|
346
|
0
|
close();
|
347
|
0
|
System.exit(0);
|
348
|
|
}
|
349
|
|
|
350
|
0
|
public boolean isConnected(){
|
351
|
0
|
return socket != null && socket.isConnected();
|
352
|
|
}
|
353
|
|
|
354
|
0
|
void close(){
|
355
|
0
|
if(reader != null){
|
356
|
0
|
try{ reader.close(); }catch(Throwable t){}
|
357
|
|
}
|
358
|
0
|
if(writer != null){
|
359
|
0
|
try{ writer.close(); }catch(Throwable t){}
|
360
|
|
}
|
361
|
0
|
if(socket != null){
|
362
|
0
|
try{ socket.close(); }catch(Throwable t){}
|
363
|
|
}
|
364
|
|
}
|
365
|
|
|
366
|
|
|
367
|
|
|
368
|
|
|
369
|
0
|
public String getAddress() {
|
370
|
0
|
return address;
|
371
|
|
}
|
372
|
|
|
373
|
|
|
374
|
|
|
375
|
|
|
376
|
0
|
public int getPort() {
|
377
|
0
|
return port;
|
378
|
|
}
|
379
|
|
|
380
|
|
|
381
|
|
|
382
|
|
|
383
|
0
|
public void setAddress(String string) {
|
384
|
0
|
address = string;
|
385
|
|
}
|
386
|
|
|
387
|
|
|
388
|
|
|
389
|
|
|
390
|
0
|
public void setPort(int i) {
|
391
|
0
|
port = i;
|
392
|
|
}
|
393
|
|
|
394
|
|
class ReturnListener implements Runnable{
|
395
|
|
|
396
|
|
BufferedReader reader;
|
397
|
|
|
398
|
0
|
ReturnListener( BufferedReader reader ){
|
399
|
0
|
super();
|
400
|
|
|
401
|
0
|
this.reader = reader;
|
402
|
|
}
|
403
|
|
|
404
|
|
|
405
|
|
|
406
|
|
|
407
|
0
|
public void run(){
|
408
|
|
|
409
|
|
|
410
|
|
|
411
|
0
|
String line = null;
|
412
|
0
|
try{
|
413
|
0
|
boolean firstLine = true;
|
414
|
0
|
while( (line = reader.readLine() ) != null ){
|
415
|
0
|
if( firstLine ){
|
416
|
0
|
System.out.println();
|
417
|
0
|
firstLine = false;
|
418
|
|
}
|
419
|
0
|
System.out.println( " < " + line );
|
420
|
0
|
if( !reader.ready() ){
|
421
|
0
|
System.out.println();
|
422
|
0
|
System.out.print( " > " );
|
423
|
0
|
firstLine = true;
|
424
|
|
}
|
425
|
|
}
|
426
|
|
}
|
427
|
|
catch(IOException e){
|
428
|
0
|
throw new RuntimeException( e );
|
429
|
|
}
|
430
|
|
|
431
|
|
|
432
|
|
}
|
433
|
|
|
434
|
|
}
|
435
|
|
}
|
436
|
|
|