uk.ac.rdg.resc.jstyx.server
Class AsyncStyxFile

java.lang.Object
  extended by uk.ac.rdg.resc.jstyx.server.StyxFile
      extended by uk.ac.rdg.resc.jstyx.server.AsyncStyxFile
All Implemented Interfaces:
StyxFileChangeListener
Direct Known Subclasses:
MonitoredFileOnDisk, SGSParamFile

public class AsyncStyxFile
extends StyxFile
implements StyxFileChangeListener

A wrapper for a StyxFile that implements asynchronous behaviour: the first read from a given client returns the file's data as normal. If the file is not closed, subsequent reads from the same client block until the file content is changed.

Author:
Jon Blower $Revision: 609 $ $Date: 2006-03-31 18:09:42 +0100 (Fri, 31 Mar 2006) $ $Log$ Revision 1.13 2005/11/14 21:32:30 jonblower Got SGSRun working for SC2005 demo Revision 1.12 2005/11/04 19:33:41 jonblower Changed contentsChanged() to fileContentsChanged() in StyxFileChangeListener Revision 1.11 2005/11/04 09:12:05 jonblower Made baseFile a protected field (instead of private) Revision 1.10 2005/09/08 07:08:59 jonblower Removed "String user" from list of parameters to StyxFile.write() Revision 1.9 2005/08/10 18:35:28 jonblower Added simple test main() function Revision 1.8 2005/06/20 07:17:35 jonblower Wrapped SGSParamFile as AsyncStyxFile Revision 1.7 2005/05/11 10:33:50 jonblower Implemented MonitoredFileOnDisk.java Revision 1.6 2005/05/10 08:02:18 jonblower Changes related to implementing MonitoredFileOnDisk Revision 1.5 2005/05/09 07:10:37 jonblower Minor changes Revision 1.4 2005/03/24 09:48:31 jonblower Changed 'count' from long to int throughout for reading and writing Revision 1.3 2005/03/16 17:56:23 jonblower Replaced use of java.nio.ByteBuffer with MINA's ByteBuffer to minimise copying of buffers Revision 1.2 2005/03/11 14:02:16 jonblower Merged MINA-Test_20059309 into main line of development Revision 1.1.1.1.2.1 2005/03/09 19:44:18 jonblower Changes concerned with migration to MINA Revision 1.1.1.1 2005/02/16 18:58:31 jonblower Initial import

Field Summary
protected  StyxFile baseFile
           
 
Fields inherited from class uk.ac.rdg.resc.jstyx.server.StyxFile
auth, directory, lastModifiedTime, name, parent
 
Constructor Summary
AsyncStyxFile(StyxFile file)
          Creates a new AsyncStyxFile with the same name as the underlying file.
AsyncStyxFile(StyxFile file, java.lang.String name)
           
AsyncStyxFile(StyxFile file, java.lang.String name, int permissions)
           
AsyncStyxFile(StyxFile file, java.lang.String name, int permissions, boolean isAppendOnly, boolean isExclusive)
           
 
Method Summary
protected  void clientDisconnected(StyxFileClient client)
          Called when a client disconnects from the file.
 void delete()
          Free all resources associated with this file
 void fileContentsChanged()
          This is called when the contents of the underlying StyxFile are changed (required by StyxFileChangeListener interface).
 void fileContentsChanged(boolean force)
          Called to notify that the underlying data have changed.
 StyxFile getBaseFile()
          Gets the StyxFile that is wrapped by this AsyncStyxFile
 ULong getLength()
           
static void main(java.lang.String[] args)
          Simple test function
 void read(StyxFileClient client, long offset, int count, int tag)
          Reads data from this file.
 void setMinReplyInterval(float seconds)
          Sets a minimum reply interval, i.e.
 void write(StyxFileClient client, long offset, int count, org.apache.mina.common.ByteBuffer data, boolean truncate, int tag)
          This simply calls write() in the contained StyxFile object.
 
Methods inherited from class uk.ac.rdg.resc.jstyx.server.StyxFile
addChangeListener, addClient, checkSetLastModifiedTime, checkSetLength, checkSetMode, checkSetName, clientConnected, contentsChanged, fireContentsChanged, getClient, getDirEntry, getFullPath, getGroup, getName, getNumClients, getOwner, getParent, getPermissions, getQid, getVersion, incrementVersion, isAppendOnly, isAuth, isDirectory, isExclusive, processAndReplyRead, processAndReplyRead, processAndReplyRead, refresh, remove, removeChangeListener, removeClient, rename, replyRead, replyRead, replyRead, replyRead, replyRead, replyWrite, setLastAccessTime, setLastModified, setLength, setMode, setName, setPermissions, setReadOnly
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

baseFile

protected StyxFile baseFile
Constructor Detail

AsyncStyxFile

public AsyncStyxFile(StyxFile file)
              throws StyxException
Creates a new AsyncStyxFile with the same name as the underlying file. (Note: this must be placed in a different directory from that of the underlying file to prevent name conflicts.

Throws:
StyxException

AsyncStyxFile

public AsyncStyxFile(StyxFile file,
                     java.lang.String name)
              throws StyxException
Throws:
StyxException

AsyncStyxFile

public AsyncStyxFile(StyxFile file,
                     java.lang.String name,
                     int permissions)
              throws StyxException
Throws:
StyxException

AsyncStyxFile

public AsyncStyxFile(StyxFile file,
                     java.lang.String name,
                     int permissions,
                     boolean isAppendOnly,
                     boolean isExclusive)
              throws StyxException
Throws:
StyxException
Method Detail

setMinReplyInterval

public void setMinReplyInterval(float seconds)
Sets a minimum reply interval, i.e. the minimum amount of time that must elapse between successive replies to the same client. This is most useful when using this AsyncStyxFile to provide notification of a very rapidly-changing quantity as it can save a lot of unnecessary messages being sent. (This is set to zero when the AsyncStyxFile is first created)


read

public void read(StyxFileClient client,
                 long offset,
                 int count,
                 int tag)
          throws StyxException
Description copied from class: StyxFile
Reads data from this file. This method could be synchronized in subclasses, but watch out for blocks if the read is expected to take some time to complete. Subclasses must make sure they reply to the read request by creating a java.nio.ByteBuffer or byte array of data, then calling the appropriate readReply() (this can be done at any time; it does not have to be done within the read() method). This default implementation simply throws a StyxException, which will result in an Rerror message being sent back to the client. Subclasses should override this to provide the desired behaviour when the file is read.

Overrides:
read in class StyxFile
Parameters:
client - The client that is performing the read
offset - The point in the file at which to start reading
count - The maximum number of bytes to read
tag - The tag of the incoming Tread message (this is needed when calling readReply())
Throws:
StyxException

write

public void write(StyxFileClient client,
                  long offset,
                  int count,
                  org.apache.mina.common.ByteBuffer data,
                  boolean truncate,
                  int tag)
           throws StyxException
This simply calls write() in the contained StyxFile object. This will cause the contentsChanged() method to be called, notifying any waiting clients of the change to the file

Overrides:
write in class StyxFile
Parameters:
client - The client that is performing the write operation
offset - The place in the file where the new data will be added
count - The number of bytes to write
data - The data to write. The position and limit of this ByteBuffer will be set correctly, but subclasses should note that the position might not be zero.
truncate - If this is true the file will be truncated at the end of the new data
tag - The tag of the incoming Twrite message (this is needed when calling writeReply())
Throws:
StyxException

fileContentsChanged

public void fileContentsChanged(boolean force)
Called to notify that the underlying data have changed.

Parameters:
force - If this is true, clients will be notified of changes to the underlying data, irrespective of how long they have waited.

getBaseFile

public StyxFile getBaseFile()
Gets the StyxFile that is wrapped by this AsyncStyxFile


getLength

public ULong getLength()
Overrides:
getLength in class StyxFile
Returns:
the length of the StyxFile that is wrapped by this AsyncStyxFile

fileContentsChanged

public void fileContentsChanged()
This is called when the contents of the underlying StyxFile are changed (required by StyxFileChangeListener interface).

Specified by:
fileContentsChanged in interface StyxFileChangeListener

delete

public void delete()
Free all resources associated with this file

Overrides:
delete in class StyxFile

clientDisconnected

protected void clientDisconnected(StyxFileClient client)
Called when a client disconnects from the file. We remove all references to this client

Overrides:
clientDisconnected in class StyxFile
Parameters:
client - The client that has just disconnected from the file. This will not be null.

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Simple test function

Throws:
java.lang.Exception


Copyright © 2004-2006 Reading e-Science Centre. All Rights Reserved.