-
Notifications
You must be signed in to change notification settings - Fork 4
/
Source.cpp
90 lines (69 loc) · 2.22 KB
/
Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "windows.h"
#include "NotificationService.h"
#define TEST_CHANEL "TestChanel"
typedef boost::shared_ptr<INotificationService> INotificationServicePtr;
class SubscriberClass
{
private:
INotificationServicePtr m_pNotificationService;
SubscriptionToken m_SubscriptionToken;
public:
SubscriberClass(INotificationServicePtr pNotificationService)
:m_pNotificationService(pNotificationService)
{
SubscribeForNotiications();
}
~SubscriberClass()
{
m_pNotificationService->Unsubscribe(m_SubscriptionToken);
}
void SubscribeForNotiications()
{
function<NotificationHandler> fnNotificationHandler = bind(&SubscriberClass::NotificationHandlerFunction, this, std::placeholders::_1);
m_SubscriptionToken = m_pNotificationService->Subscribe(TEST_CHANEL, fnNotificationHandler);
}
void NotificationHandlerFunction(const NotificationData& data)
{
NotificationData _data = data;
cout << _data["data1"] << endl;
cout << _data["data2"] << endl;
cout << "------------" << endl;
}
};
class PublisherClass
{
public:
PublisherClass(INotificationServicePtr pNotificationService)
:m_pNotificationService(pNotificationService)
{
PublishNotifications();
}
~PublisherClass()
{
}
private:
void PublishNotifications()
{
INotificationServicePtr pNotificationService = m_pNotificationService;
thread publishThread = std::thread{ [pNotificationService]() {
while (true)
{
NotificationData _data;
_data["data1"] = "Hello";
_data["data2"] = "World";
pNotificationService->Publish(TEST_CHANEL, _data);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
} };
publishThread.detach();
}
INotificationServicePtr m_pNotificationService;
};
int main()
{
INotificationServicePtr pNotificationService(NotificationService::GetNotificationService());
SubscriberClass objSubscriberClass(pNotificationService);
PublisherClass objPublisherClass(pNotificationService);
getchar();
return 0;
}