Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
eace210cf7f5b03851601d262a91e0a4f868dcbe
[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 /** @brief Launch a RPC call, but do not block for the answer */
95 gras_msg_cb_ctx_t 
96 gras_msg_rpc_async_call(gras_socket_t server,
97                         double timeOut,
98                         gras_msgtype_t msgtype,
99                         void *request) {
100   gras_msg_cb_ctx_t ctx = xbt_new0(s_gras_msg_cb_ctx_t,1);
101
102   if (msgtype->ctn_type) {
103     xbt_assert1(request,
104                 "RPC type '%s' convey a payload you must provide",
105                 msgtype->name);
106   } else {
107     xbt_assert1(!request,
108                 "No payload was declared for RPC type '%s'",
109                 msgtype->name);
110   }
111
112   ctx->ID = last_msg_ID++;
113   ctx->expeditor = server;
114   ctx->msgtype=msgtype;
115   ctx->timeout=timeOut;
116
117   VERB4("Send to %s:%d a RPC of type '%s' (ID=%lu)",
118         gras_socket_peer_name(server),
119         gras_socket_peer_port(server),
120         msgtype->name,ctx->ID);
121
122   gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype, request);
123
124   return ctx;
125 }
126
127 /** @brief Wait teh answer of a RPC call previously launched asynchronously */
128 void gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx,
129                              void *answer) {
130   xbt_ex_t e;
131   s_gras_msg_t received;
132
133   if (ctx->msgtype->answer_type) {
134     xbt_assert1(answer,
135                 "Answers to RPC '%s' convey a payload you must accept",
136                 ctx->msgtype->name);
137   } else {
138     xbt_assert1(!answer,
139                 "No payload was declared for answers to RPC '%s'",
140                 ctx->msgtype->name);
141   }
142
143   TRY {
144      /* The filter returns 1 when we eat an old RPC answer to something canceled */
145      do {
146         gras_msg_wait_ext(ctx->timeout,
147                           ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
148                           &received);
149      } while (received.ID != ctx->ID);
150      
151   } CATCH(e) {
152      if (!_gras_rpc_cancelled)
153        _gras_rpc_cancelled = xbt_dynar_new(sizeof(ctx),NULL);
154      xbt_dynar_push(_gras_rpc_cancelled,&ctx);
155      INFO5("canceled RPC %ld pushed onto the stack (%s from %s:%d) Reason: %s",
156            ctx->ID,ctx->msgtype->name,
157            gras_socket_peer_name(ctx->expeditor),gras_socket_peer_port(ctx->expeditor),
158            e.msg);
159      RETHROW;
160   }
161    
162   free(ctx);
163   if (received.kind == e_gras_msg_kind_rpcerror) {
164     xbt_ex_t e;
165     memcpy(&e,received.payl,received.payl_size);
166     free(received.payl);
167     VERB3("Raise a remote exception cat:%d comming from %s (%s)",
168           e.category, e.host, e.msg);
169      __xbt_ex_ctx()->ctx_ex.msg      = e.msg;
170      __xbt_ex_ctx()->ctx_ex.category = e.category;
171      __xbt_ex_ctx()->ctx_ex.value    = e.value;
172      __xbt_ex_ctx()->ctx_ex.remote   = 1;
173      __xbt_ex_ctx()->ctx_ex.host     = e.host;
174      __xbt_ex_ctx()->ctx_ex.procname = e.procname;
175      __xbt_ex_ctx()->ctx_ex.pid      = e.pid;
176      __xbt_ex_ctx()->ctx_ex.file     = e.file;
177      __xbt_ex_ctx()->ctx_ex.line     = e.line;
178      __xbt_ex_ctx()->ctx_ex.func     = e.func;
179      __xbt_ex_ctx()->ctx_ex.used     = e.used;
180      __xbt_ex_ctx()->ctx_ex.bt_strings = e.bt_strings;
181      memset(&__xbt_ex_ctx()->ctx_ex.bt,0,
182             sizeof(__xbt_ex_ctx()->ctx_ex.bt));
183     DO_THROW(__xbt_ex_ctx()->ctx_ex);
184   }
185   memcpy(answer,received.payl,received.payl_size);
186   free(received.payl);
187 }
188
189 /** @brief Conduct a RPC call */
190 void gras_msg_rpccall(gras_socket_t server,
191                       double timeout,
192                       gras_msgtype_t msgtype,
193                       void *request, void *answer) {
194
195   gras_msg_cb_ctx_t ctx;
196
197   ctx= gras_msg_rpc_async_call(server, timeout,msgtype,request);
198   gras_msg_rpc_async_wait(ctx, answer);
199 }
200
201
202 /** @brief Return the result of a RPC call
203  *
204  * It done before the actual return of the callback so that the callback can do
205  * some cleanups before leaving.
206  */
207
208 void gras_msg_rpcreturn(double timeOut,gras_msg_cb_ctx_t ctx,void *answer) {
209   DEBUG5("Return to RPC '%s' from %s:%d (tOut=%f, payl=%p)",
210          ctx->msgtype->name,
211          gras_socket_peer_name(ctx->expeditor),gras_socket_peer_port(ctx->expeditor),
212          timeOut,answer);
213   gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer, 
214                     ctx->ID, ctx->msgtype, answer);
215 }
216