updated ChatClient, added run scripts and policies
git-svn-id: svn://anubis/gvsu@141 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
b500ee6c5d
commit
b157ef4843
@ -1,24 +1,127 @@
|
|||||||
|
|
||||||
import java.rmi.registry.LocateRegistry;
|
import java.rmi.registry.LocateRegistry;
|
||||||
import java.rmi.registry.Registry;
|
import java.rmi.registry.Registry;
|
||||||
import java.math.BigDecimal;
|
import java.util.*;
|
||||||
|
|
||||||
public class ChatClient
|
public class ChatClient
|
||||||
{
|
{
|
||||||
|
private PresenceService myPServ;
|
||||||
|
private String myUserName;
|
||||||
|
private Thread myListenThread;
|
||||||
|
private ChatClientListener myListener;
|
||||||
|
|
||||||
|
private class ChatClientListener implements Runnable
|
||||||
|
{
|
||||||
|
ServerSocket mySocket;
|
||||||
|
|
||||||
|
public ChatClientListener()
|
||||||
|
{
|
||||||
|
mySocket = new ServerSocket(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort()
|
||||||
|
{
|
||||||
|
return mySocket.getLocalPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost()
|
||||||
|
{
|
||||||
|
return mySocket.getInetAddress().getHostName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
Socket s;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
s = mySocket.accept();
|
||||||
|
// TODO: create a handler thread
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChatClient(PresenceService pserv, String userName)
|
||||||
|
{
|
||||||
|
myPServ = pserv;
|
||||||
|
myUserName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
myListener = new ChatClientListener();
|
||||||
|
RegistrationInfo regInfo =
|
||||||
|
new RegistrationInfo(myUserName,
|
||||||
|
myListener.getHost(),
|
||||||
|
myListener.getPort(),
|
||||||
|
true);
|
||||||
|
try {
|
||||||
|
myPServ.register(regInfo);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Exception caught when registering!");
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Start a thread to listen for connections in */
|
||||||
|
myListenThread = new Thread(myListener);
|
||||||
|
myListenThread.start();
|
||||||
|
|
||||||
|
mainloop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mainloop()
|
||||||
|
{
|
||||||
|
BufferedReader br = new BufferedReader(
|
||||||
|
new InputStreamReader(System.in));
|
||||||
|
String line;
|
||||||
|
while ( (line = br.readLine()) != null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String args[])
|
public static void main(String args[])
|
||||||
{
|
{
|
||||||
|
String user;
|
||||||
|
String host = "localhost";
|
||||||
|
int port = 1099;
|
||||||
|
|
||||||
|
if (args.length < 1)
|
||||||
|
{
|
||||||
|
System.err.println("Usage: ChatClient <user> [host[:port]]");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
user = args[0];
|
||||||
|
|
||||||
|
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)
|
if (System.getSecurityManager() == null)
|
||||||
{
|
{
|
||||||
System.setSecurityManager(new SecurityManager());
|
System.setSecurityManager(new SecurityManager());
|
||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Registry registry = LocateRegistry.getRegistry(args[0]);
|
Registry registry = LocateRegistry.getRegistry(host, port);
|
||||||
PresenceService pserv = (PresenceService)
|
PresenceService pserv = (PresenceService)
|
||||||
registry.lookup("PresenceService");
|
registry.lookup("PresenceService");
|
||||||
Pi task = new Pi(Integer.parseInt(args[1]));
|
if (pserv == null)
|
||||||
BigDecimal pi = comp.executeTask(task);
|
{
|
||||||
System.out.println(pi);
|
System.err.println("Error: Could not connect to presence service");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ChatClient cc = new ChatClient(pserv, user);
|
||||||
|
cc.run();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,8 @@ import java.rmi.server.UnicastRemoteObject;
|
|||||||
|
|
||||||
public class PresenceServiceImpl implements PresenceService
|
public class PresenceServiceImpl implements PresenceService
|
||||||
{
|
{
|
||||||
private HashMap<String, RegistrationInfo> myRegisteredUsers;
|
private HashMap<String, RegistrationInfo> myRegisteredUsers =
|
||||||
|
new HashMap<String, RegistrationInfo>();
|
||||||
|
|
||||||
public PresenceServiceImpl()
|
public PresenceServiceImpl()
|
||||||
{
|
{
|
||||||
@ -17,11 +18,13 @@ public class PresenceServiceImpl implements PresenceService
|
|||||||
{
|
{
|
||||||
if (!myRegisteredUsers.containsKey(reg.getUserName()))
|
if (!myRegisteredUsers.containsKey(reg.getUserName()))
|
||||||
myRegisteredUsers.put(reg.getUserName(), reg);
|
myRegisteredUsers.put(reg.getUserName(), reg);
|
||||||
|
System.out.println("register(): " + reg.getUserName() + ", " + reg.getHost() + ", " + reg.getPort() + ", " + reg.getStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unregister(String userName)
|
public void unregister(String userName)
|
||||||
{
|
{
|
||||||
myRegisteredUsers.remove(userName);
|
myRegisteredUsers.remove(userName);
|
||||||
|
System.out.println("unregister(): " + userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RegistrationInfo lookup(String name)
|
public RegistrationInfo lookup(String name)
|
||||||
|
3
cs656/lab2/src/client.policy
Normal file
3
cs656/lab2/src/client.policy
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
grant codeBase "file:/home/holtropj/cs656/lab2/src/" {
|
||||||
|
permission java.security.AllPermission;
|
||||||
|
};
|
4
cs656/lab2/src/run_client.sh
Executable file
4
cs656/lab2/src/run_client.sh
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
java \
|
||||||
|
-cp /home/holtropj/cs656/lab2/src \
|
||||||
|
-Djava.security.policy=client.policy \
|
||||||
|
ChatClient localhost
|
7
cs656/lab2/src/run_server.sh
Executable file
7
cs656/lab2/src/run_server.sh
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
rmiregistry &
|
||||||
|
java \
|
||||||
|
-cp /home/holtropj/cs656/lab2/src \
|
||||||
|
-Djava.rmi.server.hostname=localhost \
|
||||||
|
-Djava.security.policy=server.policy \
|
||||||
|
PresenceServiceImpl
|
||||||
|
pkill rmiregistry
|
3
cs656/lab2/src/server.policy
Normal file
3
cs656/lab2/src/server.policy
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
grant codeBase "file:/home/holtropj/cs656/lab2/src/" {
|
||||||
|
permission java.security.AllPermission;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user