From: Mika Laitio Date: Fri, 11 Mar 2011 16:02:00 +0000 (+0200) Subject: Memory leak fixes. X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=827ba77fe55e4587ef605068ab092216cfffad2c;p=libplpbus.git Memory leak fixes. Added also a workaround for passing the string items in BusMessage. (Inside CORBA:Any parameter there were sometimes leaks) Added also a new test_bus_msg test for testing the bus messages. Signed-off-by: Mika Laitio --- diff --git a/src/idl/RequestResponseBus.hh b/src/idl/RequestResponseBus.hh index ddebb17..99684d0 100644 --- a/src/idl/RequestResponseBus.hh +++ b/src/idl/RequestResponseBus.hh @@ -66,6 +66,8 @@ _CORBA_MODULE_BEG ::CORBA::Any value; + ::CORBA::String_member str_value; + void operator>>= (cdrStream &) const; diff --git a/src/idl/RequestResponseBus.idl b/src/idl/RequestResponseBus.idl index cf5f9fe..0bbd1f0 100644 --- a/src/idl/RequestResponseBus.idl +++ b/src/idl/RequestResponseBus.idl @@ -5,6 +5,7 @@ module plpbus_orb { struct DataItem { string name; any value; + string str_value; }; typedef sequence DataItemSequence; diff --git a/src/idl/RequestResponseBusDynSK.cc b/src/idl/RequestResponseBusDynSK.cc index bfd2be9..c97d286 100644 --- a/src/idl/RequestResponseBusDynSK.cc +++ b/src/idl/RequestResponseBusDynSK.cc @@ -10,13 +10,14 @@ static ::CORBA::TypeCode::_Tracker _0RL_tcTrack(__FILE__); static CORBA::PR_structMember _0RL_structmember_plpbus__orb_mDataItem[] = { {"name", CORBA::TypeCode::PR_string_tc(0, &_0RL_tcTrack)}, - {"value", CORBA::TypeCode::PR_any_tc()} + {"value", CORBA::TypeCode::PR_any_tc()}, + {"str_value", CORBA::TypeCode::PR_string_tc(0, &_0RL_tcTrack)} }; #ifdef _0RL_tc_plpbus__orb_mDataItem # undef _0RL_tc_plpbus__orb_mDataItem #endif -static CORBA::TypeCode_ptr _0RL_tc_plpbus__orb_mDataItem = CORBA::TypeCode::PR_struct_tc("IDL:plpbus_orb/DataItem:1.0", "DataItem", _0RL_structmember_plpbus__orb_mDataItem, 2, &_0RL_tcTrack); +static CORBA::TypeCode_ptr _0RL_tc_plpbus__orb_mDataItem = CORBA::TypeCode::PR_struct_tc("IDL:plpbus_orb/DataItem:1.0", "DataItem", _0RL_structmember_plpbus__orb_mDataItem, 3, &_0RL_tcTrack); #if defined(HAS_Cplusplus_Namespace) && defined(_MSC_VER) // MSVC++ does not give the constant external linkage otherwise. diff --git a/src/idl/RequestResponseBusSK.cc b/src/idl/RequestResponseBusSK.cc index 9877936..4359c82 100644 --- a/src/idl/RequestResponseBusSK.cc +++ b/src/idl/RequestResponseBusSK.cc @@ -19,6 +19,7 @@ plpbus_orb::DataItem::operator>>= (cdrStream &_n) const { _n.marshalString(name,0); (const ::CORBA::Any&) value >>= _n; + _n.marshalString(str_value,0); } @@ -27,6 +28,7 @@ plpbus_orb::DataItem::operator<<= (cdrStream &_n) { name = _n.unmarshalString(0); (::CORBA::Any&)value <<= _n; + str_value = _n.unmarshalString(0); } diff --git a/src/plpbus/BusClient.cc b/src/plpbus/BusClient.cc index da79689..004326b 100644 --- a/src/plpbus/BusClient.cc +++ b/src/plpbus/BusClient.cc @@ -128,7 +128,7 @@ int BusClient::send_message_and_wait_response(const char *msg_req_param, char ** return ret_val; } -int BusClient::send_message_and_request_response(BusMessage *msg_req_param) { +int BusClient::send_message_and_request_response(const BusMessage *msg_req_param) { DataItemSequence *seq; seq = (DataItemSequence *)msg_req_param->_dataItemSeq; @@ -136,7 +136,7 @@ int BusClient::send_message_and_request_response(BusMessage *msg_req_param) { return 0; } -int BusClient::send_message_and_wait_response(BusMessage *msg_req_param, BusMessage **msg_rsp_param) { +int BusClient::send_message_and_wait_response(const BusMessage *msg_req_param, BusMessage **msg_rsp_param) { DataItemSequence *seq_rsp; DataItemSequence *seq; diff --git a/src/plpbus/BusClient.hh b/src/plpbus/BusClient.hh index c71778b..9b32bf8 100644 --- a/src/plpbus/BusClient.hh +++ b/src/plpbus/BusClient.hh @@ -24,8 +24,8 @@ namespace plpbus { virtual int add_client_listener(IClientListener *listener_param); virtual int send_message_and_request_response(const char *msg_req_param); virtual int send_message_and_wait_response(const char *msg_req_param, char **msg_rsp_param); - virtual int send_message_and_request_response(BusMessage *msg_req_param); - virtual int send_message_and_wait_response(BusMessage *msg_req_param, BusMessage **msg_rsp_param); + virtual int send_message_and_request_response(const BusMessage *msg_req_param); + virtual int send_message_and_wait_response(const BusMessage *msg_req_param, BusMessage **msg_rsp_param); virtual void request_shutdown(); private: PortableServer::POA_var _poa; diff --git a/src/plpbus/BusMessage.cc b/src/plpbus/BusMessage.cc index 12b35c6..6da32de 100644 --- a/src/plpbus/BusMessage.cc +++ b/src/plpbus/BusMessage.cc @@ -40,24 +40,64 @@ BusMessage::BusMessage(long type_param, } BusMessage::~BusMessage() { + long cnt; + DataItemSequence *seq; + + log_debug("BusMessage DESTRUCTOR\n"); + seq = (DataItemSequence *)_dataItemSeq; + if (seq != NULL) { + cnt = seq->length(); +/* + DataItem *item; + + for (long ii = 0; ii < cnt; ii++) { + item = seq[ii]; + log_debug("deleting BusMessage item:\n"); + printout_dataitem(ii, item); + delete(item); + } +*/ + delete(seq); + } +} + +int BusMessage::add_cstring_parameter(const char *arg_name_param, const char *value_param) { + long ln; + int ret_val; + DataItemSequence *seq; + DataItem *item; + + ret_val = 0; + seq = (DataItemSequence *)_dataItemSeq; + if (arg_name_param != NULL) { + ln = seq->length(); + seq->length(ln + 1); + item = &((*seq)[ln]); + item->name = arg_name_param; + item->str_value = value_param; + } + else { + ret_val = -1; + } + return ret_val; } int BusMessage::add_string_parameter(string arg_name_param, string value_param) { - long length; + long ln; int ret_val; - DataItem *item; DataItemSequence *seq; + DataItem *item; + //str = (const char)"hooo"; ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; if (arg_name_param.empty() == false) { - length = seq->length(); - item = new DataItem(); - item->name = strdup(arg_name_param.c_str()); - item->value <<= value_param.c_str(); - seq->length(length + 1); - (*seq)[length] = *item; + ln = seq->length(); + seq->length(ln + 1); + item = &((*seq)[ln]); + item->name = arg_name_param.c_str(); + item->str_value = value_param.c_str(); } else { ret_val = -1; @@ -69,19 +109,16 @@ int BusMessage::add_long_parameter(string arg_name_param, long value_param) { long length; int ret_val; - DataItem *item; DataItemSequence *seq; ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; if (arg_name_param.empty() == false) { length = seq->length(); - item = new DataItem(); - item->name = strdup(arg_name_param.c_str()); - item->value <<= value_param; - seq->length(length + 1); - (*seq)[length] = *item; + (*seq)[length].name = arg_name_param.c_str(); + (*seq)[length].value <<= value_param; + (*seq)[length].str_value = ""; } else { ret_val = -1; @@ -93,19 +130,16 @@ int BusMessage::add_int_parameter(string arg_name_param, int value_param) { long length; int ret_val; - DataItem *item; DataItemSequence *seq; ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; if (arg_name_param.empty() == false) { length = seq->length(); - item = new DataItem(); - item->name = strdup(arg_name_param.c_str()); - item->value <<= (long)value_param; - seq->length(length + 1); - (*seq)[length] = *item; + (*seq)[length].name = arg_name_param.c_str(); + (*seq)[length].value <<= (long)value_param; + (*seq)[length].str_value = ""; } else { ret_val = -1; @@ -117,7 +151,6 @@ int BusMessage::add_double_parameter(string arg_name_param, double value_param) { long length; int ret_val; - DataItem *item; DataItemSequence *seq; ret_val = 0; @@ -125,12 +158,10 @@ int BusMessage::add_double_parameter(string arg_name_param, double value_param) seq = (DataItemSequence *)_dataItemSeq; if (arg_name_param.empty() == false) { length = seq->length(); - item = new DataItem(); - item->name = strdup(arg_name_param.c_str()); - item->value <<= (double)value_param; - seq->length(length + 1); - (*seq)[length] = *item; + (*seq)[length].name = arg_name_param.c_str(); + (*seq)[length].value <<= (double)value_param; + (*seq)[length].str_value = ""; } else { ret_val = -1; @@ -143,19 +174,16 @@ int BusMessage::add_float_parameter(string arg_name_param, float value_param) { long length; int ret_val; - DataItem *item; DataItemSequence *seq; ret_val = 0; seq = (DataItemSequence *)_dataItemSeq; if (arg_name_param.empty() == false) { length = seq->length(); - item = new DataItem(); - item->name = strdup(arg_name_param.c_str()); - item->value <<= (float)value_param; - seq->length(length + 1); - (*seq)[length] = *item; + (*seq)[length].name = arg_name_param.c_str(); + (*seq)[length].value <<= (float)value_param; + (*seq)[length].str_value = ""; } else { ret_val = -1; @@ -203,7 +231,8 @@ string BusMessage::get_string_parameter(string arg_name_param, int *err_flg) { seq = (DataItemSequence *)_dataItemSeq; item = get_dataitem_by_param_name(seq, arg_name_param); if (item != NULL) { - item->value >>= CORBA::Any::to_string(ch, 0); + //item->value >>= CORBA::Any::to_string(ch, 0); + ch = item->str_value; ret_val = ch; *err_flg = PLP_OK; } @@ -283,14 +312,6 @@ void BusMessage::printout_dataitem(long index, void *item) { name = dataitem->name; kind = dataitem->value.type()->kind(); switch(kind) { - case tk_string: - { - const char *val; - - dataitem->value >>= val; - log_debug("\t[%ld] %s: %s\n", index, name, val); - } - break; case tk_long: { long val; @@ -316,6 +337,12 @@ void BusMessage::printout_dataitem(long index, void *item) { } break; default: + { + const char *val; + val = dataitem->str_value; + log_debug("\t[%ld] %s: %s\n", index, name, val); + } + break; log_error("\t[%ld], name: %s, value unknown\n", index, name); } } @@ -337,6 +364,7 @@ void BusMessage::printout() { count = seq->length(); log_debug("BusMessage.printout(): parameter count: %ld\n", count); for (long ii = 0; ii < count; ii++) { + printf("item[%ld]\n", ii); item = (*seq)[ii]; printout_dataitem(ii, &item); } diff --git a/src/plpbus/BusMessage.hh b/src/plpbus/BusMessage.hh index 2ebd635..21faff6 100644 --- a/src/plpbus/BusMessage.hh +++ b/src/plpbus/BusMessage.hh @@ -19,7 +19,8 @@ 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(std::string arg_name_param, std::string string_value_param); + int add_cstring_parameter(const char *arg_name_param, const char *value_param); + int add_string_parameter(std::string arg_name_param, std::string value_param); int add_long_parameter(std::string arg_name_param, long value_param); int add_int_parameter(std::string arg_name_param, int value_param); int add_double_parameter(std::string arg_name_param, double value_param); diff --git a/src/plpbus/BusMessageInternal.cc b/src/plpbus/BusMessageInternal.cc index ee1a25f..be06076 100644 --- a/src/plpbus/BusMessageInternal.cc +++ b/src/plpbus/BusMessageInternal.cc @@ -4,20 +4,22 @@ * Created on: Aug 18, 2010 * Author: lamikr */ - +#include #include "BusMessageInternal.hh" using namespace plpbus_orb; +using namespace plpbus; -namespace plpbus { - BusMessageInternal::BusMessageInternal(DataItemSequence seq_param) : BusMessage() { - DataItemSequence *seq; +BusMessageInternal::BusMessageInternal(DataItemSequence seq_param) : BusMessage() { + DataItemSequence *seq; - seq = (DataItemSequence *)_dataItemSeq; + seq = (DataItemSequence *)_dataItemSeq; + if (seq != NULL) { delete(seq); - _dataItemSeq = new DataItemSequence(seq_param); } + _dataItemSeq = new DataItemSequence(seq_param); +} - BusMessageInternal::~BusMessageInternal() { - } +BusMessageInternal::~BusMessageInternal() { + log_debug("BusMessageInternal() DESTRUCTOR\n"); } diff --git a/src/plpbus/IServerListener.hh b/src/plpbus/IServerListener.hh index 4ea186d..d0e3627 100644 --- a/src/plpbus/IServerListener.hh +++ b/src/plpbus/IServerListener.hh @@ -13,8 +13,8 @@ namespace plpbus { class IServerListener { public: - virtual int request_received(const char *msg_in, char **msg_out) = 0; - virtual int request_received(const BusMessage *msg_req, BusMessage **msg_rsp) = 0; + virtual int request_received(const char *msg_in, const char **msg_out) = 0; + virtual int request_received(const BusMessage *msg_req, const BusMessage *msg_rsp) = 0; protected: IServerListener() {}; virtual ~IServerListener() {}; diff --git a/src/plpbus/OrbServerImpl.cc b/src/plpbus/OrbServerImpl.cc index 3fa74a0..6e09748 100644 --- a/src/plpbus/OrbServerImpl.cc +++ b/src/plpbus/OrbServerImpl.cc @@ -36,7 +36,7 @@ OrbServerImpl::~OrbServerImpl() void OrbServerImpl::send_message_and_request_response(OrbClient_ptr response_listener_param, const char* msg_req) { - char *msg_rsp; + const char *msg_rsp; msg_rsp = NULL; if (CORBA::is_nil(response_listener_param) == false) { @@ -56,8 +56,8 @@ void OrbServerImpl::send_message_and_request_response(OrbClient_ptr response_lis } char *OrbServerImpl::send_message_and_wait_response(const char* msg_req_param, ::CORBA::Long& err_flg) { - char *msg_rsp; - char *ret_val; + const char *msg_rsp; + char *ret_val; err_flg = 0; msg_rsp = NULL; @@ -79,6 +79,7 @@ void OrbServerImpl::send_dataitem_message_and_request_response(OrbClient_ptr res BusMessage *msg_req; BusMessage *msg_rsp; DataItemSequence *seq; + int err_flg; if (CORBA::is_nil(response_listener_param) == false) { if (_listener != NULL) { @@ -87,11 +88,15 @@ void OrbServerImpl::send_dataitem_message_and_request_response(OrbClient_ptr res log_debug("msg_req:\n"); msg_req->printout(); msg_rsp = NULL; - _listener->request_received(msg_req, &msg_rsp); + err_flg = 0; + msg_rsp = new BusMessage(msg_req->get_type(&err_flg)); + _listener->request_received(msg_req, msg_rsp); seq = (DataItemSequence *)msg_rsp->_dataItemSeq; log_debug("msg_rsp length: %ld\n", seq->length()); msg_rsp->printout(); response_listener_param->receive_response_dataitem_sequence(*seq); + delete(msg_req); + delete(msg_rsp); } else { log_error("send_dataitem_message_and_request_response() error, server_callback == NULL\n"); @@ -104,11 +109,13 @@ CORBA::Long OrbServerImpl::send_dataitem_message_and_wait_response(const DataIte BusMessage *msg_req; BusMessage *msg_rsp; DataItemSequence *seq; + int err_flg; seq = NULL; msg_rsp = NULL; msg_req = new BusMessageInternal(req_seq_param); - _listener->request_received(msg_req, &msg_rsp); + msg_rsp = new BusMessage(msg_req->get_type(&err_flg)); + _listener->request_received(msg_req, msg_rsp); if (msg_rsp != NULL) { seq = (DataItemSequence *)msg_rsp->_dataItemSeq; } @@ -135,7 +142,7 @@ void OrbServerImpl::add_event_listener(OrbClient_ptr client_param, srvr_thread->start(); } else { - cerr << "Failed to add event listener, listener NULL.\n"; + log_error("Failed to add event listener, listener NULL.\n"); } } @@ -143,15 +150,18 @@ void OrbServerImpl::shutdown() { omni_mutex_lock sync(server_thread_mutex); if (is_shutdown_pending() == 0) { - cout << "shutdown request received!" << endl; + log_debug("shutdown request received!\n"); // Tell the servers to exit, and wait for them to do so. _shutdown_pending = 1; while(_server_thread_count > 0) { - server_thread_shutdown_signal.wait(); + server_thread_shutdown_signal.timedwait(5, 0); + log_debug("signal received\n"); } // Shutdown the ORB (but do not wait for completion). This also // causes the main thread to unblock from CORBA::ORB::run(). + log_debug("calling orb shutdown\n"); _orb->shutdown(0); + log_debug("orb shutdown called\n"); } } @@ -169,7 +179,7 @@ int OrbServerImpl::init() { retVal = 0; } else { - cout << "init() failed" << endl; + log_error("init() failed\n"); } return retVal; } @@ -194,12 +204,12 @@ int OrbServerImpl::launch(const char *server_name) server_name, CONST_CONTEXT_KIND__PLPBUS); if (ok_flg == true) { - cout << "Registered to naming service: " << server_name << endl; + log_debug("Registered to naming service: %s\n", server_name); _orb->run(); ret_val = 0; } else { - cout << "Failed to register to naming service: " << server_name << endl; + log_error("Failed to register to naming service: %s\n", server_name); } } return ret_val; @@ -232,7 +242,7 @@ PortableServer::POA_var OrbServerImpl::create_poa(CORBA::ORB_var orb) { } } else { - cerr << "Failed to create RootPOA." << endl; + log_error("Failed to create RootPOA.\n"); } return ret_val; } @@ -334,23 +344,23 @@ bool OrbServerImpl::bind_naming_context_and_service(CosNaming::NamingContext_var * service existed already for the naming context with similar description. * Replace the existing one with a new one. */ - cout << "service " << service_name_param << " existed, replacing it." << endl; + log_warning("service %s exist, replacing it\n", service_name_param); service_context_param->rebind(context_data, service_ref_param); retVal = true; } } catch (CosNaming::NamingContext::InvalidName&) { - cerr << "Could not register service to name server, invalid service name." << endl; + log_error("Could not register service to name server, invalid service name.\n"); } catch (CosNaming::NamingContext::NotFound&) { - cerr << "Could not register service to name server, service object reference is invalid." << endl; + log_error("Could not register service to name server, service object reference is invalid.\n"); } catch (CosNaming::NamingContext::CannotProceed&) { // This should not happen! - cerr << "Could not register service to name server, unknown error." << endl; + log_error("Could not register service to name server, unknown error.\n"); } catch(CORBA::SystemException& ex) { - cerr << "Could not register service to name server, unknown error." << endl; + log_error("Could not register service to name server, unknown error.\n"); } return retVal; } @@ -360,9 +370,8 @@ int OrbServerImpl::add_server_listener(IServerListener *listener_param) int retVal; retVal = 0; - cout << "register_request_received_callback() started" << endl; + log_debug("register_request_received_callback() started\n"); _listener = listener_param; - cout << "register_callback() done" << endl; return retVal; } @@ -371,15 +380,20 @@ int OrbServerImpl::is_shutdown_pending() { } void OrbServerImpl::server_thread_closed() { - bool send_signal = false; + bool send_signal; + log_debug("server_thread_closed() started\n"); + send_signal = false; server_thread_mutex.lock(); + log_debug("server_thread_closed(), server_thread_count: %d\n", _server_thread_count); _server_thread_count--; if (_server_thread_count == 0) { send_signal = true; } server_thread_mutex.unlock(); if (send_signal == true) { + log_debug("server_thread_closed(), sending signal\n"); server_thread_shutdown_signal.signal(); + log_debug("server_thread_closed(), signal send\n"); } } diff --git a/src/plpbus/ServerEventThread.hh b/src/plpbus/ServerEventThread.hh index da0561b..03245d8 100644 --- a/src/plpbus/ServerEventThread.hh +++ b/src/plpbus/ServerEventThread.hh @@ -20,14 +20,14 @@ namespace plpbus { ServerEventThread(plpbus_orb::OrbClient_ptr client, const char *msg, int period, - plpbus::OrbServerImpl *server_obj); + OrbServerImpl *server_obj); virtual void run(void* arg); private: plpbus_orb::OrbClient_var _orb_client; CORBA::String_var _orb_msg; int _interval; - plpbus::OrbServerImpl *_server_obj; + OrbServerImpl *_server_obj; }; } diff --git a/src_test/Makefile.am b/src_test/Makefile.am index f15dbe9..21b0494 100644 --- a/src_test/Makefile.am +++ b/src_test/Makefile.am @@ -1,6 +1,7 @@ noinst_PROGRAMS = test_bus_admin \ test_bus_client \ - test_bus_server + test_bus_server \ + test_bus_msg test_bus_admin_SOURCES = test_admin.cc test_bus_admin_LDADD = $(SRC_LIBS) ../src/.libs/libplpbus.a @@ -11,6 +12,9 @@ test_bus_client_LDADD = $(SRC_LIBS) ../src/.libs/libplpbus.a test_bus_server_SOURCES = test_server.cc test_bus_server_LDADD = $(SRC_LIBS) ../src/.libs/libplpbus.a +test_bus_msg_SOURCES = test_bus_msg.cc +test_bus_msg_LDADD = $(SRC_LIBS) ../src/.libs/libplpbus.a + AM_CPPFLAGS = $(SRC_CFLAGS) -I../src -I../src/idl DISTCLEANFILES = Makefile.in \ No newline at end of file diff --git a/src_test/test_bus_msg.cc b/src_test/test_bus_msg.cc new file mode 100644 index 0000000..580e5d8 --- /dev/null +++ b/src_test/test_bus_msg.cc @@ -0,0 +1,50 @@ +/* + * test_bus_msg.c + * + * Created on: Mar 11, 2011 + * Author: lamikr + */ +#include +#include + +#include "plpbus/BusMessage.hh" + +using namespace std; +using namespace plpbus; + +int main(int argc, char** argv) +{ + BusMessage *bus_msg; + string str1; + string str2; + const char *c1; + const char *c2; + char *c3; + char *c4; + int err_flg; + + str1 = "str"; + str2 = "jou"; + //c1 = (const char *)"c1"; + //c2 = (const char *)"c2"; + c1 = strdup("c1"); + c2 = strdup("c2"); + c3 = strdup("c3"); + c4 = strdup("c4"); + bus_msg = new BusMessage(12); + if (bus_msg != NULL) { + bus_msg->add_int_parameter("int", 1); + bus_msg->add_long_parameter("long", 1); + bus_msg->add_cstring_parameter((char *)c1, (char *)c2); + bus_msg->add_cstring_parameter(c3, c4); + bus_msg->add_double_parameter("double", 1.01); + bus_msg->add_float_parameter("float", 1.01); + log_debug("params added\n"); + bus_msg->add_string_parameter(str1, str2); + bus_msg->printout(); + bus_msg->get_long_parameter("long", &err_flg); + bus_msg->get_string_parameter(str1, &err_flg); + delete(bus_msg); + } +} + diff --git a/src_test/test_client.cc b/src_test/test_client.cc index 71ba4ae..64d06bc 100644 --- a/src_test/test_client.cc +++ b/src_test/test_client.cc @@ -42,12 +42,12 @@ int ClientListenerImpl::response_received(const BusMessage *msg_rsp_param) { } int ClientListenerImpl::event_received(const char *event_param) { - cout << "event_received(char *" << event_param << ") " << endl; + log_debug("event_received(%s)\n", event_param); return 0; } int ClientListenerImpl::event_received(const BusMessage *event_param) { - cout << "event_received(BusMessage *)" << endl; + log_debug("event_received(BusMessage *)\n"); return 0; } @@ -56,43 +56,46 @@ int main(int argc, char** argv) BusClient *client; ClientListenerImpl *lstnr; int err_flg; - BusMessage *busmsg; + BusMessage *bus_msg; if (argc >= 3) { client = new BusClient(); lstnr = new ClientListenerImpl(); err_flg = client->init(argv[1]); if (err_flg == 0) { - cout << "init success" << endl; + log_debug("init success\n"); err_flg = client->add_client_listener(lstnr); if (err_flg == 0) { int err_flg; char *rsp; - 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); + bus_msg = new BusMessage(MSG_ID_HELLO); + if (bus_msg != NULL) { + bus_msg->add_string_parameter("a", "hello"); + bus_msg->add_string_parameter("b", "world"); + client->send_message_and_request_response(bus_msg); - rsp = NULL; - err_flg = client->send_message_and_wait_response(argv[2], &rsp); - cout << "rsp: " << rsp << endl; - err_flg = client->send_message_and_request_response(argv[2]); - if (err_flg == 0) { - cout << "request message send successfully" << endl; + rsp = NULL; + err_flg = client->send_message_and_wait_response(argv[2], &rsp); + log_debug("rsp: %s\n", rsp); + err_flg = client->send_message_and_request_response(argv[2]); + if (err_flg == 0) { + log_debug("request message send successfully\n"); + } + sleep(5); + delete(bus_msg); } - sleep(10); } else { - cout << "client failed to add response listener" << endl; + log_error("client failed to add response listener\n"); } } else { - cout << "client failed to init" << endl; + log_error("client failed to init\n"); } } else { - cout << "usage: " << endl; + log_info("usage: \n"); } return 0; } diff --git a/src_test/test_server.cc b/src_test/test_server.cc index 5c26776..ae00f4f 100644 --- a/src_test/test_server.cc +++ b/src_test/test_server.cc @@ -13,26 +13,26 @@ class ServerListenerImpl : public IServerListener public: inline ServerListenerImpl() {} virtual ~ServerListenerImpl() {} - virtual int request_received(const char *msg_req, char **msg_rsp); - virtual int request_received(const BusMessage *msg_req, BusMessage **msg_rsp); + virtual int request_received(const char *msg_req, const char **msg_rsp); + virtual int request_received(const BusMessage *msg_req, const BusMessage *msg_rsp); }; -int ServerListenerImpl::request_received(const char *msg_req_param, char **msg_rsp_param) { +int ServerListenerImpl::request_received(const char *msg_req_param, const char **msg_rsp_param) { log_debug("request_received(), request_param: %s\n", msg_req_param); - *msg_rsp_param = strdup("jee"); + *msg_rsp_param = "jee"; log_debug("request_received(), response param set: %s\n", *msg_rsp_param); return 0; } -int ServerListenerImpl::request_received(const BusMessage *msg_req_param, BusMessage **msg_rsp_param) { +int ServerListenerImpl::request_received(const BusMessage *msg_req_param, const BusMessage *msg_rsp_param) { long type; int err_flg; 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); + //*msg_rsp_param = new BusMessage(MSG_ID_HELLO); double dbl = (double)1.0; - (*msg_rsp_param)->add_double_parameter("rsp_param_double", dbl); + ((BusMessage *)msg_rsp_param)->add_double_parameter("rsp_param_double", dbl); return 0; } @@ -43,10 +43,16 @@ int main(int argc, char** argv) { if (argc >= 2) { log_info("starting server\n"); server = new BusServer(); - server->init(); - listener = new ServerListenerImpl(); - server->add_server_listener(listener); - server->launch(argv[1]); + if (server != NULL) { + server->init(); + listener = new ServerListenerImpl(); + if (listener != NULL) { + server->add_server_listener(listener); + server->launch(argv[1]); + } + delete(server); + delete(listener); + } } else { log_info("usage: give server name as a parameter\n");