4
* Copyright Ericsson AB 2000-2010. 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 erl_global.c.
23
* See the erl_global_SUITE.erl file for a "table of contents".
32
static void cmd_erl_connect(ETERM* args);
33
static void cmd_erl_global_register(ETERM *args);
34
static void cmd_erl_global_whereis(ETERM *args);
35
static void cmd_erl_global_names(ETERM *args);
36
static void cmd_erl_global_unregister(ETERM *args);
37
static void cmd_erl_close_connection(ETERM *args);
39
static void send_errno_result(int value);
43
int num_args; /* Number of arguments. */
44
void (*func)(ETERM* args);
46
"erl_connect", 4, cmd_erl_connect,
47
"erl_close_connection", 1, cmd_erl_close_connection,
48
"erl_global_register", 2, cmd_erl_global_register,
49
"erl_global_whereis", 2, cmd_erl_global_whereis,
50
"erl_global_names", 1, cmd_erl_global_names,
51
"erl_global_unregister", 2, cmd_erl_global_unregister,
56
* Sends a list contaning all data types to the Erlang side.
77
if (!ERL_IS_TUPLE(term) || ERL_TUPLE_SIZE(term) != 2) {
78
fail("term should be a tuple of size 2");
81
Func = erl_element(1, term);
82
if (!ERL_IS_ATOM(Func)) {
83
fail("function name should be an atom");
85
Args = erl_element(2, term);
86
if (!ERL_IS_TUPLE(Args)) {
87
fail("function arguments should be a tuple");
90
for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++) {
91
int n = strlen(commands[i].name);
92
if (ERL_ATOM_SIZE(Func) != n) {
95
if (memcmp(ERL_ATOM_PTR(Func), commands[i].name, n) == 0) {
97
if (ERL_TUPLE_SIZE(Args) != commands[i].num_args) {
98
fail("wrong number of arguments");
100
commands[i].func(Args);
109
#define VERIFY_TYPE(Test, Term) \
111
fail("wrong type for " #Term); \
116
cmd_erl_connect(ETERM* args)
125
number = ERL_TUPLE_ELEMENT(args, 0);
126
VERIFY_TYPE(ERL_IS_INTEGER, number);
127
node = ERL_TUPLE_ELEMENT(args, 1);
128
VERIFY_TYPE(ERL_IS_ATOM, node);
129
cookie = ERL_TUPLE_ELEMENT(args, 2);
130
VERIFY_TYPE(ERL_IS_ATOM, cookie);
132
if (ERL_ATOM_SIZE(cookie) == 0) {
133
res = erl_connect_init(ERL_INT_VALUE(number), 0, 0);
135
memcpy(buffer, ERL_ATOM_PTR(cookie), ERL_ATOM_SIZE(cookie));
136
buffer[ERL_ATOM_SIZE(cookie)] = '\0';
137
res = erl_connect_init(ERL_INT_VALUE(number), buffer, 0);
141
send_errno_result(res);
145
memcpy(buffer, ERL_ATOM_PTR(node), ERL_ATOM_SIZE(node));
146
buffer[ERL_ATOM_SIZE(node)] = '\0';
147
send_errno_result(erl_connect(buffer));
151
cmd_erl_close_connection(ETERM* args)
156
number = ERL_TUPLE_ELEMENT(args, 0);
157
VERIFY_TYPE(ERL_IS_INTEGER, number);
158
res = erl_mk_int(erl_close_connection(ERL_INT_VALUE(number)));
164
cmd_erl_global_register(ETERM* args)
166
ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0);
167
ETERM* name = ERL_TUPLE_ELEMENT(args, 1);
168
ETERM* pid = erl_mk_pid(erl_thisnodename(), 14, 0, 0);
172
VERIFY_TYPE(ERL_IS_INTEGER, fd_term);
173
VERIFY_TYPE(ERL_IS_ATOM, name);
175
memcpy(buffer, ERL_ATOM_PTR(name), ERL_ATOM_SIZE(name));
176
buffer[ERL_ATOM_SIZE(name)] = '\0';
178
send_errno_result(erl_global_register(ERL_INT_VALUE(fd_term), buffer, pid));
183
cmd_erl_global_whereis(ETERM* args)
185
ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0);
186
ETERM* name = ERL_TUPLE_ELEMENT(args, 1);
191
VERIFY_TYPE(ERL_IS_INTEGER, fd_term);
192
VERIFY_TYPE(ERL_IS_ATOM, name);
194
memcpy(buffer, ERL_ATOM_PTR(name), ERL_ATOM_SIZE(name));
195
buffer[ERL_ATOM_SIZE(name)] = '\0';
197
pid = erl_global_whereis(ERL_INT_VALUE(fd_term), buffer, NULL);
203
cmd_erl_global_names(ETERM* args)
205
ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0);
207
ETERM* res_array[2], *res_tuple, *name;
211
VERIFY_TYPE(ERL_IS_INTEGER, fd_term);
213
names = erl_global_names(ERL_INT_VALUE(fd_term), &count);
215
res_array[0] = erl_mk_empty_list();
216
for(i=0; i<count; i++) {
217
name = erl_mk_string(names[i]);
218
res_array[0] = erl_cons(name, res_array[0]);
223
res_array[1] = erl_mk_int(count);
224
res_tuple = erl_mk_tuple(res_array, 2);
226
send_term(res_tuple);
228
erl_free_compound(res_array[0]);
229
erl_free_term(res_array[1]);
230
erl_free_term(res_tuple);
234
cmd_erl_global_unregister(ETERM* args)
236
ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0);
237
ETERM* name = ERL_TUPLE_ELEMENT(args, 1);
241
VERIFY_TYPE(ERL_IS_INTEGER, fd_term);
242
VERIFY_TYPE(ERL_IS_ATOM, name);
244
memcpy(buffer, ERL_ATOM_PTR(name), ERL_ATOM_SIZE(name));
245
buffer[ERL_ATOM_SIZE(name)] = '\0';
247
send_errno_result(erl_global_unregister(ERL_INT_VALUE(fd_term), buffer));
251
send_errno_result(int value)
256
res_array[0] = erl_mk_int(value);
257
res_array[1] = erl_mk_int(erl_errno);
258
res_tuple = erl_mk_tuple(res_array, 2);
259
send_term(res_tuple);
260
erl_free_term(res_array[0]);
261
erl_free_term(res_array[1]);
262
erl_free_term(res_tuple);