br.shob.client
Class ClientCommLayer

java.lang.Object
  |
  +--br.shob.AbstractCommLayer
        |
        +--br.shob.client.ClientCommLayer

public class ClientCommLayer
extends AbstractCommLayer

This class is the client-side CommLayer used by a SharedSpace instance. It inherits the comunication protocol form the AbstractCommLayer and provides client-side facilities to the SharedSpace.

As of SharedObjects 0.5, this class is capable of dinamically loading classes from remote hosts. This means that real implementation of shared objects is not required to be accessible to the client machines, so objects can be manipulated through known interfaces. In addition, different implementations of the same SharedReference can be used simultaneously on client machines without interfering with one another.

Version:
$Revision: 1.12 $
Author:
crolmos, dukejeffrie
Created:
November 24, 2001

Field Summary
protected  Map classLoadersMap
          A mapping of class loaders by peer.
protected  HostId serverId
          The server's host id.
protected  SharedSpace shob
          The SharedSpace instance that uses this CommLayer
protected  ServerSocket signedSocket
          The listening socket for this CommLayer
 
Fields inherited from class br.shob.AbstractCommLayer
keyPairGeneratorAlgorithm, logger, myId, signatureAlgorithm
 
Constructor Summary
ClientCommLayer(SharedSpace shob, String hostname, int port)
          Constructor for the ClientCommLayer object
 
Method Summary
 TimeStamp beginTrans(String globalName)
          Asks the server to begin a transaction on a shared object
protected  void checkMessage(Message m, int type, String errorMsg)
          Checks a message for errors.
 HostId commit(SharedObject obj)
          Asks the server to commit a transaction on a shared object
 SharedObject get(String globalName, HostId owner)
          this method is called when the owner needs to update the original object.
protected  ShobClassLoader getClassLoader(String globalName)
          Gets a class loader who can load classes for the given global name
 long getCurrentTime()
          Gets the currentTime attribute of the ClientCommLayer object
 byte[] getRemoteClassArray(String globalName, String className, HostId owner)
          Gets the bytes representing a class to be defined.
 HostId identify(URL remoteURL)
          Gets the HostId for a peer URL.
 boolean isLoggedIn()
          Checks if this CommLayer is logged to the global server.
 void login(String hostname, int port)
          Connects to the global server
 void logout()
          Logs out from the global server, releasing any global resources (such as transaction locks) that this CommLayer might be using.
 SharedObject reload(String globalName)
          Reloads a shard object from its owner.
 SharedObject reload(String globalName, HostId owner)
          Reloads a shared object from its owner.
 HostId request(String globalName)
          Asks the server for the owner of a shared object
 HostId rollback(String globalName)
          Asks the server to rollback a transaction.
 void send(Socket s, String globalName)
          Sends an object in the owned list through the socket.
 void sendCached(Socket s, String globalName)
          Sends an object in the cached list through the socket.
 void sendClass(Socket s, String globalName, String className)
          Sends a class file through the socket.
 void share(SharedObject obj)
          Registers an object at the global server.
 void start()
          Starts this CommLayer
 void unlock(String globalName, HostId writer, TimeStamp stamp)
          Asks the server to unlock an object and finish a transaction (after commit or reload)
 void unshare(String globalName)
          Unregisters an object at the server.
 void update(String globalName, HostId writer)
          Updates the owned object list with the object taken from the writer's cache and finishes a commit process
 HostId validate(String globalName, TimeStamp stamp)
          Validates a time stamp.
 
Methods inherited from class br.shob.AbstractCommLayer
connect, connect, getLogger, isOnline, recvMessage, recvMessage, sendMessage, setLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

classLoadersMap

protected Map classLoadersMap
A mapping of class loaders by peer.


serverId

protected HostId serverId
The server's host id.


shob

protected SharedSpace shob
The SharedSpace instance that uses this CommLayer


signedSocket

protected ServerSocket signedSocket
The listening socket for this CommLayer

Constructor Detail

ClientCommLayer

public ClientCommLayer(SharedSpace shob,
                       String hostname,
                       int port)
                throws CommLayerException
Constructor for the ClientCommLayer object

Parameters:
shob - The SharedSpace that will use this CommLayer
hostname - The global space server's hostname
port - The global space server's listening port
Throws:
CommLayerException - if a communication error occurs.
Method Detail

beginTrans

public TimeStamp beginTrans(String globalName)
                     throws ShobException
Asks the server to begin a transaction on a shared object

Parameters:
globalName - the global name of the shared object
Returns:
A timestamp indicating the last modification of the shared object
Throws:
ShobException - if a communication error occurs

commit

public HostId commit(SharedObject obj)
              throws ShobException
Asks the server to commit a transaction on a shared object

Parameters:
obj - The shared object's new value
Returns:
The HostId of the object's owner
Throws:
ShobException - wrapping a lower level exception.

get

public SharedObject get(String globalName,
                        HostId owner)
                 throws ShobException
this method is called when the owner needs to update the original object.

Parameters:
globalName - the global name of the shared object
owner - the object owner's HostId
Returns:
the object's current version
Throws:
ShobException - wrapping a lower level exception.

getCurrentTime

public long getCurrentTime()
Gets the currentTime attribute of the ClientCommLayer object

Returns:
The currentTime value

getRemoteClassArray

public byte[] getRemoteClassArray(String globalName,
                                  String className,
                                  HostId owner)
                           throws ShobException
Gets the bytes representing a class to be defined. The byte array returned contains the format of a valid class file as defined by the Java Virtual Machine Specification.

Parameters:
globalName - The global name of the object which needs that class to be correctly loaded. Used in security checking
className - the fully qualified name of the class to load
owner - HostId of the owner of the object which needs the class.
Returns:
a class file as a byte array
Throws:
ShobException - wrapping a lower level exception.

identify

public HostId identify(URL remoteURL)
                throws ShobException
Gets the HostId for a peer URL. When the argument is not the server's address, it asks the server for the authenticated HostId.

Parameters:
remoteURL - The URL of a SharedSpace (only hostname and port are used)
Returns:
the HostId of a SharedSpace
Throws:
ShobException - wrapping a lower level exception.

isLoggedIn

public boolean isLoggedIn()
Checks if this CommLayer is logged to the global server.

Returns:
true when the server was identified (which means that client authentication was processed for this CommLayer), false otherwise.

login

public void login(String hostname,
                  int port)
           throws ShobException
Connects to the global server

Parameters:
hostname - the hostname of the server
port - the port number the server listens to
Throws:
ShobException - wrapping a lower level exception.

logout

public void logout()
            throws ShobException
Logs out from the global server, releasing any global resources (such as transaction locks) that this CommLayer might be using. Usually, this is done at exit time, when the SharedSpace is no longer in use.

Throws:
ShobException - wrapping a lower level exception.

reload

public SharedObject reload(String globalName,
                           HostId owner)
                    throws ShobException
Reloads a shared object from its owner.

Parameters:
globalName - The global name of the object
owner - the owner of the object
Returns:
the current value for the object
Throws:
ShobException - wrapping a lower level exception.

reload

public SharedObject reload(String globalName)
                    throws ShobException
Reloads a shard object from its owner. This implementation makes a request for the global name and then calls reload(glogalName, owner)

Parameters:
globalName - a global name
Returns:
the shared object
Throws:
ShobException - wrapping a lower level exception.
See Also:
reload(String,HostId)

request

public HostId request(String globalName)
               throws ShobException
Asks the server for the owner of a shared object

Parameters:
globalName - global name of the object
Returns:
HostId of the object's owner
Throws:
ShobException - wrapping a lower level exception.

rollback

public HostId rollback(String globalName)
                throws ShobException
Asks the server to rollback a transaction. The owner is returned for a future reload

Parameters:
globalName - the object where a transaction is taking place
Returns:
the owner of the object
Throws:
ShobException - wrapping a lower level exception.

send

public void send(Socket s,
                 String globalName)
          throws CommLayerException
Sends an object in the owned list through the socket.

Parameters:
s - the socket to send it through
globalName - the global name of the object
Throws:
CommLayerException - if a communication error occurs

sendCached

public void sendCached(Socket s,
                       String globalName)
                throws CommLayerException
Sends an object in the cached list through the socket. This is primarily used in commits.

Parameters:
s - the socket to send it through
globalName - the global name of the object
Throws:
CommLayerException - if a communication error occurs

sendClass

public void sendClass(Socket s,
                      String globalName,
                      String className)
               throws CommLayerException
Sends a class file through the socket. It is used in dynamic class loading.

Parameters:
s - the socket to send it through
globalName - the global name of the object
className - the name of the class to send
Throws:
CommLayerException - if a communication error occurs or the class is not found

share

public void share(SharedObject obj)
           throws ShobException
Registers an object at the global server.

Parameters:
obj - the shared object to share
Throws:
ShobException - Description of the Exception

start

public void start()
           throws CommLayerException
Starts this CommLayer

Throws:
CommLayerException - wrapping a lower level exception.

unlock

public void unlock(String globalName,
                   HostId writer,
                   TimeStamp stamp)
            throws ShobException
Asks the server to unlock an object and finish a transaction (after commit or reload)

Parameters:
globalName - the object
writer - the transaction locker
stamp - the timestamp to use as last modified time
Throws:
ShobException - wrapping a lower level exception.

unshare

public void unshare(String globalName)
             throws ShobException
Unregisters an object at the server. The shared space making this request must be the owner of the object referenced.

Parameters:
globalName - the global name of the object
Throws:
ShobException - wrapping a lower level exception.

update

public void update(String globalName,
                   HostId writer)
            throws ShobException
Updates the owned object list with the object taken from the writer's cache and finishes a commit process

Parameters:
globalName - the global name of the object
writer - the object's current writer
Throws:
ShobException - wrapping a lower level exception.

validate

public HostId validate(String globalName,
                       TimeStamp stamp)
                throws ShobException
Validates a time stamp. Asks the server to ckeck if the timestamp is valid. When it is not valid, the server sends back the HostId of the object's owner so the shared space can reload the object.

Parameters:
globalName - global name of the object
stamp - currently known last modification value
Returns:
null if the timestamp is valid, Owner if it is old.
Throws:
ShobException - wrapping a lower level exception.

checkMessage

protected void checkMessage(Message m,
                            int type,
                            String errorMsg)
                     throws ShobException
Checks a message for errors. If the message has not the expected type, it throws an exception, if the message corresponds to an error, it also throws an exception. If the message is valid and informs of success, it finishes quietly.

Parameters:
m - the message to be checked
type - the expected type for the message
errorMsg - a prefix for the eventual error message
Throws:
ShobException - if the message is not valid or informs that a remote error occurred.

getClassLoader

protected ShobClassLoader getClassLoader(String globalName)
Gets a class loader who can load classes for the given global name

Parameters:
globalName - Description of the Parameter
Returns:
The classLoader value