1 /* ei.c */
2
3 #include "ei.h"
4 #include <unistd.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 typedef unsigned char byte;
9
10 int read_cmd(byte *buf);
11 int write_cmd(byte *buf, int len);
12 int foo(int x);
13 int bar(int y);
14
fail(int place)15 static void fail(int place) {
16 fprintf(stderr, "Something went wrong %d\n", place);
17 exit(1);
18 }
19
main()20 int main() {
21 byte buf[100];
22 int index = 0;
23 int version = 0;
24 int arity = 0;
25 char atom[128];
26 long in = 0;
27 int res = 0;
28 ei_x_buff res_buf;
29 ei_init();
30 while (read_cmd(buf) > 0) {
31 if (ei_decode_version(buf, &index, &version) != 0)
32 fail(1);
33 if (ei_decode_tuple_header(buf, &index, &arity) != 0)
34 fail(2);
35 if (arity != 2)
36 fail(3);
37 if (ei_decode_atom(buf, &index, atom) != 0)
38 fail(4);
39 if (ei_decode_long(buf, &index, &in) != 0)
40 fail(5);
41 if (strncmp(atom, "foo", 3) == 0) {
42 res = foo((int)in);
43 } else if (strncmp(atom, "bar", 3) == 0) {
44 res = bar((int)in);
45 }
46 if (ei_x_new_with_version(&res_buf) != 0)
47 fail(6);
48 if (ei_x_encode_long(&res_buf, res) != 0)
49 fail(7);
50 write_cmd(res_buf.buff, res_buf.index);
51
52 if (ei_x_free(&res_buf) != 0)
53 fail(8);
54 index = 0;
55 }
56 }
57
58