com.ericsson.otp.erlang
Class OtpNode

java.lang.Object
  |
  +--com.ericsson.otp.erlang.AbstractNode
        |
        +--com.ericsson.otp.erlang.OtpLocalNode
              |
              +--com.ericsson.otp.erlang.OtpNode

public class OtpNode
extends OtpLocalNode

Represents a local OTP node. This class is used when you do not wish to manage connections yourself - outgoing connections are established as needed, and incoming connections accepted automatically. This class supports the use of a mailbox API for communication, while management of the underlying communication mechanism is automatic and hidden from the application programmer.

Once an instance of this class has been created, obtain one or more mailboxes in order to send or receive messages. The first message sent to a given node will cause a connection to be set up to that node. Any messages received will be delivered to the appropriate mailboxes.

To shut down the node, call close(). This will prevent the node from accepting additional connections and it will cause all existing connections to be closed. Any unread messages in existing mailboxes can still be read, however no new messages will be delivered to the mailboxes.

Note that the use of this class requires that Epmd (Erlang Port Mapper Daemon) is running on each cooperating host. This class does not start Epmd automatically as Erlang does, you must start it manually or through some other means. See the Erlang documentation for more information about this.


Inner Class Summary
 class OtpNode.Acceptor
           
 class OtpNode.Mailboxes
           
 
Fields inherited from class com.ericsson.otp.erlang.OtpLocalNode
epmd, port
 
Constructor Summary
OtpNode(java.lang.String node)
           Create a node using the default cookie.
OtpNode(java.lang.String node, java.lang.String cookie)
          Create a node.
OtpNode(java.lang.String node, java.lang.String cookie, int port)
          Create a node.
 
Method Summary
 void close()
          Close the node.
 void closeMbox(OtpMbox mbox)
          Close the specified mailbox.
 OtpMbox createMbox()
          Create an unnamed mailbox that can be used to send and receive messages with other, similar mailboxes and with Erlang processes.
 OtpMbox createMbox(java.lang.String name)
          Create an named mailbox that can be used to send and receive messages with other, similar mailboxes and with Erlang processes.
protected  void finalize()
           
 java.lang.String[] getNames()
          Get a list of all known registered names on this node.
 boolean ping(java.lang.String node, long timeout)
           Determine if another node is alive.
 boolean registerName(java.lang.String name, OtpMbox mbox)
           Register or remove a name for the given mailbox.
 void registerStatusHandler(OtpNodeStatus handler)
          Register interest in certain system events.
 OtpErlangPid whereis(java.lang.String name)
          Determine the pid corresponding to a registered name on this node.
 
Methods inherited from class com.ericsson.otp.erlang.OtpLocalNode
createPid, createPort, createRef, getEpmd, port, setEpmd
 
Methods inherited from class com.ericsson.otp.erlang.AbstractNode
alive, cookie, host, node, setCookie, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

OtpNode

public OtpNode(java.lang.String node)
        throws java.io.IOException

Create a node using the default cookie. The default cookie is found by reading the first line of the .erlang.cookie file in the user's home directory. The home directory is obtained from the System property "user.home".

If the file does not exist, an empty string is used. This method makes no attempt to create the file.

Parameters:
node - the name of this node.
Throws:
java.io.IOException - if communication could not be initialized.

OtpNode

public OtpNode(java.lang.String node,
               java.lang.String cookie)
        throws java.io.IOException
Create a node.
Parameters:
node - the name of this node.
cookie - the authorization cookie that will be used by this node when it communicates with other nodes.
Throws:
java.io.IOException - if communication could not be initialized.

OtpNode

public OtpNode(java.lang.String node,
               java.lang.String cookie,
               int port)
        throws java.io.IOException
Create a node.
Parameters:
node - the name of this node.
cookie - the authorization cookie that will be used by this node when it communicates with other nodes.
port - the port number you wish to use for incoming connections. Specifying 0 lets the system choose an available port.
Throws:
java.io.IOException - if communication could not be initialized.
Method Detail

close

public void close()
Close the node. Unpublish the node from Epmd (preventing new connections) and close all existing connections.

finalize

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

createMbox

public OtpMbox createMbox()
Create an unnamed mailbox that can be used to send and receive messages with other, similar mailboxes and with Erlang processes. Messages can be sent to this mailbox by using its associated pid.
Returns:
a mailbox.

closeMbox

public void closeMbox(OtpMbox mbox)
Close the specified mailbox.
Parameters:
the - mailbox to close.

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 the mailbox to other pids, they will be broken when this method is called and exit signals will be sent.


createMbox

public OtpMbox createMbox(java.lang.String name)
Create an named mailbox that can be used to send and receive messages with other, similar mailboxes and with Erlang processes. Messages can be sent to this mailbox by using its registered name or the associated pid.
Parameters:
name - a name to register for this mailbox. The name must be unique within this OtpNode.
Returns:
a mailbox, or null if the name was already in use.

registerName

public boolean registerName(java.lang.String name,
                            OtpMbox mbox)

Register or remove a name for the given 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.
mbox - the mailbox to associate with the name.
Returns:
true if the name was available, or false otherwise.

getNames

public java.lang.String[] getNames()
Get a list of all known registered names on this node.
Returns:
an array of Strings, containins all known registered names on this node.

whereis

public OtpErlangPid whereis(java.lang.String name)
Determine the pid corresponding to a registered name on this node.
Returns:
the pid corresponding to the registered name, or null if the name is not known on this node.

registerStatusHandler

public void registerStatusHandler(OtpNodeStatus handler)
Register interest in certain system events. The OtpNodeStatus handler object contains callback methods, that will be called when certain events occur.
Parameters:
handler - the callback object to register. To clear the handler, specify null as the handler to use.

ping

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

Determine if another node is alive. This method has the side effect of setting up a connection to the remote node (if possible). Only a single outgoing message is sent; the timeout is how long to wait for a response.

Only a single attempt is made to connect to the remote node, so for example it is not possible to specify an extremely long timeout and expect to be notified when the node eventually comes up. If you wish to wait for a remote node to be started, the following construction may be useful:

// ping every 2 seconds until positive response
while (!me.ping(him,2000));
Parameters:
node - the name of the node to ping.
timeout - the time, in milliseconds, to wait for response before returning false.
Returns:
true if the node was alive and the correct ping response was returned. false if the correct response was not returned on time.