Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't pass NULL to xbt_die.
[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                          xbt_datadesc_type_t payload_request,
29                          xbt_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                            xbt_datadesc_type_t payload_request,
57                            xbt_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   XBT_DEBUG
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       XBT_VERB
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_reset_f(void *ctx)
108 {
109   memset(ctx, 0, sizeof(s_gras_msg_cb_ctx_t));
110 }
111
112 /** @brief Launch a RPC call, but do not block for the answer */
113 gras_msg_cb_ctx_t
114 gras_msg_rpc_async_call_(xbt_socket_t server,
115                          double timeOut,
116                          gras_msgtype_t msgtype, void *request)
117 {
118   gras_msg_cb_ctx_t ctx = xbt_mallocator_get(gras_msg_ctx_mallocator);
119
120   if (msgtype->ctn_type) {
121     xbt_assert(request,
122                 "RPC type '%s' convey a payload you must provide",
123                 msgtype->name);
124   } else {
125     xbt_assert(!request,
126                 "No payload was declared for RPC type '%s'",
127                 msgtype->name);
128   }
129
130   ctx->ID = last_msg_ID++;
131   ctx->expeditor = server;
132   ctx->msgtype = msgtype;
133   ctx->timeout = timeOut;
134
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);
138
139   gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype,
140                     request);
141
142   return ctx;
143 }
144
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)
147 {
148   xbt_ex_t e;
149   s_gras_msg_t received;
150
151   if (ctx->msgtype->answer_type) {
152     xbt_assert(answer,
153                 "Answers to RPC '%s' convey a payload you must accept",
154                 ctx->msgtype->name);
155   } else {
156     xbt_assert(!answer,
157                 "No payload was declared for answers to RPC '%s'",
158                 ctx->msgtype->name);
159   }
160
161   TRY {
162     /* The filter returns 1 when we eat an old RPC answer to something canceled */
163     do {
164       gras_msg_wait_ext_(ctx->timeout,
165                          ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
166                          &received);
167     } while (received.ID != ctx->ID);
168
169   }
170   CATCH(e) {
171     if (!_gras_rpc_cancelled)
172       _gras_rpc_cancelled = xbt_dynar_new(sizeof(ctx), NULL);
173     xbt_dynar_push(_gras_rpc_cancelled, &ctx);
174     XBT_INFO
175         ("canceled RPC %ld 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);
179     RETHROW;
180   }
181
182   xbt_mallocator_release(gras_msg_ctx_mallocator, ctx);
183   if (received.kind == e_gras_msg_kind_rpcerror) {
184     xbt_ex_t e;
185     memcpy(&e, received.payl, received.payl_size);
186     free(received.payl);
187     XBT_VERB("Raise a remote exception cat:%d coming from %s (%s)",
188           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());
204   }
205   memcpy(answer, received.payl, received.payl_size);
206   free(received.payl);
207 }
208
209 /** @brief Conduct a RPC call */
210 void gras_msg_rpccall_(xbt_socket_t server,
211                        double timeout,
212                        gras_msgtype_t msgtype, void *request, void *answer)
213 {
214
215   gras_msg_cb_ctx_t ctx;
216
217   ctx = gras_msg_rpc_async_call_(server, timeout, msgtype, request);
218   gras_msg_rpc_async_wait(ctx, answer);
219 }
220
221
222 /** @brief Return the result of a RPC call
223  *
224  * It done before the actual return of the callback so that the callback can do
225  * some cleanups before leaving.
226  */
227
228 void gras_msg_rpcreturn(double timeOut, gras_msg_cb_ctx_t ctx,
229                         void *answer)
230 {
231   xbt_assert(ctx->answer_due,
232               "RPC return not allowed here. Either not a RPC message or already returned a result");
233   ctx->answer_due = 0;
234   XBT_DEBUG("Return to RPC '%s' from %s:%d (tOut=%f, payl=%p)",
235          ctx->msgtype->name,
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);
240 }