1 /* rpc - RPC implementation on top of GRAS messages */
3 /* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "gras/Msg/msg_private.h"
11 xbt_set_t _gras_rpctype_set = NULL;
12 xbt_dynar_t _gras_rpc_cancelled = NULL;
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_rpc, gras_msg, "RPC mecanism");
17 /** @brief declare a new versionned RPC type of the given name and payloads
19 * @param name: name as it should be used for logging messages (must be uniq)
20 * @param payload_request: datatype of request
21 * @param payload_answer: datatype of answer
23 * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair
27 gras_msgtype_declare_rpc(const char *name,
28 gras_datadesc_type_t payload_request,
29 gras_datadesc_type_t payload_answer)
32 gras_msgtype_declare_ext(name, 0,
33 e_gras_msg_kind_rpccall,
34 payload_request, payload_answer);
38 /** @brief declare a new versionned RPC type of the given name and payloads
40 * @param name: name as it should be used for logging messages (must be uniq)
41 * @param version: something like versionning symbol
42 * @param payload_request: datatype of request
43 * @param payload_answer: datatype of answer
45 * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair
48 * Use this version instead of gras_rpctype_declare when you change the
49 * semantic or syntax of a message and want your programs to be able to deal
50 * with both versions. Internally, each will be handled as an independent
51 * message type, so you can register differents for each of them.
54 gras_msgtype_declare_rpc_v(const char *name,
56 gras_datadesc_type_t payload_request,
57 gras_datadesc_type_t payload_answer)
60 gras_msgtype_declare_ext(name, version,
61 e_gras_msg_kind_rpccall,
62 payload_request, payload_answer);
66 static unsigned long int last_msg_ID = 0;
68 static int msgfilter_rpcID(gras_msg_t msg, void *ctx)
70 unsigned long int ID = *(unsigned long int *) ctx;
71 int res = msg->ID == ID &&
72 (msg->kind == e_gras_msg_kind_rpcanswer
73 || msg->kind == e_gras_msg_kind_rpcerror);
75 gras_msg_cb_ctx_t rpc_ctx;
79 ("Filter a message of ID %lu, type '%s' and kind '%s'. Waiting for ID=%lu. %s",
80 msg->ID, msg->type->name, e_gras_msg_kind_names[msg->kind], ID,
81 res ? "take it" : "reject");
83 if (res && !_gras_rpc_cancelled)
86 /* Check whether it is an old answer to a message we already canceled */
87 xbt_dynar_foreach(_gras_rpc_cancelled, cursor, rpc_ctx) {
88 if (msg->ID == rpc_ctx->ID && msg->kind == e_gras_msg_kind_rpcanswer) {
90 ("Got an answer to the already canceled (timeouted?) RPC %ld. Ignore it (leaking the payload!).",
92 xbt_dynar_cursor_rm(_gras_rpc_cancelled, &cursor);
100 /* Mallocator cruft */
101 xbt_mallocator_t gras_msg_ctx_mallocator = NULL;
102 void *gras_msg_ctx_mallocator_new_f(void)
104 return xbt_new0(s_gras_msg_cb_ctx_t, 1);
107 void gras_msg_ctx_mallocator_free_f(void *ctx)
112 void gras_msg_ctx_mallocator_reset_f(void *ctx)
114 memset(ctx, 0, sizeof(s_gras_msg_cb_ctx_t));
117 /** @brief Launch a RPC call, but do not block for the answer */
119 gras_msg_rpc_async_call_(gras_socket_t server,
121 gras_msgtype_t msgtype, void *request)
123 gras_msg_cb_ctx_t ctx = xbt_mallocator_get(gras_msg_ctx_mallocator);
125 if (msgtype->ctn_type) {
127 "RPC type '%s' convey a payload you must provide",
130 xbt_assert1(!request,
131 "No payload was declared for RPC type '%s'", msgtype->name);
134 ctx->ID = last_msg_ID++;
135 ctx->expeditor = server;
136 ctx->msgtype = msgtype;
137 ctx->timeout = timeOut;
139 VERB4("Send to %s:%d a RPC of type '%s' (ID=%lu)",
140 gras_socket_peer_name(server),
141 gras_socket_peer_port(server), msgtype->name, ctx->ID);
143 gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype,
149 /** @brief Wait the answer of a RPC call previously launched asynchronously */
150 void gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx, void *answer)
153 s_gras_msg_t received;
155 if (ctx->msgtype->answer_type) {
157 "Answers to RPC '%s' convey a payload you must accept",
161 "No payload was declared for answers to RPC '%s'",
166 /* The filter returns 1 when we eat an old RPC answer to something canceled */
168 gras_msg_wait_ext_(ctx->timeout,
169 ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
171 } while (received.ID != ctx->ID);
175 if (!_gras_rpc_cancelled)
176 _gras_rpc_cancelled = xbt_dynar_new(sizeof(ctx), NULL);
177 xbt_dynar_push(_gras_rpc_cancelled, &ctx);
178 INFO5("canceled RPC %ld pushed onto the stack (%s from %s:%d) Reason: %s",
179 ctx->ID, ctx->msgtype->name,
180 gras_socket_peer_name(ctx->expeditor),
181 gras_socket_peer_port(ctx->expeditor), e.msg);
185 xbt_mallocator_release(gras_msg_ctx_mallocator, ctx);
186 if (received.kind == e_gras_msg_kind_rpcerror) {
188 memcpy(&e, received.payl, received.payl_size);
190 VERB3("Raise a remote exception cat:%d comming from %s (%s)",
191 e.category, e.host, e.msg);
192 __xbt_ex_ctx()->ctx_ex.msg = e.msg;
193 __xbt_ex_ctx()->ctx_ex.category = e.category;
194 __xbt_ex_ctx()->ctx_ex.value = e.value;
195 __xbt_ex_ctx()->ctx_ex.remote = 1;
196 __xbt_ex_ctx()->ctx_ex.host = e.host;
197 __xbt_ex_ctx()->ctx_ex.procname = e.procname;
198 __xbt_ex_ctx()->ctx_ex.pid = e.pid;
199 __xbt_ex_ctx()->ctx_ex.file = e.file;
200 __xbt_ex_ctx()->ctx_ex.line = e.line;
201 __xbt_ex_ctx()->ctx_ex.func = e.func;
202 __xbt_ex_ctx()->ctx_ex.used = e.used;
203 __xbt_ex_ctx()->ctx_ex.bt_strings = e.bt_strings;
204 memset(&__xbt_ex_ctx()->ctx_ex.bt, 0, sizeof(__xbt_ex_ctx()->ctx_ex.bt));
205 DO_THROW(__xbt_ex_ctx()->ctx_ex);
207 memcpy(answer, received.payl, received.payl_size);
211 /** @brief Conduct a RPC call */
212 void gras_msg_rpccall_(gras_socket_t server,
214 gras_msgtype_t msgtype, void *request, void *answer)
217 gras_msg_cb_ctx_t ctx;
219 ctx = gras_msg_rpc_async_call_(server, timeout, msgtype, request);
220 gras_msg_rpc_async_wait(ctx, answer);
224 /** @brief Return the result of a RPC call
226 * It done before the actual return of the callback so that the callback can do
227 * some cleanups before leaving.
230 void gras_msg_rpcreturn(double timeOut, gras_msg_cb_ctx_t ctx, void *answer)
232 xbt_assert0(ctx->answer_due,
233 "RPC return not allowed here. Either not a RPC message or already returned a result");
235 DEBUG5("Return to RPC '%s' from %s:%d (tOut=%f, payl=%p)",
237 gras_socket_peer_name(ctx->expeditor),
238 gras_socket_peer_port(ctx->expeditor), timeOut, answer);
239 gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer, ctx->ID,
240 ctx->msgtype, answer);