1   
2   
3   
4   
5   package functional.dumchat;
6   import java.io.BufferedReader;
7   import java.io.InputStreamReader;
8   import java.net.InetAddress;
9   import java.util.Collection;
10  import java.util.Iterator;
11  
12  import junit.framework.TestCase;
13  
14  import org.neo.swarm.SwarmContainer;
15  import org.neo.swarm.config.JMXConfig;
16  import org.neo.swarm.network.NetUtils;
17  /***
18   * DumChat - console based peer to peer chat app, with JMX console for management
19   * 
20   * @author navery
21   */
22  public class DumChat extends TestCase {
23  	
24  	public void testBlah() throws Exception {
25  		
26  	}
27  	public static void main(String[] args) {
28  		if (args.length < 3) {
29  			System.out.println("Invalid number of arguments [username, localtcpport, jmxconsoleport]");
30  			System.out.println(" usage: DumChat larry  8080 8091");
31  			System.exit(1);
32  		}			
33  		try {
34  		
35  		String address = NetUtils.getMyIPAddress();
36  		DumChat dumChat = new DumChat(args[0], address, Integer.parseInt(args[1]), Integer.parseInt(args[2]));
37  		} catch (Exception ex) {
38  			ex.printStackTrace();
39  		}
40  	}
41  	
42  	public DumChat(String username, String localHost, int myPort, int jmxPort) throws Exception {
43  		
44  		System.out.println("DumChat args:" + username + " myAddress: " + localHost + ":" + myPort + ";" + jmxPort);
45  		SwarmContainer silc1 = new JMXConfig("AC"+myPort, InetAddress.getLocalHost(), myPort, jmxPort, false).setup();		
46  
47  		try {
48  			
49  			silc1.getAppContext("AC"+myPort).addComponent(username, DumUser.class, new Object[]{username});
50  			
51  			silc1.start();			
52  			Thread.sleep(3000);
53  			displayHelp();
54  			System.out.println(" ready to start chatting......your username is:" + username);
55  
56  
57  			User you = null;
58  			String msg;
59  			
60  			boolean exit = false;
61  			InputStreamReader input = new InputStreamReader(System.in);
62  			BufferedReader reader = new BufferedReader(input);
63  			
64  			
65  			while (!exit) {
66  				msg = reader.readLine();
67  				while (msg == null) {
68  					System.out.println("Got a null msg");
69  					Thread.sleep(2000);
70  					msg = reader.readLine();
71  				}
72  				if (msg.equals("exit")) {
73  					exit = true;
74  				} else if (msg.startsWith("help")) {
75  					displayHelp();
76  				} else if (msg.startsWith("chat ")){
77  					
78  					
79  				    String otheruserName = msg.substring(msg.indexOf("chat ") + "chat ".length());
80  				    System.out.println("starting chat session with:" + otheruserName);
81  				    you = (User) silc1.getComponent(otheruserName);
82  				} else if (msg.equals("who")) {
83  					
84  					
85  					System.out.println("showing all current users:");
86  					Collection users = silc1.getComponentsOfType(User.class);
87  					Iterator iter = users.iterator();
88  					while (iter.hasNext()) {
89  					    System.out.println("user:" + ((User) iter.next()).getName());
90  					}
91  					System.out.println("==================");
92  				} else if (you != null) {
93  					you.send(username + ">>" + msg);
94  				}
95  			}
96  		}
97  		 catch (Exception e) {
98  			e.printStackTrace();
99  		}
100 		silc1.stop();
101 	}
102 	private void displayHelp() {
103 		System.out.println("============== help =============");
104 		System.out.println("who		- displays online users");
105 		System.out.println("chat xxx  	- starts chat session with user xxx");
106 		System.out.println("exit		- quits");
107 		System.out.println("================================");
108 	}
109 }