From 15b6554ce37fd29cb507a40e49bbbfe1a438b84b Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Wed, 2 Mar 2011 21:33:06 +0200 Subject: [PATCH] Force to add type_id identifier for messages. Receiver can then use the type-id for identifying the messages from each others. Signed-off-by: Mika Laitio --- src/plpbus/BusMessage.cc | 63 +++++++++++++------------------- src/plpbus/BusMessage.hh | 19 ++++++---- src/plpbus/BusMessageInternal.cc | 6 ++- src_test/test_client.cc | 4 +- src_test/test_server.cc | 4 +- 5 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/plpbus/BusMessage.cc b/src/plpbus/BusMessage.cc index 1743d4c..18fc487 100644 --- a/src/plpbus/BusMessage.cc +++ b/src/plpbus/BusMessage.cc @@ -16,33 +16,27 @@ using namespace plpbus; using namespace CORBA; BusMessage::BusMessage() { - _dataItemSeq = new DataItemSequence(); - log_debug("new BusMessage()\n"); + _dataItemSeq = new DataItemSequence(0); } -BusMessage::BusMessage(const char *name_param, const char *value_param) { - DataItem *item; - DataItemSequence *seq; +BusMessage::BusMessage(long type_param) { + _dataItemSeq = new DataItemSequence(0); - if (name_param != NULL) { - item = new DataItem(); - item->name = strdup(name_param); - item->value <<= strdup(value_param); + add_long_parameter(CONST_MSG_ID_KEY_NAME, type_param); +} - seq = new DataItemSequence(0); - seq->length(1); - (*seq)[0] = *item; - _dataItemSeq = seq; - } - else { - _dataItemSeq = new DataItemSequence(0); - } +BusMessage::BusMessage(long type_param, + const char *arg_name_param, + const char *arg_string_value_param) { + _dataItemSeq = new DataItemSequence(0); + add_long_parameter(CONST_MSG_ID_KEY_NAME, type_param); + add_string_parameter(arg_name_param, arg_string_value_param); } BusMessage::~BusMessage() { } -int BusMessage::add_string_parameter(const char *name_param, const char *value_param) +int BusMessage::add_string_parameter(const char *arg_name_param, const char *value_param) { long length; int ret_val; @@ -51,10 +45,10 @@ int BusMessage::add_string_parameter(const char *name_param, const char *value_p ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; - if (name_param != NULL) { + if (arg_name_param != NULL) { length = seq->length(); item = new DataItem(); - item->name = strdup(name_param); + item->name = strdup(arg_name_param); if (value_param != NULL) { item->value <<= strdup(value_param); seq->length(length + 1); @@ -62,13 +56,12 @@ int BusMessage::add_string_parameter(const char *name_param, const char *value_p } } else { - seq->length(0); ret_val = -1; } return ret_val; } -int BusMessage::add_long_parameter(const char *name_param, long value_param) +int BusMessage::add_long_parameter(const char *arg_name_param, long value_param) { long length; int ret_val; @@ -77,23 +70,22 @@ int BusMessage::add_long_parameter(const char *name_param, long value_param) ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; - if (name_param != NULL) { + if (arg_name_param != NULL) { length = seq->length(); item = new DataItem(); - item->name = strdup(name_param); + item->name = strdup(arg_name_param); item->value <<= value_param; seq->length(length + 1); (*seq)[length] = *item; } else { - seq->length(0); ret_val = -1; } return ret_val; } -int BusMessage::add_int_parameter(const char *name_param, int value_param) +int BusMessage::add_int_parameter(const char *arg_name_param, int value_param) { long length; int ret_val; @@ -102,23 +94,22 @@ int BusMessage::add_int_parameter(const char *name_param, int value_param) ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; - if (name_param != NULL) { + if (arg_name_param != NULL) { length = seq->length(); item = new DataItem(); - item->name = strdup(name_param); + item->name = strdup(arg_name_param); item->value <<= (long)value_param; seq->length(length + 1); (*seq)[length] = *item; } else { - seq->length(0); ret_val = -1; } return ret_val; } -int BusMessage::add_double_parameter(const char *name_param, double value_param) +int BusMessage::add_double_parameter(const char *arg_name_param, double value_param) { long length; int ret_val; @@ -128,24 +119,23 @@ int BusMessage::add_double_parameter(const char *name_param, double value_param) ret_val = 0; log_debug("add_double_parameter() started\n"); seq = (DataItemSequence *)_dataItemSeq; - if (name_param != NULL) { + if (arg_name_param != NULL) { length = seq->length(); item = new DataItem(); - item->name = strdup(name_param); + item->name = strdup(arg_name_param); item->value <<= (long)value_param; seq->length(length + 1); (*seq)[length] = *item; } else { - seq->length(0); ret_val = -1; } log_debug("add_double_parameter() done\n"); return ret_val; } -int BusMessage::add_float_parameter(const char *name_param, float value_param) +int BusMessage::add_float_parameter(const char *arg_name_param, float value_param) { long length; int ret_val; @@ -154,17 +144,16 @@ int BusMessage::add_float_parameter(const char *name_param, float value_param) ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; - if (name_param != NULL) { + if (arg_name_param != NULL) { length = seq->length(); item = new DataItem(); - item->name = strdup(name_param); + item->name = strdup(arg_name_param); item->value <<= (long)value_param; seq->length(length + 1); (*seq)[length] = *item; } else { - seq->length(0); ret_val = -1; } return ret_val; diff --git a/src/plpbus/BusMessage.hh b/src/plpbus/BusMessage.hh index 1f9f48a..855a399 100644 --- a/src/plpbus/BusMessage.hh +++ b/src/plpbus/BusMessage.hh @@ -8,22 +8,25 @@ #ifndef BUSMESSAGE_H_ #define BUSMESSAGE_H_ +#define CONST_MSG_ID_KEY_NAME "_msgid" + namespace plpbus { class BusMessage { public: void *_dataItemSeq; - BusMessage(); - BusMessage(const char *name_param, const char *string_value_param); + 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 *name_param, const char *string_value_param); - int add_long_parameter(const char *name_param, long value_param); - int add_int_parameter(const char *name_param, int value_param); - int add_double_parameter(const char *name_param, double value_param); - int add_float_parameter(const char *name_param, float value_param); + int add_string_parameter(const char *arg_name_param, const char *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_double_parameter(const char *arg_name_param, double value_param); + int add_float_parameter(const char *arg_name_param, float value_param); void printout(); - private: + protected: + BusMessage(); void printout_dataitem(long index, void *item); }; } diff --git a/src/plpbus/BusMessageInternal.cc b/src/plpbus/BusMessageInternal.cc index b66f18e..ee1a25f 100644 --- a/src/plpbus/BusMessageInternal.cc +++ b/src/plpbus/BusMessageInternal.cc @@ -10,7 +10,11 @@ using namespace plpbus_orb; namespace plpbus { - BusMessageInternal::BusMessageInternal(DataItemSequence seq_param) { + BusMessageInternal::BusMessageInternal(DataItemSequence seq_param) : BusMessage() { + DataItemSequence *seq; + + seq = (DataItemSequence *)_dataItemSeq; + delete(seq); _dataItemSeq = new DataItemSequence(seq_param); } diff --git a/src_test/test_client.cc b/src_test/test_client.cc index aeca510..03cdcd7 100644 --- a/src_test/test_client.cc +++ b/src_test/test_client.cc @@ -12,6 +12,8 @@ using namespace std; using namespace plpbus; +#define MSG_ID_HELLO 0 + class ClientListenerImpl : public IClientListener { public: @@ -61,7 +63,7 @@ int main(int argc, char** argv) int err_flg; char *rsp; - busmsg = new BusMessage(); + busmsg = new BusMessage(MSG_ID_HELLO); busmsg->add_string_parameter("a", "hello"); busmsg->add_string_parameter("b", "world"); client->send_message_and_request_response(busmsg); diff --git a/src_test/test_server.cc b/src_test/test_server.cc index 1a6a17f..a310b1d 100644 --- a/src_test/test_server.cc +++ b/src_test/test_server.cc @@ -6,6 +6,8 @@ using namespace std; using namespace plpbus; +#define MSG_ID_HELLO 0 + class ServerListenerImpl : public IServerListener { public: @@ -24,7 +26,7 @@ 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"); - *msg_rsp_param = new BusMessage(); + *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"); -- 2.41.1