Package com.ericsson.otp.erlang

# Class OtpSelf

java.lang.Object
com.ericsson.otp.erlang.AbstractNode
com.ericsson.otp.erlang.OtpLocalNode
com.ericsson.otp.erlang.OtpSelf

All Implemented Interfaces:
  `OtpTransportFactory`

Direct Known Subclasses:
  `OtpServer`

public class OtpSelf extends OtpLocalNode
Represents an OTP node. It is used to connect to remote nodes or accept incoming connections from remote nodes.

When the Java node will be connecting to a remote Erlang, Java or C node, it must first identify itself as a node by creating an instance of this class, after which it may connect to the remote node.

When you create an instance of this class, it will bind a socket to a port so that incoming connections can be accepted. However the port number will not be made available to other nodes wishing to connect until you explicitly register with the port mapper daemon by calling `publishPort()`.

     OtpSelf self = new OtpSelf("client", "authcookie"); // identify self
     OtpPeer other = new OtpPeer("server"); // identify peer

     OtpConnection conn = self.connect(other); // connect to peer

## Field Summary

### Fields inherited from class com.ericsson.otp.erlang.OtpLocalNode

`epmd, port`

## Constructor Summary

Constructors
Constructor
Description
`OtpSelf(String node)`
Create a self node using the default cookie.
`OtpSelf(String node, OtpTransportFactory transportFactory)`
Create a self node using the default cookie and custom transport factory.
`OtpSelf(String node, String cookie)`
Create a self node.
`OtpSelf(String node, String cookie, int port)`
Create a self node.
`OtpSelf(String node, String cookie, int port, OtpTransportFactory transportFactory)`
Create a self node.
`OtpSelf(String node, String cookie, OtpTransportFactory transportFactory)`
Create a self node.

## Method Summary

Modifier and Type
Method
Description
`OtpConnection`
`accept()`
Accept an incoming connection from a remote node.
`OtpConnection`
`connect(OtpPeer other)`
Open a connection to a remote node.
`OtpErlangPid`
`pid()`
Get the Erlang PID that will be used as the sender id in all "anonymous" messages sent by this node.
`boolean`
`publishPort()`
Make public the information needed by remote nodes that may wish to connect to this one.
`void`
`unPublishPort()`
Unregister the server node's name and port number from the Erlang port mapper, thus preventing any new connections from remote nodes.

### 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, createServerTransport, createTransport, createTransport, createTransport, host, node, setCookie, toString`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### OtpSelf

public OtpSelf(String node) throws IOException

Create a self 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` - in case of server transport failure

### OtpSelf

public OtpSelf(String node, OtpTransportFactory transportFactory) throws IOException

Create a self node using the default cookie and custom transport factory. 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` - in case of server transport failure

### OtpSelf

public OtpSelf(String node, String cookie) throws IOException
Create a self 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` - in case of server transport failure

### OtpSelf

public OtpSelf(String node, String cookie, OtpTransportFactory transportFactory) throws IOException
Create a self 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` - in case of server transport failure

### OtpSelf

public OtpSelf(String node, String cookie, int port) throws IOException
Create a self 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` - in case of server transport failure

### OtpSelf

public OtpSelf(String node, String cookie, int port, OtpTransportFactory transportFactory) throws IOException
Create a self 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` - in case of server transport failure

## Method Details

### pid

public OtpErlangPid pid()
Get the Erlang PID that will be used as the sender id in all "anonymous" messages sent by this node. Anonymous messages are those sent via send methods in `OtpConnection` that do not specify a sender.

Returns:
  the Erlang PID that will be used as the sender id in all anonymous messages sent by this node.

### publishPort

public boolean publishPort() throws IOException
Make public the information needed by remote nodes that may wish to connect to this one. This method establishes a connection to the Erlang port mapper (Epmd) and registers the server node's name and port so that remote nodes are able to connect.

This method will fail if an Epmd process is not running on the localhost. See the Erlang documentation for information about starting Epmd.

Note that once this method has been called, the node is expected to be available to accept incoming connections. For that reason you should make sure that you call `accept()` shortly after calling `publishPort()`. When you no longer intend to accept connections you should call `unPublishPort()`.

Returns:
  true if the operation was successful, false if the node was already registered.
Throws:
  `IOException` - if the port mapper could not be contacted.

### unPublishPort

public void unPublishPort()
Unregister the server node's name and port number from the Erlang port mapper, thus preventing any new connections from remote nodes.

### accept

public OtpConnection accept() throws IOException, OtpAuthException
Accept an incoming connection from a remote node. A call to this method will block until an incoming connection is at least attempted.

Returns:
  a connection to a remote node.
Throws:
  `IOException` - if a remote node attempted to connect but no common protocol was found.
  `OtpAuthException` - if a remote node attempted to connect, but was not authorized to connect.

### connect

public OtpConnection connect(OtpPeer other) throws IOException, UnknownHostException, OtpAuthException
Open a connection to a remote node.

Parameters:
  `other` - the remote node to which you wish to connect.
Returns:
  a connection to the remote node.
Throws:
  `UnknownHostException` - if the remote host could not be found.
  `IOException` - if it was not possible to connect to the remote node.
  `OtpAuthException` - if the connection was refused by the remote node.