Class OtpNode
- All Implemented Interfaces:
OtpTransportFactory
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.
-
Nested Class Summary
-
Field Summary
Fields inherited from class com.ericsson.otp.erlang.OtpLocalNode
epmd, port
-
Constructor Summary
ConstructorDescriptionCreate a node using the default cookie.OtpNode
(String node, OtpTransportFactory transportFactory) Create a node using the default cookie.Create a node.Create a node.OtpNode
(String node, String cookie, int port, OtpTransportFactory transportFactory) Create a node.OtpNode
(String node, String cookie, OtpTransportFactory transportFactory) Create a node. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the node.void
Close the specified mailbox with reason 'normal'.void
closeMbox
(OtpMbox mbox, OtpErlangObject reason) Close the specified mailbox with the given reason.Create an unnamedmailbox
that can be used to send and receive messages with other, similar mailboxes and with Erlang processes.createMbox
(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()
String[]
getNames()
Get a list of all known registered names on this node.boolean
Determine if another node is alive.boolean
registerName
(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) Determine thepid
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, createServerTransport, createTransport, createTransport, host, node, setCookie, toString
-
Constructor Details
-
OtpNode
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
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
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
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
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() -
createMbox
Create an unnamedmailbox
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 associatedpid
.- Returns:
- a mailbox.
-
closeMbox
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
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
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 associatedpid
.- 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
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
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
Determine thepid
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
Register interest in certain system events. TheOtpNodeStatus
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
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)
-