only send regular talk messages to users if they are available
git-svn-id: svn://anubis/gvsu@160 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
c7e988e92b
commit
9d9f1cbd76
@ -18,22 +18,22 @@ import java.net.*;
|
||||
*/
|
||||
public class ChatClient
|
||||
{
|
||||
private PresenceService myPServ;
|
||||
private String myUserName;
|
||||
private Thread myListenThread;
|
||||
private ChatClientListener myListener;
|
||||
private RegistrationInfo myRegistrationInfo;
|
||||
private PresenceService myPServ;
|
||||
private String myUserName;
|
||||
private Thread myListenThread;
|
||||
private ChatClientListener myListener;
|
||||
private RegistrationInfo myRegistrationInfo;
|
||||
|
||||
/**
|
||||
* This class provides a thread to listen for incoming connections.
|
||||
*/
|
||||
private class ChatClientListener implements Runnable
|
||||
{
|
||||
ServerSocket mySocket;
|
||||
private class ChatClientListener implements Runnable
|
||||
{
|
||||
ServerSocket mySocket;
|
||||
|
||||
public ChatClientListener()
|
||||
{
|
||||
try
|
||||
public ChatClientListener()
|
||||
{
|
||||
try
|
||||
{
|
||||
/* Just listen on any available TCP port */
|
||||
mySocket = new ServerSocket(0);
|
||||
@ -44,23 +44,23 @@ public class ChatClient
|
||||
e.printStackTrace();
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getPort()
|
||||
{
|
||||
return mySocket.getLocalPort();
|
||||
}
|
||||
public int getPort()
|
||||
{
|
||||
return mySocket.getLocalPort();
|
||||
}
|
||||
|
||||
public String getHost()
|
||||
{
|
||||
return mySocket.getInetAddress().getHostName();
|
||||
}
|
||||
public String getHost()
|
||||
{
|
||||
return mySocket.getInetAddress().getHostName();
|
||||
}
|
||||
|
||||
/* called when the thread is first scheduled */
|
||||
public void run()
|
||||
{
|
||||
Socket s;
|
||||
/* wait indefinitely for new connections */
|
||||
public void run()
|
||||
{
|
||||
Socket s;
|
||||
/* wait indefinitely for new connections */
|
||||
for (;;)
|
||||
{
|
||||
try
|
||||
@ -73,14 +73,14 @@ public class ChatClient
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class provides a listener for any connections that come in.
|
||||
* All it does is print any received chat messages from the remote
|
||||
* user to this user's screen.
|
||||
*/
|
||||
public class ChatClientListenerThread implements Runnable
|
||||
public class ChatClientListenerThread implements Runnable
|
||||
{
|
||||
Socket mySocket;
|
||||
|
||||
@ -111,18 +111,18 @@ public class ChatClient
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* construct the main ChatClient object */
|
||||
public ChatClient(PresenceService pserv, String userName)
|
||||
{
|
||||
myPServ = pserv;
|
||||
myUserName = userName;
|
||||
}
|
||||
public ChatClient(PresenceService pserv, String userName)
|
||||
{
|
||||
myPServ = pserv;
|
||||
myUserName = userName;
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
myListener = new ChatClientListener();
|
||||
myRegistrationInfo =
|
||||
@ -141,46 +141,46 @@ public class ChatClient
|
||||
return;
|
||||
}
|
||||
|
||||
/* Start a thread to listen for connections in */
|
||||
myListenThread = new Thread(myListener);
|
||||
myListenThread.start();
|
||||
/* Start a thread to listen for connections in */
|
||||
myListenThread = new Thread(myListener);
|
||||
myListenThread.start();
|
||||
|
||||
mainloop();
|
||||
}
|
||||
mainloop();
|
||||
}
|
||||
|
||||
/* call the register function in the presence service */
|
||||
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 register()
|
||||
{
|
||||
try {
|
||||
myPServ.register(myRegistrationInfo);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception caught when registering!");
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* call the unregister function in the presence service */
|
||||
private boolean unregister()
|
||||
{
|
||||
try {
|
||||
myPServ.unregister(myUserName);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception caught when unregistering!");
|
||||
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;
|
||||
}
|
||||
|
||||
/* this is the main wait-for-input-and-respond loop */
|
||||
private void mainloop()
|
||||
{
|
||||
BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(System.in));
|
||||
String line;
|
||||
try
|
||||
private void mainloop()
|
||||
{
|
||||
BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(System.in));
|
||||
String line;
|
||||
try
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
@ -238,8 +238,16 @@ public class ChatClient
|
||||
{
|
||||
user = rest.substring(0, idx);
|
||||
RegistrationInfo ri = myPServ.lookup(user);
|
||||
String message = rest.substring(idx + 1);
|
||||
sendUserMessage(user, message);
|
||||
/* only send a message to available users */
|
||||
if (ri.getStatus())
|
||||
{
|
||||
String message = rest.substring(idx + 1);
|
||||
sendUserMessage(user, message);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println(user + " is busy!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -308,8 +316,8 @@ public class ChatClient
|
||||
e.printStackTrace();
|
||||
}
|
||||
unregister();
|
||||
System.exit(0);
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
/* a useful subroutine to break out the task of sending a
|
||||
* message to a particular user */
|
||||
@ -338,31 +346,31 @@ public class ChatClient
|
||||
/* the main method is invoked when the ChatClient is first started */
|
||||
public static void main(String args[])
|
||||
{
|
||||
String user;
|
||||
String host = "localhost"; /* default host */
|
||||
int port = 1099; /* default port */
|
||||
String user;
|
||||
String host = "localhost"; /* default host */
|
||||
int port = 1099; /* default port */
|
||||
|
||||
if (args.length < 1)
|
||||
{
|
||||
System.err.println("Usage: ChatClient <user> [host[:port]]");
|
||||
return;
|
||||
}
|
||||
if (args.length < 1)
|
||||
{
|
||||
System.err.println("Usage: ChatClient <user> [host[:port]]");
|
||||
return;
|
||||
}
|
||||
|
||||
user = args[0];
|
||||
user = user.replace(' ', '_');
|
||||
user = args[0];
|
||||
user = user.replace(' ', '_');
|
||||
|
||||
if (args.length >= 2)
|
||||
{
|
||||
StringTokenizer st = new StringTokenizer(args[1], ":");
|
||||
if (st.hasMoreTokens())
|
||||
{
|
||||
host = st.nextToken();
|
||||
if (st.hasMoreTokens())
|
||||
{
|
||||
port = Integer.parseInt(st.nextToken());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (args.length >= 2)
|
||||
{
|
||||
StringTokenizer st = new StringTokenizer(args[1], ":");
|
||||
if (st.hasMoreTokens())
|
||||
{
|
||||
host = st.nextToken();
|
||||
if (st.hasMoreTokens())
|
||||
{
|
||||
port = Integer.parseInt(st.nextToken());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (System.getSecurityManager() == null)
|
||||
{
|
||||
@ -373,13 +381,13 @@ public class ChatClient
|
||||
Registry registry = LocateRegistry.getRegistry(host, port);
|
||||
PresenceService pserv = (PresenceService)
|
||||
registry.lookup("PresenceService");
|
||||
if (pserv == null)
|
||||
{
|
||||
System.err.println("Error: Could not connect to presence service");
|
||||
return;
|
||||
}
|
||||
ChatClient cc = new ChatClient(pserv, user);
|
||||
cc.run();
|
||||
if (pserv == null)
|
||||
{
|
||||
System.err.println("Error: Could not connect to presence service");
|
||||
return;
|
||||
}
|
||||
ChatClient cc = new ChatClient(pserv, user);
|
||||
cc.run();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user