osl.manager.basic
Class StreamInputActorImpl

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--osl.manager.ActorImpl
              |
              +--osl.manager.basic.StreamInputActorImpl

public class StreamInputActorImpl
extends ActorImpl
implements StreamInputActor

This class defines the implementation of an actor used to manage an input stream on behalf of external actors. We require an implementation (rather than an Actor) because if a security manager is running (i.e. startfoundry was specified with the -secure option), then user-written actors will not have direct access to several standard streams (e.g. System.out). Since instances of ActorImpl are privileged they may control such streams and provide their services to specific actors. The interface exported to external actors is defined by the StreamInputActor interface. In the current implementation, this actor will always use System.in as the encapsulated input stream.

Version:
$Revision: 1.2 $ ($Date: 1998/07/18 18:59:48 $)
See Also:
osl.manager.basic.StreamInputActor, Serialized Form

Field Summary
protected  WaitQueue mailQueue
          The queue which holds incoming requests.
protected  ActorManager ourManager
          The manager which manages this implementation.
 
Fields inherited from class osl.manager.ActorImpl
actorClass, classRef, context, extensionMethods, nextID, self
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
StreamInputActorImpl()
           
 
Method Summary
protected  void actorDeliver(ActorMsgRequest msg)
          This method is called by a manager to deliver a new message to the local actor.
protected  void actorInitialize(ActorManager ourMgr, ActorName you, ActorCreateRequest req)
          This method is called by a manager to initialize an actor implementation after it is instantiated.
protected  void actorPostMigrateRebuild(ActorManager ourMgr)
          This method is called by the new manager of an actor implementation just after migration has occurred.
 java.lang.Integer available()
          Return the number of bytes that can be read from the internal input stream without blocking.
 void available(ActorName client, java.lang.String method)
          Determine the number of bytes that can be read from the internal input stream without blocking, and send the result to the specified caller.
 void close()
          Close the internal input stream.
protected  ActorName implCreate(ActorCreateRequest req)
          Request a new actor to be created.
protected  void implDestroy(java.lang.String reason)
          Request to remove this actor from the system.
protected  java.lang.Object implInvokeService(ServiceName name, java.lang.String meth, java.lang.Object[] args)
          Request a service invocation on the named node service.
protected  void implMigrate(ActorManagerName loc)
          Request that this actor wishes to be migrated to a new location.
protected  java.lang.Object implSend(ActorMsgRequest msg)
          Request a message to be sent.
 void mark(java.lang.Integer readlimit)
          Mark the current position in the internal stream.
 java.lang.Boolean markSupported()
          Test if the internal input stream supports the mark and reset methods.
 void markSupported(ActorName client, java.lang.String method)
          Test if the internal input stream supports the mark and reset methods.
 java.lang.Integer read()
          Read the next byte of data from an input stream.
 void read(ActorName client, java.lang.String method)
          Read the next byte of data from an input stream and send it to a specified actor.
 void read(ActorName client, java.lang.String method, java.lang.Integer max)
          Read an array of bytes from the input stream and send them to a specified actor.
 java.lang.Byte[] read(java.lang.Integer max)
          Read an array of bytes from the input stream and return them to the caller.
 java.lang.Character[] readln()
          Read a line of characters from the internal input stream.
 void readln(ActorName client, java.lang.String method)
          Read a line of characters from the internal input stream.
 void reset()
          Reposition the internal stream to the position marked by a previous call to mark.
 void run()
          The main run loop for this implementation.
 void skip(ActorName client, java.lang.String method, java.lang.Long n)
          Skip over and discard n bytes of data from the input stream.
 java.lang.Long skip(java.lang.Long n)
          Skip over and discard n bytes of data from the input stream.
 
Methods inherited from class osl.manager.ActorImpl
buildAsynchException, finalize, formatAsynchException, implExtension, mgrActorCreate, mgrActorFatalError, mgrActorInvokeService, mgrActorMigrate, mgrActorSend, registerExtension, removeExtension, setCreatingThread, stampRequest
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ourManager

protected ActorManager ourManager
The manager which manages this implementation.

mailQueue

protected WaitQueue mailQueue
The queue which holds incoming requests.
Constructor Detail

StreamInputActorImpl

public StreamInputActorImpl()
Method Detail

actorInitialize

protected void actorInitialize(ActorManager ourMgr,
                               ActorName you,
                               ActorCreateRequest req)
This method is called by a manager to initialize an actor implementation after it is instantiated. The manager provides a reference to itself, a reference giving the name of the new actor, and a create request describing the new Actor that should be created and managed. After calling this method, a manager will call the run method in this class to start the actor executing. Note that the protection of this method prevents it from being called external to osl.manager. This was done to prevent arbitrary classes from initializing new actors. However, managers in different packages may invoke this method using implInitialize.

For this implementation, the manager reference and actor name are saved, but the actor create request is discarded since no internal actor is actually created.

Parameters:
ourMgr - The ActorManager which should be used by this actor implementation to invoke actor services.
you - The ActorName that should be used as the name of the new actor.
rtClass - The run-time Class of the user-written actor that should be instantiated by this implementation. This class will always be an extension of the Actor class.
initArgs - The array of arguments to pass to the constructor of the user-defined actor when it is instantiated.
Overrides:
actorInitialize in class ActorImpl
See Also:
ActorManager.implInitialize(osl.manager.ActorImpl, osl.manager.ActorName, osl.manager.ActorCreateRequest)

actorDeliver

protected void actorDeliver(ActorMsgRequest msg)
This method is called by a manager to deliver a new message to the local actor. This method is protected so that it has package level protection and therefore may not be invoked directly by user-written actor code.

For this implementation, we deposit the new message in our mail queue so that it can be processed in the main run loop.

Parameters:
msg - The ActorMsgRequest structure to be delivered. This structure must be maintained by the actor as it is required if an exception is returned to the manager.
Overrides:
actorDeliver in class ActorImpl

actorPostMigrateRebuild

protected void actorPostMigrateRebuild(ActorManager ourMgr)
This method is called by the new manager of an actor implementation just after migration has occurred. The implementation is expected to perform any re-initialization necessary after a migration (e.g. rebuilding transient fields). This method will be called before the implementations thread is restarted.

This method should never be called for this implementation as StreamInputActorImpls will never migrate. If this method IS called for some reason then a runtime exception is thrown.

Parameters:
ourMgr - A reference to the new manager of the implementation after migration has occurred.
Throws:
java.lang.RuntimeException - Thrown if this method is ever called.
Overrides:
actorPostMigrateRebuild in class ActorImpl

implSend

protected java.lang.Object implSend(ActorMsgRequest msg)
                             throws RemoteCodeException
Request a message to be sent. The message argument is forwarded to the manager. If the RPC field of the message is set to true then the caller is blocked while the RPC takes place. This method is protected so that it has package level protection and therefore may not be invoked directly by user-written actor code.

This method should never be called in this implementation since no internal actor is ever created.

Parameters:
msg - The ActorMsgRequest describing the message to send.
Returns:
If this is an RPC request, then the return value from the message call is returned. Otherwise, null is returned.
Throws:
RemoteCodeException - Thrown only if this is an RPC exception which throws an exception during invocation. An exception thrown in any other case is an error.
java.lang.RuntimeException - Thrown if this method is ever called.
Overrides:
implSend in class ActorImpl
See Also:
Actor.send(ActorName, String), Actor.call(ActorName, String)

implCreate

protected ActorName implCreate(ActorCreateRequest req)
                        throws java.lang.SecurityException,
                               RemoteCodeException
Request a new actor to be created. The request argument is forwarded to the manager and the returned name is passed on to the Actor caller. This method is protected so that it has package level protection and therefore may not be invoked directly by user-written actor code.

This method should never be called in this implementation since no internal actor is ever created.

Parameters:
req - The ActorCreateRequest describing the new actor to create.
Returns:
The ActorName of the new actor.
Throws:
java.lang.SecurityException - Thrown if the behavior of the new actor is not a subclass of Actor.
RemoteCodeException - Thrown as a wrapper for any other error that is encountered while attempting the create. Note that such errors may also be thrown asynchronously.
java.lang.RuntimeException - Thrown if this method is ever called.
Overrides:
implCreate in class ActorImpl
See Also:
Actor.create(String), Actor.create(Class), Actor.create(ActorManagerName, String), Actor.create(ActorManagerName, Class)

implMigrate

protected void implMigrate(ActorManagerName loc)
Request that this actor wishes to be migrated to a new location. By convention, this call is meant to indicate a request rather than serving as a notice to immediately migrate the actor. Normally, the implementation will record the request but wait to migrate the actor until the current message being processed has completed. Once this occurs the actor is migrated and restarted at its new node. This method is protected so that it has package level protection and therefore may not be invoked directly by user-written actor code.

This method should never be called in this implementation since no internal actor is ever created.

Parameters:
loc - The ActorManagerName of the node to migrate to.
Throws:
java.lang.RuntimeException - Thrown if this method is ever called.
Overrides:
implMigrate in class ActorImpl
See Also:
Actor.migrate(osl.manager.ActorManagerName), Actor.cancelMigrate()

implInvokeService

protected java.lang.Object implInvokeService(ServiceName name,
                                             java.lang.String meth,
                                             java.lang.Object[] args)
                                      throws ServiceNotFoundException,
                                             ServiceException
Request a service invocation on the named node service. This request is forwarded to the actor manager and the appropriate return value is provided. This method is protected so that it has package level protection and therefore may not be invoked directly by user-written actor code.

This method should never be called in this implementation since no internal actor is ever created.

Parameters:
name - The ServiceName describing the service to invoke.
args - The Object argument to pass to the service invocation function.
Returns:
The Object returned as a result of the service invocation.
Throws:
ServiceNotFoundException - Thrown if no instance of the named service can be found on this node.
ServiceException - Thrown if the service throws an exception while processing the request.
java.lang.RuntimeException - Thrown if this method is ever called.
Overrides:
implInvokeService in class ActorImpl
See Also:
Actor.invokeService(osl.service.ServiceName, java.lang.String)

implDestroy

protected void implDestroy(java.lang.String reason)
Request to remove this actor from the system. Normally, this method will not return as the actor is immediately removed from the system. Note that any actor garbage collection process is ignored in this call so that this actor may be removed even though it is accessible by other actors.

This method should never be called in this implementation since no internal actor is ever created.

Parameters:
reason - A String giving a "reason" for the removal. This string should normally be appended to the log for the actor before removing it from the system.
Throws:
java.lang.RuntimeException - Thrown if this method is ever called.
Overrides:
implDestroy in class ActorImpl
See Also:
Actor.destroy(java.lang.String)

read

public java.lang.Integer read()
                       throws java.io.IOException
Read the next byte of data from an input stream. The value returned is an Integer in the range 0 to 255 (i.e. a character). This method blocks until a byte of data is available, or end-of-file is reached. If end-of-file is encountered then a -1 is returned. Normally, this method will be invoked using the "call" actor operation, as this is the only way to obtain the data returned from this method.

Specified by:
read in interface StreamInputActor
Returns:
An Integer containing the next byte of data or -1 if end-of-file is reached.
Throws:
java.io.IOException - Thrown if an I/O error occurs while reading the attached stream.

read

public void read(ActorName client,
                 java.lang.String method)
          throws java.io.IOException
Read the next byte of data from an input stream and send it to a specified actor. The value returned is an Integer in the range 0 to 255 (i.e. a character). This method blocks until a byte of data is available, or end-of-file is reached. If end-of-file is encountered then a -1 is returned. The result is sent to the actor with name client by invoking method method. Thus, client is expected to define a method with signature:

public type method(Integer);
where type may be any legal return type. Any error resulting from the sending of the result (e.g. NoSuchMethodException, RemoteCodeException, etc) is ignored by the StreamInputActor (but it IS logged to the Actor log file). Normally, this method is used by actors wishing to perform asynchronous I/O.

Specified by:
read in interface StreamInputActor
Parameters:
client - The ActorName of the actor which should receive the data.
method - The String name of the method in client which will accept the data.
Throws:
java.io.IOException - Thrown if an I/O error occurs while reading the attached stream. For asynchronous calls, this exception is normally returned as an invocation of the asynchException" method.
See Also:
StreamInputActor.read()

read

public java.lang.Byte[] read(java.lang.Integer max)
                      throws java.io.IOException
Read an array of bytes from the input stream and return them to the caller. The maximum number of bytes to read is specified by max. This method blocks until input is available. Normally, this method will be invoked using the "call" actor operation, as this is the only way to obtain the data returned from this method.

Specified by:
read in interface StreamInputActor
Parameters:
max - An Integer giving the maximum number of bytes to read from the stream.
Returns:
A Byte array giving the data read from the stream. The return value is null if no data was available because end-of-file was encountered. Otherwise, the length of the array indicates the actual number of bytes read.
Throws:
java.io.IOException - Thrown if an I/O error occurs while reading the attached stream, or if max is less than one.

read

public void read(ActorName client,
                 java.lang.String method,
                 java.lang.Integer max)
          throws java.io.IOException
Read an array of bytes from the input stream and send them to a specified actor. The maximum number of bytes to read is specified by max. This method blocks until input is available. The result is sent to the actor with name client by invoking method method. Thus, client is expected to define a method with signature:

public type method(Byte[]);
where type may be any legal return type. Any error resulting from the sending of the result (e.g. NoSuchMethodException, RemoteCodeException, etc) is ignored by the StreamInputActor (but it IS logged to the Actor log file). Normally, this method is used by actors wishing to perform asynchronous I/O.

Specified by:
read in interface StreamInputActor
Parameters:
client - The ActorName of the actor which should receive the data.
method - The String name of the method in client which will accept the data.
max - An Integer giving the maximum number of bytes to read from the stream.
Throws:
java.io.IOException - Thrown if an I/O error occurs while reading the attached stream, or if max is less than one.
See Also:
StreamInputActor.read(Integer)

skip

public java.lang.Long skip(java.lang.Long n)
                    throws java.io.IOException
Skip over and discard n bytes of data from the input stream. Depending on the internal InputStream, the actual number of bytes skipped may vary. The number of bytes skipped is returned as the result of this method. Normally, this method will be invoked using the "call" actor operation, as this is the only way to obtain the data returned from this method.

Specified by:
skip in interface StreamInputActor
Parameters:
n - A Long giving the number of bytes to be skipped.
Returns:
An Long giving the actual number of bytes skipped.
Throws:
java.io.IOException - Thrown if an I/O error occurs while skipping bytes.

skip

public void skip(ActorName client,
                 java.lang.String method,
                 java.lang.Long n)
          throws java.io.IOException
Skip over and discard n bytes of data from the input stream. Depending on the internal InputStream, the actual number of bytes skipped may vary. The number of bytes skipped is sent to the actor with name client by invoking method method. Thus, client is expected to define a method with signature:

public type method(Long);
where type may be any legal return type. Any error resulting from the sending of the result (e.g. NoSuchMethodException, RemoteCodeException, etc) is ignored by the StreamInputActor (but it IS logged to the Actor log file). Normally, this method is used by actors wishing to perform asynchronous I/O.

Specified by:
skip in interface StreamInputActor
Parameters:
client - The ActorName of the actor which should receive the number of bytes skipped.
method - The String name of the method in client which will accept the number of bytes skipped.
n - A Long giving the number of bytes to be skipped.
Throws:
java.io.IOException - Thrown if an I/O error occurs while skipping bytes.
See Also:
StreamInputActor.skip(Long)

available

public java.lang.Integer available()
                            throws java.io.IOException
Return the number of bytes that can be read from the internal input stream without blocking. The number of bytes available is returned as an Integer to the caller. Normally, this method will be invoked using the "call" actor operation, as this is the only way to obtain the data returned from this method.

Specified by:
available in interface StreamInputActor
Returns:
An Integer giving the number of bytes that can be read from this input stream without blocking.
Throws:
java.io.IOException - Thrown if an I/O error occurs while attempting to determine the number of bytes available.

available

public void available(ActorName client,
                      java.lang.String method)
               throws java.io.IOException
Determine the number of bytes that can be read from the internal input stream without blocking, and send the result to the specified caller. The number of bytes available is sent to the actor with name client by invoking method method. Thus, client is expected to define a method with signature:

public type method(Integer);
where type may be any legal return type. Any error resulting from the sending of the result (e.g. NoSuchMethodException, RemoteCodeException, etc) is ignored by the StreamInputActor (but it IS logged to the Actor log file). Normally, this method is used by actors wishing to perform asynchronous I/O.

Specified by:
available in interface StreamInputActor
Parameters:
client - The ActorName of the actor which should receive the number of bytes available.
method - The String name of the method in client which will accept the number of bytes available.
Throws:
java.io.IOException - Thrown if an I/O error occurs while attempting to determine the number of bytes available.
See Also:
StreamInputActor.available()

close

public void close()
           throws java.io.IOException
Close the internal input stream. As this method has no return value, it may be called either synchronously or asynchronously.
Specified by:
close in interface StreamInputActor
Throws:
java.io.IOException - Thrown if an I/O error occurs while attempting to close the stream.

mark

public void mark(java.lang.Integer readlimit)
Mark the current position in the internal stream. Later calls to reset will reposition the internal stream at the last marked position. A readlimit may be specified which indicates the number of bytes which may be read before the mark position becomes invalid. As this method has no return value, it may be called either synchronously or asynchronously.

Specified by:
mark in interface StreamInputActor
Parameters:
readlimit - An Integer indicating the maximum number of bytes that can be read before the mark position becomes invalid.
Throws:
java.io.IOException - Thrown if an I/O error occurs while placing the mark.

reset

public void reset()
           throws java.io.IOException
Reposition the internal stream to the position marked by a previous call to mark. As this method has no return value, it may be called either synchronously or asynchronously.

Specified by:
reset in interface StreamInputActor
Throws:
java.io.IOException - Thrown if the internal stream has not been marked, or if the previously placed mark has been invalidated.

markSupported

public java.lang.Boolean markSupported()
Test if the internal input stream supports the mark and reset methods. A Boolean is returned to the caller indicating the result of the query. Normally, this method will be invoked using the "call" actor operation, as this is the only way to obtain the data returned from this method.

Specified by:
markSupported in interface StreamInputActor
Returns:
A Boolean indicating true if mark and reset are supported, and false otherwise.

markSupported

public void markSupported(ActorName client,
                          java.lang.String method)
Test if the internal input stream supports the mark and reset methods. The Boolean result is sent to the actor with name client by invoking method method. Thus, client is expected to define a method with signature:

public type method(Boolean);
where type may be any legal return type. Any error resulting from the sending of the result (e.g. NoSuchMethodException, RemoteCodeException, etc) is ignored by the StreamInputActor (but it IS logged to the Actor log file). Normally, this method is used by actors wishing to perform asynchronous I/O.

Specified by:
markSupported in interface StreamInputActor
Parameters:
client - The ActorName of the actor which should receive the markSupported status
method - The String name of the method in client which will accept the markSupported status.
See Also:
StreamInputActor.markSupported()

readln

public java.lang.Character[] readln()
                             throws java.io.IOException
Read a line of characters from the internal input stream. A line is any sequence of characters terminated by a newline. This call will block until either a newline terminates a sequence of characters, or end-of-file is encountered. In either case, a Character array containing the characters read (minus the newline terminator) is returned to the caller. If no characters were available (e.g. because end-of-file was encountered immediately), then null is returned. Normally, this method will be invoked using the "call" actor operation, as this is the only way to obtain the data returned from this method.

Specified by:
readln in interface StreamInputActor
Returns:
A Character array containing all the characters read up to a terminating newline or end-of-file, or null if no characters were available do to end-of-file.
Throws:
java.io.IOException - Thrown if an I/O error occurs while reading a line of characters.

readln

public void readln(ActorName client,
                   java.lang.String method)
            throws java.io.IOException
Read a line of characters from the internal input stream. A line is any sequence of characters terminated by a newline. This call will block until either a newline terminates a sequence of characters, or end-of-file is encountered. In either case, a Character array containing the characters read (minus the newline terminator) is sent to the actor with name client by invoking method method. If no characters were available (e.g. because end-of-file was encountered immediately), then null is sent to client. Thus, client is expected to define a method with signature:

public type method(Character);
where type may be any legal return type. Any error resulting from the sending of the result (e.g. NoSuchMethodException, RemoteCodeException, etc) is ignored by the StreamInputActor (but it IS logged to the Actor log file). Normally, this method is used by actors wishing to perform asynchronous I/O.

Specified by:
readln in interface StreamInputActor
Parameters:
client - The ActorName of the actor which should receive the line of characters.
method - The String name of the method in client which should recieve the line of characters.
Throws:
java.io.IOException - Thrown if an I/O error occurs while reading a line of characters.
See Also:
StreamInputActor.readln()

run

public void run()
The main run loop for this implementation. All messages are processed within this loop and replies are generated and sent as necessary.
Overrides:
run in class java.lang.Thread