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;
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_rpc,gras_msg,"RPC mecanism");
17 /** @brief declare a new versionned RPC type of the given name and payloads
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
23 * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair
27 gras_msgtype_declare_rpc(const char *name,
28 gras_datadesc_type_t payload_request,
29 gras_datadesc_type_t payload_answer) {
31 gras_msgtype_declare_ext(name, 0,
32 e_gras_msg_kind_rpccall,
33 payload_request, payload_answer);
37 /** @brief declare a new versionned RPC type of the given name and payloads
39 * @param name: name as it should be used for logging messages (must be uniq)
40 * @param version: something like versionning symbol
41 * @param payload_request: datatype of request
42 * @param payload_answer: datatype of answer
44 * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair
47 * Use this version instead of gras_rpctype_declare when you change the
48 * semantic or syntax of a message and want your programs to be able to deal
49 * with both versions. Internally, each will be handled as an independent
50 * message type, so you can register differents for each of them.
53 gras_msgtype_declare_rpc_v(const char *name,
55 gras_datadesc_type_t payload_request,
56 gras_datadesc_type_t payload_answer) {
58 gras_msgtype_declare_ext(name, version,
59 e_gras_msg_kind_rpccall,
60 payload_request, payload_answer);
64 static unsigned long int last_msg_ID = 0;
66 static int msgfilter_rpcID(gras_msg_t msg, void* ctx) {
67 unsigned long int ID= *(unsigned long int*)ctx;
68 int res = msg->ID == ID &&
69 (msg->kind == e_gras_msg_kind_rpcanswer || msg->kind == e_gras_msg_kind_rpcerror);
71 DEBUG5("Filter a message of ID %lu, type '%s' and kind '%s'. Waiting for ID=%lu. %s",
72 msg->ID,msg->type->name,e_gras_msg_kind_names[msg->kind],ID,
73 res?"take it": "reject");
77 /** @brief Launch a RPC call, but do not block for the answer */
79 gras_msg_rpc_async_call(gras_socket_t server,
81 gras_msgtype_t msgtype,
83 gras_msg_cb_ctx_t ctx = xbt_new0(s_gras_msg_cb_ctx_t,1);
85 ctx->ID = last_msg_ID++;
86 ctx->expeditor = server;
90 VERB5("Send to %s:%d a RPC of type '%s' (ID=%lu) (exception%s caught)",
91 gras_socket_peer_name(server),
92 gras_socket_peer_port(server),
93 msgtype->name,ctx->ID,
94 (__xbt_ex_ctx()->ctx_caught?"":" not"));
96 gras_msg_send_ext(server, e_gras_msg_kind_rpccall, ctx->ID, msgtype, request);
101 /** @brief Wait teh answer of a RPC call previously launched asynchronously */
102 void gras_msg_rpc_async_wait(gras_msg_cb_ctx_t ctx,
104 s_gras_msg_t received;
106 gras_msg_wait_ext(ctx->timeout,
107 ctx->msgtype, NULL, msgfilter_rpcID, &ctx->ID,
110 if (received.kind == e_gras_msg_kind_rpcerror) {
112 memcpy(&e,received.payl,received.payl_size);
114 VERB3("Raise a remote exception cat:%d comming from %s (%s)",
115 e.category, e.host, e.msg);
116 __xbt_ex_ctx()->ctx_ex.msg = e.msg;
117 __xbt_ex_ctx()->ctx_ex.category = e.category;
118 __xbt_ex_ctx()->ctx_ex.value = e.value;
119 __xbt_ex_ctx()->ctx_ex.remote = 1;
120 __xbt_ex_ctx()->ctx_ex.host = e.host;
121 __xbt_ex_ctx()->ctx_ex.procname = e.procname;
122 __xbt_ex_ctx()->ctx_ex.pid = e.pid;
123 __xbt_ex_ctx()->ctx_ex.file = e.file;
124 __xbt_ex_ctx()->ctx_ex.line = e.line;
125 __xbt_ex_ctx()->ctx_ex.func = e.func;
126 __xbt_ex_ctx()->ctx_ex.used = e.used;
127 __xbt_ex_ctx()->ctx_ex.bt_strings = e.bt_strings;
128 memset(&__xbt_ex_ctx()->ctx_ex.bt,0,
129 sizeof(__xbt_ex_ctx()->ctx_ex.bt));
130 DO_THROW(__xbt_ex_ctx()->ctx_ex);
132 memcpy(answer,received.payl,received.payl_size);
136 /** @brief Conduct a RPC call */
137 void gras_msg_rpccall(gras_socket_t server,
139 gras_msgtype_t msgtype,
140 void *request, void *answer) {
142 gras_msg_cb_ctx_t ctx;
144 ctx= gras_msg_rpc_async_call(server, timeout,msgtype,request);
145 gras_msg_rpc_async_wait(ctx, answer);
149 /** @brief Return the result of a RPC call
151 * It done before the actual return of the callback so that the callback can do
152 * some cleanups before leaving.
155 void gras_msg_rpcreturn(double timeOut,gras_msg_cb_ctx_t ctx,void *answer) {
156 gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer,
157 ctx->ID, ctx->msgtype, answer);