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 /** @brief Launch a RPC call, but do not block for the answer */
96 gras_msg_rpc_async_call(gras_socket_t server,
98 gras_msgtype_t msgtype,
100 gras_msg_cb_ctx_t ctx = xbt_new0(s_gras_msg_cb_ctx_t,1);
102 if (msgtype->ctn_type) {
104 "RPC type '%s' convey a payload you must provide",
107 xbt_assert1(!request,
108 "No payload was declared for RPC type '%s'",
112 ctx->ID = last_msg_ID++;
113 ctx->expeditor = server;
114 ctx->msgtype=msgtype;
115 ctx->timeout=timeOut;
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);
122 gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype, request);
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,
131 s_gras_msg_t received;
133 if (ctx->msgtype->answer_type) {
135 "Answers to RPC '%s' convey a payload you must accept",
139 "No payload was declared for answers to RPC '%s'",
144 /* The filter returns 1 when we eat an old RPC answer to something canceled */
146 gras_msg_wait_ext(ctx->timeout,
147 ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
149 } while (received.ID != ctx->ID);
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),
163 if (received.kind == e_gras_msg_kind_rpcerror) {
165 memcpy(&e,received.payl,received.payl_size);
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);
185 memcpy(answer,received.payl,received.payl_size);
189 /** @brief Conduct a RPC call */
190 void gras_msg_rpccall(gras_socket_t server,
192 gras_msgtype_t msgtype,
193 void *request, void *answer) {
195 gras_msg_cb_ctx_t ctx;
197 ctx= gras_msg_rpc_async_call(server, timeout,msgtype,request);
198 gras_msg_rpc_async_wait(ctx, answer);
202 /** @brief Return the result of a RPC call
204 * It done before the actual return of the callback so that the callback can do
205 * some cleanups before leaving.
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)",
211 gras_socket_peer_name(ctx->expeditor),gras_socket_peer_port(ctx->expeditor),
213 gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer,
214 ctx->ID, ctx->msgtype, answer);