A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://www.erlang.org/doc/apps/erl_interface/ei_connect below:

ei_connect — erl_interface v5.6

Communicate with distributed Erlang.

Description

This module enables C-programs to communicate with Erlang nodes, using the Erlang distribution over TCP/IP.

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 is not shown in the listing provided by erlang:nodes/0 in ERTS.

The environment variable ERL_EPMD_PORT can be used to indicate which logical cluster a C-node belongs to.

Time-Out Functions

Most functions appear in a version with the suffix _tmo appended to the function name. Those functions take an extra argument, a time-out in milliseconds. The semantics is this: for each communication primitive involved in the operation, if the primitive does not complete within the time specified, the function returns an error and erl_errno is set to ETIMEDOUT. With communication primitive is meant an operation on the socket, like connect, accept, recv, or send.

Clearly the time-outs are for implementing fault tolerance, not to keep hard real-time promises. The _tmo functions are for detecting non-responsive peers and to avoid blocking on socket operations.

A time-out value of 0 (zero) means that time-outs are disabled. Calling a _tmo function with the last argument as 0 is therefore the same thing as calling the function without the _tmo suffix.

As with all other functions starting with ei_, you are not expected to put the socket in non-blocking mode yourself in the program. Every use of non-blocking mode is embedded inside the time-out functions. The socket will always be back in blocking mode after the operations are completed (regardless of the result). To avoid problems, leave the socket options alone. ei handles any socket options that need modification.

In all other senses, the _tmo functions inherit all the return values and the semantics from the functions without the _tmo suffix.

User Supplied Socket Implementation

By default ei supplies a TCP/IPv4 socket interface that is used when communicating. The user can however plug in his/her own IPv4 socket implementation. This, for example, in order to communicate over TLS. A user supplied socket implementation is plugged in by passing a callback structure to either ei_connect_init_ussi() or ei_connect_xinit_ussi().

All callbacks in the ei_socket_callbacks structure should return zero on success; and a posix error code on failure.

The addr argument of the listen, accept, and connect callbacks refer to appropriate address structure for currently used protocol. Currently ei only supports IPv4. That is, at this time addr always points to a struct sockaddr_in structure.

The ei_socket_callbacks structure may be enlarged in the future. All fields not set, needs to be zeroed out. Currently the following fields exist:

Data Types ei_gethostbyaddr() ei_gethostbyaddr_r() ei_gethostbyname() ei_gethostbyname_r()
struct hostent * ei_gethostbyaddr(const char *addr, int len, int type);
struct hostent * ei_gethostbyaddr_r(const char *addr, int length,  int type,
  struct hostent *hostp, char *buffer,   int buflen,  int *h_errnop);
struct hostent * ei_gethostbyname(const char *name);
struct hostent * ei_gethostbyname_r(const char *name,  struct hostent *hostp,
  char *buffer,  int buflen,  int *h_errnop);

Convenience functions for some common name lookup functions.

ei_accept()
int ei_accept(ei_cnode *ec, int listensock, ErlConnect *conp);

Used by a server process to accept a connection from a client process.

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.

ei_accept_tmo()
int ei_accept_tmo(ei_cnode *ec, int listensock, ErlConnect *conp, unsigned timeout_ms);

Equivalent to ei_accept with an optional time-out argument, see the description at the beginning of this manual page.

ei_close_connection()
int ei_close_connection(int fd);

Closes a previously opened connection or listen socket.

Available since OTP 21.3

ei_connect() ei_xconnect() ei_connect_host_port()

Available since OTP 23.0

ei_xconnect_host_port()
int ei_connect(ei_cnode* ec, char *nodename);
int ei_xconnect(ei_cnode* ec, Erl_IpAddr adr, char *alivename);
int ei_connect_host_port(ei_cnode* ec, char *hostname, int port);
int ei_xconnect_host_port(ei_cnode* ec, Erl_IpAddr adr, int port);

Sets up a connection to an Erlang node.

ei_xconnect() requires the IP address of the remote host and the alive name of the remote node to be specified. ei_connect() provides an alternative interface and determines the information from the node name provided. The ei_xconnect_host_port() function provides yet another alternative that will work even if there is no EPMD instance on the host where the remote node is running. The ei_xconnect_host_port() function requires the IP address and port of the remote node to be specified. The ei_connect_host_port() function is an alternative to ei_xconnect_host_port() that lets the user specify a hostname instead of an IP address.

These functions return an open file descriptor on success, or a negative value indicating that an error occurred. In the latter case they set erl_errno to one of the following:

Also, errno values from socket(2) and connect(2) system calls may 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 ***/
int fd = ei_connect(&ec, NODE);

/*** Variant 2 ***/
struct in_addr addr;
addr.s_addr = inet_addr(IP_ADDR);
fd = ei_xconnect(&ec, &addr, ALIVE);

Available since OTP 23.0

ei_connect_init() ei_connect_init_ussi()

Available since OTP 21.3

ei_connect_xinit() ei_connect_xinit_ussi()
int ei_connect_init(ei_cnode* ec, const char* this_node_name, const char *cookie, unsigned creation);
int ei_connect_init_ussi(ei_cnode* ec, const char* this_node_name, const char *cookie,
  unsigned creation, ei_socket_callbacks *cbs, int cbs_sz, void *setup_context);
int ei_connect_xinit(ei_cnode* ec, const char *thishostname, const char *thisalivename,
  const char *thisnodename, Erl_IpAddr thisipaddr, const char *cookie, unsigned creation);
int ei_connect_xinit_ussi(ei_cnode* ec, const char *thishostname, const char *thisalivename,
  const char *thisnodename, Erl_IpAddr thisipaddr, const char *cookie, unsigned creation,
  ei_socket_callbacks *cbs, int cbs_sz, void *setup_context);

Initializes the ec structure, to identify the node name and cookie of the server. One of them must be called before other functions that works on the ei_cnode type or a file descriptor associated with a connection to another node is used.

A C-node acting as a server is assigned a creation number when it calls ei_publish().

A connection is closed by simply closing the socket. For information about how to close the socket gracefully (when there are outgoing packets before close), see the relevant system documentation.

These functions return a negative value indicating that an error occurred.

Example 1:

unsigned n = 0;
struct in_addr addr;
ei_cnode ec;
addr.s_addr = inet_addr("150.236.14.75");
if (ei_connect_xinit(&ec,
                     "chivas",
                     "madonna",
                     "madonna@chivas.du.etx.ericsson.se",
                     &addr;
                     "cookie...",
                     n++) < 0) {
    fprintf(stderr,"ERROR when initializing: %d",erl_errno);
    exit(-1);
}

Example 2:

if (ei_connect_init(&ec, "madonna", "cookie...", n++) < 0) {
    fprintf(stderr,"ERROR when initializing: %d",erl_errno);
    exit(-1);
}

Available since OTP 21.3

ei_connect_tmo() ei_xconnect_tmo() ei_connect_host_port_tmo()

Available since OTP 23.0

ei_xconnect_host_port_tmo()
int ei_connect_tmo(ei_cnode* ec, char *nodename, unsigned timeout_ms);
int ei_xconnect_tmo(ei_cnode* ec, Erl_IpAddr adr, char *alivename, unsigned timeout_ms);
int ei_connect_host_port_tmo(ei_cnode* ec, char *hostname, int port, unsigned ms);
int ei_xconnect_host_port_tmo(ei_cnode* ec, Erl_IpAddr adr, int port, unsigned ms);

Equivalent to ei_connect, ei_xconnect, ei_connect_host_port and ei_xconnect_host_port with an optional time-out argument, see the description at the beginning of this manual page.

Available since OTP 23.0

ei_get_tracelevel()

Available since OTP R13B04

ei_set_tracelevel()
int ei_get_tracelevel(void);
void ei_set_tracelevel(int level);

Used to set tracing on the distribution. The levels are different verbosity levels. A higher level means more information. See also section Debug Information.

These functions are not thread safe.

Available since OTP R13B04

ei_listen()

Available since OTP 21.3

ei_xlisten()
int ei_listen(ei_cnode *ec, int *port, int backlog);
int ei_xlisten(ei_cnode *ec, Erl_IpAddr adr, int *port, int backlog);

Used by a server process to setup a listen socket which later can be used for accepting connections from client processes.

ei_listen will create a socket, bind to a port on the local interface identified by adr (or all local interfaces if ei_listen() is called), and mark the socket as a passive socket (that is, a socket that will be used for accepting incoming connections).

On success, a file descriptor is returned which can be used in a call to ei_accept(). On failure, ERL_ERROR is returned and erl_errno is set to EIO.

Available since OTP 21.3

ei_make_pid()
int ei_make_pid(ei_cnode *ec, erlang_pid *pid);

Creates a new process identifier in the argument pid. This process identifier refers to a conseptual process residing on the C-node identified by the argument ec. On success 0 is returned. On failure ERL_ERROR is returned and erl_errno is set.

The C-node identified by ec must have been initialized and must have received a name prior to the call to ei_make_pid(). Initialization of the C-node is done by a call to ei_connect_init() or friends. If the name is dynamically assigned from the peer node, the C-node also has to be connected.

Available since OTP 23.0

ei_make_ref()
int ei_make_ref(ei_cnode *ec, erlang_ref *ref);

Creates a new reference in the argument ref. This reference originates from the C-node identified by the argument ec. On success 0 is returned. On failure ERL_ERROR is returned and erl_errno is set.

The C-node identified by ec must have been initialized and must have received a name prior to the call to ei_make_ref(). Initialization of the C-node is done by a call to ei_connect_init() or friends. If the name is dynamically assigned from the peer node, the C-node also has to be connected.

Available since OTP 23.0

ei_publish()
int ei_publish(ei_cnode *ec, int port);

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 either of these functions, the process should have called bind() and listen() on an open socket.

To unregister with EPMD, simply close the returned descriptor. Do not use ei_unpublish(), which is deprecated anyway.

On success, the function returns a descriptor connecting the calling process to EPMD. On failure, -1 is returned and erl_errno is set to EIO.

Also, errno values from socket(2) and connect(2) system calls may be propagated into erl_errno.

ei_publish_tmo()
int ei_publish_tmo(ei_cnode *ec, int port, unsigned timeout_ms);

Equivalent to ei_publish with an optional time-out argument, see the description at the beginning of this manual page.

ei_receive()
int ei_receive(int fd, unsigned char* bufp, int bufsize);

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 ERL_ERROR and sets erl_errno to one of the following:

ei_receive_encoded()
int ei_receive_encoded(int fd, char **mbufp, int *bufsz,  erlang_msg *msg, int *msglen);

This function is retained for compatibility with code generated by the interface compiler and with code following examples in the same application.

In essence, the function performs the same operation as ei_xreceive_msg, but instead of using an ei_x_buff, the function expects a pointer to a character pointer (mbufp), where the character pointer is to point to a memory area allocated by malloc. Argument bufsz is to be a pointer to an integer containing the exact size (in bytes) of the memory area. The function may reallocate the memory area and will in such cases put the new size in *bufsz and update *mbufp.

Returns either ERL_TICK or the msgtype field of the erlang_msg *msg. The length of the message is put in *msglen. On error a value < 0 is returned.

It is recommended to use ei_xreceive_msg instead when possible, for the sake of readability. However, the function will be retained in the interface for compatibility and will not be removed in future releases without prior notice.

ei_receive_encoded_tmo()
int ei_receive_encoded_tmo(int fd, char **mbufp, int *bufsz,  erlang_msg *msg,
  int *msglen, unsigned timeout_ms);

Equivalent to ei_receive_encoded with an optional time-out argument, see the description at the beginning of this manual page.

ei_receive_msg() ei_xreceive_msg()
int ei_receive_msg(int fd, erlang_msg* msg, ei_x_buff* x);
int ei_xreceive_msg(int fd, erlang_msg* msg, ei_x_buff* x);

Receives a message to the buffer in x. ei_xreceive_msg allows the buffer in x to grow, but ei_receive_msg fails if the message is larger than the pre-allocated buffer in x.

On success, the functions return ERL_MSG and the msg struct is initialized.

msgtype identifies the type of message, and is one of the following:

The return value is the same as for ei_receive.

ei_receive_msg_tmo() ei_xreceive_msg_tmo()
int ei_receive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned imeout_ms);
int ei_xreceive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned timeout_ms);

Equivalent to ei_receive_msg and ei_xreceive_msg with an optional time-out argument, see the description at the beginning of this manual page.

ei_receive_tmo()
int ei_receive_tmo(int fd, unsigned char* bufp, int bufsize, unsigned timeout_ms);

Equivalent to ei_receive with an optional time-out argument, see the description at the beginning of this manual page.

ei_reg_send()
int ei_reg_send(ei_cnode* ec, int fd, char* server_name, char* buf, int len);

Sends an Erlang term to a registered process.

Returns 0 if successful, otherwise -1. In the latter case it sets erl_errno to EIO.

Example:

Send the atom "ok" to the process "worker":

ei_x_buff x;
ei_x_new_with_version(&x);
ei_x_encode_atom(&x, "ok");
if (ei_reg_send(&ec, fd, x.buff, x.index) < 0)
    handle_error();
ei_reg_send_tmo()
int ei_reg_send_tmo(ei_cnode* ec, int fd, char* server_name, char* buf, int len,
  unsigned timeout_ms);

Equivalent to ei_reg_send with an optional time-out argument, see the description at the beginning of this manual page.

ei_rpc() ei_rpc_to() ei_xrpc_to()

Available since OTP 24.0

ei_rpc_from()
int ei_rpc(ei_cnode *ec, int fd, char *mod, char *fun, const char *argbuf,
  int argbuflen, ei_x_buff *x);
int ei_rpc_to(ei_cnode *ec, int fd, char *mod, char *fun, const char *argbuf,
  int argbuflen);
int ei_xrpc_to(ei_cnode *ec, int fd, char *mod, char *fun, const char *argbuf,
  int argbuflen, int flags);
int ei_rpc_from(ei_cnode *ec, int fd, int timeout, erlang_msg *msg, ei_x_buff *x);

Supports calling Erlang functions on remote nodes. ei_rpc_to() sends an RPC request to a remote node and ei_rpc_from() receives the results of such a call. ei_rpc() combines the functionality of these two functions by sending an RPC request and waiting for the results.

The ei_xrpc_to() function is equivalent to ei_rpc_to() when its flags parameter is set to 0. When the flags parameter of ei_xrpc_to() is set to EI_RPC_FETCH_STDOUT, stdout (standard output) data are forwarded. See the documentation for the flags parameter for more information about the EI_RPC_FETCH_STDOUT flag.

See also rpc:call/4 in Kernel.

ei_rpc() returns the number of bytes in the result on success and -1 on failure. ei_rpc_from() returns the number of bytes, otherwise one of ERL_TICK, ERL_TIMEOUT, and ERL_ERROR. The functions ei_rpc_to() and ei_xrpc_to() returns 0 if successful, otherwise -1. When failing, all four functions set erl_errno to one of the following:

Example:

Check to see if an Erlang process is alive:

int index = 0, is_alive;
ei_x_buff args, result;

ei_x_new(&result);
ei_x_new(&args);
ei_x_encode_list_header(&args, 1);
ei_x_encode_pid(&args, &check_pid);
ei_x_encode_empty_list(&args);

if (ei_rpc(&ec, fd, "erlang", "is_process_alive",
           args.buff, args.index, &result) < 0)
    handle_error();

if (ei_decode_version(result.buff, &index) < 0
    || ei_decode_bool(result.buff, &index, &is_alive) < 0)
    handle_error();
ei_self()
erlang_pid * ei_self(ei_cnode *ec);

Retrieves a generic pid of the C-node. Every C-node has a (pseudo) pid used in ei_send_reg, ei_rpc(), and others. This is contained in a field in the ec structure. Do not modify this structure.

On success a pointer to the process identifier is returned. On failure NULL is returned and erl_errno is set.

The C-node identified by ec must have been initialized and must have received a name prior to the call to ei_self(). Initialization of the C-node is done by a call to ei_connect_init() or friends. If the name is dynamically assigned from the peer node, the C-node also has to be connected.

ei_send()
int ei_send(int fd, erlang_pid* to, char* buf, int len);

Sends an Erlang term to a process.

Returns 0 if successful, otherwise -1. In the latter case it sets erl_errno to EIO.

ei_send_encoded()
int ei_send_encoded(int fd, erlang_pid* to, char* buf, int len);

Works exactly as ei_send, the alternative name is retained for backward compatibility. The function will not be removed without prior notice.

ei_send_encoded_tmo()
int ei_send_encoded_tmo(int fd, erlang_pid* to, char* buf, int len, unsigned timeout_ms);

Equivalent to ei_send_encoded with an optional time-out argument, see the description at the beginning of this manual page.

ei_send_reg_encoded()
int ei_send_reg_encoded(int fd, const erlang_pid *from, const char *to, const char *buf, int len);

This function is retained for compatibility with code generated by the interface compiler and with code following examples in the same application.

The function works as ei_reg_send with one exception. Instead of taking ei_cnode as first argument, it takes a second argument, an erlang_pid, which is to be the process identifier of the sending process (in the Erlang distribution protocol).

A suitable erlang_pid can be retrieved from the ei_cnode structure by calling ei_self(cnode_pointer).

ei_send_reg_encoded_tmo()
int ei_send_reg_encoded_tmo(int fd, const erlang_pid *from, const char *to, const char *buf,
  int len, unsigned timeout_ms);

Equivalent to ei_send_reg_encoded with an optional time-out argument, see the description at the beginning of this manual page.

ei_send_tmo()
int ei_send_tmo(int fd, erlang_pid* to, char* buf, int len, unsigned timeout_ms);

Equivalent to ei_send with an optional time-out argument, see the description at the beginning of this manual page.

ei_thisnodename() ei_thishostname() ei_thisalivename()
const char * ei_thisnodename(ei_cnode *ec);
const char * ei_thishostname(ei_cnode *ec);
const char * ei_thisalivename(ei_cnode *ec);

Can be used to retrieve information about the C-node. These values are initially set with ei_connect_init() or ei_connect_xinit().

These function simply fetch the appropriate field from the ec structure. Read the field directly will probably be safe for a long time, so these functions are not really needed.

ei_unpublish()
int ei_unpublish(ei_cnode *ec);

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 close the descriptor that was returned by ei_publish().

Warning

This function is deprecated and will be removed in a future release.

ec is the node structure of the node to unregister.

If the node was successfully unregistered from EPMD, the function returns 0. Otherwise, -1 is returned and erl_errno is set to EIO.

ei_unpublish_tmo()
int ei_unpublish_tmo(ei_cnode *ec, unsigned timeout_ms);

Equivalent to ei_unpublish with an optional time-out argument, see the description at the beginning of this manual page.

Debug Information

If a connection attempt fails, the following can be checked:

The connection attempt can be traced by setting a trace level by either using ei_set_tracelevel or by setting environment variable EI_TRACELEVEL. The trace levels have the following messages:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4