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