com.ericsson.otp.erlang
Class OtpServer

java.lang.Object
  |
  +--com.ericsson.otp.erlang.OtpNode
        |
        +--com.ericsson.otp.erlang.OtpSelf
              |
              +--com.ericsson.otp.erlang.OtpServer

public class OtpServer
extends OtpSelf

Represents a local OTP client or server node. It is used when you want other nodes to be able to establish connections to this one. 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 explicitely register with the port mapper daemon by calling publishPort().

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.

Setting up a connection may be done as follows:

 OtpServer self = new OtpServer("server","cookie"); // identify self
 self.publishPort(); // make port information available
 
 OtpConnection conn = self.accept(); // get incoming connection
 

See Also:
OtpSelf

Constructor Summary
OtpServer(OtpSelf self)
          Create an OtpServer from an existing OtpSelf.
OtpServer(java.lang.String node, java.lang.String cookie)
          Create an OtpServer, using a vacant port chosen by the operating system.
OtpServer(java.lang.String node, java.lang.String cookie, int port)
          Create an OtpServer, using the specified port number.
 
Method Summary
 OtpConnection accept()
          Accept an incoming connection from a remote node.
 int port()
          Get the port number used 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.OtpSelf
connect, pid
 
Methods inherited from class com.ericsson.otp.erlang.OtpNode
alive, cookie, host, node, setCookie, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

OtpServer

public OtpServer(OtpSelf self)
          throws java.io.IOException
Create an OtpServer from an existing OtpSelf.
Parameters:
self - an existing self node.
Throws:
java.io.IOException - if a ServerSocket could not be created.

OtpServer

public OtpServer(java.lang.String node,
                 java.lang.String cookie)
          throws java.io.IOException
Create an OtpServer, using a vacant port chosen by the operating system. To determine what port was chosen, call the object's port() method.
Parameters:
node - the name of the node.
cookie - the authorization cookie that will be used by this node when accepts connections from remote nodes.
Throws:
java.io.IOException - if a ServerSocket could not be created.

OtpServer

public OtpServer(java.lang.String node,
                 java.lang.String cookie,
                 int port)
          throws java.io.IOException
Create an OtpServer, using the specified port number.
Parameters:
node - a name for this node, as above.
cookie - the authorization cookie that will be used by this node when accepts connections from remote nodes.
port - the port number to bind the socket to.
Throws:
java.io.IOException - if a ServerSocket could not be created or if the chosen port number was not available.
Method Detail

port

public int port()
Get the port number used by this node.
Returns:
the port number this server node is accepting connections on.

publishPort

public boolean publishPort()
                    throws java.io.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:
java.io.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 java.io.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:
java.io.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.