Added C++ wrapper for the Notification Service Provider and Consumer.
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / examples / linux / notificationserviceconsumer.cpp
1 /******************************************************************\r
2  *\r
3  * Copyright 2016 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  ******************************************************************/\r
20 \r
21 #include <iostream>\r
22 \r
23 #include <unistd.h>\r
24 #include "NSConsumerService.h"\r
25 #include "NSMessage.h"\r
26 #include "NSProvider.h"\r
27 #include "ocstack.h"\r
28 \r
29 #define TAG "NotiConsumerWrapperExample"\r
30 using namespace std;\r
31 using namespace OIC::Service;\r
32 \r
33 bool isExit = false;\r
34 std::string REMOTE_SERVER_ADDRESS;\r
35 \r
36 void onNotificationPostedCb(OIC::Service::NSMessage *notification)\r
37 {\r
38     std::cout << "id : " << notification->getMessageId() << std::endl;\r
39     std::cout << "title : " << notification->getTitle() << std::endl;\r
40     std::cout << "content : " <<  notification->getContentText() << std::endl;\r
41     std::cout << "source : " <<  notification->getSourceName() << std::endl;\r
42 }\r
43 \r
44 void onNotificationSyncCb(OIC::Service::NSSyncInfo *sync)\r
45 {\r
46     std::cout << "Sync ID : " <<  sync->getMessageId() << std::endl;\r
47     std::cout << "Sync STATE : " << (int) sync->getState() << std::endl;\r
48 }\r
49 \r
50 void onDiscoverNotificationCb(OIC::Service::NSProvider *provider)\r
51 {\r
52     std::cout << "notification resource discovered" << std::endl;\r
53     provider->subscribe();\r
54     std::cout << "startSubscribing" << std::endl;\r
55 }\r
56 \r
57 void onSubscriptionAcceptedCb(OIC::Service::NSProvider *provider)\r
58 {\r
59     std::cout << "Subscription accepted" << std::endl;\r
60     std::cout << "subscribed provider Id : " << provider->getProviderId() << std::endl;\r
61     provider->setListener(onNotificationPostedCb, onNotificationSyncCb);\r
62 }\r
63 \r
64 void *OCProcessThread(void *ptr)\r
65 {\r
66     (void) ptr;\r
67 \r
68     while (!isExit)\r
69     {\r
70         usleep(2000);\r
71         if (OCProcess() != OC_STACK_OK)\r
72         {\r
73             OCStop();\r
74             break;\r
75         }\r
76     }\r
77 \r
78     return NULL;\r
79 }\r
80 \r
81 int main(void)\r
82 {\r
83     pthread_t OCThread;\r
84 \r
85     std::cout << "start Iotivity" << std::endl;\r
86     if (OCInit1(OC_CLIENT, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS) != OC_STACK_OK)\r
87     {\r
88         std::cout << "OCInit fail" << std::endl;\r
89         return 0;\r
90     }\r
91 \r
92     NSConsumerService::ConsumerConfig cfg;\r
93     cfg.m_discoverCb = onDiscoverNotificationCb;\r
94     cfg.m_acceptedCb = onSubscriptionAcceptedCb;\r
95 \r
96     pthread_create(&OCThread, NULL, OCProcessThread, NULL);\r
97 \r
98     std::cout << "Start notification consumer service" << std::endl;\r
99     while (!isExit)\r
100     {\r
101         int num;\r
102 \r
103         std::cout << "1. Start Consumer" << std::endl;\r
104         std::cout << "2. Stop Consumer" << std::endl;\r
105         std::cout << "3. Enable  NS Consumer RemoteService" << std::endl;\r
106         std::cout << "5. Exit" << std::endl;\r
107 \r
108         std::cout << "Input: " << std::endl;\r
109         std::cin >> num;\r
110         switch (num)\r
111         {\r
112             case 1:\r
113                 std::cout << "1. Start the Notification Consumer" << std::endl;\r
114                 NSConsumerService::getInstance()->Start(cfg);\r
115                 break;\r
116             case 2:\r
117                 std::cout << "2. Stop the Notification Consumer" << std::endl;\r
118                 NSConsumerService::getInstance()->Stop();\r
119                 break;\r
120             case 3:\r
121                 {\r
122                     std::cout << "3. Enable NS Consumer RemoteService" << std::endl;\r
123                     std::cout << "Input the Server Address :";\r
124                     std::cin >> REMOTE_SERVER_ADDRESS;\r
125                     NSConsumerService::getInstance()->EnableRemoteService(REMOTE_SERVER_ADDRESS);\r
126                     break;\r
127                 }\r
128             case 5:\r
129                 std::cout << "5. Exit" << std::endl;\r
130                 isExit = true;\r
131                 break;\r
132             default:\r
133                 break;\r
134         }\r
135     }\r
136 \r
137     return 0;\r
138 }\r