]> pilppa.com Git - libplpbus.git/commitdiff
small cleanups for error handling and memory leaks
authorMika Laitio <lamikr@pilppa.org>
Thu, 17 Mar 2011 22:59:19 +0000 (00:59 +0200)
committerMika Laitio <lamikr@pilppa.org>
Thu, 17 Mar 2011 22:59:19 +0000 (00:59 +0200)
src/plpbus/BusClient.cc
src/plpbus/BusClient.hh
src/plpbus/OrbServerImpl.cc
src_test/test_client.cc

index 004326bea08d4ad9f5f05197c9b6ce45170cc7da..75583fe4cd9ffaf008830944d825403348eebff4 100644 (file)
@@ -5,7 +5,9 @@
  *      Author: lamikr
  */
 
+#include <malloc.h>
 #include <plp/log.h>
+#include <plp/retval.h>
 
 #include "BusClient.hh"
 #include "ClientServerCommon.hh"
@@ -47,11 +49,12 @@ class OrbClientImpl : public virtual POA_plpbus_orb::OrbClient {
                }
 
                virtual void receive_response_dataitem_sequence(const ::plpbus_orb::DataItemSequence& msg_rsp_param) {
-                       BusMessageInternal      *bus_msg;
+                       const BusMessageInternal        *bus_msg;
 
                        if (listener != NULL) {
                                bus_msg = new BusMessageInternal(msg_rsp_param);
                                listener->response_received(bus_msg);
+                               delete(bus_msg);
                        }
                }
 };
@@ -65,6 +68,9 @@ BusClient::BusClient()
 
 BusClient::~BusClient()
 {
+       if (_poa != NULL) {
+               //_poa->destroy(true, true);
+       }
        if (_orb != NULL) {
                _orb->destroy();
                _orb    = NULL;
@@ -76,12 +82,14 @@ int BusClient::init(const char *server_name) {
        int                     argc;
        char                    **argv;
        CORBA::Object_var       server_obj;
+       int                     ret_val;
 
        argc    = 0;
        argv    = NULL;
-       log_info("init() started\n");;
+       ret_val = PLP_ERR;
+       log_info("CORBA client init() started\n");
        _orb    = CORBA::ORB_init(argc, argv);
-       if (_orb != NULL) {
+       if (CORBA::is_nil(_orb) == false) {
                log_debug("ORB_init() done, finding server: %s\n", server_name);
                server_obj      = find_server_object_by_name(_orb,
                                                        CONST_CONTEXT_NAME__PLPBUS,
@@ -90,13 +98,19 @@ int BusClient::init(const char *server_name) {
                                                        CONST_CONTEXT_NAME__PLPBUS);
                if (CORBA::is_nil(server_obj) == false) {
                        _server         = plpbus_orb::OrbServer::_narrow(server_obj);
-                       log_info("Server object found: %s", server_name);
+                       if (CORBA::is_nil(_server) == false) {
+                               log_info("CORBA service found: %s", server_name);
+                               ret_val = PLP_OK;
+                       }
                }
                else {
-                       log_error("Could not find server object: %s\n", server_name);
+                       log_error("Could not find CORBA service: %s\n", server_name);
                }
        }
-       return 0;
+       else {
+               log_error("Could not init CORBA client\n");
+       }
+       return ret_val;
 }
 
 int BusClient::add_client_listener(IClientListener *listener_param) {
@@ -123,8 +137,11 @@ int BusClient::send_message_and_request_response(const char *msg_req_param) {
 
 int BusClient::send_message_and_wait_response(const char *msg_req_param, char **msg_rsp_param) {
        CORBA::Long     ret_val;
+       char            *rsp;
 
-       *msg_rsp_param  = _server->send_message_and_wait_response(msg_req_param, ret_val);
+       rsp             = _server->send_message_and_wait_response(msg_req_param, ret_val);
+       *msg_rsp_param  = strdup(rsp);
+       CORBA::string_free(rsp);
        return ret_val;
 }
 
@@ -185,13 +202,14 @@ PortableServer::POA_var BusClient::create_poa(CORBA::ORB_var orb) {
        return ret_val;
 }
 
-CosNaming::NamingContext_var BusClient::get_name_service_context(CORBA::ORB_var orb_param) {
+CosNaming::NamingContext_var BusClient::get_name_service_context(CORBA::ORB_var orb_param,
+                                                               const char *nameservice_name_param) {
        CORBA::Object_var               ns_obj;
        CosNaming::NamingContext_var    ret_val;
 
        ret_val = NULL;
        try {
-               ns_obj  = orb_param->resolve_initial_references(CONST_NAME_SERVICE_NAME);
+               ns_obj  = orb_param->resolve_initial_references(nameservice_name_param);
                if (CORBA::is_nil(ns_obj) == false) {
                        // narrow the object reference
                        ret_val = CosNaming::NamingContext::_narrow(ns_obj);
@@ -222,9 +240,12 @@ CORBA::Object_var BusClient::find_server_object_by_name(CORBA::ORB_var orb_param
        CORBA::Object_var               ret_val;
        CosNaming::NamingContext_var    ns_root_context;
        CosNaming::Name                 service_data;
+       char *n;
 
-       ret_val         = CORBA::Object::_nil();
-       ns_root_context = get_name_service_context(orb_param);
+       ret_val = CORBA::Object::_nil();
+       n       = strdup(CONST_NAME_SERVICE_NAME);
+       ns_root_context = get_name_service_context(orb_param, n);
+       free(n);
        if (CORBA::is_nil(ns_root_context) == false) {
                // define search criteria
                service_data.length(2);
index 9b32bf8a054f14714910325fc058a7781a088ec2..32ab7ad9166cc7c64d3a5ae4e75d6e52f370cd31 100644 (file)
@@ -11,6 +11,7 @@
 #include <iostream>
 #include <omniORB4/CORBA.h>
 
+
 #include "RequestResponseBus.hh"
 #include "IClientListener.hh"
 #include "BusMessage.hh"
@@ -34,7 +35,7 @@ namespace plpbus {
                        plpbus_orb::OrbClient_var       _client;
 
                        PortableServer::POA_var create_poa(CORBA::ORB_var orb);
-                       CosNaming::NamingContext_var get_name_service_context(CORBA::ORB_var orb_param);
+                       CosNaming::NamingContext_var get_name_service_context(CORBA::ORB_var orb_param, const char *nameservice_name);
                        /**
                         * Finds server object by it's name by using following keywords:
                         * name[0].id   = (const char *)CONST_LEAF_CONTEXT_NAME__PLPBUS;
index 6e097486f5e7571effb81d332957bbed22393a3c..9a48aba32fd12eba13b916924b871c9bcec902c4 100644 (file)
@@ -64,13 +64,14 @@ char *OrbServerImpl::send_message_and_wait_response(const char* msg_req_param, :
        if (_listener != NULL) {
                log_debug("send_message_and_wait_response(): %s, server_callback != NULL\n", msg_req_param);
                _listener->request_received(msg_req_param, &msg_rsp);
+               //ret_val       = CORBA::string_dup(msg_rsp);
+               ret_val = CORBA::string_dup(msg_rsp);
        }
        else {
                log_error("send_message_and_wait_response() error, server callback == NULL\n");
-               msg_rsp = strdup(msg_req_param);
+               ret_val = CORBA::string_dup(msg_req_param);
                err_flg = -1;
        }
-       ret_val = CORBA::string_dup(msg_rsp);
        return ret_val;
 }
 
@@ -288,9 +289,7 @@ CosNaming::NamingContext_var OrbServerImpl::get_service_naming_context(const cha
                                        }
                                }
                                else {
-                                       log_debug("get_service_naming_context() 8\n");
                                        ret_val = CosNaming::NamingContext::_narrow(service_obj);
-                                       log_debug("get_service_naming_context() 9\n");
                                        if (CORBA::is_nil(ret_val) ) {
                                                log_error("Failed to get existing context from name service for %s, narrowing failed.\n", service_name_param);
                                        }
index 64d06bc1817351fc7dc126d441ea8fcf5784a1fa..26f50c80230113d4c8e6672998dee25967a675cf 100644 (file)
@@ -4,6 +4,7 @@
  *  Created on: Jun 9, 2010
  *      Author: lamikr
  */
+#include <malloc.h>
 #include <plp/log.h>
 
 #include "plpbus/BusServer.hh"
@@ -78,6 +79,7 @@ int main(int argc, char** argv)
                                        rsp     = NULL;
                                        err_flg = client->send_message_and_wait_response(argv[2], &rsp);
                                        log_debug("rsp: %s\n", rsp);
+                                       free(rsp);
                                        err_flg = client->send_message_and_request_response(argv[2]);
                                        if (err_flg == 0) {
                                                log_debug("request message send successfully\n");
@@ -93,6 +95,8 @@ int main(int argc, char** argv)
                else {
                        log_error("client failed to init\n");
                }
+               delete(client);
+               delete(lstnr);
        }
        else {
                log_info("usage: <server_name> <message text>\n");