-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvtQueue.cpp
More file actions
112 lines (100 loc) · 2.49 KB
/
EvtQueue.cpp
File metadata and controls
112 lines (100 loc) · 2.49 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <ostream>
#include <sstream>
#include <memory> // std::shared_ptr
#include <chrono>
#include <stdexcept> // std::runtime_error
#include "EvtQueue.hpp"
using namespace bal;
using namespace std;
#define INTERVAL_SLEEP_MS 2000
/**
* Constructor
*/
EvtQueue::EvtQueue() : _stopEventQueue(true)
{
}
/**
* Launch a thread that will service the queue
*/
int EvtQueue::LaunchQueue()
{
_stopEventQueue.store(false);
_msWait.store(INTERVAL_SLEEP_MS);
_th = std::thread(&EvtQueue::ThreadRunner, this);
//_th.detach();
return 0;
}
/**
* Sets queue thread to exit
*/
int EvtQueue::StopQueueAndWait()
{
_stopEventQueue.store(true);
_th.join();
return 0;
}
/**
* Thread runner that will service events from the queue
*/
void EvtQueue::ThreadRunner()
{
uint64_t eventCount = 0;
for (;;)
{
std::cout<<"inside the thread loop"<<endl;
while (!_equeue.waitFor(std::chrono::milliseconds(1000)))
{
cout<<","<<flush;
}
if (_stopEventQueue.load())
{
break; // jump out, thread will finish
}
cout<<"processing event on queue"<<endl;
_equeue.processOne();
}
}
/**
* Append listener callback to be called on an event on a queue
* Listener is of IQueueEventListener interface
*/
int EvtQueue::AppendListener(std::vector<EventType> &evTypes, IQueueEventListner &listener)
{
for (auto &eventType : evTypes)
{
_equeue.appendListener(eventType, [&](EventType type, const std::string &str, std::shared_ptr<GeneralArgs> args) {
listener.reactEventFn(eventType, str, args);
});
}
return 0;
}
/**
* Put an event on event queue.
*
* If no argsPtr is supplied, the evType has to support missing argsPtr
* @param evType event type
* @param textId event Id
* @param argsPtr communication package pointer
*/
int EvtQueue::PutOnQueue(EventType evType, const std::string &textId, std::shared_ptr<GeneralArgs> argsPtr)
{
try
{
// add only if consumer thread is running
if (!_stopEventQueue.load())
{
_equeue.enqueue(evType, textId, argsPtr);
return 0;
}
else
{
return 1;
}
}
catch (std::exception e)
{
throw std::runtime_error(std::string("Error: ") +
typeid(e).name() + ": " + e.what());
}
}