added skeleton command-handlers

git-svn-id: svn://anubis/gvsu@142 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-09-17 23:42:58 +00:00
parent b157ef4843
commit 005fb6efdd

View File

@ -9,6 +9,7 @@ public class ChatClient
private String myUserName;
private Thread myListenThread;
private ChatClientListener myListener;
private RegistrationInfo myRegistrationInfo;
private class ChatClientListener implements Runnable
{
@ -49,16 +50,13 @@ public class ChatClient
public void run()
{
myListener = new ChatClientListener();
RegistrationInfo regInfo =
myRegistrationInfo =
new RegistrationInfo(myUserName,
myListener.getHost(),
myListener.getPort(),
true);
try {
myPServ.register(regInfo);
} catch (Exception e) {
System.out.println("Exception caught when registering!");
e.printStackTrace();
if (!register())
{
return;
}
@ -69,6 +67,30 @@ public class ChatClient
mainloop();
}
private boolean register()
{
try {
myPServ.register(myRegistrationInfo);
} catch (Exception e) {
System.out.println("Exception caught when registering!");
e.printStackTrace();
return false;
}
return true;
}
private boolean unregister()
{
try {
myPServ.unregister(myUserName);
} catch (Exception e) {
System.out.println("Exception caught when unregistering!");
e.printStackTrace();
return false;
}
return true;
}
private void mainloop()
{
BufferedReader br = new BufferedReader(
@ -76,7 +98,43 @@ public class ChatClient
String line;
while ( (line = br.readLine()) != null)
{
line = line.trim();
if (line.equals(""))
continue;
String command, rest;
int spaceIndex = line.indexOf(' ');
if (spaceIndex > 0)
{
command = line.substring(0, spaceIndex);
rest = line.substring(spaceIndex + 1);
rest = rest.trim();
}
else
{
command = line;
rest = "";
}
if (command.equals("help") || command.equals("?"))
{
System.out.println("Commands:\n");
System.out.println(" quit - disconnect and exit program");
System.out.println(" friends - view a list of all peers on the system");
}
else if (command.equals("quit"))
{
// TODO: shut down listen thread
break;
}
else if (command.equals("friends"))
{
}
else
{
System.out.println("Unrecognized Command! Type 'help' or '?' for help!");
}
}
System.exit(0);
}
public static void main(String args[])