/* ei.c */ #include "ei.h" #include #include #include typedef unsigned char byte; int read_cmd(byte *buf); int write_cmd(byte *buf, int len); int foo(int x); int bar(int y); static void fail(int place) { fprintf(stderr, "Something went wrong %d\n", place); exit(1); } int main() { byte buf[100]; int index = 0; int version = 0; int arity = 0; char atom[128]; long in = 0; int res = 0; ei_x_buff res_buf; ei_init(); while (read_cmd(buf) > 0) { if (ei_decode_version(buf, &index, &version) != 0) fail(1); if (ei_decode_tuple_header(buf, &index, &arity) != 0) fail(2); if (arity != 2) fail(3); if (ei_decode_atom(buf, &index, atom) != 0) fail(4); if (ei_decode_long(buf, &index, &in) != 0) fail(5); if (strncmp(atom, "foo", 3) == 0) { res = foo((int)in); } else if (strncmp(atom, "bar", 3) == 0) { res = bar((int)in); } if (ei_x_new_with_version(&res_buf) != 0) fail(6); if (ei_x_encode_long(&res_buf, res) != 0) fail(7); write_cmd(res_buf.buff, res_buf.index); if (ei_x_free(&res_buf) != 0) fail(8); index = 0; } }