KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > simple > chat > Chat


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.chat;
48
49 import java.io.IOException JavaDoc;
50
51 import org.mr.MantaAgentConstants;
52 import org.mr.api.simple.Message;
53 import org.mr.api.simple.MessageListener;
54 import org.mr.api.simple.Publisher;
55 import org.mr.api.simple.SimpleAPI;
56 import org.mr.api.simple.SimpleException;
57 import org.mr.api.simple.Subscriber;
58
59 /*============================================================================
60   For instructions on how to run this sample please refer to the file
61   sample\simple\chat\Readme.txt under the MantaRay installation directory.
62 ============================================================================*/

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

73 public class Chat implements MessageListener {
74     private Publisher publisher;
75     private Subscriber subscriber;
76
77     public Chat(Publisher publisher, Subscriber subscriber)
78         throws SimpleException
79     {
80         this.publisher = publisher;
81         this.subscriber = subscriber;
82         this.subscriber.subscribe(this);
83     }
84
85     public void go() {
86         // send loop
87
try {
88             while (true) {
89                 StringBuffer JavaDoc text = new StringBuffer JavaDoc();
90                 int c;
91                 boolean shouldExit = false;
92
93                 // read text from user
94
System.out.print("chat> ");
95                 while ((c = System.in.read()) != (int) '\n') {
96                     if (c == -1) { // EOF
97
System.out.println();
98                         System.out.println("Goodbye.");
99                         shouldExit = true;
100                     } else {
101                         text.append((char) c);
102                     }
103                 }
104
105                 // create message and send it
106
if (shouldExit) break;
107                 Message message = new Message(text.toString().getBytes());
108                 this.publisher.send(message);
109             }
110         } catch (SimpleException e) {
111             System.err.println("Exception thrown in send loop: " + e);
112             System.err.println("Exiting...");
113         } catch (IOException JavaDoc e) {
114             System.err.println("I/O Error while reading from stdin: " + e);
115             System.err.println("Exiting...");
116         }
117     }
118
119     public void onMessage(Message message, String JavaDoc topicName) {
120         // extract text from incoming message, and print it
121
String JavaDoc text = new String JavaDoc(message.getPayload());
122         System.out.println();
123         System.out.println("> " + text);
124         System.out.print("chat> ");
125     }
126
127     public static String JavaDoc topicName;
128     public static String JavaDoc mantaConf;
129
130     public static void main(String JavaDoc args[]) {
131         parseArgs(args);
132
133         try {
134             // create API object, and instantiate publisher and subscriber
135
SimpleAPI api = new SimpleAPI(mantaConf);
136             Publisher publisher = api.openPublisher(topicName);
137             Subscriber subscriber = api.openSubscriber(topicName);
138
139             // create chat object
140
Chat chat = new Chat(publisher, subscriber);
141
142             // go go go
143
chat.go();
144         } catch (SimpleException e) {
145             System.err.println("Exception thrown while initializing: " + e);
146             System.err.println("Exiting...");
147         }
148         System.exit(0);
149     }
150
151     private static void parseArgs(String JavaDoc[] args) {
152         for (int i = 0; i < args.length; i++) {
153             String JavaDoc arg = args[i];
154             if (!arg.startsWith("-")) {
155                 System.err.println("error: unexpected argument -- " + arg);
156                 printUsage();
157                 System.exit(1);
158             } else {
159                 if (arg.equals("-c")) {
160                     if (i == args.length - 1 || args[i+1].startsWith("-")) {
161                         System.err.println("error: missing config-dir " +
162                                            "argument");
163                         printUsage();
164                         System.exit(1);
165                     }
166                     mantaConf = args[++i];
167                     continue;
168                 } else if (arg.equals("-t")) {
169                     if (i == args.length - 1 || args[i+1].startsWith("-")) {
170                         System.err.println("error: missing topic argument");
171                         printUsage();
172                         System.exit(1);
173                     }
174                     topicName = args[++i];
175                     continue;
176                 } else if (arg.equals("-h")) {
177                     printUsage();
178                     System.exit(1);
179                 } else {
180                     System.err.println("unrecognized option -- " + arg);
181                 }
182             }
183         }
184
185         if(mantaConf == null){
186             mantaConf = System.getProperty(MantaAgentConstants.MANTA_CONFIG);
187         }
188         if (mantaConf == null) {
189             mantaConf = "./default_config.xml";
190         }
191         if (topicName == null) {
192             System.err.println("error: you did not specify a topic");
193             System.exit(1);
194         }
195     }
196
197     private static void printUsage() {
198         System.out.println("Usage: java Chat -c <config-dir> -t <topic>");
199         System.out.println("\t-c config-dir\tmanta installation dir, should " +
200                            "contain manta.jar");
201         System.out.println("\t-t topic\ttopic name used for sending and " +
202                            "receiving messages");
203     }
204
205 } // Chat
206
Popular Tags