From 3325b4c3b02fdd377f8460c4a1d19bd75b4e32ab Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Sat, 5 Mar 2011 22:51:34 +0200 Subject: [PATCH] Methods for reading data from the BusMessage datatype. BusMessage datatype is used for passing data between client and server by using plpbus(corba)... Earlier it was only possible to add data to datatype but not actually to read it. Signed-off-by: Mika Laitio --- src/plpbus/BusMessage.cc | 114 ++++++++++++++++++++------------------- src/plpbus/BusMessage.hh | 18 ++++--- src_test/test_client.cc | 4 +- src_test/test_server.cc | 4 +- 4 files changed, 73 insertions(+), 67 deletions(-) diff --git a/src/plpbus/BusMessage.cc b/src/plpbus/BusMessage.cc index 7cab6e9..16eaec4 100644 --- a/src/plpbus/BusMessage.cc +++ b/src/plpbus/BusMessage.cc @@ -5,7 +5,11 @@ * Author: lamikr */ +#include +#include + #include +#include #include "BusMessage.hh" #include "RequestResponseBus.hh" @@ -38,7 +42,7 @@ BusMessage::BusMessage(long type_param, BusMessage::~BusMessage() { } -int BusMessage::add_string_parameter(const char *arg_name_param, const char *value_param) +int BusMessage::add_string_parameter(string arg_name_param, string value_param) { long length; int ret_val; @@ -47,15 +51,13 @@ int BusMessage::add_string_parameter(const char *arg_name_param, const char *val ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; - if (arg_name_param != NULL) { + if (arg_name_param.empty() == false) { length = seq->length(); item = new DataItem(); - item->name = strdup(arg_name_param); - if (value_param != NULL) { - item->value <<= strdup(value_param); - seq->length(length + 1); - (*seq)[length] = *item; - } + item->name = strdup(arg_name_param.c_str()); + item->value <<= strdup(value_param.c_str()); + seq->length(length + 1); + (*seq)[length] = *item; } else { ret_val = -1; @@ -87,7 +89,7 @@ int BusMessage::add_long_parameter(const char *arg_name_param, long value_param) return ret_val; } -int BusMessage::add_int_parameter(const char *arg_name_param, int value_param) +int BusMessage::add_int_parameter(string arg_name_param, int value_param) { long length; int ret_val; @@ -96,10 +98,10 @@ int BusMessage::add_int_parameter(const char *arg_name_param, int value_param) ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; - if (arg_name_param != NULL) { + if (arg_name_param.empty() == false) { length = seq->length(); item = new DataItem(); - item->name = strdup(arg_name_param); + item->name = strdup(arg_name_param.c_str()); item->value <<= (long)value_param; seq->length(length + 1); @@ -161,7 +163,7 @@ 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) { +static DataItem *get_dataitem_by_param_name(DataItemSequence *seq, string arg_name_param) { DataItem *ret_val; long int count; DataItem *item; @@ -172,7 +174,7 @@ static DataItem *get_dataitem_by_param_name(DataItemSequence *seq, const char *a for (long ii = 0; ii < count; ii++) { item = &(*seq)[ii]; item_name = item->name; - if (strcmp(arg_name_param, item_name) == 0) { + if (strcmp(arg_name_param.c_str(), item_name) == 0) { ret_val = item; break; } @@ -180,90 +182,92 @@ static DataItem *get_dataitem_by_param_name(DataItemSequence *seq, const char *a return ret_val; } -bool BusMessage::get_type(long *type_param) { - bool ret_val; +long BusMessage::get_type(int *err_flg) { + long ret_val; long res; - ret_val = get_long_parameter(CONST_MSG_ID_KEY_NAME, &res); - if (ret_val == true) { - *type_param = res; + res = get_long_parameter(CONST_MSG_ID_KEY_NAME, err_flg); + if (*err_flg == PLP_OK) { + ret_val = 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; +string BusMessage::get_string_parameter(string arg_name_param, int *err_flg) { + string ret_val; + DataItem *item; + DataItemSequence *seq; + const char *ch; - ret_val = false; - seq = (DataItemSequence *)_dataItemSeq; - item = get_dataitem_by_param_name(seq, arg_name_param); + *err_flg = PLP_ERR_DATA_NOT_FOUND; + seq = (DataItemSequence *)_dataItemSeq; + item = get_dataitem_by_param_name(seq, arg_name_param); if (item != NULL) { - item->value >>= *value_param; - ret_val = true; + item->value >>= CORBA::Any::to_string(ch, 0); + ret_val = ch; + *err_flg = PLP_OK; } return ret_val; } -bool BusMessage::get_long_parameter(const char *arg_name_param, long *value_param) { - bool ret_val; +long BusMessage::get_long_parameter(string arg_name_param, int *err_flg) { + long ret_val; DataItem *item; DataItemSequence *seq; - ret_val = false; - seq = (DataItemSequence *)_dataItemSeq; - item = get_dataitem_by_param_name(seq, arg_name_param); + *err_flg = PLP_ERR_DATA_NOT_FOUND; + seq = (DataItemSequence *)_dataItemSeq; + item = get_dataitem_by_param_name(seq, arg_name_param); if (item != NULL) { - item->value >>= *value_param; - ret_val = true; + item->value >>= ret_val; + *err_flg = PLP_OK; } return ret_val; } -bool BusMessage::get_int_parameter(const char *arg_name_param, int *value_param) { - bool ret_val; +int BusMessage::get_int_parameter(string arg_name_param, int *err_flg) { + int ret_val; DataItem *item; DataItemSequence *seq; long tmp; - ret_val = false; - seq = (DataItemSequence *)_dataItemSeq; - item = get_dataitem_by_param_name(seq, arg_name_param); + *err_flg = PLP_ERR_DATA_NOT_FOUND; + 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; + ret_val = (int)tmp; + *err_flg = PLP_OK; } return ret_val; } -bool BusMessage::get_double_parameter(const char *arg_name_param, double *value_param) { - bool ret_val; +double BusMessage::get_double_parameter(string arg_name_param, int *err_flg) { + double ret_val; DataItem *item; DataItemSequence *seq; - ret_val = false; - seq = (DataItemSequence *)_dataItemSeq; - item = get_dataitem_by_param_name(seq, arg_name_param); + *err_flg = PLP_ERR_DATA_NOT_FOUND; + seq = (DataItemSequence *)_dataItemSeq; + item = get_dataitem_by_param_name(seq, arg_name_param); if (item != NULL) { - item->value >>= *value_param; - ret_val = true; + item->value >>= ret_val; + *err_flg = PLP_OK; } return ret_val; } -bool BusMessage::get_float_parameter(const char *arg_name_param, float *value_param) { - bool ret_val; +float BusMessage::get_float_parameter(string arg_name_param, int *err_flg) { + float ret_val; DataItem *item; DataItemSequence *seq; - ret_val = false; - seq = (DataItemSequence *)_dataItemSeq; - item = get_dataitem_by_param_name(seq, arg_name_param); + *err_flg = PLP_ERR_DATA_NOT_FOUND; + seq = (DataItemSequence *)_dataItemSeq; + item = get_dataitem_by_param_name(seq, arg_name_param); if (item != NULL) { - item->value >>= *value_param; - ret_val = true; + item->value >>= ret_val; + *err_flg = PLP_OK; } return ret_val; } diff --git a/src/plpbus/BusMessage.hh b/src/plpbus/BusMessage.hh index 6068d30..4627eb4 100644 --- a/src/plpbus/BusMessage.hh +++ b/src/plpbus/BusMessage.hh @@ -8,6 +8,8 @@ #ifndef BUSMESSAGE_H_ #define BUSMESSAGE_H_ +#include + namespace plpbus { class BusMessage { @@ -17,17 +19,17 @@ namespace plpbus { BusMessage(long type_param); BusMessage(long type_param, const char *arg_name_param, const char *arg_string_value_param); virtual ~BusMessage(); - int add_string_parameter(const char *arg_name_param, const char *string_value_param); + int add_string_parameter(std::string arg_name_param, std::string string_value_param); int add_long_parameter(const char *arg_name_param, long value_param); - int add_int_parameter(const char *arg_name_param, int value_param); + int add_int_parameter(std::string 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); + long get_type(int *err_flg); + std::string get_string_parameter(std::string arg_name_param, int *err_flg); + long get_long_parameter(std::string arg_name_param, int *err_flg); + int get_int_parameter(std::string arg_name_param, int *err_flg); + double get_double_parameter(std::string arg_name_param, int *err_flg); + float get_float_parameter(std::string arg_name_param, int *err_flg); void printout(); protected: BusMessage(); diff --git a/src_test/test_client.cc b/src_test/test_client.cc index 2974767..71ba4ae 100644 --- a/src_test/test_client.cc +++ b/src_test/test_client.cc @@ -33,9 +33,9 @@ int ClientListenerImpl::response_received(const char *msg_rsp_param) { int ClientListenerImpl::response_received(const BusMessage *msg_rsp_param) { long type; + int err_flg; - type = 0; - ((BusMessage *)msg_rsp_param)->get_type(&type); + type = ((BusMessage *)msg_rsp_param)->get_type(&err_flg); 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 cefb5d6..5c26776 100644 --- a/src_test/test_server.cc +++ b/src_test/test_server.cc @@ -26,9 +26,9 @@ 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) { long type; + int err_flg; - type = 0; - ((BusMessage *)msg_req_param)->get_type(&type); + type = ((BusMessage *)msg_req_param)->get_type(&err_flg); log_debug("request_received, msg type: %ld\n", type); *msg_rsp_param = new BusMessage(MSG_ID_HELLO); double dbl = (double)1.0; -- 2.41.1