9ba2b0c6ada752dabb8e9d8a8f00b68ea06f5532
[platform/framework/native/channel-service.git] / src / FIo_ChannelServiceStub.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        FIo_ChannelServiceStub.cpp
20  * @brief       This is the implementation file for the  _ChannelServiceStub class.
21  *
22  */
23
24 #include <string>
25 #include <vector>
26 #include <iostream>
27 #include <new>
28
29 #include <FBaseSysLog.h>
30 #include <FBaseRt_EventDispatcher.h>
31 #include "FIo_IpcServer.h"
32 #include "FIo_ChannelServiceStub.h"
33 #include "FIo_ChannelMessages.h"
34 #include "FIo_ChannelService.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Io;
40
41 using namespace std;
42
43 namespace Tizen { namespace Io
44 {
45
46 _ChannelServiceStub::_ChannelServiceStub(void)
47         : __pIpcServer(null)
48         , __pChannelService(null)
49 {
50
51 }
52
53 _ChannelServiceStub::~_ChannelServiceStub(void)
54 {
55         if (__pIpcServer != null)
56         {
57                 __pIpcServer->Stop();
58                 delete __pIpcServer;
59         }
60 }
61
62 result
63 _ChannelServiceStub::Construct(void)
64 {
65         SysAssertf(__pIpcServer == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
66
67         result r = E_SUCCESS;
68         _IpcServer* pIpcServer = null;
69
70         pIpcServer = new (std::nothrow) _IpcServer;
71         SysTryReturnResult(NID_IO, pIpcServer != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
72
73         r = pIpcServer->Construct("osp.io.ipcserver.channelmanager", *this, false);
74         SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to create IPC server(ChannelService)", GetErrorMessage(r));
75
76         __pIpcServer = pIpcServer;
77
78         return E_SUCCESS;
79
80 CATCH:
81         delete pIpcServer;
82
83         return r;
84 }
85
86 void
87 _ChannelServiceStub::SetChannelService(_ChannelService& service)
88 {
89         __pChannelService = &service;
90 }
91
92 bool
93 _ChannelServiceStub::OnRegisterChannelMessage(const Tizen::Base::String& appId, int* pResult)
94 {
95         SysAssertf(__pChannelService != null, "Channel service has not been initialized.\n");
96
97         int clientId = __pIpcServer->GetClientId();
98
99         *pResult = __pChannelService->RegisterChannel(appId, clientId);
100
101         return true;
102 }
103
104 bool
105 _ChannelServiceStub::OnSendRequestMessage(const String& src,
106                                                                                   const String& dest,
107                                                                                   const ArrayList& args,
108                                                                                   int requestId,
109                                                                                   int* pResult)
110 {
111         SysAssertf(__pChannelService != null, "Channel service has not been initialized.\n");
112
113         *pResult = __pChannelService->SendRequest(src, dest, args, requestId);
114
115         const_cast<ArrayList*>(&args)->RemoveAll(true);
116
117         return true;
118 }
119
120 bool
121 _ChannelServiceStub::OnSendNullRequestMessage(const String& src,
122                                                                                   const String& dest,
123                                                                                   int requestId,
124                                                                                   int* pResult)
125 {
126         SysAssertf(__pChannelService != null, "Channel service has not been initialized.\n");
127
128         *pResult = __pChannelService->SendNullRequest(src, dest, requestId);
129
130         return true;
131 }
132
133 bool
134 _ChannelServiceStub::OnSendResponseMessage(const String& src,
135                                                                                    const String& dest,
136                                                                                    const ArrayList& args,
137                                                                                    int requestId,
138                                                                                    int* pResult)
139 {
140         SysAssertf(__pChannelService != null, "Channel service has not been initialized.\n");
141
142         *pResult = __pChannelService->SendResponse(src, dest, args, requestId);
143
144         const_cast<ArrayList*>(&args)->RemoveAll(true);
145
146         return true;
147 }
148
149 bool
150 _ChannelServiceStub::OnSendNullResponseMessage(const String& src,
151                                                                                    const String& dest,
152                                                                                    int requestId,
153                                                                                    int* pResult)
154 {
155         SysAssertf(__pChannelService != null, "Channel service has not been initialized.\n");
156
157         *pResult = __pChannelService->SendNullResponse(src, dest, requestId);
158
159         return true;
160 }
161
162 result
163 _ChannelServiceStub::SendRequest(int clientId, const String& src, const String& dest, const ArrayList& args, int requestId)
164 {
165         SysAssertf(__pIpcServer != null, "Not yet constructed. Construct() should be called before use.\n");
166
167         return __pIpcServer->SendResponse(clientId, new ChannelServiceMsg_sendRequestAsync(src, dest, args, requestId));
168 }
169
170 result
171 _ChannelServiceStub::SendNullRequest(int clientId, const String& src, const String& dest, int requestId)
172 {
173         SysAssertf(__pIpcServer != null, "Not yet constructed. Construct() should be called before use.\n");
174
175         return __pIpcServer->SendResponse(clientId, new ChannelServiceMsg_sendNullRequestAsync(src, dest, requestId));
176 }
177
178 result
179 _ChannelServiceStub::SendResponse(int clientId, const String& src, const String& dest, const ArrayList& args, int requestId)
180 {
181         SysAssertf(__pIpcServer != null, "Not yet constructed. Construct() should be called before use.\n");
182
183         return __pIpcServer->SendResponse(clientId, new ChannelServiceMsg_sendResponseAsync(src, dest, args, requestId));
184 }
185
186 result
187 _ChannelServiceStub::SendNullResponse(int clientId, const String& src, const String& dest, int requestId)
188 {
189         SysAssertf(__pIpcServer != null, "Not yet constructed. Construct() should be called before use.\n");
190
191         return __pIpcServer->SendResponse(clientId, new ChannelServiceMsg_sendNullResponseAsync(src, dest, requestId));
192 }
193
194 void
195 _ChannelServiceStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
196 {
197         IPC_BEGIN_MESSAGE_MAP(_ChannelServiceStub, message)
198         IPC_MESSAGE_HANDLER_EX(ChannelServiceMsg_register, &server, OnRegisterChannelMessage)
199         IPC_MESSAGE_HANDLER_EX(ChannelServiceMsg_sendRequest, &server, OnSendRequestMessage)
200         IPC_MESSAGE_HANDLER_EX(ChannelServiceMsg_sendNullRequest, &server, OnSendNullRequestMessage)
201         IPC_MESSAGE_HANDLER_EX(ChannelServiceMsg_sendResponse, &server, OnSendResponseMessage)
202         IPC_MESSAGE_HANDLER_EX(ChannelServiceMsg_sendNullResponse, &server, OnSendNullResponseMessage)
203         IPC_END_MESSAGE_MAP_EX()
204 }
205
206 void
207 _ChannelServiceStub::OnIpcServerStarted(const _IpcServer& server)
208 {
209
210 }
211
212 void
213 _ChannelServiceStub::OnIpcServerStopped(const _IpcServer& server)
214 {
215
216 }
217
218 void
219 _ChannelServiceStub::OnIpcClientConnected(const _IpcServer& server, int clientId)
220 {
221
222 }
223
224 void
225 _ChannelServiceStub::OnIpcClientDisconnected(const _IpcServer& server, int clientId)
226 {
227         SysAssertf(__pChannelService != null, "Channel service has not been initialized.");
228         SysLog(NID_IO, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
229         __pChannelService->UnregisterChannel(clientId);
230         SysLog(NID_IO, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
231 }
232
233 }}