From: Mika Laitio Date: Sun, 12 Aug 2012 22:41:26 +0000 (+0300) Subject: added error checks X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=4998c68abed158e1203e8495a08c28ca09cc7627;p=libplpdevicebus.git added error checks added error checks for methods used when parsing and getting the device list from the server. Signed-off-by: Mika Laitio --- diff --git a/src_client/DeviceManagerClient.cc b/src_client/DeviceManagerClient.cc index 6fabbd5..801e4d0 100644 --- a/src_client/DeviceManagerClient.cc +++ b/src_client/DeviceManagerClient.cc @@ -10,6 +10,7 @@ #include #include +#include #include #include "DeviceManagerClient.hh" @@ -42,12 +43,10 @@ DeviceManagerClient::~DeviceManagerClient() { clean_device_list(_device_list); } -const std::list *DeviceManagerClient::get_device_list(BusClient *client_param) { - send_request__get_device_list(client_param); - return _device_list; -} +const std::list *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 *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param) { +list *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 *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; - 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); } } diff --git a/src_client/DeviceManagerClient.hh b/src_client/DeviceManagerClient.hh index 6eb0e0d..cb88c90 100644 --- a/src_client/DeviceManagerClient.hh +++ b/src_client/DeviceManagerClient.hh @@ -20,12 +20,15 @@ namespace plpdevicebus { public: DeviceManagerClient(); virtual ~DeviceManagerClient(); - const std::list *get_device_list(plpbus::BusClient *client_param); - plpbus::BusMessage *get_latest_data(plpbus::BusClient *client_param, std::string device_id_param); + const std::list *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 *_device_list; - void send_request__get_device_list(plpbus::BusClient *client_param); - list *parse_device_list_msg(plpbus::BusMessage *dev_list_msg_param); + //void send_request__get_device_list(plpbus::BusClient *client_param); + list *parse_device_list_msg(plpbus::BusMessage *dev_list_msg_param, + int *err_flg); }; } diff --git a/src_client/DeviceManagerController.cc b/src_client/DeviceManagerController.cc index c28712d..bdd2348 100644 --- a/src_client/DeviceManagerController.cc +++ b/src_client/DeviceManagerController.cc @@ -54,12 +54,13 @@ DeviceManagerController::~DeviceManagerController() { } } -const std::list *DeviceManagerController::get_device_list() { +const std::list *DeviceManagerController::get_device_list(int *err_flg) { const list *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; @@ -67,13 +68,14 @@ const std::list *DeviceManagerController::get_device_list() { } catch(...) { log_error("Could not get a device list\n"); - ret_val = new list; + *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; diff --git a/src_client/DeviceManagerController.hh b/src_client/DeviceManagerController.hh index a147810..0d739a3 100644 --- a/src_client/DeviceManagerController.hh +++ b/src_client/DeviceManagerController.hh @@ -20,8 +20,8 @@ namespace plpdevicebus { public: DeviceManagerController(); virtual ~DeviceManagerController(); - const std::list *get_device_list(); - BusMessage *get_latest_data(std::string device_id_param); + const std::list *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; diff --git a/src_server/DeviceManagerServer.cc b/src_server/DeviceManagerServer.cc index 22a8c6c..d83accb 100644 --- a/src_server/DeviceManagerServer.cc +++ b/src_server/DeviceManagerServer.cc @@ -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); }