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