~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to system/doc/tutorial/cnode_s2.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* cnode_s2.c */
 
2
 
 
3
#include <stdio.h>
 
4
#include <sys/types.h>
 
5
#include <sys/socket.h>
 
6
#include <netinet/in.h>
 
7
 
 
8
#include "erl_interface.h"
 
9
#include "ei.h"
 
10
 
 
11
#define BUFSIZE 1000
 
12
 
 
13
int main(int argc, char **argv) {
 
14
  struct in_addr addr;                     /* 32-bit IP number of host */
 
15
  int port;                                /* Listen port number */
 
16
  int listen;                              /* Listen socket */
 
17
  int fd;                                  /* fd to Erlang node */
 
18
  ErlConnect conn;                         /* Connection data */
 
19
 
 
20
  int loop = 1;                            /* Loop flag */
 
21
  int got;                                 /* Result of receive */
 
22
  unsigned char buf[BUFSIZE];              /* Buffer for incoming message */
 
23
  ErlMessage emsg;                         /* Incoming message */
 
24
 
 
25
  ETERM *fromp, *tuplep, *fnp, *argp, *resp;
 
26
  int res;
 
27
  
 
28
  port = atoi(argv[1]);
 
29
 
 
30
  erl_init(NULL, 0);
 
31
 
 
32
  addr.s_addr = inet_addr("134.138.177.89");
 
33
  if (erl_connect_xinit("idril", "cnode", "cnode@idril.du.uab.ericsson.se",
 
34
                        &addr, "secretcookie", 0) == -1)
 
35
    erl_err_quit("erl_connect_xinit");
 
36
 
 
37
  /* Make a listen socket */
 
38
  if ((listen = my_listen(port)) <= 0)
 
39
    erl_err_quit("my_listen");
 
40
 
 
41
  if (erl_publish(port) == -1)
 
42
    erl_err_quit("erl_publish");
 
43
 
 
44
  if ((fd = erl_accept(listen, &conn)) == ERL_ERROR)
 
45
    erl_err_quit("erl_accept");
 
46
  fprintf(stderr, "Connected to %s\n\r", conn.nodename);
 
47
 
 
48
  while (loop) {
 
49
 
 
50
    got = erl_receive_msg(fd, buf, BUFSIZE, &emsg);
 
51
    if (got == ERL_TICK) {
 
52
      /* ignore */
 
53
    } else if (got == ERL_ERROR) {
 
54
      loop = 0;
 
55
    } else {
 
56
 
 
57
      if (emsg.type == ERL_REG_SEND) {
 
58
        fromp = erl_element(2, emsg.msg);
 
59
        tuplep = erl_element(3, emsg.msg);
 
60
        fnp = erl_element(1, tuplep);
 
61
        argp = erl_element(2, tuplep);
 
62
 
 
63
        if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) {
 
64
          res = foo(ERL_INT_VALUE(argp));
 
65
        } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 3) == 0) {
 
66
          res = bar(ERL_INT_VALUE(argp));
 
67
        }
 
68
 
 
69
        resp = erl_format("{cnode, ~i}", res);
 
70
        erl_send(fd, fromp, resp);
 
71
 
 
72
        erl_free_term(emsg.from); erl_free_term(emsg.msg);
 
73
        erl_free_term(fromp); erl_free_term(tuplep);
 
74
        erl_free_term(fnp); erl_free_term(argp);
 
75
        erl_free_term(resp);
 
76
      }
 
77
    }
 
78
  }
 
79
}
 
80
 
 
81
  
 
82
int my_listen(int port) {
 
83
  int listen_fd;
 
84
  struct sockaddr_in addr;
 
85
  int on = 1;
 
86
 
 
87
  if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
 
88
    return (-1);
 
89
 
 
90
  setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
 
91
 
 
92
  memset((void*) &addr, 0, (size_t) sizeof(addr));
 
93
  addr.sin_family = AF_INET;
 
94
  addr.sin_port = htons(port);
 
95
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
 
96
 
 
97
  if (bind(listen_fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
 
98
    return (-1);
 
99
 
 
100
  listen(listen_fd, 5);
 
101
  return listen_fd;
 
102
}