Merge "Flow control for DataControl" into tizen_2.1
[platform/framework/native/appfw.git] / src / system / FSys_CommunicationDispatcherClient.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSys_CommunicationDispatcherClient.cpp
20  * @brief               This is the implementation file for _CommunicationDispatcherClient class.
21  */
22
23 #include <FBaseSysLog.h>
24
25 #include <FIo_AppServiceIpcMessages.h>
26
27 #include "FSys_CommunicationDispatcherClient.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Io;
32
33 namespace Tizen { namespace System
34 {
35
36 static const wchar_t* _COMMUNICATION_DISPATCHER_IPC_ID = L"osp.app.ipcserver.communicationdispatcher";
37
38 _CommunicationDispatcherClient* _CommunicationDispatcherClient::__pCommunicationDispatcherClient = null;
39
40 _CommunicationDispatcherClient::_CommunicationDispatcherClient()
41 : __pIpcClient(null)
42 {
43         __communicationDispatcherClientList.Construct(0, 0, __strHashCodeProvider, __strComparer);
44
45         result r = E_SUCCESS;
46         __pIpcClient = new (std::nothrow) _IpcClient();
47         SysTryCatch(NID_SYS, __pIpcClient != null, , r, "It is failed to create IPC client");
48
49         r = __pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID, this);
50         SysTryCatch(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
51
52 CATCH:
53         SetLastResult(r);
54
55 }
56
57 _CommunicationDispatcherClient::~_CommunicationDispatcherClient()
58 {
59         delete __pIpcClient;
60         __pIpcClient = null;
61
62         __communicationDispatcherClientList.RemoveAll();
63 }
64
65 _CommunicationDispatcherClient*
66 _CommunicationDispatcherClient::GetInstance()
67 {
68         if(__pCommunicationDispatcherClient == null)
69         {
70                 __pCommunicationDispatcherClient = new (std::nothrow) _CommunicationDispatcherClient();
71         }
72         return __pCommunicationDispatcherClient;
73 }
74
75 _IpcClient*
76 _CommunicationDispatcherClient::GetIpcClient()
77 {
78         return __pIpcClient;
79 }
80
81 result
82 _CommunicationDispatcherClient::RegisterCommunicationListener(String key, _ICommunicationDispatcherListener& listener)
83 {
84         result r = E_SUCCESS;
85         bool exist = false;
86
87         r = __communicationDispatcherClientList.ContainsKey(key, exist);
88         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
89         SysTryReturnResult(NID_SYS, exist == false, E_OBJ_ALREADY_EXIST, "Required key is already registered.");
90         r = __communicationDispatcherClientList.Add(key, &listener);
91
92         return r;
93 }
94
95 result
96 _CommunicationDispatcherClient::UnregisterCommunicationListener(String key)
97 {
98         result r = E_SUCCESS;
99         bool exist = false;
100         r = __communicationDispatcherClientList.ContainsKey(key, exist);
101         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
102         SysTryReturnResult(NID_SYS, exist == true, E_OBJ_NOT_FOUND, "Required key is not registered.");
103         r = __communicationDispatcherClientList.Remove(key);
104         return r;
105 }
106
107 void
108 _CommunicationDispatcherClient::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
109 {
110         SysLog(NID_SYS, "Enter.");
111         IPC_BEGIN_MESSAGE_MAP(_CommunicationDispatcherClient, message)
112         IPC_MESSAGE_HANDLER_EX(IoService_Data, &client, OnDataReceived)
113         IPC_END_MESSAGE_MAP_EX()
114         SysLog(NID_SYS, "Exit.");
115 }
116
117 void
118 _CommunicationDispatcherClient::OnDataReceived(const ArrayList& data)
119 {
120         result r = E_SUCCESS;
121         String* pServiceId = (String*) data.GetAt(0);
122
123          _ICommunicationDispatcherListener* listener = null;
124
125         SysTryReturnVoidResult(NID_SYS, pServiceId != null, E_SYSTEM, "There is no service id.");
126
127         r = __communicationDispatcherClientList.GetValue(*pServiceId, listener);
128
129         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS && listener != null, E_SYSTEM, "Service[%ls] is available, but listener does not exist. [%s]", pServiceId->GetPointer(), GetErrorMessage(r));
130
131         listener->OnDataReceived(data);
132
133         SysLog(NID_SYS, "Message is delivered to \"%ls\"[%x]", pServiceId->GetPointer(), listener);
134         ArrayList* temp = const_cast< ArrayList *> (&data);
135         temp->RemoveAll(true);
136 }
137
138 }}