4
* Copyright Ericsson AB 2001-2009. All Rights Reserved.
6
* The contents of this file are subject to the Erlang Public License,
7
* Version 1.1, (the "License"); you may not use this file except in
8
* compliance with the License. You should have received a copy of the
9
* Erlang Public License along with this software. If not, it can be
10
* retrieved online at http://www.erlang.org/.
12
* Software distributed under the License is distributed on an "AS IS"
13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
* the License for the specific language governing rights and limitations
21
* Purpose: Tests the functions in ei_connect.c.
22
* Author: Bjorn Gustavsson (rewritten somewhat by Jakob Cederlund)
24
* See the ei_connect_SUITE.erl file for a "table of contents".
33
#include "ei_runner.h"
35
static void cmd_ei_connect_init(char* buf, int len);
36
static void cmd_ei_connect(char* buf, int len);
37
static void cmd_ei_send(char* buf, int len);
38
static void cmd_ei_send_funs(char* buf, int len);
39
static void cmd_ei_reg_send(char* buf, int len);
40
static void cmd_ei_rpc(char* buf, int len);
41
static void cmd_ei_set_get_tracelevel(char* buf, int len);
43
static void send_errno_result(int value);
50
int num_args; /* Number of arguments. */
51
void (*func)(char* buf, int len);
53
"ei_connect_init", 3, cmd_ei_connect_init,
54
"ei_connect", 1, cmd_ei_connect,
55
"ei_send", 3, cmd_ei_send,
56
"ei_send_funs", 3, cmd_ei_send_funs,
57
"ei_reg_send", 3, cmd_ei_reg_send,
58
"ei_rpc", 4, cmd_ei_rpc,
59
"ei_set_get_tracelevel", 1, cmd_ei_set_get_tracelevel,
64
* Sends a list contaning all data types to the Erlang side.
75
if (get_bin_term(&x, &term)) {
79
char* buf = x.buff, func[MAXATOMLEN];
80
int index = x.index, arity;
81
if (term.ei_type != ERL_SMALL_TUPLE_EXT || term.arity != 2)
82
fail("term should be a tuple of size 2");
83
if (ei_decode_atom(buf, &index, func) < 0)
84
fail("function name should be an atom");
85
if (ei_decode_tuple_header(buf, &index, &arity) != 0)
86
fail("function arguments should be a tuple");
87
for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++) {
88
if (strcmp(func, commands[i].name) == 0) {
89
if (arity != commands[i].num_args)
90
fail("wrong number of arguments");
91
commands[i].func(buf + index, x.buffsz - index);
95
if (i >= sizeof(commands)/sizeof(commands[0])) {
96
message("\"%d\" \n", func);
104
static void cmd_ei_connect_init(char* buf, int len)
106
int index = 0, r = 0;
110
char cookie[MAXATOMLEN], * cp = cookie;
112
if (ei_decode_long(buf, &index, &l) < 0)
113
fail("expected int");
114
sprintf(b, "c%d", l);
115
/* FIXME don't use internal and maybe use skip?! */
116
ei_get_type_internal(buf, &index, &type, &size);
117
if (ei_decode_atom(buf, &index, cookie) < 0)
118
fail("expected atom (cookie)");
119
if (cookie[0] == '\0')
121
r = ei_connect_init(&ec, b, cp, 0);
122
ei_x_new_with_version(&res);
123
ei_x_encode_long(&res, r);
128
static void cmd_ei_connect(char* buf, int len)
133
if (ei_decode_atom(buf, &index, node) < 0)
134
fail("expected atom");
135
i=ei_connect(&ec, node);
141
send_errno_result(i);
144
static void cmd_ei_set_get_tracelevel(char* buf, int len)
151
if (ei_decode_long(buf, &index, &level) < 0) {
152
fail("expected long");
155
ei_set_tracelevel((int)level);
157
ret = (long) ei_get_tracelevel();
159
ei_x_new_with_version(&x);
160
ei_x_encode_tuple_header(&x, 2);
161
ei_x_encode_atom(&x, "tracelevel");
162
ei_x_encode_long(&x, ret);
167
static void cmd_ei_send(char* buf, int len)
174
if (ei_decode_long(buf, &index, &fd) < 0)
175
fail("expected long");
176
if (ei_decode_pid(buf, &index, &pid) < 0)
177
fail("expected pid (node)");
178
if (ei_x_new_with_version(&x) < 0)
179
fail("ei_x_new_with_version");
180
if (ei_x_append_buf(&x, &buf[index], len - index) < 0)
182
send_errno_result(ei_send(fd, &pid, x.buff, x.index));
186
static void cmd_ei_send_funs(char* buf, int len)
192
erlang_fun fun1, fun2;
194
if (ei_decode_long(buf, &index, &fd) < 0)
195
fail("expected long");
196
if (ei_decode_pid(buf, &index, &pid) < 0)
197
fail("expected pid (node)");
198
if (ei_decode_tuple_header(buf, &index, &n) < 0)
199
fail("expected tuple");
201
fail("expected tuple");
202
if (ei_decode_fun(buf, &index, &fun1) < 0)
203
fail("expected Fun1");
204
if (ei_decode_fun(buf, &index, &fun2) < 0)
205
fail("expected Fun2");
206
if (ei_x_new_with_version(&x) < 0)
207
fail("ei_x_new_with_version");
208
if (ei_x_encode_tuple_header(&x, 2) < 0)
209
fail("encode tuple header");
210
if (ei_x_encode_fun(&x, &fun1) < 0)
212
if (ei_x_encode_fun(&x, &fun2) < 0)
216
send_errno_result(ei_send(fd, &pid, x.buff, x.index));
220
static void cmd_ei_reg_send(char* buf, int len)
224
char reg_name[MAXATOMLEN];
228
if (ei_decode_long(buf, &index, &fd) < 0)
229
fail("expected long (fd)");
230
if (ei_decode_atom(buf, &index, reg_name) < 0)
231
fail("expected atom (reg name)");
232
if (ei_x_new_with_version(&x) < 0)
233
fail("ei_x_new_with_version");
234
if (ei_x_append_buf(&x, &buf[index], len - index) < 0)
236
send_errno_result(ei_reg_send(&ec, fd,
237
reg_name, x.buff, x.index));
241
static void cmd_ei_rpc(char* buf, int len)
248
char mod[MAXATOMLEN], func[MAXATOMLEN];
250
#if 0 && defined(__WIN32__)
254
if (ei_decode_long(buf, &index, &fd) < 0)
255
fail("expected long");
256
if (ei_decode_pid(buf, &index, &pid) < 0)
257
fail("expected pid (node)");
258
if (ei_decode_tuple_header(buf, &index, &n) < 0 && n < 2)
259
fail("expected tuple {module, function}");
260
if (ei_decode_atom(buf, &index, mod) < 0)
261
fail("expected atom (module)");
262
if (ei_decode_atom(buf, &index, func) < 0)
263
fail("expected atom (function)");
264
message("pid %s %d %d %d\n", pid.node, pid.num, pid.serial, pid.creation);
265
message("{%s, %s}\n", mod, func);
266
if (ei_x_new(&rpc_x) < 0)
268
if (ei_rpc(&ec, fd, mod, func, &buf[index], len - index, &rpc_x) < 0)
270
if (ei_x_new_with_version(&x) < 0)
271
fail("ei_x_new_with_version");
272
if (ei_x_append(&x, &rpc_x) < 0)
275
/*send_errno_result(ei_send(&ec, fd, &pid, x.buff, x.index));*/
280
static void send_errno_result(int value)
283
ei_x_new_with_version(&x);
284
ei_x_encode_tuple_header(&x, 2);
285
ei_x_encode_long(&x, value);
286
ei_x_encode_long(&x, erl_errno);