2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIo_MessagePortStub.cpp
20 * @brief This is the implementation file for the _MessagePortStub class.
24 #include <message-port-messages.h>
28 #include <FBaseSysLog.h>
30 #include "FIo_MessagePortStub.h"
31 #include "FIo_MessagePortService.h"
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;
39 _MessagePortStub::_MessagePortStub(void)
46 _MessagePortStub::~_MessagePortStub(void)
48 if (__pIpcServer != null)
56 _MessagePortStub::Construct(void)
58 SysAssertf(__pIpcServer == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
61 IpcServer* pIpcServer = new (std::nothrow) IpcServer;
62 SysTryReturnResult(NID_IO, pIpcServer != null, E_OUT_OF_MEMORY, "Not enough memory.");
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));
67 __pIpcServer = pIpcServer;
78 _MessagePortStub::SetMessagePortService(_MessagePortService& service)
80 __pService = &service;
84 _MessagePortStub::OnRegisterMessagePort(const BundleBuffer& buffer, int* pResult)
86 SysAssertf(__pService != null, "Message port service has not been initialized.\n");
88 int clientId = __pIpcServer->GetClientId();
90 *pResult = __pService->RegisterMessagePort(clientId, buffer);
92 bundle_free(buffer.b);
98 _MessagePortStub::OnCheckRemotePort(const BundleBuffer& buffer, int* pResult)
100 SysAssertf(__pService != null, "Message port service has not been initialized.\n");
102 *pResult = __pService->CheckRemotePort(buffer);
104 bundle_free(buffer.b);
110 _MessagePortStub::OnSendMessage(const BundleBuffer& metadata, const BundleBuffer& buffer, int* pResult)
112 SysAssertf(__pService != null, "MessagePort service has not been initialized.\n");
114 *pResult = __pService->SendMessage(metadata, buffer);
116 bundle_free(metadata.b);
117 bundle_free(buffer.b);
123 _MessagePortStub::SendMessage(int clientId, const BundleBuffer& metadata, const BundleBuffer& buffer)
125 SysAssertf(__pService != null, "MessagePort service has not been initialized.\n");
127 result r = __pIpcServer->SendResponse(clientId, new MessagePort_sendMessageAsync(metadata, buffer));
129 SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to send a response message.");
135 _MessagePortStub::OnIpcRequestReceived(IpcServer& server, const IPC::Message& message)
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()
145 _MessagePortStub::OnIpcServerStarted(const IpcServer& server)
151 _MessagePortStub::OnIpcServerStopped(const IpcServer& server)
157 _MessagePortStub::OnIpcClientConnected(const IpcServer& server, int clientId)
159 SysLog(NID_IO, "Ipc connected");
163 _MessagePortStub::OnIpcClientDisconnected(const IpcServer& server, int clientId)
165 SysAssertf(__pService != null, "Channel service has not been initialized.\n");
166 SysLog(NID_IO, "Unregister - clientId = %d", clientId);
168 __pService->UnregisterMessagePort(clientId);