messageServerP
Class Communication

java.lang.Object
  extended by java.rmi.server.RemoteObject
      extended by java.rmi.server.RemoteServer
          extended by java.rmi.server.UnicastRemoteObject
              extended by messageServerP.Communication
All Implemented Interfaces:
MSForBSInterface, MSForMSInterface, MSForServletInterface, java.io.Serializable, java.rmi.Remote

public class Communication
extends java.rmi.server.UnicastRemoteObject
implements MSForBSInterface, MSForServletInterface, MSForMSInterface, java.io.Serializable

The message server Communication class implements all the communications interfaces offered by the message server with RMI. It also implements some methods using other internet communication paradigms.
This class is security critical because it contains all the methods accessible by other hosts on the Internet

one line to give the program's name and an idea of what it does. Copyright (C) 2005 Marco Cornolti (cornolti@cli.di.unipi.it)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author:
Marco Cornolti
See Also:
Serialized Form

Field Summary
private static long serialVersionUID
          Gossip Message Server version
 
Fields inherited from class java.rmi.server.RemoteObject
ref
 
Constructor Summary
protected Communication()
          Constructor for the remote object.
 
Method Summary
 void addToFriends(User user, java.lang.String userPw, User userToAdd)
          Used by a servlet to tell the message server to add another user in his contact list as friend.
 void assign(java.lang.String MSPw, User user, java.lang.String password)
          Assigns a new user to the message server.
 void assignMovedUser(ManagedUser user)
          Assigns a user management (sending all its datas) to the message server.
 void blockAssignation(java.lang.String MSPw, boolean status)
          Puts the MS in the 'blocked' or 'unblocked' status, depending on the boolean status given by argument.
 void blockContact(User user, java.lang.String pw, User userToBlock)
          Marks userToBlock in the user's contact list as blocked, so that no more messages from that user are accepted.
 void changePassword(User user, java.lang.String oldPw, java.lang.String newPw)
          Changes the user password.
 void deleteDownloadedMsgs(User user, java.lang.String pw)
          Deletes the messages that are already downloaded by a User.
 void deleteUserAdmin(User nick, java.lang.String MSpw)
          Deletes a user.
 java.util.Vector<Message> downloadMsgs(User user, java.lang.String pw)
          Downloads the messages stored in the Message server for a user.
 void forwardMessage(Message msgRef)
          Forwards a message.
 ContactList getContactList(User user, java.lang.String pw)
          Returns user list of contacts.
 User getManagedUserContact(User user)
          Gets a User IP address and TCP port to directely connect to its servlet.
 Statistics getStatistics(java.lang.String MSPw)
          Returns the statistics of the message server.
 User getUserContact(User user)
          Gets a User IP address and TCP port to directely connect to its servlet.
 java.util.Vector<User> getUsersList(java.lang.String MSPw)
          Returns the list of users managed by the Message Server.
 boolean inListOfUserContains(User listUser, User user)
          Checks whether or not a user (U1) accepts messages sent from another user (U2)
 void moveUser(java.lang.String MSPw, User user, MessageServer ms)
          Moves a user assignation from the message server to the specified one.
 void removeContact(User user, java.lang.String pw, User userToRemove)
          Removes userToRemove from user's contact list, so that no more messages from that user are accepted.
static void sendLoad()
          Sends to the bootstrap the current load via a UDP packet.
 void setState(User user, java.lang.String pw, boolean state)
          Sets a user state.
 void shutdown(java.lang.String MSPw)
          Shuts down the MS server.
 boolean testPassword(java.lang.String pw)
          Tests a message server administration password.
 boolean testPorts(int port, java.net.InetAddress ip)
          Tests the servlet port trying to connect to them.
 void testPresence()
          Tests the presence of a Message Server
 
Methods inherited from class java.rmi.server.UnicastRemoteObject
clone, exportObject, exportObject, exportObject, unexportObject
 
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
 
Methods inherited from class java.rmi.server.RemoteObject
equals, getRef, hashCode, toString, toStub
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Gossip Message Server version

See Also:
Constant Field Values
Constructor Detail

Communication

protected Communication()
                 throws java.rmi.RemoteException
Constructor for the remote object.

Throws:
java.rmi.RemoteException - if the remote object could not be created.
Method Detail

testPresence

public void testPresence()
Description copied from interface: MSForBSInterface
Tests the presence of a Message Server

Specified by:
testPresence in interface MSForBSInterface

deleteUserAdmin

public void deleteUserAdmin(User nick,
                            java.lang.String MSpw)
                     throws WrongPasswordException,
                            BlockedException,
                            UserNotManagedException
Description copied from interface: MSForBSInterface
Deletes a user. This method is invoked by the global administrator.

Specified by:
deleteUserAdmin in interface MSForBSInterface
Parameters:
nick - the user to delete.
MSpw - the Message Server administration password.
Throws:
WrongPasswordException - if the password is not right.
BlockedException - if the MS is currently blocked.
UserNotManagedException - if the specified user is not managed by the Message server.

blockAssignation

public void blockAssignation(java.lang.String MSPw,
                             boolean status)
                      throws WrongPasswordException
Description copied from interface: MSForBSInterface
Puts the MS in the 'blocked' or 'unblocked' status, depending on the boolean status given by argument. If the MS is blocked, no new assignations are accepted. The MS must be put in bloked status when their users have to be transferred to another MS before shutting down.

Specified by:
blockAssignation in interface MSForBSInterface
Parameters:
MSPw - the MS administration password.
status - true if the status must be set to 'blocked', false if the status must be set to 'unblocked'.
Throws:
WrongPasswordException - if the password is wrong.

moveUser

public void moveUser(java.lang.String MSPw,
                     User user,
                     MessageServer ms)
              throws WrongPasswordException,
                     UserNotManagedException,
                     java.rmi.RemoteException,
                     AssignationNotAcceptedException,
                     GenericErrorException
Description copied from interface: MSForBSInterface
Moves a user assignation from the message server to the specified one.

Specified by:
moveUser in interface MSForBSInterface
Parameters:
MSPw - the message server password.
user - the user to move.
ms - the message server to move the user to.
Throws:
WrongPasswordException - id the MS admin password is wrong.
UserNotManagedException - if the user is not managed by this MS (or does not exists).
java.rmi.RemoteException - if a communication problem occurs.
AssignationNotAcceptedException - if the MS the user was wanted to be moved to refused the assignation (i.e. is blocked).
GenericErrorException - if a communication problem with the second MS occurred.

getStatistics

public Statistics getStatistics(java.lang.String MSPw)
                         throws WrongPasswordException
Description copied from interface: MSForBSInterface
Returns the statistics of the message server.

Specified by:
getStatistics in interface MSForBSInterface
Parameters:
MSPw - the MS password
Returns:
an object containing the statistics of the message server.
Throws:
WrongPasswordException - if the administration password is wrong.

shutdown

public void shutdown(java.lang.String MSPw)
              throws UsersStillPresentException,
                     WrongPasswordException,
                     BlockedException
Description copied from interface: MSForBSInterface
Shuts down the MS server. Must be invoked when no users are managed by the MS and the MS is blocked.

Specified by:
shutdown in interface MSForBSInterface
Parameters:
MSPw - the Message Server password.
Throws:
UsersStillPresentException - if the Message Server still manages some users.
WrongPasswordException - if the password is wrong.
BlockedException - if the Message Server is not blocked.

assign

public void assign(java.lang.String MSPw,
                   User user,
                   java.lang.String password)
            throws BlockedException,
                   WrongPasswordException
Description copied from interface: MSForBSInterface
Assigns a new user to the message server.

Specified by:
assign in interface MSForBSInterface
Parameters:
MSPw - the MS password.
user - the user to add
password - the user password
Throws:
BlockedException - if the server is blocked and does not accept new users.
WrongPasswordException - if the Message Server Password is wrong.

getUsersList

public java.util.Vector<User> getUsersList(java.lang.String MSPw)
                                    throws WrongPasswordException
Description copied from interface: MSForBSInterface
Returns the list of users managed by the Message Server. (Used only for debug purposals?)

Specified by:
getUsersList in interface MSForBSInterface
Parameters:
MSPw - the MS password
Returns:
a vector containing the managed users or null if the password is wrong.
Throws:
WrongPasswordException - if the password is wrong.

testPassword

public boolean testPassword(java.lang.String pw)
Description copied from interface: MSForBSInterface
Tests a message server administration password.

Specified by:
testPassword in interface MSForBSInterface
Parameters:
pw - the password to check
Returns:
true if the password is right, false otherwise.

addToFriends

public void addToFriends(User user,
                         java.lang.String userPw,
                         User userToAdd)
                  throws UserNotManagedException,
                         WrongPasswordException,
                         UserBlockedException,
                         UserAlreadyInsertedException
Description copied from interface: MSForServletInterface
Used by a servlet to tell the message server to add another user in his contact list as friend.

Specified by:
addToFriends in interface MSForServletInterface
Parameters:
user - the invoking user.
userPw - the invoking user password.
userToAdd - the user to add.
Throws:
UserNotManagedException - if the user is not managed by the local MS.
WrongPasswordException - if the password is not right.
UserBlockedException - if the user is blocked.
UserAlreadyInsertedException - if the user is already contained in the incoming list.

downloadMsgs

public java.util.Vector<Message> downloadMsgs(User user,
                                              java.lang.String pw)
                                       throws UserNotManagedException,
                                              UserBlockedException,
                                              WrongPasswordException
Description copied from interface: MSForServletInterface
Downloads the messages stored in the Message server for a user. After calling this method, the servlet has to call deleteDownloadedMsgs to free the server deleting old messages.

Specified by:
downloadMsgs in interface MSForServletInterface
Parameters:
user - the user whose messages are wanted.
pw - the user's password
Returns:
An empty Vector if the user has no messages. A vector containing the user's messages otherwise.
Throws:
UserNotManagedException - if the user is not managed by this message server.
UserBlockedException - if the user is blocked (i.e. to be moved).
WrongPasswordException - if the given password is wrong.

setState

public void setState(User user,
                     java.lang.String pw,
                     boolean state)
              throws UserNotManagedException,
                     WrongPasswordException,
                     GenericErrorException,
                     UserBlockedException
Description copied from interface: MSForServletInterface
Sets a user state. Must be invoked whenever the user gets on-line or is about to go off-line.

Specified by:
setState in interface MSForServletInterface
Parameters:
user - the user
pw - the user password
state - true if the state is on-line, false if the state is off-line
Throws:
UserNotManagedException - if the user is not managed by the MS.
WrongPasswordException - if the password is wrong.
GenericErrorException - if another error occurred, i.e. the server could not define the servlet ip address.
UserBlockedException - if the user is blocked.

forwardMessage

public void forwardMessage(Message msgRef)
                    throws NotAllowedException,
                           UserNotManagedException,
                           java.rmi.RemoteException,
                           GenericErrorException,
                           UserNotExistsException,
                           SystemErrorException
Description copied from interface: MSForServletInterface
Forwards a message. The message recipient or the message sender must be managed by the MS. If the recipient is managed by the MS, the message gets stored and is ready to be downloaded. If the recipient is not local but the sender is, the MS forwards the message to the correct MS.

Specified by:
forwardMessage in interface MSForMSInterface
Specified by:
forwardMessage in interface MSForServletInterface
Parameters:
msgRef - the message to forward.
Throws:
NotAllowedException - if the recipient user does have the sender in its blockedlist.
UserNotManagedException - if the MS does not manage the recipient nor the sender.
java.rmi.RemoteException - if a communication problem occurs.
GenericErrorException - if another error has occurred.
UserNotExistsException - if the recipient does not exist in the whole system.
SystemErrorException - if a system error occurred.

getContactList

public ContactList getContactList(User user,
                                  java.lang.String pw)
                           throws UserNotManagedException,
                                  WrongPasswordException,
                                  UserBlockedException
Description copied from interface: MSForServletInterface
Returns user list of contacts.

Specified by:
getContactList in interface MSForServletInterface
Parameters:
user - the user
pw - the user password
Returns:
the contact list.
Throws:
UserNotManagedException - if the user is not managed by this message server.
WrongPasswordException - if the user's password is wrong.
UserBlockedException - if the user is blocked.

changePassword

public void changePassword(User user,
                           java.lang.String oldPw,
                           java.lang.String newPw)
                    throws UserNotManagedException,
                           WrongPasswordException,
                           UserBlockedException
Description copied from interface: MSForServletInterface
Changes the user password.

Specified by:
changePassword in interface MSForServletInterface
Parameters:
user - the user
oldPw - the old password
newPw - the new password to set
Throws:
UserNotManagedException - if the user is not managed by the MS.
WrongPasswordException - if the old password is wrong.
UserBlockedException - if the user is blocked.

testPorts

public boolean testPorts(int port,
                         java.net.InetAddress ip)
Description copied from interface: MSForServletInterface
Tests the servlet port trying to connect to them.

Specified by:
testPorts in interface MSForServletInterface
Parameters:
port - the port to test.
ip - the servlet ip.
Returns:
true if the test was correctly terminated, false otherwise.

inListOfUserContains

public boolean inListOfUserContains(User listUser,
                                    User user)
                             throws UserNotManagedException
Description copied from interface: MSForMSInterface
Checks whether or not a user (U1) accepts messages sent from another user (U2)

Specified by:
inListOfUserContains in interface MSForMSInterface
Parameters:
listUser - the receiver user (U1)
user - the sender user (U2)
Returns:
true if, and only if, listUser (U1) accepts messages from user (U2).
Throws:
UserNotManagedException - if the listUser (U1) is not managed by the server.

assignMovedUser

public void assignMovedUser(ManagedUser user)
                     throws AssignationNotAcceptedException
Description copied from interface: MSForMSInterface
Assigns a user management (sending all its datas) to the message server.

Specified by:
assignMovedUser in interface MSForMSInterface
Parameters:
user - the user to assign.
Throws:
AssignationNotAcceptedException - if the assignation was not accepted.

deleteDownloadedMsgs

public void deleteDownloadedMsgs(User user,
                                 java.lang.String pw)
                          throws UserNotManagedException,
                                 WrongPasswordException,
                                 UserBlockedException
Description copied from interface: MSForServletInterface
Deletes the messages that are already downloaded by a User.

Specified by:
deleteDownloadedMsgs in interface MSForServletInterface
Parameters:
user - the user.
pw - the user password.
Throws:
UserNotManagedException - if the user is not managed by the MS.
WrongPasswordException - if the password is wrong.
UserBlockedException - is the user is blocked.

sendLoad

public static void sendLoad()
Sends to the bootstrap the current load via a UDP packet.


blockContact

public void blockContact(User user,
                         java.lang.String pw,
                         User userToBlock)
                  throws UserNotManagedException,
                         UserNotExistsException,
                         WrongPasswordException,
                         UserBlockedException
Description copied from interface: MSForServletInterface
Marks userToBlock in the user's contact list as blocked, so that no more messages from that user are accepted.

Specified by:
blockContact in interface MSForServletInterface
Parameters:
user - the user.
pw - the user's password.
userToBlock - the user to block.
Throws:
UserNotManagedException - if user is not managed by this message server.
UserNotExistsException - if the userToBlock is not present in the contact list.
WrongPasswordException - if the user's password is wrong.
UserBlockedException - if the user is blocked

getUserContact

public User getUserContact(User user)
                    throws java.rmi.RemoteException,
                           UserNotExistsException,
                           GenericErrorException,
                           UserBlockedException
Description copied from interface: MSForServletInterface
Gets a User IP address and TCP port to directely connect to its servlet.

Specified by:
getUserContact in interface MSForServletInterface
Parameters:
user - the user whose connection datas is wanted. Only the nickname is considered.
Returns:
the user with the correctly obtained IP address and TCP port.
Throws:
java.rmi.RemoteException - if a communication problem occurs.
UserNotExistsException - if the specificed user nickname does not exists.
GenericErrorException - if another error occurred (i.e. a communication problem with another message server).
UserBlockedException - if the user is blocked.

removeContact

public void removeContact(User user,
                          java.lang.String pw,
                          User userToRemove)
                   throws UserNotManagedException,
                          WrongPasswordException,
                          UserBlockedException
Description copied from interface: MSForServletInterface
Removes userToRemove from user's contact list, so that no more messages from that user are accepted.

Specified by:
removeContact in interface MSForServletInterface
Parameters:
user - the user.
pw - the user's password.
userToRemove - the user to remove.
Throws:
UserNotManagedException - if user is not managed by this message server.
WrongPasswordException - if the user's password is wrong.
UserBlockedException - if the user is currently blocked.

getManagedUserContact

public User getManagedUserContact(User user)
                           throws java.rmi.RemoteException,
                                  UserNotManagedException
Description copied from interface: MSForMSInterface
Gets a User IP address and TCP port to directely connect to its servlet. The User is managed by this MS.

Specified by:
getManagedUserContact in interface MSForMSInterface
Parameters:
user - the user whose connection datas is wanted. Only the nickname is considered.
Returns:
the user with the correctly obtained IP address and TCP port.
Throws:
java.rmi.RemoteException - if a communication problem occurs.
UserNotManagedException - if the user is not locally managed.