erl_connect(3erl) | C Library Functions | erl_connect(3erl) |
erl_connect - Communicate with distributed Erlang.
This module provides support for communication between distributed Erlang nodes and C-nodes, in a manner that is transparent to Erlang processes.
A C-node appears to Erlang as a hidden node. That is, Erlang processes that know the name of the C-node can communicate with it in a normal manner, but the node name does not appear in the listing provided by erlang:nodes/0 in ERTS.
int erl_accept(listensock, conp)
Types:
This function is used by a server process to accept a connection from a client process.
typedef struct {
char ipadr[4];
char nodename[MAXNODELEN]; } ErlConnect;
On success, conp is filled in with the address and node name of the connecting client and a file descriptor is returned. On failure, ERL_ERROR is returned and erl_errno is set to EIO.
int erl_close_connection(fd)
Types:
Closes an open connection to an Erlang node.
Fd is a file descriptor obtained from erl_connect() or erl_xconnect().
Returns 0 on success. If the call fails, a non-zero value is returned, and the reason for the error can be obtained with the appropriate platform-dependent call.
int erl_connect(node)
int erl_xconnect(addr, alive)
Types:
Sets up a connection to an Erlang node.
erl_xconnect() requires the IP address of the remote host and the alivename of the remote node to be specified. erl_connect() provides an alternative interface, and determines the information from the node name provided.
Returns an open file descriptor on success, otherwise a negative value. In the latter case erl_errno is set to one of:
Also, errno values from socket(2) and connect(2) system calls can be propagated into erl_errno.
Example:
#define NODE "madonna@chivas.du.etx.ericsson.se" #define ALIVE "madonna" #define IP_ADDR "150.236.14.75" /*** Variant 1 ***/ erl_connect( NODE ); /*** Variant 2 ***/ struct in_addr addr; addr = inet_addr(IP_ADDR); erl_xconnect( &addr , ALIVE );
int erl_connect_init(number, cookie, creation)
int erl_connect_xinit(host, alive, node, addr, cookie, creation)
Types:
Initializes the erl_connect module. In particular, these functions are used to identify the name of the C-node from which they are called. One of these functions must be called before any of the other functions in the erl_connect module are used.
erl_connect_xinit() stores for later use information about:
erl_connect_init() provides an alternative interface that does not require as much information from the caller. Instead, erl_connect_init() uses gethostbyname() to obtain default values.
If you use erl_connect_init(), your node will have a short name, that is, it will not be fully qualified. If you need to use fully qualified (long) names, use erl_connect_xinit() instead.
A C-node acting as a server is assigned a creation number when it calls erl_publish().
number is used by erl_connect_init() to construct the actual node name. In Example 2 below, "c17@a.DNS.name" is the resulting node name.
Example 1:
struct in_addr addr; addr = inet_addr("150.236.14.75"); if (!erl_connect_xinit("chivas",
"madonna",
"madonna@chivas.du.etx.ericsson.se",
&addr;
"samplecookiestring..."),
0)
erl_err_quit("<ERROR> when initializing !");
Example 2:
if (!erl_connect_init(17, "samplecookiestring...", 0))
erl_err_quit("<ERROR> when initializing !");
int erl_publish(port)
Types:
This function is used by a server process to register with the local name server EPMD, thereby allowing other processes to send messages by using the registered name. Before calling this function, the process should have called bind() and listen() on an open socket.
port is the local name to register, and is to be the same as the port number that was previously bound to the socket.
To unregister with EPMD, simply close the returned descriptor.
On success, a descriptor connecting the calling process to EPMD is returned. On failure, -1 is returned and erl_errno is set to:
Also, errno values from socket(2) and connect(2) system calls can be propagated into erl_errno.
int erl_receive(fd, bufp, bufsize)
Types:
Receives a message consisting of a sequence of bytes in the Erlang external format.
If a tick occurs, that is, the Erlang node on the other end of the connection has polled this node to see if it is still alive, the function returns ERL_TICK and no message is placed in the buffer. Also, erl_errno is set to EAGAIN.
On success, the message is placed in the specified buffer and the function returns the number of bytes actually read. On failure, the function returns a negative value and sets erl_errno to one of:
int erl_receive_msg(fd, bufp, bufsize, emsg)
Types:
Receives the message into the specified buffer and decodes into (ErlMessage *) emsg.
typedef struct {
int type;
ETERM *msg;
ETERM *to;
ETERM *from;
char to_name[MAXREGLEN]; } ErlMessage;
type identifies the type of message, one of the following:
If a tick occurs, that is, the Erlang node on the other end of the connection has polled this node to see if it is still alive, the function returns ERL_TICK indicating that the tick has been received and responded to, but no message is placed in the buffer. In this case you are to call erl_receive_msg() again.
On success, the function returns ERL_MSG and the Emsg struct is initialized as described above, or ERL_TICK, in which case no message is returned. On failure, the function returns ERL_ERROR and sets erl_errno to one of:
int erl_reg_send(fd, to, msg)
Types:
Sends an Erlang term to a registered process.
Returns 1 on success, otherwise 0. In the latter case erl_errno is set to one of:
ETERM *erl_rpc(fd, mod, fun, args)
int erl_rpc_from(fd, timeout, emsg)
int erl_rpc_to(fd, mod, fun, args)
Types:
Supports calling Erlang functions on remote nodes. erl_rpc_to() sends an RPC request to a remote node and erl_rpc_from() receives the results of such a call. erl_rpc() combines the functionality of these two functions by sending an RPC request and waiting for the results. See also rpc:call/4 in Kernel.
The actual message returned by the RPC server is a 2-tuple {rex,Reply}. If you use erl_rpc_from() in your code, this is the message you will need to parse. If you use erl_rpc(), the tuple itself is parsed for you, and the message returned to your program is the Erlang term containing Reply only. Replies to RPC requests are always ERL_SEND messages.
erl_rpc() returns the remote function's return value on success, otherwise NULL.
erl_rpc_to() returns 0 on success, otherwise a negative number.
erl_rcp_from() returns ERL_MSG on success (with Emsg now containing the reply tuple), otherwise one of ERL_TICK, ERL_TIMEOUT, or ERL_ERROR.
When failing, all three functions set erl_errno to one of:
int erl_send(fd, to, msg)
Types:
Sends an Erlang term to a process.
Returns 1 on success, otherwise 0. In the latter case erl_errno is set to one of:
const char *erl_thisalivename()
const char *erl_thiscookie()
short erl_thiscreation()
const char *erl_thishostname()
const char *erl_thisnodename()
Retrieves information about the C-node. These values are initially set with erl_connect_init() or erl_connect_xinit().
int erl_unpublish(alive)
Types:
This function can be called by a process to unregister a specified node from EPMD on the local host. This is, however, usually not allowed, unless EPMD was started with flag -relaxed_command_check, which it normally is not.
To unregister a node you have published, you should instead close the descriptor that was returned by ei_publish().
alive is the name of the node to unregister, that is, the first component of the node name, without @hostname.
If the node was successfully unregistered from EPMD, 0 is returned, otherwise -1 is returned and erl_errno is set to EIO.
int erl_xreceive_msg(fd, bufpp, bufsizep, emsg)
Types:
Similar to erl_receive_msg. The difference is that erl_xreceive_msg expects the buffer to have been allocated by malloc, and reallocates it if the received message does not fit into the original buffer. Therefore both buffer and buffer length are given as pointers; their values can change by the call.
On success, the function returns ERL_MSG and the Emsg struct is initialized as described above, or ERL_TICK, in which case no message is returned. On failure, the function returns ERL_ERROR and sets erl_errno to one of:
struct hostent *erl_gethostbyaddr(addr, length, type)
struct hostent *erl_gethostbyaddr_r(addr, length, type, hostp, buffer,
buflen, h_errnop)
struct hostent *erl_gethostbyname(name)
struct hostent *erl_gethostbyname_r(name, hostp, buffer, buflen,
h_errnop)
Types:
Convenience functions for some common name lookup functions.
If a connection attempt fails, the following can be checked:
erl_interface 3.10.4 | Ericsson AB |