Overview

## 2 Overview

### 2.1 Built-In Mechanisms

There are two interoperability mechanisms built into the Erlang runtime system. One is distributed Erlang and the other one is ports. A variation of ports is linked-in drivers.

#### 2.1.1 Distributed Erlang

An Erlang runtime system is made into a distributed Erlang node by giving it a name. A distributed Erlang node can connect to and monitor other nodes, it is also possible to spawn processes at other nodes. Message passing and error handling between processes at different nodes are transparent. There exists a number of useful `stdlib` modules intended for use in a distributed Erlang system; for example, `global` which provides global name registration. The distribution mechanism is implemented using TCP/IP sockets.

When to use: Distributed Erlang is primarily used for communication Erlang-Erlang. It can also be used for communication between Erlang and C, if the C program is implemented as a C node, see below.

Where to read more: Distributed Erlang and some distributed programming techniques are described in the Erlang book.
In the Erlang/OTP documentation there is a chapter about distributed Erlang in "Getting Started" (User's Guide).
Relevent man pages are `erlang` (describes the BIFs) and `global`, `net_adm`, `pg2`, `rpc`, `pool` and `slave`.

#### 2.1.2 Ports and Linked-In Drivers

Ports provide the basic mechanism for communication with the external world, from Erlang's point of view. They provide a byte-oriented interface to an external program. When a port has been created, Erlang can communicate with it by sending and receiving lists of bytes (not Erlang terms). This means that the programmer may have to invent a suitable encoding and decoding scheme.

The actual implementation of the port mechanism depends on the platform. In the Unix case, pipes are used and the external program should as default read from standard input and write to standard output. Theoratically, the external program could be written in any programming language as long as it can handle the interprocess communication mechanism with which the port is implemented.

The external program resides in another OS process than the Erlang runtime system. In some cases this is not acceptable, consider for example drivers with very hard time requirements. It is therefore possible to write a program in C according to certain principles and dynamically link it to the Erlang runtime system, this is called a linked-in driver.

When to use: Being the basic mechanism, ports can be used for all kinds of interoperability situations where the Erlang program and the other program runs on the same machine. Programming is fairly straight-forward.
Linked-in drivers involves writing certain callback functions in C. Very good skills are required as the code is linked to the Erlang runtime system.

| An erronous linked-in driver will cause the entire Erlang runtime system to leak memory, hang or crash.

Where to read more: Ports are described in the "Miscellaneous Items" chapter of the Erlang book. Linked-in drivers are described in Appendix E.
The BIF `open_port/2` is documented in the man page for `erlang`. For linked-in drivers, the programmer needs to read the information in the man page for `erl_ddll`.

Examples: Port example.

### 2.2 C and Java Libraries

#### 2.2.1 Erl_Interface

Very often the program at the other side of a port is a C program. To help the C programmer a library called Erl_Interface has been developed. It consists of five parts:

- `erl_marshal`, `erl_eterm`, `erl_format`, `erl_malloc` Handling of the Erlang external term format.
- `erl_connect` Communication with distributed Erlang, see C nodes below.
- `erl_error` Error print routines.
- `erl_global` Access globally registered names.
- `Registry` Store and backup of key-value pairs.

The Erlang external term format is a representation of an Erlang term as a sequence of bytes, a binary. Conversion between the two representations is done using BIFs.

    Binary = term_to_binary(Term)
    Term = binary_to_term(Binary)

A port can be set to use binaries instead of lists of bytes. It is then not necessary to invent any encoding/decoding scheme. Erl_Interface functions are used for unpacking the binary and convert it into a struct similar to an Erlang term. Such a struct can be manipulated in different ways and be converted to the Erlang external format and sent to Erlang.

When to use: In C code, in conjunction with Erlang binaries.

Where to read more: Read about the Erl_Interface User's Guide; Command Reference and Library Reference. In R5B and earlier versions the information can be found under the Kernel application.

Examples: erl_interface example.

#### 2.2.2 C Nodes

A C program which uses the Erl_Interface functions for setting up a connection to and communicating with a distributed Erlang node is called a C node, or a hidden node. The main advantage with a C node is that the communication from the Erlang programmer's point of view is extremely easy, since the C program behaves as a distributed Erlang node.

When to use: C nodes can typically be used on device processors (as opposed to control processors) where C is a better choice than Erlang due to memory limitations and/or application characteristics.

Where to read more: In the `erl_connect` part of the Erl_Interface documentation, see above. The programmer also needs to be familiar with TCP/IP sockets, see below, and distributed Erlang, see above.

Examples: C node example.

#### 2.2.3 Jinterface

In Erlang/OTP R6B, a library similar to Erl_Interface for Java was added called jinterface.

### 2.3 Standard Protocols

Sometimes communication between an Erlang program and another program using a standard protocol is desirable. Erlang/OTP currently supports TCP/IP and UDP sockets, SNMP, HTTP and IIOP (CORBA). Using one of the latter three requires good knowledge about the protocol and is not covered by this tutorial. Please refer to the documentation for the SNMP, Inets and Orber applications, respectively.

#### 2.3.1 Sockets

Simply put, connection-oriented socket communication (TCP/IP) consists of an initiator socket ("server") started at a certain host with a certain port number. A connector socket ("client") aware of the initiator's host name and port number can connect to it and data can be sent between them. Connection-less socket communication (UDP) consists of an initiator socket at a certain host with a certain port number and a connector socket sending data to it. For a detailed description of the socket concept, please refer to a suitable book about network programming. A suggestion is UNIX Network Programming, Volume 1: Networking APIs - Sockets and XTI by W. Richard Stevens, ISBN: 013490012X.

In Erlang/OTP, access to TCP/IP and UDP sockets is provided by the Kernel modules `gen_tcp` and `gen_udp`. Both are easy to use and do not require any deeper knowledge about the socket concept.

When to use: For programs running on the same or on another machine than the Erlang program.

Where to read more: The man pages for `gen_tcp` and `gen_udp`.

### 2.4 IC

IC (IDL Compiler) is an interface generator which given an IDL interface specification automatically generates stub code in Erlang, C or Java. Please refer to the IC User's Guide and IC Reference Manual.

### 2.5 Old Applications

There are two old applications of interest when talking about interoperability: IG which was removed in Erlang/OTP R6B and Jive which was removed in Erlang/OTP R7B. Both applications have been replaced by IC and are mentioned here for reference only.

IG (Interface Generator) automatically generated code for port or socket communication between an Erlang program and a C program, given a C header file with certain keywords. Jive provided a simple interface between an Erlang program and a Java program.

Copyright © 1991-2006 Ericsson AB