]> pilppa.com Git - libplpdevicebus.git/commitdiff
added error checks
authorMika Laitio <lamikr@pilppa.org>
Sun, 12 Aug 2012 22:41:26 +0000 (01:41 +0300)
committerMika Laitio <lamikr@pilppa.org>
Sun, 12 Aug 2012 22:41:26 +0000 (01:41 +0300)
added error checks for methods used
when parsing and getting the device list
from the server.

Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src_client/DeviceManagerClient.cc
src_client/DeviceManagerClient.hh
src_client/DeviceManagerController.cc
src_client/DeviceManagerController.hh
src_server/DeviceManagerServer.cc

index 6fabbd544bae6dc9c167f78a3ff80700f1d364ed..801e4d0cba6c31cda08006530c184118623e0c93 100644 (file)
@@ -10,6 +10,7 @@
 #include <plp/log.h>
 #include <plp/retval.h>
 
+#include <plp/Data.hh>
 #include <plp/DeviceData.hh>
 
 #include "DeviceManagerClient.hh"
@@ -42,12 +43,10 @@ DeviceManagerClient::~DeviceManagerClient() {
        clean_device_list(_device_list);
 }
 
-const std::list<plp::Device *> *DeviceManagerClient::get_device_list(BusClient *client_param) {
-       send_request__get_device_list(client_param);
-       return _device_list;
-}
+const std::list<plp::Device *> *DeviceManagerClient::get_device_list(BusClient *client_param,
+                                               int *err_flg) {
+       //send_request__get_device_list(client_param);
 
-void DeviceManagerClient::send_request__get_device_list(BusClient *client_param) {
        BusMessage      *msg_req;
        BusMessage      *msg_rsp;
 
@@ -55,9 +54,11 @@ void DeviceManagerClient::send_request__get_device_list(BusClient *client_param)
        msg_req = new BusMessage(MSG_TYPE_ID__GET_DEVICE_LIST);
        client_param->send_message_and_wait_response(msg_req, &msg_rsp);
        clean_device_list(_device_list);
-       _device_list    = parse_device_list_msg(msg_rsp);
+       _device_list    = parse_device_list_msg(msg_rsp, err_flg);
        delete(msg_req);
        delete(msg_rsp);
+
+       return _device_list;
 }
 
 BusMessage *DeviceManagerClient::get_latest_data(BusClient *client_param, string device_id_param) {
@@ -72,43 +73,63 @@ BusMessage *DeviceManagerClient::get_latest_data(BusClient *client_param, string
        return msg_rsp;
 }
 
-list<Device *> *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param) {
+list<Device *> *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param, int *err_flg) {
        int                             ii;
        long                            count;
-       int                             err_flg;
        ostringstream                   key;
        string                          id;
        string                          name;
        string                          type;
+       string                          datastr;
        EnumDeviceLifeCycleStatus       state;
        int                             state_i;
        Device                          *dev;
+       Data                            *data;
        list<Device *>                  *ret_val;
 
-       log_debug("parse_device_list_msg() started\n");
-       count   = msg_param->get_long_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, &err_flg);
-       log_debug("count: %ld\n", count);
+       count   = msg_param->get_long_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, err_flg);
+       log_debug("device count: %ld\n", count);
        ret_val = new list<Device *>;
-       if (err_flg == PLP_OK) {
+       if (*err_flg == PLP_OK) {
                for (ii = 0; ii < count; ii++) {
                        key.str("");
                        key << RSP__DEVICE_LIST__ID << ii;
-                       id      = msg_param->get_string_parameter(key.str(), &err_flg);
+                       id      = msg_param->get_string_parameter(key.str(), err_flg);
+                       if (*err_flg != PLP_OK)
+                               break;
 
                        key.str("");
                        key << RSP__DEVICE_LIST__NAME << ii;
-                       name    = msg_param->get_string_parameter(key.str(), &err_flg);
+                       name    = msg_param->get_string_parameter(key.str(), err_flg);
+                       if (*err_flg != PLP_OK)
+                               break;
 
                        key.str("");
                        key << RSP__DEVICE_LIST__TYPE << ii;
-                       type    = msg_param->get_string_parameter(key.str(), &err_flg);
+                       type    = msg_param->get_string_parameter(key.str(), err_flg);
+                       if (*err_flg != PLP_OK)
+                               break;
 
                        key.str("");
                        key << RSP__DEVICE_LIST__LF_STATE << ii;
-                       state_i = msg_param->get_int_parameter(key.str(), &err_flg);
+                       state_i = msg_param->get_int_parameter(key.str(), err_flg);
+                       if (*err_flg != PLP_OK)
+                               break;
                        state   = (EnumDeviceLifeCycleStatus)state_i;
 
-                       dev     = new DeviceData(id, type, name, state);
+                       key.str("");
+                       key << RSP__DEVICE_LIST__DATA << ii;
+                       datastr = msg_param->get_string_parameter(key.str(), err_flg);
+                       if (*err_flg != PLP_OK)
+                               break;
+
+                       data    = Data::parse_string(datastr);
+                       if (data == NULL) {
+                               *err_flg = PLP_ERR;
+                               break;
+                       }
+
+                       dev     = new DeviceData(id, type, name, state, data);
                        ret_val->push_back(dev);
                }
        }
index 6eb0e0de5539f2bddc62d6c48eb1762ba26adb79..cb88c90c1e4a8a17309ed81e2be19eac4cd7a35b 100644 (file)
@@ -20,12 +20,15 @@ namespace plpdevicebus {
                public:
                        DeviceManagerClient();
                        virtual ~DeviceManagerClient();
-                       const std::list<plp::Device *> *get_device_list(plpbus::BusClient *client_param);
-                       plpbus::BusMessage *get_latest_data(plpbus::BusClient *client_param, std::string device_id_param);
+                       const std::list<plp::Device *> *get_device_list(plpbus::BusClient *client_param,
+                                               int *err_flg);
+                       plpbus::BusMessage *get_latest_data(plpbus::BusClient *client_param,
+                                               std::string device_id_param);
                private:
                        std::list<plp::Device *>        *_device_list;
-                       void send_request__get_device_list(plpbus::BusClient *client_param);
-                       list<plp::Device *> *parse_device_list_msg(plpbus::BusMessage *dev_list_msg_param);
+                       //void send_request__get_device_list(plpbus::BusClient *client_param);
+                       list<plp::Device *> *parse_device_list_msg(plpbus::BusMessage *dev_list_msg_param,
+                                                               int *err_flg);
        };
 }
 
index c28712dce1f8ff41f0771ea9e3d2c959ae12335f..bdd2348bd6c67bd34f823703224b7f4cd7d14c22 100644 (file)
@@ -54,12 +54,13 @@ DeviceManagerController::~DeviceManagerController() {
        }
 }
 
-const std::list<plp::Device *> *DeviceManagerController::get_device_list() {
+const std::list<plp::Device *> *DeviceManagerController::get_device_list(int *err_flg) {
        const list<Device *>    *ret_val;
 
+       ret_val = NULL;
        try {
                if (_dev_man != NULL) {
-                       ret_val = _dev_man->get_device_list(_bus_client);
+                       ret_val = _dev_man->get_device_list(_bus_client, err_flg);
                }
                else {
                        ret_val = new list<Device *>;
@@ -67,13 +68,14 @@ const std::list<plp::Device *> *DeviceManagerController::get_device_list() {
        }
        catch(...) {
                log_error("Could not get a device list\n");
-               ret_val = new list<Device *>;
+               *err_flg        = PLP_ERR;
        }
        return ret_val;
 }
 
 
-BusMessage *DeviceManagerController::get_latest_data(string device_id_param) {
+BusMessage *DeviceManagerController::get_latest_data(string device_id_param,
+                                               int *err_flg) {
        BusMessage      *ret_val;
 
        ret_val = NULL;
index a147810b1371cc39121cf1c189aaa9cb3b8cd0c0..0d739a34662a91becfc21d2750021ff2a27c63f1 100644 (file)
@@ -20,8 +20,8 @@ namespace plpdevicebus {
                public:
                        DeviceManagerController();
                        virtual ~DeviceManagerController();
-                       const std::list<plp::Device *> *get_device_list();
-                       BusMessage *get_latest_data(std::string device_id_param);
+                       const std::list<plp::Device *> *get_device_list(int *err_flg);
+                       BusMessage *get_latest_data(std::string device_id_param, int *err_flg);
                private:
                        DeviceManagerClient     *_dev_man;
                        BusClient               *_bus_client;
index 22a8c6c9e4a881bd13ad3045d87cdc40bf73a862..d83accb671f6d4c06c786ce09b868ab5801844fe 100644 (file)
@@ -72,6 +72,7 @@ void DeviceManagerServer::get_device_list(const BusMessage *ret_val) {
        SensorDevice                    *sensor;
        int                             indx;
        ostringstream                   key;
+       string                          data_str;
 
        indx    = 0;
        ((BusMessage *)ret_val)->add_int_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, _dev_lst.size());
@@ -99,8 +100,9 @@ void DeviceManagerServer::get_device_list(const BusMessage *ret_val) {
                                key << RSP__DEVICE_LIST__DATA << indx;
                                data    = sensor->get_data();
                                if (data != NULL) {
-                                       log_debug("returning data: %s\n", data->to_string().c_str());
-                                       ((BusMessage *)ret_val)->add_string_parameter(key.str(), data->to_string());
+                                       data_str = data->to_string();
+                                       log_debug("returning data: %s\n", data_str.c_str());
+                                       ((BusMessage *)ret_val)->add_string_parameter(key.str(), data_str);
                                        delete(data);
                                }
                        }
@@ -164,14 +166,18 @@ void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param, const BusMe
                                if (data != NULL) {
                                        cnt     = data->get_value_count();
                                        ((BusMessage *)ret_val)->add_int_parameter(RSP__GET_LATEST_DATA__VALUE_COUNT, cnt);
-                                       ((BusMessage *)ret_val)->add_string_parameter(RSP__GET_LATEST_DATA__DATE, data->get_date().to_string());
-                                       add_data_values_to_bus_message(ret_val, data, RSP__GET_LATEST_DATA__VALUE);
+                                       ((BusMessage *)ret_val)->add_string_parameter(RSP__GET_LATEST_DATA__DATE,
+                                                                               data->get_date().to_string());
+                                       add_data_values_to_bus_message(ret_val,
+                                                                       data, RSP__GET_LATEST_DATA__VALUE);
                                        ((BusMessage *)ret_val)->printout();
                                        dr      = ((DataReader *)reader)->get_daily_summary(MIN);
                                        if (dr != NULL) {
                                                if (dr->get_count() > 0) {
                                                        data    = dr->get_first()->clone();;
-                                                       add_data_values_to_bus_message(ret_val, data, RSP__GET_LATEST_DATA__MIN_VALUE);
+                                                       add_data_values_to_bus_message(ret_val,
+                                                                       data,
+                                                                       RSP__GET_LATEST_DATA__MIN_VALUE);
                                                }
                                                delete(dr);
                                        }
@@ -179,7 +185,9 @@ void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param, const BusMe
                                        if (dr != NULL) {
                                                if (dr->get_count() > 0) {
                                                        data    = dr->get_first()->clone();
-                                                       add_data_values_to_bus_message(ret_val, data, RSP__GET_LATEST_DATA__MAX_VALUE);
+                                                       add_data_values_to_bus_message(ret_val,
+                                                                               data,
+                                                                               RSP__GET_LATEST_DATA__MAX_VALUE);
                                                }
                                                delete(dr);
                                        }