com.ericsson.otp.erlang
Class OtpMbox

java.lang.Object
  extended by com.ericsson.otp.erlang.OtpMbox

public class OtpMbox
extends java.lang.Object

Provides a simple mechanism for exchanging messages with Erlang processes or other instances of this class.

Each mailbox is associated with a unique pid that contains information necessary for delivery of messages. When sending messages to named processes or mailboxes, the sender pid is made available to the recipient of the message. When sending messages to other mailboxes, the recipient can only respond if the sender includes the pid as part of the message contents. The sender can determine his own pid by calling self().

Mailboxes can be named, either at creation or later. Messages can be sent to named mailboxes and named Erlang processes without knowing the pid that identifies the mailbox. This is neccessary in order to set up initial communication between parts of an application. Each mailbox can have at most one name.

Since this class was intended for communication with Erlang, all of the send methods take OtpErlangObject arguments. However this class can also be used to transmit arbitrary Java objects (as long as they implement one of java.io.Serializable or java.io.Externalizable) by encapsulating the object in a OtpErlangBinary.

Messages to remote nodes are externalized for transmission, and as a result the recipient receives a copy of the original Java object. To ensure consistent behaviour when messages are sent between local mailboxes, such messages are cloned before delivery.

Additionally, mailboxes can be linked in much the same way as Erlang processes. If a link is active when a mailbox is closed, any linked Erlang processes or OtpMboxes will be sent an exit signal. As well, exit signals will be (eventually) sent if a mailbox goes out of scope and its finalize() method called. However due to the nature of finalization (i.e. Java makes no guarantees about when finalize() will be called) it is recommended that you always explicitly close mailboxes if you are using links instead of relying on finalization to notify other parties in a timely manner.

When retrieving messages from a mailbox that has received an exit signal, an OtpErlangExit exception will be raised. Note that the exception is queued in the mailbox along with other messages, and will not be raised until it reaches the head of the queue and is about to be retrieved.


Method Summary
 void close()
          Close this mailbox.
 boolean equals(java.lang.Object o)
          Determine if two mailboxes are equal.
 void exit(OtpErlangObject reason)
          Close this mailbox with the given reason.
 void exit(OtpErlangPid to, OtpErlangObject reason)
           Send an exit signal to a remote pid.
 void exit(OtpErlangPid to, java.lang.String reason)
           Equivalent to exit(to, new OtpErlangAtom(reason)).
 void exit(java.lang.String reason)
          Equivalent to exit(new OtpErlangAtom(reason)).
protected  void finalize()
           
 java.lang.String getName()
          Get the registered name of this mailbox.
 java.lang.String[] getNames()
           Get a list of all known registered names on the same node as this mailbox.
 void link(OtpErlangPid to)
           Link to a remote mailbox or Erlang process.
 boolean ping(java.lang.String node, long timeout)
           Create a connection to a remote node.
 OtpErlangObject receive()
          Block until a message arrives for this mailbox.
 OtpErlangObject receive(long timeout)
          Wait for a message to arrive for this mailbox.
 OtpInputStream receiveBuf()
          Block until a message arrives for this mailbox.
 OtpInputStream receiveBuf(long timeout)
          Wait for a message to arrive for this mailbox.
 OtpMsg receiveMsg()
          Block until a message arrives for this mailbox.
 OtpMsg receiveMsg(long timeout)
          Wait for a message to arrive for this mailbox.
 boolean registerName(java.lang.String name)
           Register or remove a name for this mailbox.
 OtpErlangPid self()
           Get the identifying pid associated with this mailbox.
 void send(OtpErlangPid to, OtpErlangObject msg)
          Send a message to a remote pid, representing either another mailbox or an Erlang process.
 void send(java.lang.String name, OtpErlangObject msg)
          Send a message to a named mailbox created from the same node as this mailbox.
 void send(java.lang.String name, java.lang.String node, OtpErlangObject msg)
          Send a message to a named mailbox created from another node.
 void unlink(OtpErlangPid to)
           Remove a link to a remote mailbox or Erlang process.
 OtpErlangPid whereis(java.lang.String name)
          Determine the pid corresponding to a registered name on this node.
 
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

self

public OtpErlangPid self()

Get the identifying pid associated with this mailbox.

The pid associated with this mailbox uniquely identifies the mailbox and can be used to address the mailbox. You can send the pid to a remote communicating part so that he can know where to send his response.

Returns:
the self pid for this mailbox.

registerName

public boolean registerName(java.lang.String name)

Register or remove a name for this mailbox. Registering a name for a mailbox enables others to send messages without knowing the pid of the mailbox. A mailbox can have at most one name; if the mailbox already had a name, calling this method will supercede that name.

Parameters:
name - the name to register for the mailbox. Specify null to unregister the existing name from this mailbox.
Returns:
true if the name was available, or false otherwise.

getName

public java.lang.String getName()
Get the registered name of this mailbox.

Returns:
the registered name of this mailbox, or null if the mailbox had no registerd name.

receive

public OtpErlangObject receive()
                        throws OtpErlangExit,
                               OtpErlangDecodeException
Block until a message arrives for this mailbox.

Returns:
an OtpErlangObject representing the body of the next message waiting in this mailbox.
Throws:
OtpErlangDecodeException - if the message can not be decoded.
OtpErlangExit - if a linked pid has exited or has sent an exit signal to this mailbox.

receive

public OtpErlangObject receive(long timeout)
                        throws OtpErlangExit,
                               OtpErlangDecodeException
Wait for a message to arrive for this mailbox.

Parameters:
timeout - the time, in milliseconds, to wait for a message before returning null.
Returns:
an OtpErlangObject representing the body of the next message waiting in this mailbox.
Throws:
OtpErlangDecodeException - if the message can not be decoded.
OtpErlangExit - if a linked pid has exited or has sent an exit signal to this mailbox.

receiveBuf

public OtpInputStream receiveBuf()
                          throws OtpErlangExit
Block until a message arrives for this mailbox.

Returns:
a byte array representing the still-encoded body of the next message waiting in this mailbox.
Throws:
OtpErlangExit - if a linked pid has exited or has sent an exit signal to this mailbox.

receiveBuf

public OtpInputStream receiveBuf(long timeout)
                          throws java.lang.InterruptedException,
                                 OtpErlangExit
Wait for a message to arrive for this mailbox.

Parameters:
timeout - the time, in milliseconds, to wait for a message before returning null.
Returns:
a byte array representing the still-encoded body of the next message waiting in this mailbox.
Throws:
OtpErlangExit - if a linked pid has exited or has sent an exit signal to this mailbox.
java.lang.InterruptedException - if no message if the method times out before a message becomes available.

receiveMsg

public OtpMsg receiveMsg()
                  throws OtpErlangExit
Block until a message arrives for this mailbox.

Returns:
an OtpMsg containing the header information as well as the body of the next message waiting in this mailbox.
Throws:
OtpErlangExit - if a linked pid has exited or has sent an exit signal to this mailbox.

receiveMsg

public OtpMsg receiveMsg(long timeout)
                  throws java.lang.InterruptedException,
                         OtpErlangExit
Wait for a message to arrive for this mailbox.

Parameters:
timeout - the time, in milliseconds, to wait for a message.
Returns:
an OtpMsg containing the header information as well as the body of the next message waiting in this mailbox.
Throws:
OtpErlangExit - if a linked pid has exited or has sent an exit signal to this mailbox.
java.lang.InterruptedException - if no message if the method times out before a message becomes available.

send

public void send(OtpErlangPid to,
                 OtpErlangObject msg)
Send a message to a remote pid, representing either another mailbox or an Erlang process.

Parameters:
to - the pid identifying the intended recipient of the message.
msg - the body of the message to send.

send

public void send(java.lang.String name,
                 OtpErlangObject msg)
Send a message to a named mailbox created from the same node as this mailbox.

Parameters:
name - the registered name of recipient mailbox.
msg - the body of the message to send.

send

public void send(java.lang.String name,
                 java.lang.String node,
                 OtpErlangObject msg)
Send a message to a named mailbox created from another node.

Parameters:
name - the registered name of recipient mailbox.
node - the name of the remote node where the recipient mailbox is registered.
msg - the body of the message to send.

exit

public void exit(OtpErlangObject reason)
Close this mailbox with the given reason.

After this operation, the mailbox will no longer be able to receive messages. Any delivered but as yet unretrieved messages can still be retrieved however.

If there are links from this mailbox to other pids, they will be broken when this method is called and exit signals will be sent.

Parameters:
reason - an Erlang term describing the reason for the exit.

exit

public void exit(java.lang.String reason)
Equivalent to exit(new OtpErlangAtom(reason)).

See Also:
exit(OtpErlangObject)

exit

public void exit(OtpErlangPid to,
                 OtpErlangObject reason)

Send an exit signal to a remote pid. This method does not cause any links to be broken, except indirectly if the remote pid exits as a result of this exit signal.

Parameters:
to - the pid to which the exit signal should be sent.
reason - an Erlang term indicating the reason for the exit.

exit

public void exit(OtpErlangPid to,
                 java.lang.String reason)

Equivalent to exit(to, new OtpErlangAtom(reason)).

See Also:
exit(OtpErlangPid, OtpErlangObject)

link

public void link(OtpErlangPid to)
          throws OtpErlangExit

Link to a remote mailbox or Erlang process. Links are idempotent, calling this method multiple times will not result in more than one link being created.

If the remote process subsequently exits or the mailbox is closed, a subsequent attempt to retrieve a message through this mailbox will cause an OtpErlangExit exception to be raised. Similarly, if the sending mailbox is closed, the linked mailbox or process will receive an exit signal.

If the remote process cannot be reached in order to set the link, the exception is raised immediately.

Parameters:
to - the pid representing the object to link to.
Throws:
OtpErlangExit - if the pid referred to does not exist or could not be reached.

unlink

public void unlink(OtpErlangPid to)

Remove a link to a remote mailbox or Erlang process. This method removes a link created with link(). Links are idempotent; calling this method once will remove all links between this mailbox and the remote pid.

Parameters:
to - the pid representing the object to unlink from.

ping

public boolean ping(java.lang.String node,
                    long timeout)

Create a connection to a remote node.

Strictly speaking, this method is not necessary simply to set up a connection, since connections are created automatically first time a message is sent to a pid on the remote node.

This method makes it possible to wait for a node to come up, however, or check that a node is still alive.

This method calls a method with the same name in Otpnode but is provided here for convenience.

Parameters:
node - the name of the node to ping.
timeout - the time, in milliseconds, before reporting failure.

getNames

public java.lang.String[] getNames()

Get a list of all known registered names on the same node as this mailbox.

This method calls a method with the same name in Otpnode but is provided here for convenience.

Returns:
an array of Strings containing all registered names on this node.

whereis

public OtpErlangPid whereis(java.lang.String name)
Determine the pid corresponding to a registered name on this node.

This method calls a method with the same name in Otpnode but is provided here for convenience.

Returns:
the pid corresponding to the registered name, or null if the name is not known on this node.

close

public void close()
Close this mailbox.

After this operation, the mailbox will no longer be able to receive messages. Any delivered but as yet unretrieved messages can still be retrieved however.

If there are links from this mailbox to other pids, they will be broken when this method is called and exit signals with reason 'normal' will be sent.

This is equivalent to exit("normal").


finalize

protected void finalize()
Overrides:
finalize in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Determine if two mailboxes are equal.

Overrides:
equals in class java.lang.Object
Returns:
true if both Objects are mailboxes with the same identifying pids.