Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace INFO0+bprintf by INFO6
[simgrid.git] / src / gras / Msg / rpc.c
1 /* rpc - RPC implementation on top of GRAS messages                         */
2
3 /* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "gras/Msg/msg_private.h"
10
11 xbt_set_t _gras_rpctype_set = NULL;
12 xbt_dynar_t _gras_rpc_cancelled = NULL;
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_rpc, gras_msg, "RPC mecanism");
15
16
17 /** @brief declare a new versionned RPC type of the given name and payloads
18  *
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
22  *
23  * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair
24  * of messages.
25  */
26 void
27 gras_msgtype_declare_rpc(const char *name,
28                          gras_datadesc_type_t payload_request,
29                          gras_datadesc_type_t payload_answer)
30 {
31
32   gras_msgtype_declare_ext(name, 0,
33                            e_gras_msg_kind_rpccall,
34                            payload_request, payload_answer);
35
36 }
37
38 /** @brief declare a new versionned RPC type of the given name and payloads
39  *
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
44  *
45  * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair
46  * of messages.
47  *
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.
52  */
53 void
54 gras_msgtype_declare_rpc_v(const char *name,
55                            short int version,
56                            gras_datadesc_type_t payload_request,
57                            gras_datadesc_type_t payload_answer)
58 {
59
60   gras_msgtype_declare_ext(name, version,
61                            e_gras_msg_kind_rpccall,
62                            payload_request, payload_answer);
63
64 }
65
66 static unsigned long int last_msg_ID = 0;
67
68 static int msgfilter_rpcID(gras_msg_t msg, void *ctx)
69 {
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);
74   unsigned int cursor;
75   gras_msg_cb_ctx_t rpc_ctx;
76
77
78   DEBUG5
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");
82
83   if (res && !_gras_rpc_cancelled)
84     return res;
85
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) {
89       VERB1
90           ("Got an answer to the already canceled (timeouted?) RPC %ld. Ignore it (leaking the payload!).",
91            msg->ID);
92       xbt_dynar_cursor_rm(_gras_rpc_cancelled, &cursor);
93       return 1;
94     }
95   }
96
97   return res;
98 }
99
100 /* Mallocator cruft */
101 xbt_mallocator_t gras_msg_ctx_mallocator = NULL;
102 void *gras_msg_ctx_mallocator_new_f(void)
103 {
104   return xbt_new0(s_gras_msg_cb_ctx_t, 1);
105 }
106
107 void gras_msg_ctx_mallocator_free_f(void *ctx)
108 {
109   xbt_free(ctx);
110 }
111
112 void gras_msg_ctx_mallocator_reset_f(void *ctx)
113 {
114   memset(ctx, 0, sizeof(s_gras_msg_cb_ctx_t));
115 }
116
117 /** @brief Launch a RPC call, but do not block for the answer */
118 gras_msg_cb_ctx_t
119 gras_msg_rpc_async_call_(gras_socket_t server,
120                          double timeOut,
121                          gras_msgtype_t msgtype, void *request)
122 {
123   gras_msg_cb_ctx_t ctx = xbt_mallocator_get(gras_msg_ctx_mallocator);
124
125   if (msgtype->ctn_type) {
126     xbt_assert1(request,
127                 "RPC type '%s' convey a payload you must provide",
128                 msgtype->name);
129   } else {
130     xbt_assert1(!request,
131                 "No payload was declared for RPC type '%s'",
132                 msgtype->name);
133   }
134
135   ctx->ID = last_msg_ID++;
136   ctx->expeditor = server;
137   ctx->msgtype = msgtype;
138   ctx->timeout = timeOut;
139
140   VERB4("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);
143
144   gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype,
145                     request);
146
147   return ctx;
148 }
149
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)
152 {
153   xbt_ex_t e;
154   s_gras_msg_t received;
155
156   if (ctx->msgtype->answer_type) {
157     xbt_assert1(answer,
158                 "Answers to RPC '%s' convey a payload you must accept",
159                 ctx->msgtype->name);
160   } else {
161     xbt_assert1(!answer,
162                 "No payload was declared for answers to RPC '%s'",
163                 ctx->msgtype->name);
164   }
165
166   TRY {
167     /* The filter returns 1 when we eat an old RPC answer to something canceled */
168     do {
169       gras_msg_wait_ext_(ctx->timeout,
170                          ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
171                          &received);
172     } while (received.ID != ctx->ID);
173
174   }
175   CATCH(e) {
176     if (!_gras_rpc_cancelled)
177       _gras_rpc_cancelled = xbt_dynar_new(sizeof(ctx), NULL);
178     xbt_dynar_push(_gras_rpc_cancelled, &ctx);
179     INFO5
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);
184     RETHROW;
185   }
186
187   xbt_mallocator_release(gras_msg_ctx_mallocator, ctx);
188   if (received.kind == e_gras_msg_kind_rpcerror) {
189     xbt_ex_t e;
190     memcpy(&e, received.payl, received.payl_size);
191     free(received.payl);
192     VERB3("Raise a remote exception cat:%d comming from %s (%s)",
193           e.category, e.host, e.msg);
194     __xbt_ex_ctx()->ctx_ex.msg = e.msg;
195     __xbt_ex_ctx()->ctx_ex.category = e.category;
196     __xbt_ex_ctx()->ctx_ex.value = e.value;
197     __xbt_ex_ctx()->ctx_ex.remote = 1;
198     __xbt_ex_ctx()->ctx_ex.host = e.host;
199     __xbt_ex_ctx()->ctx_ex.procname = e.procname;
200     __xbt_ex_ctx()->ctx_ex.pid = e.pid;
201     __xbt_ex_ctx()->ctx_ex.file = e.file;
202     __xbt_ex_ctx()->ctx_ex.line = e.line;
203     __xbt_ex_ctx()->ctx_ex.func = e.func;
204     __xbt_ex_ctx()->ctx_ex.used = e.used;
205     __xbt_ex_ctx()->ctx_ex.bt_strings = e.bt_strings;
206     memset(&__xbt_ex_ctx()->ctx_ex.bt, 0,
207            sizeof(__xbt_ex_ctx()->ctx_ex.bt));
208     DO_THROW(__xbt_ex_ctx()->ctx_ex);
209   }
210   memcpy(answer, received.payl, received.payl_size);
211   free(received.payl);
212 }
213
214 /** @brief Conduct a RPC call */
215 void gras_msg_rpccall_(gras_socket_t server,
216                        double timeout,
217                        gras_msgtype_t msgtype, void *request, void *answer)
218 {
219
220   gras_msg_cb_ctx_t ctx;
221
222   ctx = gras_msg_rpc_async_call_(server, timeout, msgtype, request);
223   gras_msg_rpc_async_wait(ctx, answer);
224 }
225
226
227 /** @brief Return the result of a RPC call
228  *
229  * It done before the actual return of the callback so that the callback can do
230  * some cleanups before leaving.
231  */
232
233 void gras_msg_rpcreturn(double timeOut, gras_msg_cb_ctx_t ctx,
234                         void *answer)
235 {
236   xbt_assert0(ctx->answer_due,
237               "RPC return not allowed here. Either not a RPC message or already returned a result");
238   ctx->answer_due = 0;
239   DEBUG5("Return to RPC '%s' from %s:%d (tOut=%f, payl=%p)",
240          ctx->msgtype->name,
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);
245 }