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_LOG_NEW_DEFAULT_SUBCATEGORY(gras_rpc,gras_msg,"RPCing");
14 xbt_set_t _gras_rpctype_set = NULL;
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_rpc,gras_msg,"RPC mecanism");
19 /** @brief declare a new versionned RPC type of the given name and payloads
21 * @param name: name as it should be used for logging messages (must be uniq)
22 * @param payload_request: datatype of request
23 * @param payload_answer: datatype of answer
25 * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair
29 gras_msgtype_declare_rpc(const char *name,
30 gras_datadesc_type_t payload_request,
31 gras_datadesc_type_t payload_answer) {
33 gras_msgtype_declare_ext(name, 0,
34 e_gras_msg_kind_rpccall,
35 payload_request, payload_answer);
39 /** @brief declare a new versionned RPC type of the given name and payloads
41 * @param name: name as it should be used for logging messages (must be uniq)
42 * @param version: something like versionning symbol
43 * @param payload_request: datatype of request
44 * @param payload_answer: datatype of answer
46 * Registers a new RPC message to the GRAS mechanism. RPC are constituted of a pair
49 * Use this version instead of gras_rpctype_declare when you change the
50 * semantic or syntax of a message and want your programs to be able to deal
51 * with both versions. Internally, each will be handled as an independent
52 * message type, so you can register differents for each of them.
55 gras_msgtype_declare_rpc_v(const char *name,
57 gras_datadesc_type_t payload_request,
58 gras_datadesc_type_t payload_answer) {
60 gras_msgtype_declare_ext(name, version,
61 e_gras_msg_kind_rpccall,
62 payload_request, payload_answer);
66 static unsigned long int last_msg_ID = 0;
68 static int msgfilter_rpcID(gras_msg_t msg, void* ctx) {
69 unsigned long int ID= *(unsigned long int*)ctx;
70 int res = msg->ID == ID &&
71 (msg->kind == e_gras_msg_kind_rpcanswer || msg->kind == e_gras_msg_kind_rpcerror);
73 DEBUG5("Filter a message of ID %lu, type '%s' and kind '%s'. Waiting for ID=%lu. %s",
74 msg->ID,msg->type->name,e_gras_msg_kind_names[msg->kind],ID,
75 res?"take it": "reject");
79 /** @brief Conduct a RPC call
82 void gras_msg_rpccall(gras_socket_t server,
84 gras_msgtype_t msgtype,
85 void *request, void *answer) {
87 unsigned long int msg_ID = last_msg_ID++;
88 s_gras_msg_t received;
90 DEBUG4("Send to %s:%d a RPC of type '%s' (ID=%lu)",
91 gras_socket_peer_name(server),
92 gras_socket_peer_port(server),
93 msgtype->name,msg_ID);
95 gras_msg_send_ext(server, e_gras_msg_kind_rpccall, msg_ID, msgtype, request);
96 gras_msg_wait_ext(timeOut,
97 msgtype, NULL, msgfilter_rpcID, &msg_ID,
99 if (received.kind == e_gras_msg_kind_rpcerror) {
100 /* Damn. Got an exception. Extract it and revive it */
102 VERB0("Raise a remote exception");
103 memcpy(&e,received.payl,received.payl_size);
105 memcpy((void*)&(__xbt_ex_ctx()->ctx_ex),&e,sizeof(xbt_ex_t));
106 DO_THROW(__xbt_ex_ctx()->ctx_ex);
109 memcpy(answer,received.payl,received.payl_size);
114 /** @brief Return the result of a RPC call
116 * It done before the actual return of the callback so that the callback can do
117 * some cleanups before leaving.
120 void gras_msg_rpcreturn(double timeOut,gras_msg_cb_ctx_t ctx,void *answer) {
121 gras_msg_send_ext(ctx->expeditor, e_gras_msg_kind_rpcanswer, ctx->ID, ctx->msgtype, answer);