3 /* rpc - RPC implementation on top of GRAS messages */
5 /* Copyright (c) 2005 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "gras/Msg/msg_private.h"
12 xbt_set_t _gras_rpctype_set = NULL;
13 xbt_dynar_t _gras_rpc_cancelled = NULL;
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_rpc,gras_msg,"RPC mecanism");
18 /** @brief declare a new versionned RPC type of the given name and payloads
20 * @param name: name as it should be used for logging messages (must be uniq)
21 * @param payload_request: datatype of request
22 * @param payload_answer: datatype of answer
24 * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair
28 gras_msgtype_declare_rpc(const char *name,
29 gras_datadesc_type_t payload_request,
30 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) {
59 gras_msgtype_declare_ext(name, version,
60 e_gras_msg_kind_rpccall,
61 payload_request, payload_answer);
65 static unsigned long int last_msg_ID = 0;
67 static int msgfilter_rpcID(gras_msg_t msg, void* ctx) {
68 unsigned long int ID= *(unsigned long int*)ctx;
69 int res = msg->ID == ID &&
70 (msg->kind == e_gras_msg_kind_rpcanswer || msg->kind == e_gras_msg_kind_rpcerror);
72 gras_msg_cb_ctx_t rpc_ctx;
75 DEBUG5("Filter a message of ID %lu, type '%s' and kind '%s'. Waiting for ID=%lu. %s",
76 msg->ID,msg->type->name,e_gras_msg_kind_names[msg->kind],ID,
77 res?"take it": "reject");
79 if (res && !_gras_rpc_cancelled)
82 /* Check whether it is an old answer to a message we already canceled */
83 xbt_dynar_foreach(_gras_rpc_cancelled,cursor,rpc_ctx) {
84 if (msg->ID == rpc_ctx->ID && msg->kind==e_gras_msg_kind_rpcanswer) {
85 VERB1("Got an answer to the already canceled (timeouted?) RPC %ld. Ignore it (leaking the payload!).",msg->ID);
86 xbt_dynar_cursor_rm (_gras_rpc_cancelled, &cursor);
94 /* Mallocator cruft */
95 xbt_mallocator_t gras_msg_ctx_mallocator = NULL;
96 void* gras_msg_ctx_mallocator_new_f(void) {
97 return xbt_new0(s_gras_msg_cb_ctx_t,1);
99 void gras_msg_ctx_mallocator_free_f(void* ctx) {
102 void gras_msg_ctx_mallocator_reset_f(void* ctx) {
103 memset(ctx, sizeof(s_gras_msg_cb_ctx_t),0);
106 /** @brief Launch a RPC call, but do not block for the answer */
108 gras_msg_rpc_async_call_(gras_socket_t server,
110 gras_msgtype_t msgtype,
112 gras_msg_cb_ctx_t ctx = xbt_mallocator_get(gras_msg_ctx_mallocator);
114 if (msgtype->ctn_type) {
116 "RPC type '%s' convey a payload you must provide",
119 xbt_assert1(!request,
120 "No payload was declared for RPC type '%s'",
124 ctx->ID = last_msg_ID++;
125 ctx->expeditor = server;
126 ctx->msgtype=msgtype;
127 ctx->timeout=timeOut;
129 VERB4("Send to %s:%d a RPC of type '%s' (ID=%lu)",
130 gras_socket_peer_name(server),
131 gras_socket_peer_port(server),
132 msgtype->name,ctx->ID);
134 gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype, request);
139 /** @brief Wait the answer of a RPC call previously launched asynchronously */
140 void gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx,
143 s_gras_msg_t received;
145 if (ctx->msgtype->answer_type) {
147 "Answers to RPC '%s' convey a payload you must accept",
151 "No payload was declared for answers to RPC '%s'",
156 /* The filter returns 1 when we eat an old RPC answer to something canceled */
158 gras_msg_wait_ext_(ctx->timeout,
159 ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
161 } while (received.ID != ctx->ID);
164 if (!_gras_rpc_cancelled)
165 _gras_rpc_cancelled = xbt_dynar_new(sizeof(ctx),NULL);
166 xbt_dynar_push(_gras_rpc_cancelled,&ctx);
167 INFO5("canceled RPC %ld pushed onto the stack (%s from %s:%d) Reason: %s",
168 ctx->ID,ctx->msgtype->name,
169 gras_socket_peer_name(ctx->expeditor),gras_socket_peer_port(ctx->expeditor),
174 xbt_mallocator_release(gras_msg_ctx_mallocator, ctx);
175 if (received.kind == e_gras_msg_kind_rpcerror) {
177 memcpy(&e,received.payl,received.payl_size);
179 VERB3("Raise a remote exception cat:%d comming from %s (%s)",
180 e.category, e.host, e.msg);
181 __xbt_ex_ctx()->ctx_ex.msg = e.msg;
182 __xbt_ex_ctx()->ctx_ex.category = e.category;
183 __xbt_ex_ctx()->ctx_ex.value = e.value;
184 __xbt_ex_ctx()->ctx_ex.remote = 1;
185 __xbt_ex_ctx()->ctx_ex.host = e.host;
186 __xbt_ex_ctx()->ctx_ex.procname = e.procname;
187 __xbt_ex_ctx()->ctx_ex.pid = e.pid;
188 __xbt_ex_ctx()->ctx_ex.file = e.file;
189 __xbt_ex_ctx()->ctx_ex.line = e.line;
190 __xbt_ex_ctx()->ctx_ex.func = e.func;
191 __xbt_ex_ctx()->ctx_ex.used = e.used;
192 __xbt_ex_ctx()->ctx_ex.bt_strings = e.bt_strings;
193 memset(&__xbt_ex_ctx()->ctx_ex.bt,0,
194 sizeof(__xbt_ex_ctx()->ctx_ex.bt));
195 DO_THROW(__xbt_ex_ctx()->ctx_ex);
197 memcpy(answer,received.payl,received.payl_size);
201 /** @brief Conduct a RPC call */
202 void gras_msg_rpccall_(gras_socket_t server,
204 gras_msgtype_t msgtype,
205 void *request, void *answer) {
207 gras_msg_cb_ctx_t ctx;
209 ctx = gras_msg_rpc_async_call_(server, timeout,msgtype,request);
210 gras_msg_rpc_async_wait(ctx, answer);
214 /** @brief Return the result of a RPC call
216 * It done before the actual return of the callback so that the callback can do
217 * some cleanups before leaving.
220 void gras_msg_rpcreturn(double timeOut,gras_msg_cb_ctx_t ctx,void *answer) {
221 xbt_assert0(ctx->answer_due,
222 "RPC return not allowed here. Either not a RPC message or already returned a result");
224 DEBUG5("Return to RPC '%s' from %s:%d (tOut=%f, payl=%p)",
226 gras_socket_peer_name(ctx->expeditor),gras_socket_peer_port(ctx->expeditor),
228 gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer,
229 ctx->ID, ctx->msgtype, answer);