-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (61 loc) · 2.74 KB
/
main.cpp
File metadata and controls
79 lines (61 loc) · 2.74 KB
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
/**
Demo project for asynchronous event dispatching using event queue. Once run, it asks user
to enter a string, then packs this string into an event arguments and posts on event queue.
The events on the queue are serviced asynchrounsly by a separate worker thread.
Servicing an event means that a subscriber's call back functions are called with the event
data as arguments.
*/
#include "eventpp/eventqueue.h"
#include <iostream>
#include <vector>
#include <chrono>
#include "EvtQueue.hpp"
class Handler: public bal::IQueueEventListner
{
void reactEventFn(bal::EventType evType, const std::string &textId, std::shared_ptr<bal::GeneralArgs> argsPtr)
{
std::cout << std::boolalpha << "Got event: " << evType << " textId " << textId << std::endl;
}
};
int main () {
// std::shared_ptr<>bal::EvtQueue queue;
std::shared_ptr<bal::EvtQueue> queue = std::make_shared<bal::EvtQueue>();
Handler handler;
std::vector<bal::EventType> allEvtTypes;
for (int i = bal::EventType::eventMSStatus; i <= bal::EventType::eventOther; i++)
{
allEvtTypes.push_back(static_cast<bal::EventType>(i));
}
queue->AppendListener(allEvtTypes, handler);
queue->LaunchQueue();
std::string inputstr{};
while( inputstr!="quit")
{
std::cout<< "Enter new command:"<<std::endl;
std::getline(std::cin,inputstr);
std::cout<< "Entered "<<inputstr <<std::endl;
std::shared_ptr<bal::GeneralArgs> args = std::make_shared<bal::GeneralArgs>(bal::UCommandID::GetData);
queue->PutOnQueue(bal::EventType::eventMSStatus, inputstr, args);
}
queue->StopQueueAndWait();
/*eventpp::EventQueue<int, void (const std::string &, const bool)> queue;
queue.appendListener(3, [](const std::string s, bool b) {
std::cout << std::boolalpha << "Got event 3, s is " << s << " b is " << b << std::endl;
});
queue.appendListener(5, [](const std::string s, bool b) {
std::cout << std::boolalpha << "Got event 5, s is " << s << " b is " << b << std::endl;
});
// The listeners are not triggered during enqueue.
queue.enqueue(3, "Hello", true);
queue.enqueue(5, "World", false);
// Process the event queue, dispatch all queued events.
queue.process();
auto microsecondsUTC1 = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
while(!queue.waitFor(std::chrono::milliseconds(20))){
std::cout<<"."<<std::flush;
}
std::cout<<std::endl;
auto microsecondsUTC2 = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
std::cout << "timediff="<<microsecondsUTC2-microsecondsUTC1<<std::endl;
*/
}