Class OtpNode

All Implemented Interfaces:
OtpTransportFactory

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, except when using a transport factory extending the OtpGenericTransportFactory abstract class. 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.

  • Constructor Details

    • OtpNode

      public OtpNode(String node) throws 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:
      IOException - if communication could not be initialized.
    • OtpNode

      public OtpNode(String node, OtpTransportFactory transportFactory) throws 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.
      transportFactory - the transport factory to use when creating connections.
      Throws:
      IOException - if communication could not be initialized.
    • OtpNode

      public OtpNode(String node, String cookie) throws 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:
      IOException - if communication could not be initialized.
    • OtpNode

      public OtpNode(String node, String cookie, OtpTransportFactory transportFactory) throws 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.
      transportFactory - the transport factory to use when creating connections.
      Throws:
      IOException - if communication could not be initialized.
    • OtpNode

      public OtpNode(String node, String cookie, int port) throws 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:
      IOException - if communication could not be initialized.
    • OtpNode

      public OtpNode(String node, String cookie, int port, OtpTransportFactory transportFactory) throws 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.
      transportFactory - the transport factory to use when creating connections.
      Throws:
      IOException - if communication could not be initialized.
  • Method Details

    • 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 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 with reason 'normal'.
      Parameters:
      mbox - 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 with reason 'normal' will be sent.

    • closeMbox

      public void closeMbox(OtpMbox mbox, OtpErlangObject reason)
      Close the specified mailbox with the given reason.
      Parameters:
      mbox - the mailbox to close.
      reason - an Erlang term describing the reason for the termination.

      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 with the given reason will be sent.

    • createMbox

      public OtpMbox createMbox(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(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 supersede 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 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(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 ahandler)
      Register interest in certain system events. The OtpNodeStatus handler object contains callback methods, that will be called when certain events occur.
      Parameters:
      ahandler - the callback object to register. To clear the handler, specify null as the handler to use.
    • ping

      public boolean ping(String anode, 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:
      anode - 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.
    • setFlags

      public void setFlags(int flags)