Class ShieldedPlug
- java.lang.Object
-
- ej.sp.ShieldedPlug
-
public class ShieldedPlug extends Object
A shielded plug is a database that contains several memory blocks.
A shielded plug can be created at runtime using
createDatabase(int)or be created at startup and retrieved bygetDatabase(int).The list of memory blocks IDs can be retrieve using
getIDs().
There are two sorts of shielded plugs:- The immutable ones (
isImmutable()) that cannot be modified. - The mutable ones (!
isImmutable()) can be modified by adding or removing blocks usingcreate(int, int)orcreate(int, int, int)ordelete(int).
Each block has fixed length (
getLength(int)passing the block ID).
All access to a database is serialized by the implementation: there will be only one access (either read or write) at a time. Each access to a block is atomic, this avoids inconsistency:- It can be read using one of the read methods that match its length.
- It can be written using one the write methods that match its length.
Each memory block has a flag that indicates that an update has occurred since the last read. It is possible to test this state:
isPending(int). This flag is set to false when reading, and to true when writing.A task can wait for the modification of a memory block by using
waitFor(int). This method suspends the current task if and only if the method pending returns false on the specified memory block. A task can also wait on several memory blocks, the task is released when one of the blocks is modifiedwaitFor(int[])).Each memory block has a flag indicating if its data are available or not. It is possible to test this flag using
isDataAvailable(int). This flag is initiallyfalseand is set totruewhen writing data. It can be set tofalseusing the methodreset(int). - The immutable ones (
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcreate(int blockID, int length)Creates a block with the given ID.voidcreate(int blockID, int length, int maxTasks)Creates a block with the given ID.static ShieldedPlugcreateDatabase(int ID)Creates a new empty database with the given ID.voiddelete(int blockID)Deletes the block with the given ID.static ShieldedPluggetDatabase(int ID)Returns the database defined at the given ID.intgetID()Gets the ID of this database.int[]getIDs()Gets the list of IDs of the blocks available in this database.intgetLength(int blockID)Returns the length of the block with the given ID.intgetMaxTasks(int blockID)Gets the maximum number of tasks that can wait at the same time on the block defined with the given ID.SPReadergetReader(int blockID)intgetSize()Gets the number of blocks of this database.SPWritergetWriter(int blockID)booleanisDataAvailable(int blockID)Determines whether data in the block with the given ID are available or not.
By default no data is available in a block.booleanisImmutable()Gets if this database is immutable or not.booleanisPending(int blockID)Gets if data has been written into the block since last read.voidread(int blockID, byte[] data)Fills the given array with data from the block with the given ID.voidread(int blockID, byte[] data, int destOffset)Fills the given array withblock.lengthbytes from the block with the given ID.doublereadDouble(int blockID)Reads eight input bytes from the block with the given ID and returns adoublevalue.
The way thedoubleis built from the eight bytes is platform dependent.floatreadFloat(int blockID)Reads four input bytes from the block with the given ID and returns afloatvalue.
The way thefloatis built from the four bytes is platform dependent.intreadInt(int blockID)Reads four input bytes from the block with the given ID and returns anintvalue.
The way theintis built from the four bytes is platform dependent.longreadLong(int blockID)Reads eight input bytes from the block with the given ID and returns alongvalue.
The way thelongis built from the eight bytes is platform dependent.ObjectreadObject(int blockID)Invokes thereadObjectmethod of theSPReaderregistered for the block with the given ID.voidreset(int blockID)Resets data of the block with the given ID.
After execution of this method,isDataAvailable(int)method would returnfalse(unless data were written after callingreset(int)and before callingisDataAvailable(int)).voidsetReader(int blockID, SPReader reader)voidsetWriter(int blockID, SPWriter writer)voidwaitFor(int blockID)Causes current thread to wait until another thread write data into the block with the given ID.
If data has been written in the block since last read, this method returns immediately.int[]waitFor(int[] blockIDs)Causes current thread to wait until another thread write data into at least one block from the blocks with the given IDs.
If data has been written in one block since last read from it, this method returns immediately.voidwrite(int blockID, byte[] data)Writes block length bytes from the specified byte array to the block with the given ID.
Thewrite(blockID, data)method has the same effect as:voidwrite(int blockID, byte[] data, int srcOffset)Writes block length bytes from the specified byte array to the block with the given ID.
Elementdata[destOffset]is the first byte written to the block.voidwriteDouble(int blockID, double value)Writes adoublevalue, which is comprised of eight bytes, to the block with the given ID.
The way thedoubleis written from the eight bytes is platform dependent.voidwriteFloat(int blockID, float value)Writes afloatvalue, which is comprised of four bytes, to the block with the given ID.
The way thefloatis written from the four bytes is platform dependent.voidwriteInt(int blockID, int value)Writes anintvalue, which is comprised of four bytes, to the block with the given ID.
The way theintis written from the four bytes is platform dependent.voidwriteLong(int blockID, long value)Writes alongvalue, which is comprised of eight bytes, to the block with the given ID.
The way thelongis written from the eight bytes is platform dependent.voidwriteObject(int blockID, Object o)Invokes thewriteObjectmethod of theSPWriterregistered for the block with the given ID.
-
-
-
Method Detail
-
getDatabase
public static ShieldedPlug getDatabase(int ID)
Returns the database defined at the given ID.- Parameters:
ID- the identification number of the requested database- Returns:
- the database with the given ID
- Throws:
IllegalArgumentException- if no database is defined with the given ID
-
createDatabase
public static ShieldedPlug createDatabase(int ID)
Creates a new empty database with the given ID.- Parameters:
ID- the identification number of the created database- Returns:
- the created database
- Throws:
IllegalArgumentException- if a database with the given ID already existsSecurityException- if the platform cannot create dynamically databases
-
isImmutable
public boolean isImmutable()
Gets if this database is immutable or not.- Returns:
trueif no block can be added or remove to this database,falseotherwise
-
delete
public void delete(int blockID)
Deletes the block with the given ID.- Parameters:
blockID- the ID of the block to delete- Throws:
IllegalArgumentException- if no block is defined with the given IDSecurityException- if this database is immutable
-
create
public void create(int blockID, int length, int maxTasks)Creates a block with the given ID.- Parameters:
blockID- the ID of the block to createlength- the length in bytes of the block to createmaxTasks- maximum number of tasks that can wait at the same time for a modification of the block- Throws:
IllegalArgumentException- if a block is already defined with the given IDSecurityException- if this database is immutable
-
create
public void create(int blockID, int length)Creates a block with the given ID. An unlimited number of tasks will be able to wait at the same time for a modification of the block.- Parameters:
blockID- the ID of the block to createlength- the length in bytes of the block to create- Throws:
IllegalArgumentException- if a block is already defined with the given IDSecurityException- if this database is immutable
-
getID
public int getID()
Gets the ID of this database.- Returns:
- the ID of this database
-
getSize
public int getSize()
Gets the number of blocks of this database.- Returns:
- the number of blocks in this database
-
getIDs
public int[] getIDs()
Gets the list of IDs of the blocks available in this database.- Returns:
- the list of the IDs of the blocks available in this database
-
getLength
public int getLength(int blockID)
Returns the length of the block with the given ID.- Parameters:
blockID- the ID of the block- Returns:
- the length in bytes
- Throws:
IllegalArgumentException- if no block is defined with the given ID
-
getMaxTasks
public int getMaxTasks(int blockID)
Gets the maximum number of tasks that can wait at the same time on the block defined with the given ID.- Parameters:
blockID- the ID of the block- Returns:
- the maximum number of tasks that can wait at the same time on the block defined with the given ID, or -1 if infinite
- Throws:
IllegalArgumentException- if no block is defined with the given ID
-
read
public void read(int blockID, byte[] data) throws EmptyBlockExceptionFills the given array with data from the block with the given ID. The number of bytes read is equal to the length of the block.
Theread(blockID, data)method has the same effect as:read(blockID, data, 0)- Parameters:
blockID- the ID of the blockdata- the buffer into which the data is read- Throws:
IllegalArgumentException- if no block is defined with the given IDEmptyBlockException- if no data is available in the blockIndexOutOfBoundsException- ifdata.lengthis lower than block length
-
read
public void read(int blockID, byte[] data, int destOffset) throws EmptyBlockExceptionFills the given array withblock.lengthbytes from the block with the given ID. The first byte read is stored into elementdata[destOffset].If
destOffsetis negative ordestOffset+ block length is greater than the length of the arraydata, then anIndexOutOfBoundsExceptionis thrown.- Parameters:
blockID- the ID of the blockdata- the buffer into which the data is readdestOffset- the start offset in arraydataat which the data is written- Throws:
IllegalArgumentException- if no block is defined with the given IDEmptyBlockException- if no data is available in the blockIndexOutOfBoundsException- ifdestOffsetis negative or ifdata.lengthis lower thandestOffset+ block length
-
readInt
public int readInt(int blockID) throws EmptyBlockExceptionReads four input bytes from the block with the given ID and returns anintvalue.
The way theintis built from the four bytes is platform dependent.
This method is suitable for reading bytes written by the
writeIntmethod.- Parameters:
blockID- the ID of the block- Returns:
- the
intvalue read - Throws:
IllegalArgumentException- if no block is defined with the given IDEmptyBlockException- if no data is available in the blockIndexOutOfBoundsException- if block length is not four bytes
-
readLong
public long readLong(int blockID) throws EmptyBlockExceptionReads eight input bytes from the block with the given ID and returns alongvalue.
The way thelongis built from the eight bytes is platform dependent.
This method is suitable for reading bytes written by the
writeLongmethod.- Parameters:
blockID- the ID of the block- Returns:
- the
longvalue read - Throws:
IllegalArgumentException- if no block is defined with the given IDEmptyBlockException- if no data is available in the blockIndexOutOfBoundsException- if block length is not height bytes
-
readFloat
public float readFloat(int blockID) throws EmptyBlockExceptionReads four input bytes from the block with the given ID and returns afloatvalue.
The way thefloatis built from the four bytes is platform dependent.
This method is suitable for reading bytes written by the
writeFloatmethod.- Parameters:
blockID- the ID of the block- Returns:
- the
floatvalue read - Throws:
IllegalArgumentException- if no block is defined with the given IDEmptyBlockException- if no data is available in the blockIndexOutOfBoundsException- if block length is not four bytes
-
readDouble
public double readDouble(int blockID) throws EmptyBlockExceptionReads eight input bytes from the block with the given ID and returns adoublevalue.
The way thedoubleis built from the eight bytes is platform dependent.
This method is suitable for reading bytes written by the
writeDoublemethod.- Parameters:
blockID- the ID of the block- Returns:
- the
doublevalue read - Throws:
IllegalArgumentException- if no block is defined with the given IDEmptyBlockException- if no data is available in the blockIndexOutOfBoundsException- if block length is not height bytes
-
readObject
public Object readObject(int blockID) throws EmptyBlockException
Invokes thereadObjectmethod of theSPReaderregistered for the block with the given ID.
TheSPReaderis responsible for the de-serialization of the object from the block.- Parameters:
blockID- the ID of the block- Returns:
- the object read from the block
- Throws:
IllegalArgumentException- if no block is defined with the given IDEmptyBlockException- if no data is available in the blockNullPointerException- if noSPReaderhas been registered for the blockIndexOutOfBoundsException- if block length is lower than the size needed for object de-serialization
-
setReader
public void setReader(int blockID, SPReader reader)Registers the givenSPReaderto de-serialize objects from the block with the given ID.
If anSPReaderis already defined for the block, it is replaced by the givenSPReader.- Parameters:
blockID- the ID of the blockreader- theSPReader- Throws:
IllegalArgumentException- if no block is defined with the given ID
-
getReader
@Nullable public SPReader getReader(int blockID)
Gets theSPReaderused to de-serialize objects from the block with the given ID.
If noSPReaderis defined for the block,nullis returned.- Parameters:
blockID- the ID of the block- Returns:
- the
SPReaderset or null if none - Throws:
IllegalArgumentException- if no block is defined with the given ID
-
write
public void write(int blockID, byte[] data)Writes block length bytes from the specified byte array to the block with the given ID.
Thewrite(blockID, data)method has the same effect as:write(blockID, data, 0)- Parameters:
blockID- the ID of the blockdata- the data to write- Throws:
IllegalArgumentException- if no block is defined with the given IDIndexOutOfBoundsException- ifdata.lengthvalue is lower than block length
-
write
public void write(int blockID, byte[] data, int srcOffset)Writes block length bytes from the specified byte array to the block with the given ID.
Elementdata[destOffset]is the first byte written to the block.If
destOffsetis negative, ordestOffset+ block length is greater than the length of the arraydata, then anIndexOutOfBoundsExceptionis thrown.- Parameters:
blockID- the ID of the blockdata- the data to writesrcOffset- the start offset in the data- Throws:
IllegalArgumentException- if no block is defined with the given IDIndexOutOfBoundsException- ifdestOffsetis negative or ifdata.lengthis lower thanoffset+ block length
-
writeInt
public void writeInt(int blockID, int value)Writes anintvalue, which is comprised of four bytes, to the block with the given ID.
The way theintis written from the four bytes is platform dependent.
The bytes written by this method may be read by the
readIntmethod, which will then return anintequal tovalue.- Parameters:
blockID- the ID of the blockvalue- theintvalue to be written- Throws:
IllegalArgumentException- if no block is defined with the given IDIndexOutOfBoundsException- if block length is not four bytes
-
writeLong
public void writeLong(int blockID, long value)Writes alongvalue, which is comprised of eight bytes, to the block with the given ID.
The way thelongis written from the eight bytes is platform dependent.
The bytes written by this method may be read by the
readLongmethod, which will then return alongequal tovalue.- Parameters:
blockID- the ID of the blockvalue- thelongvalue to be written- Throws:
IllegalArgumentException- if no block is defined with the given IDIndexOutOfBoundsException- if block length is not eight bytes
-
writeFloat
public void writeFloat(int blockID, float value)Writes afloatvalue, which is comprised of four bytes, to the block with the given ID.
The way thefloatis written from the four bytes is platform dependent.
The bytes written by this method may be read by the
readFloatmethod, which will then return afloatequal tovalue.- Parameters:
blockID- the ID of the blockvalue- thefloatvalue to be written- Throws:
IllegalArgumentException- if no block is defined with the given IDIndexOutOfBoundsException- if block length is not four bytes
-
writeDouble
public void writeDouble(int blockID, double value)Writes adoublevalue, which is comprised of eight bytes, to the block with the given ID.
The way thedoubleis written from the eight bytes is platform dependent.
The bytes written by this method may be read by the
readDoublemethod, which will then return adoubleequal tovalue.- Parameters:
blockID- ID of the blockvalue- thedoublevalue to be written- Throws:
IllegalArgumentException- if no block is defined with the given IDIndexOutOfBoundsException- if block length is not eight bytes
-
writeObject
public void writeObject(int blockID, Object o)Invokes thewriteObjectmethod of theSPWriterregistered for the block with the given ID.
TheSPWriteris responsible for the serialization of the object into the block.- Parameters:
blockID- the ID of the blocko- the object to be written- Throws:
IllegalArgumentException- if no block is defined with the given IDNullPointerException- if noSPWriterhas been registered for the blockIndexOutOfBoundsException- if block length is lower than the size needed for object serialization
-
setWriter
public void setWriter(int blockID, SPWriter writer)Registers the givenSPWriterto serialize objects into the block with the given ID.
If anSPWriteris already defined for the block, it is replaced by the givenSPWriter.- Parameters:
blockID- the ID of the blockwriter- theSPWriter- Throws:
IllegalArgumentException- if no block is defined with the given ID
-
getWriter
@Nullable public SPWriter getWriter(int blockID)
Gets theSPWriterused to serialize objects into the block with the given ID.
If noSPWriteris defined for the block,nullis returned.- Parameters:
blockID- the ID of the block- Returns:
- the
SPWriterset or null if none - Throws:
IllegalArgumentException- if no block is defined with the given ID
-
waitFor
public void waitFor(int blockID) throws InterruptedExceptionCauses current thread to wait until another thread write data into the block with the given ID.
If data has been written in the block since last read, this method returns immediately.- Parameters:
blockID- the ID of the block- Throws:
InterruptedException- if another thread has interrupted the current thread The interrupted status of the current thread is cleared when this exception is thrownIllegalArgumentException- if no block is defined with the given IDTooManyWaitingThreadsException- if too many threads are waiting for new data
-
waitFor
public int[] waitFor(int[] blockIDs) throws InterruptedExceptionCauses current thread to wait until another thread write data into at least one block from the blocks with the given IDs.
If data has been written in one block since last read from it, this method returns immediately.- Parameters:
blockIDs- the list of block IDs- Returns:
- the list of IDs of the blocks that has been written
- Throws:
InterruptedException- if another thread has interrupted the current thread The interrupted status of the current thread is cleared when this exception is thrownIllegalArgumentException- if one of the ID does not correspond to an existing blockTooManyWaitingThreadsException- if too many threads are waiting for new data
-
isPending
public boolean isPending(int blockID)
Gets if data has been written into the block since last read.- Parameters:
blockID- the ID of the block- Returns:
trueif data has been written into the block since last read,falseotherwise- Throws:
IllegalArgumentException- if no block is defined with the given ID
-
isDataAvailable
public boolean isDataAvailable(int blockID)
Determines whether data in the block with the given ID are available or not.
By default no data is available in a block. When data are written in a block, they remain available until methodreset(int)is called.- Parameters:
blockID- the ID of the block- Returns:
trueif data is available in the blockfalseotherwise- Throws:
IllegalArgumentException- if no block is defined with the given ID
-
reset
public void reset(int blockID)
Resets data of the block with the given ID.
After execution of this method,isDataAvailable(int)method would returnfalse(unless data were written after callingreset(int)and before callingisDataAvailable(int)).- Parameters:
blockID- the ID of the block- Throws:
IllegalArgumentException- if no block is defined with the given ID
-
-