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 xbt_datadesc_type_t payload_request,
29 xbt_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 xbt_datadesc_type_t payload_request,
57 xbt_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 %lu. 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_reset_f(void *ctx)
109 memset(ctx, 0, sizeof(s_gras_msg_cb_ctx_t));
112 /** @brief Launch a RPC call, but do not block for the answer */
114 gras_msg_rpc_async_call_(xbt_socket_t server,
116 gras_msgtype_t msgtype, void *request)
118 gras_msg_cb_ctx_t ctx = xbt_mallocator_get(gras_msg_ctx_mallocator);
120 if (msgtype->ctn_type) {
122 "RPC type '%s' convey a payload you must provide",
126 "No payload was declared for RPC type '%s'",
130 ctx->ID = last_msg_ID++;
131 ctx->expeditor = server;
132 ctx->msgtype = msgtype;
133 ctx->timeout = timeOut;
135 XBT_VERB("Send to %s:%d a RPC of type '%s' (ID=%lu)",
136 xbt_socket_peer_name(server),
137 xbt_socket_peer_port(server), msgtype->name, ctx->ID);
139 gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype,
145 /** @brief Wait the answer of a RPC call previously launched asynchronously */
146 void gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx, void *answer)
149 s_gras_msg_t received;
151 if (ctx->msgtype->answer_type) {
153 "Answers to RPC '%s' convey a payload you must accept",
157 "No payload was declared for answers to RPC '%s'",
162 /* The filter returns 1 when we eat an old RPC answer to something canceled */
164 gras_msg_wait_ext_(ctx->timeout,
165 ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
167 } while (received.ID != ctx->ID);
171 if (!_gras_rpc_cancelled)
172 _gras_rpc_cancelled = xbt_dynar_new(sizeof(ctx), NULL);
173 xbt_dynar_push(_gras_rpc_cancelled, &ctx);
175 ("canceled RPC %lu pushed onto the stack (%s from %s:%d) Reason: %s",
176 ctx->ID, ctx->msgtype->name,
177 xbt_socket_peer_name(ctx->expeditor),
178 xbt_socket_peer_port(ctx->expeditor), e.msg);
182 xbt_mallocator_release(gras_msg_ctx_mallocator, ctx);
183 if (received.kind == e_gras_msg_kind_rpcerror) {
185 memcpy(&e, received.payl, received.payl_size);
187 XBT_VERB("Raise a remote exception cat:%d coming from %s (%s)",
188 (int)e.category, e.host, e.msg);
189 __xbt_running_ctx_fetch()->exception.msg = e.msg;
190 __xbt_running_ctx_fetch()->exception.category = e.category;
191 __xbt_running_ctx_fetch()->exception.value = e.value;
192 __xbt_running_ctx_fetch()->exception.remote = 1;
193 __xbt_running_ctx_fetch()->exception.host = e.host;
194 __xbt_running_ctx_fetch()->exception.procname = e.procname;
195 __xbt_running_ctx_fetch()->exception.pid = e.pid;
196 __xbt_running_ctx_fetch()->exception.file = e.file;
197 __xbt_running_ctx_fetch()->exception.line = e.line;
198 __xbt_running_ctx_fetch()->exception.func = e.func;
199 __xbt_running_ctx_fetch()->exception.used = e.used;
200 __xbt_running_ctx_fetch()->exception.bt_strings = e.bt_strings;
201 memset(&__xbt_running_ctx_fetch()->exception.bt, 0,
202 sizeof(__xbt_running_ctx_fetch()->exception.bt));
203 DO_THROW(__xbt_running_ctx_fetch());
205 memcpy(answer, received.payl, received.payl_size);
209 /** @brief Conduct a RPC call */
210 void gras_msg_rpccall_(xbt_socket_t server,
212 gras_msgtype_t msgtype, void *request, void *answer)
215 gras_msg_cb_ctx_t ctx;
217 ctx = gras_msg_rpc_async_call_(server, timeout, msgtype, request);
218 gras_msg_rpc_async_wait(ctx, answer);
222 /** @brief Return the result of a RPC call
224 * It done before the actual return of the callback so that the callback can do
225 * some cleanups before leaving.
228 void gras_msg_rpcreturn(double timeOut, gras_msg_cb_ctx_t ctx,
231 xbt_assert(ctx->answer_due,
232 "RPC return not allowed here. Either not a RPC message or already returned a result");
234 XBT_DEBUG("Return to RPC '%s' from %s:%d (tOut=%f, payl=%p)",
236 xbt_socket_peer_name(ctx->expeditor),
237 xbt_socket_peer_port(ctx->expeditor), timeOut, answer);
238 gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer, ctx->ID,
239 ctx->msgtype, answer);