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",
131 "No payload was declared for RPC type '%s'",
135 ctx->ID = last_msg_ID++;
136 ctx->expeditor = server;
137 ctx->msgtype = msgtype;
138 ctx->timeout = timeOut;
140 XBT_VERB("Send to %s:%d a RPC of type '%s' (ID=%lu)",
141 gras_socket_peer_name(server),
142 gras_socket_peer_port(server), msgtype->name, ctx->ID);
144 gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype,
150 /** @brief Wait the answer of a RPC call previously launched asynchronously */
151 void gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx, void *answer)
154 s_gras_msg_t received;
156 if (ctx->msgtype->answer_type) {
158 "Answers to RPC '%s' convey a payload you must accept",
162 "No payload was declared for answers to RPC '%s'",
167 /* The filter returns 1 when we eat an old RPC answer to something canceled */
169 gras_msg_wait_ext_(ctx->timeout,
170 ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
172 } while (received.ID != ctx->ID);
176 if (!_gras_rpc_cancelled)
177 _gras_rpc_cancelled = xbt_dynar_new(sizeof(ctx), NULL);
178 xbt_dynar_push(_gras_rpc_cancelled, &ctx);
180 ("canceled RPC %ld pushed onto the stack (%s from %s:%d) Reason: %s",
181 ctx->ID, ctx->msgtype->name,
182 gras_socket_peer_name(ctx->expeditor),
183 gras_socket_peer_port(ctx->expeditor), e.msg);
187 xbt_mallocator_release(gras_msg_ctx_mallocator, ctx);
188 if (received.kind == e_gras_msg_kind_rpcerror) {
190 memcpy(&e, received.payl, received.payl_size);
192 XBT_VERB("Raise a remote exception cat:%d coming from %s (%s)",
193 e.category, e.host, e.msg);
194 __xbt_running_ctx_fetch()->exception.msg = e.msg;
195 __xbt_running_ctx_fetch()->exception.category = e.category;
196 __xbt_running_ctx_fetch()->exception.value = e.value;
197 __xbt_running_ctx_fetch()->exception.remote = 1;
198 __xbt_running_ctx_fetch()->exception.host = e.host;
199 __xbt_running_ctx_fetch()->exception.procname = e.procname;
200 __xbt_running_ctx_fetch()->exception.pid = e.pid;
201 __xbt_running_ctx_fetch()->exception.file = e.file;
202 __xbt_running_ctx_fetch()->exception.line = e.line;
203 __xbt_running_ctx_fetch()->exception.func = e.func;
204 __xbt_running_ctx_fetch()->exception.used = e.used;
205 __xbt_running_ctx_fetch()->exception.bt_strings = e.bt_strings;
206 memset(&__xbt_running_ctx_fetch()->exception.bt, 0,
207 sizeof(__xbt_running_ctx_fetch()->exception.bt));
208 DO_THROW(__xbt_running_ctx_fetch());
210 memcpy(answer, received.payl, received.payl_size);
214 /** @brief Conduct a RPC call */
215 void gras_msg_rpccall_(gras_socket_t server,
217 gras_msgtype_t msgtype, void *request, void *answer)
220 gras_msg_cb_ctx_t ctx;
222 ctx = gras_msg_rpc_async_call_(server, timeout, msgtype, request);
223 gras_msg_rpc_async_wait(ctx, answer);
227 /** @brief Return the result of a RPC call
229 * It done before the actual return of the callback so that the callback can do
230 * some cleanups before leaving.
233 void gras_msg_rpcreturn(double timeOut, gras_msg_cb_ctx_t ctx,
236 xbt_assert(ctx->answer_due,
237 "RPC return not allowed here. Either not a RPC message or already returned a result");
239 XBT_DEBUG("Return to RPC '%s' from %s:%d (tOut=%f, payl=%p)",
241 gras_socket_peer_name(ctx->expeditor),
242 gras_socket_peer_port(ctx->expeditor), timeOut, answer);
243 gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer, ctx->ID,
244 ctx->msgtype, answer);