filled out ChatClient
git-svn-id: svn://anubis/gvsu@143 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
005fb6efdd
commit
5e40fee749
@ -2,6 +2,8 @@
|
|||||||
import java.rmi.registry.LocateRegistry;
|
import java.rmi.registry.LocateRegistry;
|
||||||
import java.rmi.registry.Registry;
|
import java.rmi.registry.Registry;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
public class ChatClient
|
public class ChatClient
|
||||||
{
|
{
|
||||||
@ -17,7 +19,16 @@ public class ChatClient
|
|||||||
|
|
||||||
public ChatClientListener()
|
public ChatClientListener()
|
||||||
{
|
{
|
||||||
mySocket = new ServerSocket(0);
|
try
|
||||||
|
{
|
||||||
|
mySocket = new ServerSocket(0);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
System.err.println("Error creating listener thread!");
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPort()
|
public int getPort()
|
||||||
@ -33,12 +44,48 @@ public class ChatClient
|
|||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
Socket s;
|
Socket s;
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
s = mySocket.accept();
|
try
|
||||||
// TODO: create a handler thread
|
{
|
||||||
}
|
s = mySocket.accept();
|
||||||
|
Thread cclt = new Thread(new ChatClientListenerThread(s));
|
||||||
|
cclt.start();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ChatClientListenerThread implements Runnable
|
||||||
|
{
|
||||||
|
Socket mySocket;
|
||||||
|
|
||||||
|
public ChatClientListenerThread(Socket s)
|
||||||
|
{
|
||||||
|
mySocket = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
InputStream is = mySocket.getInputStream();
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
||||||
|
while (!mySocket.isClosed())
|
||||||
|
{
|
||||||
|
/* read and print a message from a peer */
|
||||||
|
String in = br.readLine();
|
||||||
|
System.out.println(in);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatClient(PresenceService pserv, String userName)
|
public ChatClient(PresenceService pserv, String userName)
|
||||||
@ -96,47 +143,148 @@ public class ChatClient
|
|||||||
BufferedReader br = new BufferedReader(
|
BufferedReader br = new BufferedReader(
|
||||||
new InputStreamReader(System.in));
|
new InputStreamReader(System.in));
|
||||||
String line;
|
String line;
|
||||||
while ( (line = br.readLine()) != null)
|
try
|
||||||
{
|
{
|
||||||
line = line.trim();
|
for (;;)
|
||||||
if (line.equals(""))
|
{
|
||||||
continue;
|
System.out.print("> ");
|
||||||
String command, rest;
|
line = br.readLine();
|
||||||
int spaceIndex = line.indexOf(' ');
|
if (line == null)
|
||||||
if (spaceIndex > 0)
|
break;
|
||||||
{
|
line = line.trim();
|
||||||
command = line.substring(0, spaceIndex);
|
if (line.equals(""))
|
||||||
rest = line.substring(spaceIndex + 1);
|
continue;
|
||||||
rest = rest.trim();
|
String command, rest;
|
||||||
}
|
int spaceIndex = line.indexOf(' ');
|
||||||
else
|
if (spaceIndex > 0)
|
||||||
{
|
{
|
||||||
command = line;
|
command = line.substring(0, spaceIndex);
|
||||||
rest = "";
|
rest = line.substring(spaceIndex + 1);
|
||||||
}
|
rest = rest.trim();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
command = line;
|
||||||
|
rest = "";
|
||||||
|
}
|
||||||
|
|
||||||
if (command.equals("help") || command.equals("?"))
|
if (command.equals("help") || command.equals("?"))
|
||||||
{
|
{
|
||||||
System.out.println("Commands:\n");
|
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");
|
||||||
System.out.println(" friends - view a list of all peers on the system");
|
System.out.println(" talk {username} {message} - send message to username");
|
||||||
}
|
System.out.println(" broadcast {message} - send message to every available user");
|
||||||
else if (command.equals("quit"))
|
System.out.println(" busy - set status to busy/away");
|
||||||
{
|
System.out.println(" available - set status to busy/away");
|
||||||
// TODO: shut down listen thread
|
System.out.println(" exit - disconnect and exit program");
|
||||||
break;
|
}
|
||||||
}
|
else if (command.equals("friends"))
|
||||||
else if (command.equals("friends"))
|
{
|
||||||
{
|
RegistrationInfo[] friends = myPServ.listRegisteredUsers();
|
||||||
}
|
System.out.println("Registered Users:");
|
||||||
else
|
for (RegistrationInfo ri : friends)
|
||||||
{
|
{
|
||||||
System.out.println("Unrecognized Command! Type 'help' or '?' for help!");
|
System.out.print(ri.getUserName());
|
||||||
}
|
System.out.print(" [");
|
||||||
}
|
System.out.print(ri.getStatus() ? "available" : "busy");
|
||||||
|
System.out.println("]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (command.equals("talk"))
|
||||||
|
{
|
||||||
|
String user = "";
|
||||||
|
int idx;
|
||||||
|
if ((idx = rest.indexOf(' ')) > 0)
|
||||||
|
{
|
||||||
|
user = rest.substring(0, idx);
|
||||||
|
RegistrationInfo ri = myPServ.lookup(user);
|
||||||
|
String message = rest.substring(idx + 1);
|
||||||
|
sendUserMessage(user, message);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.out.println("Syntax: talk {user} {message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (command.equals("broadcast"))
|
||||||
|
{
|
||||||
|
RegistrationInfo[] ris = myPServ.listRegisteredUsers();
|
||||||
|
for (RegistrationInfo ri : ris)
|
||||||
|
{
|
||||||
|
if (!ri.getUserName().equals(myUserName))
|
||||||
|
{
|
||||||
|
sendUserMessage(ri.getUserName(), rest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (command.equals("busy"))
|
||||||
|
{
|
||||||
|
if (myRegistrationInfo.getStatus())
|
||||||
|
{
|
||||||
|
myRegistrationInfo = new RegistrationInfo(
|
||||||
|
myRegistrationInfo.getUserName(),
|
||||||
|
myRegistrationInfo.getHost(),
|
||||||
|
myRegistrationInfo.getPort(),
|
||||||
|
false
|
||||||
|
);
|
||||||
|
unregister();
|
||||||
|
register();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (command.equals("available"))
|
||||||
|
{
|
||||||
|
if (!myRegistrationInfo.getStatus())
|
||||||
|
{
|
||||||
|
myRegistrationInfo = new RegistrationInfo(
|
||||||
|
myRegistrationInfo.getUserName(),
|
||||||
|
myRegistrationInfo.getHost(),
|
||||||
|
myRegistrationInfo.getPort(),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
unregister();
|
||||||
|
register();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (command.equals("exit"))
|
||||||
|
{
|
||||||
|
myListenThread.interrupt();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.out.println("Unrecognized Command! Type 'help' or '?' for help!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendUserMessage(String user, String message)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
RegistrationInfo ri = myPServ.lookup(user);
|
||||||
|
if (ri != null)
|
||||||
|
{
|
||||||
|
Socket s = new Socket(InetAddress.getByName(ri.getHost()),
|
||||||
|
ri.getPort());
|
||||||
|
OutputStream os = s.getOutputStream();
|
||||||
|
message = myUserName + ": " + message;
|
||||||
|
os.write(message.getBytes());
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
System.err.println("Error sending message");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String args[])
|
public static void main(String args[])
|
||||||
{
|
{
|
||||||
String user;
|
String user;
|
||||||
@ -150,6 +298,7 @@ public class ChatClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
user = args[0];
|
user = args[0];
|
||||||
|
user = user.replace(' ', '_');
|
||||||
|
|
||||||
if (args.length >= 2)
|
if (args.length >= 2)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user