5bb8768a98301b11a1d19774883c133c39a8737e
[platform/framework/native/channel-service.git] / src / FIo_MessagePortStub.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_MessagePortStub.cpp
20  * @brief       This is the implementation file for the  _MessagePortStub class.
21  *
22  */
23
24 #include <message-port-messages.h>
25
26 #include <bundle.h>
27
28 #include <FBaseSysLog.h>
29
30 #include "FIo_MessagePortStub.h"
31 #include "FIo_MessagePortService.h"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Io;
37 using namespace Tizen::App;
38
39 _MessagePortStub::_MessagePortStub(void)
40         : __pIpcServer(null)
41         , __pService(null)
42 {
43
44 }
45
46 _MessagePortStub::~_MessagePortStub(void)
47 {
48         if (__pIpcServer != null)
49         {
50                 __pIpcServer->Stop();
51                 delete __pIpcServer;
52         }
53 }
54
55 result
56 _MessagePortStub::Construct(void)
57 {
58         SysAssertf(__pIpcServer == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
59
60         result r = E_SUCCESS;
61         IpcServer* pIpcServer = new (std::nothrow) IpcServer;
62         SysTryReturnResult(NID_IO, pIpcServer != null, E_OUT_OF_MEMORY, "Not enough memory.");
63
64         r = pIpcServer->Construct("message-port-server", *this, false);
65         SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to create IPC server(MessagePort)", GetErrorMessage(r));
66
67         __pIpcServer = pIpcServer;
68
69         return E_SUCCESS;
70
71 CATCH:
72         delete pIpcServer;
73
74         return r;
75 }
76
77 void
78 _MessagePortStub::SetMessagePortService(_MessagePortService& service)
79 {
80         __pService = &service;
81 }
82
83 bool
84 _MessagePortStub::OnRegisterMessagePort(const BundleBuffer& buffer, int* pResult)
85 {
86         SysAssertf(__pService != null, "Message port service has not been initialized.\n");
87
88         int clientId = __pIpcServer->GetClientId();
89
90         *pResult = __pService->RegisterMessagePort(clientId, buffer);
91
92         bundle_free(buffer.b);
93
94         return true;
95 }
96
97 bool
98 _MessagePortStub::OnCheckRemotePort(const BundleBuffer& buffer, int* pResult)
99 {
100         SysAssertf(__pService != null, "Message port service has not been initialized.\n");
101
102         *pResult = __pService->CheckRemotePort(buffer);
103
104         bundle_free(buffer.b);
105
106         return true;
107 }
108
109 bool
110 _MessagePortStub::OnSendMessage(const BundleBuffer& metadata, const BundleBuffer& buffer, int* pResult)
111 {
112         SysAssertf(__pService != null, "MessagePort service has not been initialized.\n");
113
114         *pResult = __pService->SendMessage(metadata, buffer);
115
116         bundle_free(metadata.b);
117         bundle_free(buffer.b);
118
119         return true;
120 }
121
122 result
123 _MessagePortStub::SendMessage(int clientId, const BundleBuffer& metadata, const BundleBuffer& buffer)
124 {
125         SysAssertf(__pService != null, "MessagePort service has not been initialized.\n");
126
127         result r = __pIpcServer->SendResponse(clientId, new MessagePort_sendMessageAsync(metadata, buffer));
128
129         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to send a response message.");
130
131         return E_SUCCESS;
132 }
133
134 void
135 _MessagePortStub::OnIpcRequestReceived(IpcServer& server, const IPC::Message& message)
136 {
137         IPC_BEGIN_MESSAGE_MAP(_MessagePortStub, message)
138         IPC_MESSAGE_HANDLER_EX(MessagePort_registerPort, &server, OnRegisterMessagePort)
139         IPC_MESSAGE_HANDLER_EX(MessagePort_checkRemotePort, &server, OnCheckRemotePort)
140         IPC_MESSAGE_HANDLER_EX(MessagePort_sendMessage, &server, OnSendMessage)
141         IPC_END_MESSAGE_MAP_EX()
142 }
143
144 void
145 _MessagePortStub::OnIpcServerStarted(const IpcServer& server)
146 {
147
148 }
149
150 void
151 _MessagePortStub::OnIpcServerStopped(const IpcServer& server)
152 {
153
154 }
155
156 void
157 _MessagePortStub::OnIpcClientConnected(const IpcServer& server, int clientId)
158 {
159         SysLog(NID_IO, "Ipc connected");
160 }
161
162 void
163 _MessagePortStub::OnIpcClientDisconnected(const IpcServer& server, int clientId)
164 {
165         SysAssertf(__pService != null, "MessagePort service has not been initialized.");
166         SysLog(NID_IO, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
167         SysLog(NID_IO, "Unregister - client =  %d", clientId);
168         SysLog(NID_IO, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
169
170         __pService->UnregisterMessagePort(clientId);
171 }
172