| 1 46 47 package sample.simple.chat; 48 49 import java.io.IOException ; 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 63 64 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 try { 88 while (true) { 89 StringBuffer text = new StringBuffer (); 90 int c; 91 boolean shouldExit = false; 92 93 System.out.print("chat> "); 95 while ((c = System.in.read()) != (int) '\n') { 96 if (c == -1) { System.out.println(); 98 System.out.println("Goodbye."); 99 shouldExit = true; 100 } else { 101 text.append((char) c); 102 } 103 } 104 105 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 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 topicName) { 120 String text = new String (message.getPayload()); 122 System.out.println(); 123 System.out.println("> " + text); 124 System.out.print("chat> "); 125 } 126 127 public static String topicName; 128 public static String mantaConf; 129 130 public static void main(String args[]) { 131 parseArgs(args); 132 133 try { 134 SimpleAPI api = new SimpleAPI(mantaConf); 136 Publisher publisher = api.openPublisher(topicName); 137 Subscriber subscriber = api.openSubscriber(topicName); 138 139 Chat chat = new Chat(publisher, subscriber); 141 142 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 [] args) { 152 for (int i = 0; i < args.length; i++) { 153 String 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 } | Popular Tags |