From cc8c00aeb93557b1e73e98d106236606b781502d Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Wed, 2 Mar 2011 23:00:55 +0200 Subject: [PATCH] Added methods for querying paramerer values from the message parameter by using parameter name as a key. Signed-off-by: Mika Laitio --- src/plpbus/BusMessage.cc | 121 +++++++++++++++++++++++++++++++++++++-- src/plpbus/BusMessage.hh | 8 ++- src_test/test_client.cc | 12 +++- src_test/test_server.cc | 12 ++-- 4 files changed, 138 insertions(+), 15 deletions(-) diff --git a/src/plpbus/BusMessage.cc b/src/plpbus/BusMessage.cc index 18fc487..7cab6e9 100644 --- a/src/plpbus/BusMessage.cc +++ b/src/plpbus/BusMessage.cc @@ -15,6 +15,8 @@ using namespace plpbus_orb; using namespace plpbus; using namespace CORBA; +#define CONST_MSG_ID_KEY_NAME "_msgtype" + BusMessage::BusMessage() { _dataItemSeq = new DataItemSequence(0); } @@ -159,13 +161,122 @@ int BusMessage::add_float_parameter(const char *arg_name_param, float value_para return ret_val; } +static DataItem *get_dataitem_by_param_name(DataItemSequence *seq, const char *arg_name_param) { + DataItem *ret_val; + long int count; + DataItem *item; + const char *item_name; + + ret_val = NULL; + count = seq->length(); + for (long ii = 0; ii < count; ii++) { + item = &(*seq)[ii]; + item_name = item->name; + if (strcmp(arg_name_param, item_name) == 0) { + ret_val = item; + break; + } + } + return ret_val; +} + +bool BusMessage::get_type(long *type_param) { + bool ret_val; + long res; + + ret_val = get_long_parameter(CONST_MSG_ID_KEY_NAME, &res); + if (ret_val == true) { + *type_param = res; + } + return ret_val; +} + +bool BusMessage::get_string_parameter(const char *arg_name_param, const char **value_param) { + bool ret_val; + DataItem *item; + DataItemSequence *seq; + + ret_val = false; + seq = (DataItemSequence *)_dataItemSeq; + item = get_dataitem_by_param_name(seq, arg_name_param); + if (item != NULL) { + item->value >>= *value_param; + ret_val = true; + } + return ret_val; +} + +bool BusMessage::get_long_parameter(const char *arg_name_param, long *value_param) { + bool ret_val; + DataItem *item; + DataItemSequence *seq; + + ret_val = false; + seq = (DataItemSequence *)_dataItemSeq; + item = get_dataitem_by_param_name(seq, arg_name_param); + if (item != NULL) { + item->value >>= *value_param; + ret_val = true; + } + return ret_val; +} + +bool BusMessage::get_int_parameter(const char *arg_name_param, int *value_param) { + bool ret_val; + DataItem *item; + DataItemSequence *seq; + long tmp; + + ret_val = false; + seq = (DataItemSequence *)_dataItemSeq; + item = get_dataitem_by_param_name(seq, arg_name_param); + if (item != NULL) { + item->value >>= tmp; + *value_param = (int)tmp; + ret_val = true; + } + return ret_val; +} + +bool BusMessage::get_double_parameter(const char *arg_name_param, double *value_param) { + bool ret_val; + DataItem *item; + DataItemSequence *seq; + + ret_val = false; + seq = (DataItemSequence *)_dataItemSeq; + item = get_dataitem_by_param_name(seq, arg_name_param); + if (item != NULL) { + item->value >>= *value_param; + ret_val = true; + } + return ret_val; +} + +bool BusMessage::get_float_parameter(const char *arg_name_param, float *value_param) { + bool ret_val; + DataItem *item; + DataItemSequence *seq; + + ret_val = false; + seq = (DataItemSequence *)_dataItemSeq; + item = get_dataitem_by_param_name(seq, arg_name_param); + if (item != NULL) { + item->value >>= *value_param; + ret_val = true; + } + return ret_val; +} + void BusMessage::printout_dataitem(long index, void *item) { CORBA::TCKind kind; + const char *name; DataItem *dataitem; dataitem = (DataItem *)item; if (item != NULL) { if (dataitem->name != NULL) { + name = dataitem->name; kind = dataitem->value.type()->kind(); switch(kind) { case tk_string: @@ -173,14 +284,14 @@ void BusMessage::printout_dataitem(long index, void *item) { const char *val; dataitem->value >>= val; - log_debug("\t[%ld], value: %s\n", index, val); + log_debug("\t[%ld] %s: %s\n", index, name, val); } break; case tk_long: { long val; dataitem->value >>= val; - log_debug("\t[%ld], value: %ld\n", index, val); + log_debug("\t[%ld] %s: %ld\n", index, name, val); //val_str = strdup("long"); } break; @@ -188,7 +299,7 @@ void BusMessage::printout_dataitem(long index, void *item) { { double val; dataitem->value >>= val; - log_debug("\t[%ld], value: %f\n", index, val); + log_debug("\t[%ld] %s: %f\n", index, name, val); //val_str = strdup("double"); } break; @@ -196,12 +307,12 @@ void BusMessage::printout_dataitem(long index, void *item) { { float val; dataitem->value >>= val; - log_debug("\t[l%ld] value: %f\n", index, val); + log_debug("\t[l%ld] %s: %f\n", index, name, val); //val_str = strdup("float"); } break; default: - log_error("\t[%ld], value unknown\n", index); + log_error("\t[%ld], name: %s, value unknown\n", index, name); } } else { diff --git a/src/plpbus/BusMessage.hh b/src/plpbus/BusMessage.hh index 855a399..6068d30 100644 --- a/src/plpbus/BusMessage.hh +++ b/src/plpbus/BusMessage.hh @@ -8,8 +8,6 @@ #ifndef BUSMESSAGE_H_ #define BUSMESSAGE_H_ -#define CONST_MSG_ID_KEY_NAME "_msgid" - namespace plpbus { class BusMessage { @@ -24,6 +22,12 @@ namespace plpbus { int add_int_parameter(const char *arg_name_param, int value_param); int add_double_parameter(const char *arg_name_param, double value_param); int add_float_parameter(const char *arg_name_param, float value_param); + bool get_type(long *type_param); + bool get_string_parameter(const char *arg_name_param, const char **value_param); + bool get_long_parameter(const char *arg_name_param, long *value_param); + bool get_int_parameter(const char *arg_name_param, int *value_param); + bool get_double_parameter(const char *arg_name_param, double *value_param); + bool get_float_parameter(const char *arg_name_param, float *value_param); void printout(); protected: BusMessage(); diff --git a/src_test/test_client.cc b/src_test/test_client.cc index 03cdcd7..2974767 100644 --- a/src_test/test_client.cc +++ b/src_test/test_client.cc @@ -4,6 +4,7 @@ * Created on: Jun 9, 2010 * Author: lamikr */ +#include #include "plpbus/BusServer.hh" #include "plpbus/BusClient.hh" @@ -12,7 +13,7 @@ using namespace std; using namespace plpbus; -#define MSG_ID_HELLO 0 +#define MSG_ID_HELLO 33 class ClientListenerImpl : public IClientListener { @@ -26,12 +27,17 @@ class ClientListenerImpl : public IClientListener }; int ClientListenerImpl::response_received(const char *msg_rsp_param) { - cout << "response_received(char *: " << msg_rsp_param << ") " << endl; + log_debug("response_received(char *%s)\n", msg_rsp_param); return 0; } int ClientListenerImpl::response_received(const BusMessage *msg_rsp_param) { - cout << "response_received(BusMessage *) " << endl; + long type; + + type = 0; + ((BusMessage *)msg_rsp_param)->get_type(&type); + log_debug("response_received(), type: %ld\n", type); + ((BusMessage *)msg_rsp_param)->printout(); return 0; } diff --git a/src_test/test_server.cc b/src_test/test_server.cc index a310b1d..cefb5d6 100644 --- a/src_test/test_server.cc +++ b/src_test/test_server.cc @@ -6,7 +6,7 @@ using namespace std; using namespace plpbus; -#define MSG_ID_HELLO 0 +#define MSG_ID_HELLO 33 class ServerListenerImpl : public IServerListener { @@ -25,16 +25,18 @@ int ServerListenerImpl::request_received(const char *msg_req_param, char **msg_r } int ServerListenerImpl::request_received(const BusMessage *msg_req_param, BusMessage **msg_rsp_param) { - log_debug("request_received(BusMessage *) started\n"); + long type; + + type = 0; + ((BusMessage *)msg_req_param)->get_type(&type); + log_debug("request_received, msg type: %ld\n", type); *msg_rsp_param = new BusMessage(MSG_ID_HELLO); double dbl = (double)1.0; (*msg_rsp_param)->add_double_parameter("rsp_param_double", dbl); - log_debug("request_received(BusMessage *) done\n"); return 0; } -int main(int argc, char** argv) -{ +int main(int argc, char** argv) { BusServer *server; ServerListenerImpl *listener; -- 2.41.1