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