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;
40 ConvertBundleToMap(const char *pKey, const int type, const bundle_keyval_t *pVal, void *pData)
42 SysLog(NID_IO, "CB key = %s", pKey);
46 _MessagePortStub::_MessagePortStub(void)
53 _MessagePortStub::~_MessagePortStub(void)
59 _MessagePortStub::Construct(void)
61 SysAssertf(__pIpcServer == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
64 IpcServer* pIpcServer = new (std::nothrow) IpcServer;
65 SysTryReturnResult(NID_IO, pIpcServer != null, E_OUT_OF_MEMORY, "Not enough memory.");
67 r = pIpcServer->Construct("message-port-server", *this, false);
68 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to create IPC server(MessagePort)", GetErrorMessage(r));
70 __pIpcServer = pIpcServer;
81 _MessagePortStub::SetMessagePortService(_MessagePortService& service)
83 __pService = &service;
87 _MessagePortStub::OnRegisterMessagePort(const BundleBuffer& buffer, int* pResult)
89 SysAssertf(__pService != null, "Message port service has not been initialized.\n");
91 int clientId = __pIpcServer->GetClientId();
93 *pResult = __pService->RegisterMessagePort(clientId, buffer);
95 bundle_free(buffer.b);
101 _MessagePortStub::OnCheckRemotePort(const BundleBuffer& buffer, int* pResult)
103 SysAssertf(__pService != null, "Message port service has not been initialized.\n");
105 *pResult = __pService->CheckRemotePort(buffer);
107 bundle_free(buffer.b);
113 _MessagePortStub::OnSendMessage(const BundleBuffer& buffer, int* pResult)
115 SysAssertf(__pService != null, "MessagePort service has not been initialized.\n");
117 *pResult = __pService->SendMessage(buffer);
119 bundle_free(buffer.b);
125 _MessagePortStub::SendMessage(int clientId, const BundleBuffer& buffer)
127 SysAssertf(__pService != null, "MessagePort service has not been initialized.\n");
129 result r = __pIpcServer->SendResponse(clientId, new MessagePort_sendMessageAsync(buffer));
131 SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to send a response message.");
137 _MessagePortStub::OnIpcRequestReceived(IpcServer& server, const IPC::Message& message)
139 SysLog(NID_IO, "Message received, type %d", message.type());
141 IPC_BEGIN_MESSAGE_MAP(_MessagePortStub, message)
142 IPC_MESSAGE_HANDLER_EX(MessagePort_registerPort, &server, OnRegisterMessagePort)
143 IPC_MESSAGE_HANDLER_EX(MessagePort_checkRemotePort, &server, OnCheckRemotePort)
144 IPC_MESSAGE_HANDLER_EX(MessagePort_sendMessage, &server, OnSendMessage)
145 IPC_END_MESSAGE_MAP_EX()
149 _MessagePortStub::OnIpcServerStarted(const IpcServer& server)
155 _MessagePortStub::OnIpcServerStopped(const IpcServer& server)
161 _MessagePortStub::OnIpcClientConnected(const IpcServer& server, int clientId)
163 SysLog(NID_IO, "Ipc connected");
167 _MessagePortStub::OnIpcClientDisconnected(const IpcServer& server, int clientId)
169 SysAssertf(__pService != null, "Channel service has not been initialized.\n");
170 SysLog(NID_IO, "Unregister - clientId = %d", clientId);
172 __pService->UnregisterMessagePort(clientId);