messageServerP
Class Cache

java.lang.Object
  extended by messageServerP.Cache

public class Cache
extends java.lang.Object

Contains the MessageServer to User correspondence cached by the message server.

Author:
Marco Cornolti

Field Summary
private static Bootstrap bootstrap
          The Bootstrap Server (must be set at startup with setBootstrap(Bootstrap bs) )
static BSForMSInterface BSInterf
          The Bootstrap Server interface
private static java.util.Vector<MessageServer> messageServers
          The Message Servers.
private static java.util.Vector<java.util.List<User>> nonManagedUsers
          The list of users not managed by this MS but cached.
private static java.util.Vector<SafeMSForMSInterface> safeMSInterfs
          The Message Servers interfaces.
 
Constructor Summary
Cache()
           
 
Method Summary
private static int addEmptyMS(MessageServer msToAdd)
          Creates an empty message server with no users assigned to.
private static void addToCache(User user, MessageServer ms)
          Updates the cache adding a new corrispondence between a user and the message server the user is located in.
static void forwardMsg(Message msg)
          Forwards a message to another Message Server (recipient must not be local).
static Bootstrap getBS()
           
private static MessageServer getCachedMessageServer(User user)
          Obtains from the cache the Message Server a user is assigned to.
static MSForMSInterface getCachedMessageServerInterface(User user)
          Obtains from the cache the Message Server interface used to communicate with a user.
static int getCacheEntries()
          Returns the number of user-MS corrispondences contained in the cache.
static User getRemoteContact(User usr)
          Retrives informations about a gossip servlet connection (IP address and port) in order to let anyone contact him directely.
private static boolean refreshCache(User user)
          Refreshes the cache for a user whose message server is not right (or is not cached), sending a query to the Bootstrap Server.
static void remoteIsFriendOf(ManagedUser local, User remote)
          Checks if a remote user accepts a local user as a friend.
static MessageServer removeFromCache(User user)
          If exists, removes the correspondence between a user and the Message Server
static void sendUser(ManagedUser user, MessageServer ms)
          Moves a given user (including its message queue and contact lists) from the local message to another. does not remove the user from the local ms.
static void setBootstrap(Bootstrap bs)
          Sets the bootstrap to globally use and creates the interface.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BSInterf

public static BSForMSInterface BSInterf
The Bootstrap Server interface


bootstrap

private static Bootstrap bootstrap
The Bootstrap Server (must be set at startup with setBootstrap(Bootstrap bs) )


nonManagedUsers

private static java.util.Vector<java.util.List<User>> nonManagedUsers
The list of users not managed by this MS but cached. If a user is in the vector of index i, that means that the user is assigned to the message server messageServers.get(i) whose interface is MSInterfs.get(i)


safeMSInterfs

private static java.util.Vector<SafeMSForMSInterface> safeMSInterfs
The Message Servers interfaces.


messageServers

private static java.util.Vector<MessageServer> messageServers
The Message Servers.

Constructor Detail

Cache

public Cache()
Method Detail

setBootstrap

public static void setBootstrap(Bootstrap bs)
                         throws java.rmi.NotBoundException,
                                java.rmi.RemoteException
Sets the bootstrap to globally use and creates the interface.

Parameters:
bs - the bootstrap server
Throws:
java.rmi.NotBoundException
java.rmi.RemoteException

getBS

public static Bootstrap getBS()
Returns:
the currently used bootstrap server.

getCachedMessageServerInterface

public static MSForMSInterface getCachedMessageServerInterface(User user)
Obtains from the cache the Message Server interface used to communicate with a user. Takes O(M*logN) where N is the amount of cached User-MS corrispondence, and M is the amount of servers.

Parameters:
user - the user whose Message Server interface must be searched for.
Returns:
the user's Message Server interface, null if not found.

getCachedMessageServer

private static MessageServer getCachedMessageServer(User user)
Obtains from the cache the Message Server a user is assigned to.

Parameters:
user - the user whose Message Server must be searched for.
Returns:
the user's Message Server, null if not found.

forwardMsg

public static void forwardMsg(Message msg)
                       throws GenericErrorException,
                              java.rmi.RemoteException,
                              UserNotExistsException,
                              SystemErrorException,
                              NotAllowedException
Forwards a message to another Message Server (recipient must not be local). Updates the cache if needed.

Parameters:
msg - The message to forward.
Throws:
GenericErrorException - if a communication error occurred.
java.rmi.RemoteException - if the communication with a message server failed.
UserNotExistsException - if the Bootstrap server cannot tell the user MS.
SystemErrorException - if a system error occurred (Bootstrap thinks the user is assigned to a message server that does not actually manage it).
NotAllowedException - if the recipient does not accept messages from the sender.

remoteIsFriendOf

public static void remoteIsFriendOf(ManagedUser local,
                                    User remote)
                             throws GenericErrorException,
                                    UserNotExistsException,
                                    SystemErrorException,
                                    java.rmi.RemoteException
Checks if a remote user accepts a local user as a friend.

Parameters:
local - the local user
remote - the remote user (must not be local!)
Throws:
java.rmi.RemoteException - if a communication problem with another server occurred.
GenericErrorException
UserNotExistsException
SystemErrorException

getRemoteContact

public static User getRemoteContact(User usr)
                             throws java.rmi.RemoteException,
                                    UserNotExistsException,
                                    SystemErrorException,
                                    GenericErrorException
Retrives informations about a gossip servlet connection (IP address and port) in order to let anyone contact him directely.

Parameters:
usr - the user to get the contact of.
Returns:
the same user with the IP and port set.
Throws:
java.rmi.RemoteException - if a problem occurred while connecting to the bootstrap server.
UserNotExistsException - if the user does not exist in the system.
SystemErrorException - if a system error occurred (Bootstrap thinks the user is assigned to a message server that does not actually manage it).
GenericErrorException

refreshCache

private static boolean refreshCache(User user)
                             throws java.rmi.RemoteException,
                                    GenericErrorException
Refreshes the cache for a user whose message server is not right (or is not cached), sending a query to the Bootstrap Server.

Parameters:
user - the user whose cached message server is not right
Returns:
true if the message server has been updated, false if the bootstrap query returned the old message server (or the user does not exist).
Throws:
java.rmi.RemoteException
GenericErrorException

sendUser

public static void sendUser(ManagedUser user,
                            MessageServer ms)
                     throws GenericErrorException,
                            java.rmi.RemoteException,
                            AssignationNotAcceptedException
Moves a given user (including its message queue and contact lists) from the local message to another. does not remove the user from the local ms. NOTE: the managed user passed as argument is blocked and cannot receive messages until it gets unblocked.

Parameters:
user - the user to move
ms - the message server to move the user to
Throws:
GenericErrorException - if the message server could not be contacted.
java.rmi.RemoteException - if a communication problem occurred.
AssignationNotAcceptedException - if the server refused the user assignation.

addToCache

private static void addToCache(User user,
                               MessageServer ms)
                        throws GenericErrorException
Updates the cache adding a new corrispondence between a user and the message server the user is located in. The user must not be locally managed. Takes O(NlogN) where N is the number of users already assigned to ms.

Parameters:
user - the user
ms - the Message Server
Throws:
GenericErrorException

addEmptyMS

private static int addEmptyMS(MessageServer msToAdd)
                       throws GenericErrorException
Creates an empty message server with no users assigned to. If the message server already exists, returns its index.

Parameters:
msToAdd - the message server to add
Returns:
the new (or old) message server index in the vectors messageServer, MSInterfs, nonManagedUsers.
Throws:
GenericErrorException - if the message server could not be reached.

removeFromCache

public static MessageServer removeFromCache(User user)
If exists, removes the correspondence between a user and the Message Server

Parameters:
user - the user to delete from cache
Returns:
the old Message Server the user were associated to

getCacheEntries

public static int getCacheEntries()
Returns the number of user-MS corrispondences contained in the cache.

Returns:
the number of user-MS corrispondences contained in the cache.