KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > simple > talk > Talk


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Uri Schneider.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package sample.simple.talk;
48
49 import java.io.IOException JavaDoc;
50
51 import org.mr.MantaAgentConstants;
52 import org.mr.api.simple.Consumer;
53 import org.mr.api.simple.Message;
54 import org.mr.api.simple.Producer;
55 import org.mr.api.simple.SimpleAPI;
56 import org.mr.api.simple.SimpleException;
57
58
59 /*============================================================================
60   For instructions on how to run this sample please refer to the file
61   sample\simple\talk\Readme.txt under the MantaRay installation directory.
62 ============================================================================*/

63
64
65 /**
66  * Talk.java
67  *
68  *
69  * Created: Sun Jul 11 15:43:44 2004
70  *
71  * @author Uri Schneider
72  * @version 1.0
73  */

74 public class Talk implements Runnable JavaDoc {
75     private Producer producer;
76     private Consumer consumer;
77
78     public Talk(Producer producer, Consumer consumer) {
79         this.producer = producer;
80         this.consumer = consumer;
81     }
82
83     public void go() {
84         // start receive thread
85
Thread JavaDoc t = new Thread JavaDoc(this);
86         t.start();
87
88         // send loop
89
try {
90             while (true) {
91                 StringBuffer JavaDoc text = new StringBuffer JavaDoc();
92                 int c;
93                 boolean shouldExit = false;
94
95                 // read text from user
96
System.out.print("talk> ");
97                 while ((c = System.in.read()) != (int) '\n') {
98                     if (c == -1) { // EOF
99
System.out.println();
100                         System.out.println("Goodbye.");
101                         shouldExit = true;
102                     } else {
103                         text.append((char) c);
104                     }
105                 }
106
107                 // create message and send it
108
if (shouldExit) break;
109                 Message message = new Message(text.toString().getBytes());
110                 this.producer.send(message);
111             }
112         } catch (SimpleException e) {
113             System.err.println("Exception thrown in send loop: " + e);
114             System.err.println("Exiting...");
115         } catch (IOException JavaDoc e) {
116             System.err.println("I/O Error while reading from stdin: " + e);
117             System.err.println("Exiting...");
118         }
119     }
120
121     public void run() {
122         // receive loop
123
try {
124             while (true) {
125                 // receive a message and print it.
126
Message message = this.consumer.receive();
127                 String JavaDoc text = new String JavaDoc(message.getPayload());
128                 System.out.println();
129                 System.out.println("> " + text);
130                 System.out.print("talk> ");
131             }
132         } catch (SimpleException e) {
133             System.err.println("Exception thrown in receive loop: " + e);
134             System.err.println("Receive thread exiting... " +
135                                "No more messages will be received.");
136         }
137     }
138
139
140     public static String JavaDoc sendQueueName;
141     public static String JavaDoc receiveQueueName;
142     public static String JavaDoc mantaConf;
143
144     public static void main(String JavaDoc args[]) {
145         parseArgs(args);
146
147         try {
148             // create API object, and instantiate producer and consumer
149
SimpleAPI api = new SimpleAPI(mantaConf);
150             Producer producer = api.openProducer(sendQueueName);
151             Consumer consumer = api.openConsumer(receiveQueueName);
152             Talk talk = new Talk(producer, consumer);
153
154             talk.go();
155         } catch (SimpleException e) {
156             System.err.println("Exception thrown while initializing: " + e);
157             System.err.println("Exiting...");
158         }
159         System.exit(0);
160     }
161
162     private static void parseArgs(String JavaDoc[] args) {
163         for (int i = 0; i < args.length; i++) {
164             String JavaDoc arg = args[i];
165             if (!arg.startsWith("-")) {
166                 System.err.println("error: unexpected argument -- " + arg);
167                 printUsage();
168                 System.exit(1);
169             } else {
170                 if (arg.equals("-c")) {
171                     if (i == args.length - 1 || args[i+1].startsWith("-")) {
172                         System.err.println("error: missing config-dir " +
173                                            "argument");
174                         printUsage();
175                         System.exit(1);
176                     }
177                     mantaConf = args[++i];
178                     continue;
179                 } else if (arg.equals("-qr")) {
180                     if (i == args.length - 1 || args[i+1].startsWith("-")) {
181                         System.err.println("error: missing receive queue " +
182                                            "argument");
183                         printUsage();
184                         System.exit(1);
185                     }
186                     receiveQueueName = args[++i];
187                     continue;
188                 } else if (arg.equals("-qs")) {
189                     if (i == args.length - 1 || args[i+1].startsWith("-")) {
190                         System.err.println("error: missing send queue " +
191                                            "argument");
192                         printUsage();
193                         System.exit(1);
194                     }
195                     sendQueueName = args[++i];
196                     continue;
197                 } else if (arg.equals("-h")) {
198                     printUsage();
199                     System.exit(1);
200                 } else {
201                     System.err.println("unrecognized option -- " + arg);
202                 }
203             }
204         }
205
206         if(mantaConf == null){
207             mantaConf = System.getProperty(MantaAgentConstants.MANTA_CONFIG);
208         }
209         if (mantaConf == null) {
210             mantaConf = "./default_config.xml";
211         }
212         if (sendQueueName == null) {
213             System.err.println("error: you did not specify a send queue");
214             System.exit(1);
215         }
216         if (receiveQueueName == null) {
217             System.err.println("error: you did not specify a receive queue");
218             System.exit(1);
219         }
220     }
221
222     private static void printUsage() {
223         System.out.println("Usage: java Talk -c <config-dir> -qs <queue> " +
224                            "-qr <queue>");
225         System.out.println("\t-c config-dir\tmanta installation dir, should " +
226                            "contain manta.jar");
227         System.out.println("\t-qs queue\tqueue name used for sending " +
228                            "messages");
229         System.out.println("\t-qr queue\tqueue name used for receiving " +
230                            "messages");
231     }
232
233 } // Talk
234
Popular Tags