tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / MessagePort / MessagePortManager.cpp
1 //
2 // Tizen Web Device API
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        MessagePortManager.cpp
20  * @version     0.1
21  * @brief
22  * ##
23  */
24
25 #include "MessagePortManager.h"
26 #include "MessagePortManagerProxy.h"
27 #include <Logger.h>
28
29 namespace DeviceAPI {
30 namespace MessagePort {
31
32 using namespace std;
33 using namespace WrtDeviceApis::Commons;
34
35 MessagePortManager::MessagePortManager() :
36         IMessagePortManager()
37 {
38     //Nothing to do
39 }
40
41 MessagePortManager::~MessagePortManager()
42 {
43     //Nothing to do
44 }
45
46 void MessagePortManager::OnRequestReceived(const EventMessagePortManagerRequestLocalMessagePortPtr &event)
47 {
48     LOGD("entered");
49
50     Try
51     {
52         if (!event->getLocalMessagePortNameIsSet()) {
53             LOGE("Failed to get LocalMessagePortName from event.");
54             ThrowMsg(InvalidArgumentException, "Failed to get LocalMessagePortName from event.");
55         }
56         string messagePortName = event->getLocalMessagePortName();
57
58         LocalMessagePortPtr localMessagePort =
59                 MessagePortManagerProxySingleton::Instance().requestLocalMessagePort(messagePortName);
60
61         event->setLocalMessagePort(localMessagePort);
62         event->setResult(true);
63     }
64     Catch(InvalidArgumentException)
65     {
66         LOGE("Error [InvalidArgumentException] : %s", _rethrown_exception.GetMessage().c_str());
67         event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
68         event->setResult(false);
69     }
70     Catch(PlatformException)
71     {
72         LOGE("Error [PlatformException] : %s",  _rethrown_exception.GetMessage().c_str());
73         event->setExceptionCode(ExceptionCodes::PlatformException);
74         event->setResult(false);
75     }
76     Catch(Exception)
77     {
78         LOGE("Error [UnknownException] : %s",  _rethrown_exception.GetMessage().c_str());
79         event->setExceptionCode(ExceptionCodes::UnknownException);
80         event->setResult(false);
81     }
82 }
83
84 void MessagePortManager::OnRequestReceived(const EventMessagePortManagerRequestTrustedLocalMessagePortPtr &event)
85 {
86     LOGD("entered");
87
88     Try
89     {
90         if (!event->getLocalMessagePortNameIsSet()) {
91             LOGE("Failed to get LocalMessagePortName from event.");
92             ThrowMsg(InvalidArgumentException, "Failed to get LocalMessagePortName from event.");
93         }
94         string messagePortName = event->getLocalMessagePortName();
95
96         LocalMessagePortPtr localMessagePort =
97                 MessagePortManagerProxySingleton::Instance().requestTrustedLocalMessagePort(messagePortName);
98
99         event->setLocalMessagePort(localMessagePort);
100         event->setResult(true);
101     }
102     Catch(InvalidArgumentException)
103     {
104         LOGE("Error [InvalidArgumentException] : %s", _rethrown_exception.GetMessage().c_str());
105         event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
106         event->setResult(false);
107     }
108     Catch(PlatformException)
109     {
110         LOGE("Error [PlatformException] : %s", _rethrown_exception.GetMessage().c_str());
111         event->setExceptionCode(ExceptionCodes::PlatformException);
112         event->setResult(false);
113     }
114     Catch(Exception)
115     {
116         LOGE("Error [UnknownException] : %s", _rethrown_exception.GetMessage().c_str());
117         event->setExceptionCode(ExceptionCodes::UnknownException);
118         event->setResult(false);
119     }
120 }
121
122 void MessagePortManager::OnRequestReceived(const EventMessagePortManagerRequestRemoteMessagePortPtr &event)
123 {
124     LOGD("entered");
125
126     Try
127     {
128         if (!event->getAppIdIsSet()) {
129             LOGE("Failed to get appId from event.");
130             ThrowMsg(InvalidArgumentException, "Failed to get appId from event.");
131         }
132         if (!event->getRemoteMessagePortNameIsSet()) {
133             LOGE("Failed to get remoteMessagePortName from event.");
134             ThrowMsg(InvalidArgumentException, "Failed to get remoteMessagePortName from event.");
135         }
136         string appId = event->getAppId();
137         string messagePortName = event->getRemoteMessagePortName();
138
139         RemoteMessagePortPtr remoteMessagePort =
140                 MessagePortManagerProxySingleton::Instance().requestRemoteMessagePort(appId, messagePortName);
141
142         event->setRemoteMessagePort(remoteMessagePort);
143         event->setResult(true);
144     }
145     Catch(InvalidArgumentException)
146     {
147         LOGE("Error [InvalidArgumentException] : %s", _rethrown_exception.GetMessage().c_str());
148         event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
149         event->setResult(false);
150     }
151     Catch(NotFoundException)
152     {
153         LOGE("Error [NotFoundException] : %s", _rethrown_exception.GetMessage().c_str());
154         event->setExceptionCode(ExceptionCodes::NotFoundException);
155         event->setResult(false);
156     }
157     Catch(PlatformException)
158     {
159         LOGE("Error [PlatformException] : %s", _rethrown_exception.GetMessage().c_str());
160         event->setExceptionCode(ExceptionCodes::PlatformException);
161         event->setResult(false);
162     }
163     Catch(Exception)
164     {
165         LOGE("Error [UnknownException] : %s", _rethrown_exception.GetMessage().c_str());
166         event->setExceptionCode(ExceptionCodes::UnknownException);
167         event->setResult(false);
168     }
169 }
170
171 void MessagePortManager::OnRequestReceived(const EventMessagePortManagerRequestTrustedRemoteMessagePortPtr &event)
172 {
173     LOGD("entered");
174
175     Try
176     {
177         if (!event->getAppIdIsSet()) {
178             LOGE("Failed to get appId from event.");
179             ThrowMsg(InvalidArgumentException, "Failed to get appId from event.");
180         }
181         if (!event->getRemoteMessagePortNameIsSet()) {
182             LOGE("Failed to get remoteMessagePortName from event.");
183             ThrowMsg(InvalidArgumentException, "Failed to get remoteMessagePortName from event.");
184         }
185         string appId = event->getAppId();
186         string messagePortName = event->getRemoteMessagePortName();
187
188         RemoteMessagePortPtr remoteMessagePort =
189                 MessagePortManagerProxySingleton::Instance().requestTrustedRemoteMessagePort(appId, messagePortName);
190
191         event->setRemoteMessagePort(remoteMessagePort);
192         event->setResult(true);
193     }
194     Catch(InvalidArgumentException)
195     {
196         LOGE("Error [InvalidArgumentException] : %s", _rethrown_exception.GetMessage().c_str());
197         event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
198         event->setResult(false);
199     }
200     Catch(NotFoundException)
201     {
202         LOGE("Error [NotFoundException] : %s", _rethrown_exception.GetMessage().c_str());
203         event->setExceptionCode(ExceptionCodes::NotFoundException);
204         event->setResult(false);
205     }
206     Catch(PlatformWrongStateException)
207     {
208         LOGE("Error [PlatformWrongStateException] : %s", _rethrown_exception.GetMessage().c_str());
209         event->setExceptionCode(ExceptionCodes::PlatformWrongStateException);
210         event->setResult(false);
211     }
212     Catch(PlatformException)
213     {
214         LOGE("Error [PlatformException] : %s",  _rethrown_exception.GetMessage().c_str());
215         event->setExceptionCode(ExceptionCodes::PlatformException);
216         event->setResult(false);
217     }
218     Catch(Exception)
219     {
220         LOGE("Error [UnknownException] : %s", _rethrown_exception.GetMessage().c_str());
221         event->setExceptionCode(ExceptionCodes::UnknownException);
222         event->setResult(false);
223     }
224 }
225
226 } // MessagePort
227 } // DeviceAPI