gvsu/cs656/lab3/PresenceServiceImpl.java
josh e301f20c16 updated ChatClient.java, PresenceServiceImpl.java
git-svn-id: svn://anubis/gvsu@220 45c1a28c-8058-47b2-ae61-ca45b979098e
2008-10-27 22:54:08 +00:00

77 lines
1.9 KiB
Java

// Author: Josh Holtrop
// Date: 2008-10-30
// CS656
// Lab Excersize 3
import java.util.*;
import java.io.Serializable;
/**
* This class implements the PresenceService interface using Chord.
* It provides a presence service for the chat system.
*/
public class PresenceServiceImpl implements PresenceService
{
ChordClient myChordClient;
public PresenceServiceImpl(ChordClient chordClient)
{
myChordClient = chordClient;
}
/* a user calls this method to register with the presence service */
public void register(RegistrationInfo reg)
{
StringKey userName = new StringKey(reg.getUserName());
try {
myChordClient.chord().insert(userName, reg);
} catch (Exception e) {
e.printStackTrace();
}
}
/* a user calls this method to unregister with the presence service */
public void unregister(String userName)
{
StringKey userNameKey = new StringKey(userName);
try {
myChordClient.chord().remove(userNameKey, lookup(userName));
} catch (Exception e) {
e.printStackTrace();
}
}
/* a user calls this method to get the registration information
* for another user by name */
public RegistrationInfo lookup(String name)
{
StringKey userNameKey = new StringKey(name);
try {
Set<Serializable> vals = myChordClient.chord().retrieve(userNameKey);
Iterator<Serializable> it = vals.iterator();
if (it.hasNext())
{
RegistrationInfo data = (RegistrationInfo) it.next();
return data;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/* called when a user wishes to leave the network */
public void leave()
{
try
{
myChordClient.chord().leave();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}