diff --git a/cs656/lab3/ChatClient.java b/cs656/lab3/ChatClient.java index f3f8283..82644a6 100644 --- a/cs656/lab3/ChatClient.java +++ b/cs656/lab3/ChatClient.java @@ -287,6 +287,15 @@ public class ChatClient e.printStackTrace(); } unregister(); + + try + { + myPServ.leave(); /* Leave the Chord network */ + } + catch (Exception e) + { + e.printStackTrace(); + } System.exit(0); } @@ -343,6 +352,13 @@ public class ChatClient // Load the Chord properties files PropertiesLoader.loadPropertyFile(); + + ChordClient chordClient = new ChordClient(host, master); + + PresenceService pServ = new PresenceServiceImpl(chordClient); + + ChatClient chatClient = new ChatClient(pServ, user); + chatClient.run(); } private static void usage() diff --git a/cs656/lab3/PresenceServiceImpl.java b/cs656/lab3/PresenceServiceImpl.java index 3389e69..b92aef4 100644 --- a/cs656/lab3/PresenceServiceImpl.java +++ b/cs656/lab3/PresenceServiceImpl.java @@ -4,6 +4,9 @@ // 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. @@ -20,22 +23,54 @@ public class PresenceServiceImpl implements PresenceService /* 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 vals = myChordClient.chord().retrieve(userNameKey); + Iterator 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(); + } } }