Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Mallocator on the message contextes
[simgrid.git] / src / gras / Msg / rpc.c
1 /* $Id$ */
2
3 /* rpc - RPC implementation on top of GRAS messages                         */
4
5 /* Copyright (c) 2005 Martin Quinson. All rights reserved.                  */
6
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. */
9
10 #include "gras/Msg/msg_private.h"
11                 
12 xbt_set_t _gras_rpctype_set = NULL;
13 xbt_dynar_t _gras_rpc_cancelled = NULL;
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_rpc,gras_msg,"RPC mecanism");
16
17
18 /** @brief declare a new versionned RPC type of the given name and payloads
19  *
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
23  *
24  * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair 
25  * of messages. 
26  */
27 void
28 gras_msgtype_declare_rpc(const char           *name,
29                          gras_datadesc_type_t  payload_request,
30                          gras_datadesc_type_t  payload_answer) {
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   gras_msgtype_declare_ext(name, version, 
60                            e_gras_msg_kind_rpccall, 
61                            payload_request, payload_answer);
62
63 }
64
65 static unsigned long int last_msg_ID = 0;
66
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);
71   int cursor;
72   gras_msg_cb_ctx_t rpc_ctx;
73      
74
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");
78    
79   if (res && !_gras_rpc_cancelled)
80      return res;
81   
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);
87         return 1;
88      }
89   }
90    
91   return res;
92 }
93
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);
98 }
99 void gras_msg_ctx_mallocator_free_f(void* ctx) {
100    xbt_free(ctx);
101 }
102 void gras_msg_ctx_mallocator_reset_f(void* ctx) {
103    memset(ctx, sizeof(s_gras_msg_cb_ctx_t),0);
104 }
105
106 /** @brief Launch a RPC call, but do not block for the answer */
107 gras_msg_cb_ctx_t 
108 gras_msg_rpc_async_call(gras_socket_t server,
109                         double timeOut,
110                         gras_msgtype_t msgtype,
111                         void *request) {
112   gras_msg_cb_ctx_t ctx = xbt_mallocator_get(gras_msg_ctx_mallocator);
113
114   if (msgtype->ctn_type) {
115     xbt_assert1(request,
116                 "RPC type '%s' convey a payload you must provide",
117                 msgtype->name);
118   } else {
119     xbt_assert1(!request,
120                 "No payload was declared for RPC type '%s'",
121                 msgtype->name);
122   }
123
124   ctx->ID = last_msg_ID++;
125   ctx->expeditor = server;
126   ctx->msgtype=msgtype;
127   ctx->timeout=timeOut;
128
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);
133
134   gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype, request);
135
136   return ctx;
137 }
138
139 /** @brief Wait teh answer of a RPC call previously launched asynchronously */
140 void gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx,
141                              void *answer) {
142   xbt_ex_t e;
143   s_gras_msg_t received;
144
145   if (ctx->msgtype->answer_type) {
146     xbt_assert1(answer,
147                 "Answers to RPC '%s' convey a payload you must accept",
148                 ctx->msgtype->name);
149   } else {
150     xbt_assert1(!answer,
151                 "No payload was declared for answers to RPC '%s'",
152                 ctx->msgtype->name);
153   }
154
155   TRY {
156      /* The filter returns 1 when we eat an old RPC answer to something canceled */
157      do {
158         gras_msg_wait_ext(ctx->timeout,
159                           ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
160                           &received);
161      } while (received.ID != ctx->ID);
162      
163   } CATCH(e) {
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),
170            e.msg);
171      RETHROW;
172   }
173    
174   xbt_mallocator_release(gras_msg_ctx_mallocator, ctx);
175   if (received.kind == e_gras_msg_kind_rpcerror) {
176     xbt_ex_t e;
177     memcpy(&e,received.payl,received.payl_size);
178     free(received.payl);
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);
196   }
197   memcpy(answer,received.payl,received.payl_size);
198   free(received.payl);
199 }
200
201 /** @brief Conduct a RPC call */
202 void gras_msg_rpccall(gras_socket_t server,
203                       double timeout,
204                       gras_msgtype_t msgtype,
205                       void *request, void *answer) {
206
207   gras_msg_cb_ctx_t ctx;
208
209   ctx= gras_msg_rpc_async_call(server, timeout,msgtype,request);
210   gras_msg_rpc_async_wait(ctx, answer);
211 }
212
213
214 /** @brief Return the result of a RPC call
215  *
216  * It done before the actual return of the callback so that the callback can do
217  * some cleanups before leaving.
218  */
219
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");
223   ctx->answer_due = 0;
224   DEBUG5("Return to RPC '%s' from %s:%d (tOut=%f, payl=%p)",
225          ctx->msgtype->name,
226          gras_socket_peer_name(ctx->expeditor),gras_socket_peer_port(ctx->expeditor),
227          timeOut,answer);
228   gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer, 
229                     ctx->ID, ctx->msgtype, answer);
230 }
231