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.
Modifier and Type | Class and Description |
---|---|
class |
OtpNode.Acceptor |
class |
OtpNode.Mailboxes |
epmd, port
Constructor and Description |
---|
OtpNode(java.lang.String node)
Create a node using the default cookie.
|
OtpNode(java.lang.String node,
OtpTransportFactory transportFactory)
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.
|
OtpNode(java.lang.String node,
java.lang.String cookie,
int port,
OtpTransportFactory transportFactory)
Create a node.
|
OtpNode(java.lang.String node,
java.lang.String cookie,
OtpTransportFactory transportFactory)
Create a node.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the node.
|
void |
closeMbox(OtpMbox mbox)
Close the specified mailbox with reason 'normal'.
|
void |
closeMbox(OtpMbox mbox,
OtpErlangObject reason)
Close the specified mailbox with the given reason.
|
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 anode,
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 ahandler)
Register interest in certain system events.
|
void |
setFlags(int flags) |
OtpErlangPid |
whereis(java.lang.String name)
Determine the
pid corresponding to a registered name
on this node. |
createPid, createPort, createRef, getEpmd, port, setEpmd
alive, cookie, createServerTransport, createTransport, createTransport, host, node, setCookie, toString
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.
node
- the name of this node.java.io.IOException
- if communication could not be initialized.public OtpNode(java.lang.String node, OtpTransportFactory transportFactory) 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.
node
- the name of this node.transportFactory
- the transport factory to use when creating connections.java.io.IOException
- if communication could not be initialized.public OtpNode(java.lang.String node, java.lang.String cookie) throws java.io.IOException
node
- the name of this node.cookie
- the authorization cookie that will be used by this node when
it communicates with other nodes.java.io.IOException
- if communication could not be initialized.public OtpNode(java.lang.String node, java.lang.String cookie, OtpTransportFactory transportFactory) throws java.io.IOException
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.java.io.IOException
- if communication could not be initialized.public OtpNode(java.lang.String node, java.lang.String cookie, int port) throws java.io.IOException
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.java.io.IOException
- if communication could not be initialized.public OtpNode(java.lang.String node, java.lang.String cookie, int port, OtpTransportFactory transportFactory) throws java.io.IOException
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.java.io.IOException
- if communication could not be initialized.public void close()
protected void finalize()
finalize
in class java.lang.Object
public OtpMbox createMbox()
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
.public void closeMbox(OtpMbox mbox)
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.
public void closeMbox(OtpMbox mbox, OtpErlangObject reason)
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.
public OtpMbox createMbox(java.lang.String name)
pid
.name
- a name to register for this mailbox. The name must be unique
within this OtpNode.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.
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.public java.lang.String[] getNames()
public OtpErlangPid whereis(java.lang.String name)
pid
corresponding to a registered name
on this node.pid
corresponding to the registered
name, or null if the name is not known on this node.public void registerStatusHandler(OtpNodeStatus ahandler)
OtpNodeStatus
handler object contains callback methods, that will be
called when certain events occur.ahandler
- the callback object to register. To clear the handler, specify
null as the handler to use.public boolean ping(java.lang.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)) ;
anode
- the name of the node to ping.timeout
- the time, in milliseconds, to wait for response before
returning false.public void setFlags(int flags)