diff options
Diffstat (limited to 'mobile_src/Messaging/CallbackMgr.cpp')
-rw-r--r-- | mobile_src/Messaging/CallbackMgr.cpp | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/mobile_src/Messaging/CallbackMgr.cpp b/mobile_src/Messaging/CallbackMgr.cpp new file mode 100644 index 0000000..646405c --- /dev/null +++ b/mobile_src/Messaging/CallbackMgr.cpp @@ -0,0 +1,172 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include "CallbackMgr.h" +#include "MsgServiceHandleMgr.h" +#include <dpl/singleton_safe_impl.h> +#include <Logger.h> + +IMPLEMENT_SAFE_SINGLETON(DeviceAPI::Messaging::CallbackMgr) + +#include "ISendingObserver.h" + +extern "C" { +#include <msg_transport.h> +#include <msg.h> +} + +namespace DeviceAPI { +namespace Messaging { + +CallbackMgr::CallbackMgr() +{ + LoggerI("enter"); + // register callback once per process + if (NULL == MsgGetCommonHandle()) { + LoggerE("Unable to open handle"); + } else { + if (msg_reg_sent_status_callback(MsgGetCommonHandle(), sendCallback, + static_cast<void*>(this)) != + MSG_SUCCESS) { + LoggerE("callback registration error"); + } else { + LoggerD("callback registred succesfully"); + } + } +} + +CallbackMgr::~CallbackMgr() +{ + LoggerI("enter"); +} + +msg_error_t CallbackMgr::registerAndSend(SendingFunction sendingFn, + const msg_struct_t& message, + ISendingObserver* observer) +{ + int tempInt = 0; + msg_struct_t req = NULL; + msg_struct_t sendOpt = NULL; + + req = msg_create_struct(MSG_STRUCT_REQUEST_INFO); + sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT); + msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false); + + msg_set_struct_handle(req, MSG_REQUEST_MESSAGE_HND, message); + msg_set_struct_handle(req, MSG_REQUEST_SENDOPT_HND, sendOpt); + + DPL::Mutex::ScopedLock lock(&m_mutex); + msg_error_t err = (*sendingFn)(MsgGetCommonHandle(), req); + + if (err == MSG_SUCCESS) + { + if (observer) + { + msg_struct_list_s *addr_list = NULL; + msg_error_t tempErr; + tempErr = msg_get_list_handle(message, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list); + if(tempErr != MSG_SUCCESS) + { + LoggerE("msg_get_list_handle Err : " << tempErr); + } + char strNumber[MAX_ADDRESS_VAL_LEN] = {0,}; + msg_get_str_value(addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN); + + msg_get_int_value(req, MSG_REQUEST_REQUESTID_INT, &tempInt); + m_sendRequests[tempInt] = observer; + observer->setSendigRequestId(tempInt); + observer->setRecipient(strNumber); + } + } + + if (NULL != sendOpt) + { + msg_release_struct(&sendOpt); + } + if (NULL != req) + { + msg_release_struct(&req); + } + + return err; +} + +void CallbackMgr::unregister(int reqId) +{ + LoggerD(" reqId is : " << reqId); + DPL::Mutex::ScopedLock lock(&m_mutex); + + SendReqMap::iterator it = m_sendRequests.find(reqId); + if (it == m_sendRequests.end()) { + LoggerW("No matching request found"); + return; + } + LoggerI("Matching send request found!"); + m_sendRequests.erase(it); +} + +//void CallbackMgr::sendCallback(MSG_HANDLE_T handle, +// MSG_SENT_STATUS_S *sent_status, +void CallbackMgr::sendCallback(msg_handle_t handle, + msg_struct_t sent_status, + void *user_param) +{ +// LoggerI( +// "callback received. Req id = " << sent_status->reqId << +// " Status = " << (int)sent_status->status); + CallbackMgr* instance = static_cast<CallbackMgr*>(user_param); + if (!instance) { + LoggerE("Empty user data!"); + return; + } + + instance->call(sent_status); +} + +//void CallbackMgr::call(MSG_SENT_STATUS_S *sent_status) +void CallbackMgr::call(msg_struct_t sent_status) +{ + int reqId = 0; + int status = MSG_NETWORK_NOT_SEND; + + msg_get_int_value(sent_status, MSG_SENT_STATUS_REQUESTID_INT, &reqId); + msg_get_int_value(sent_status, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status); + + DPL::Mutex::ScopedLock lock(&m_mutex); + + // SendReqMap::iterator it = m_sendRequests.find(sent_status->reqId); + if(MSG_NETWORK_SEND_SUCCESS != status + && MSG_NETWORK_SEND_FAIL != status + && MSG_NETWORK_SEND_TIMEOUT != status) + { + LoggerW("skip status " << status); + return; + } + + SendReqMap::iterator it = m_sendRequests.find(reqId); + if (it == m_sendRequests.end()) { + LoggerW("No matching request found"); + return; + } + LoggerI("Matching send request found!"); + // call it + it->second->sendingCallback(sent_status); + m_sendRequests.erase(it); + +} +} +} |