1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2000-2010. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
/*
* Purpose: Tests the functions in erl_global.c.
*
* See the erl_global_SUITE.erl file for a "table of contents".
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "runner.h"
static void cmd_erl_connect(ETERM* args);
static void cmd_erl_global_register(ETERM *args);
static void cmd_erl_global_whereis(ETERM *args);
static void cmd_erl_global_names(ETERM *args);
static void cmd_erl_global_unregister(ETERM *args);
static void cmd_erl_close_connection(ETERM *args);
static void send_errno_result(int value);
static struct {
char* name;
int num_args; /* Number of arguments. */
void (*func)(ETERM* args);
} commands[] = {
"erl_connect", 4, cmd_erl_connect,
"erl_close_connection", 1, cmd_erl_close_connection,
"erl_global_register", 2, cmd_erl_global_register,
"erl_global_whereis", 2, cmd_erl_global_whereis,
"erl_global_names", 1, cmd_erl_global_names,
"erl_global_unregister", 2, cmd_erl_global_unregister,
};
/*
* Sends a list contaning all data types to the Erlang side.
*/
TESTCASE(interpret)
{
ETERM* term;
erl_init(NULL, 0);
outer_loop:
term = get_term();
if (term == NULL) {
report(1);
return;
} else {
ETERM* Func;
ETERM* Args;
int i;
if (!ERL_IS_TUPLE(term) || ERL_TUPLE_SIZE(term) != 2) {
fail("term should be a tuple of size 2");
}
Func = erl_element(1, term);
if (!ERL_IS_ATOM(Func)) {
fail("function name should be an atom");
}
Args = erl_element(2, term);
if (!ERL_IS_TUPLE(Args)) {
fail("function arguments should be a tuple");
}
erl_free_term(term);
for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++) {
int n = strlen(commands[i].name);
if (ERL_ATOM_SIZE(Func) != n) {
continue;
}
if (memcmp(ERL_ATOM_PTR(Func), commands[i].name, n) == 0) {
erl_free_term(Func);
if (ERL_TUPLE_SIZE(Args) != commands[i].num_args) {
fail("wrong number of arguments");
}
commands[i].func(Args);
erl_free_term(Args);
goto outer_loop;
}
}
fail("bad command");
}
}
#define VERIFY_TYPE(Test, Term) \
if (!Test(Term)) { \
fail("wrong type for " #Term); \
} else { \
}
static void
cmd_erl_connect(ETERM* args)
{
ETERM* number;
ETERM* node;
ETERM* cookie;
int res;
char buffer[256];
number = ERL_TUPLE_ELEMENT(args, 0);
VERIFY_TYPE(ERL_IS_INTEGER, number);
node = ERL_TUPLE_ELEMENT(args, 1);
VERIFY_TYPE(ERL_IS_ATOM, node);
cookie = ERL_TUPLE_ELEMENT(args, 2);
VERIFY_TYPE(ERL_IS_ATOM, cookie);
if (ERL_ATOM_SIZE(cookie) == 0) {
res = erl_connect_init(ERL_INT_VALUE(number), 0, 0);
} else {
memcpy(buffer, ERL_ATOM_PTR(cookie), ERL_ATOM_SIZE(cookie));
buffer[ERL_ATOM_SIZE(cookie)] = '\0';
res = erl_connect_init(ERL_INT_VALUE(number), buffer, 0);
}
if(!res) {
send_errno_result(res);
return;
}
memcpy(buffer, ERL_ATOM_PTR(node), ERL_ATOM_SIZE(node));
buffer[ERL_ATOM_SIZE(node)] = '\0';
send_errno_result(erl_connect(buffer));
}
static void
cmd_erl_close_connection(ETERM* args)
{
ETERM* number;
ETERM* res;
number = ERL_TUPLE_ELEMENT(args, 0);
VERIFY_TYPE(ERL_IS_INTEGER, number);
res = erl_mk_int(erl_close_connection(ERL_INT_VALUE(number)));
send_term(res);
erl_free_term(res);
}
static void
cmd_erl_global_register(ETERM* args)
{
ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0);
ETERM* name = ERL_TUPLE_ELEMENT(args, 1);
ETERM* pid = erl_mk_pid(erl_thisnodename(), 14, 0, 0);
char buffer[256];
VERIFY_TYPE(ERL_IS_INTEGER, fd_term);
VERIFY_TYPE(ERL_IS_ATOM, name);
memcpy(buffer, ERL_ATOM_PTR(name), ERL_ATOM_SIZE(name));
buffer[ERL_ATOM_SIZE(name)] = '\0';
send_errno_result(erl_global_register(ERL_INT_VALUE(fd_term), buffer, pid));
erl_free_term(pid);
}
static void
cmd_erl_global_whereis(ETERM* args)
{
ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0);
ETERM* name = ERL_TUPLE_ELEMENT(args, 1);
ETERM* pid = NULL;
char buffer[256];
VERIFY_TYPE(ERL_IS_INTEGER, fd_term);
VERIFY_TYPE(ERL_IS_ATOM, name);
memcpy(buffer, ERL_ATOM_PTR(name), ERL_ATOM_SIZE(name));
buffer[ERL_ATOM_SIZE(name)] = '\0';
pid = erl_global_whereis(ERL_INT_VALUE(fd_term), buffer, NULL);
send_term(pid);
erl_free_term(pid);
}
static void
cmd_erl_global_names(ETERM* args)
{
ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0);
ETERM* res_array[2], *res_tuple, *name;
char** names = NULL;
int count = 0, i;
VERIFY_TYPE(ERL_IS_INTEGER, fd_term);
names = erl_global_names(ERL_INT_VALUE(fd_term), &count);
res_array[0] = erl_mk_empty_list();
for(i=0; i<count; i++) {
name = erl_mk_string(names[i]);
res_array[0] = erl_cons(name, res_array[0]);
}
free(names);
res_array[1] = erl_mk_int(count);
res_tuple = erl_mk_tuple(res_array, 2);
send_term(res_tuple);
erl_free_compound(res_array[0]);
erl_free_term(res_array[1]);
erl_free_term(res_tuple);
}
static void
cmd_erl_global_unregister(ETERM* args)
{
ETERM* fd_term = ERL_TUPLE_ELEMENT(args, 0);
ETERM* name = ERL_TUPLE_ELEMENT(args, 1);
char buffer[256];
VERIFY_TYPE(ERL_IS_INTEGER, fd_term);
VERIFY_TYPE(ERL_IS_ATOM, name);
memcpy(buffer, ERL_ATOM_PTR(name), ERL_ATOM_SIZE(name));
buffer[ERL_ATOM_SIZE(name)] = '\0';
send_errno_result(erl_global_unregister(ERL_INT_VALUE(fd_term), buffer));
}
static void
send_errno_result(int value)
{
ETERM* res_array[2];
ETERM* res_tuple;
res_array[0] = erl_mk_int(value);
res_array[1] = erl_mk_int(erl_errno);
res_tuple = erl_mk_tuple(res_array, 2);
send_term(res_tuple);
erl_free_term(res_array[0]);
erl_free_term(res_array[1]);
erl_free_term(res_tuple);
}
|