Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
teocquecpp.cpp

Using the Teonet QUEUE callback

Module: cque.cThis is example application to register callback and receive timeout or success call of callback function or(and) in teonet event callback when event type equal to EV_K_CQUE_CALLBACK.

In this example we:

Created on October 19, 2017, 6:07 PM

#include "teonet.hpp"
#define TCQUE_VERSION "0.0.2"
void kq_cb(uint32_t id, int type, void *data) {
std::cout << "Got Callback Queue callback with id: " << id << ", type: "
<< type << " => " << (type ? "success" : "timeout")
<< ", data: " << (const char*)data << "\n";
}
void event_cb(teo::Teonet &teo, teo::teoEvents event, void *data,
size_t data_len, void *user_data) {
static auto &cque = teo.getCQueR();
// Check teonet event
switch(event) {
// Teonet started
case EV_K_STARTED: {
//cque = new teo::Teonet::CQue(&teo);
} break;
// Teonet started
case EV_K_STOPPED: {
//delete(cque);
} break;
// Send by timer
case EV_K_TIMER: {
static int num = 0, // Timer event counter
success_id = -1; // Success ID number
std::cout << "\nTimer event " << ++num << "...\n";
switch(num) {
// After 5 sec
case 1: {
// Add lambda callback to queue and wait timeout after 5 sec ...
auto cq = cque.add(
[&](uint32_t id, int type, void *data) {
std::cout
<< "Got lambda Callback Queue callback with"
<< " id: " << id
<< ", type: " << type
<< " => " << (type ? "success" : "timeout")
<< ", data: " << (const char*)data
<< ", teonet class version: " << cque.getTeonet()->getClassVersion()
<< "\n";
}, 5.000, (void*)(const char*)"Hello");
std::cout << "Register callback id " << cque.getId(cq) << "\n";
} break;
// After 25 sec
case 5: {
// Add callback to queue and wait success result ...
auto cq = cque.add(kq_cb, 5.000, (void*)(const char*)"Hello2");
std::cout << "Register callback id " << cque.getId(cq) << " \n";
success_id = cque.getId(cq);
} break;
// After 35 sec
case 7: {
// Execute callback queue to make success result
cque.exec(success_id);
} break;
// After 50 sec
case 10: {
// Stop teonet to finish this Example
std::cout << "\nExample stopped...\n\n";
teo.stop();
} break;
default: break;
}
} break;
// Callback QUEUE event (the same as callback queue callback). This
// event send at timeout or after ksnCQueExec calls
int type = *(int*) user_data; // Callback type: 1 - success; 0 - timeout
teo::Teonet::cqueData *cq = cque.getData(data); // Pointer to Callback QUEUE data
std::cout << "Got Callback Queue event with id: " << cque.getId(cq)
<< ", type: " << type
<< " => " << (type ? "success" : "timeout")
//<< ", data: " << (const char*)cq->data
<< "\n";
} break;
// Other events
default: break;
}
}
int main(int argc, char** argv) {
std::cout << "Teocque example ver " TCQUE_VERSION ", "
"based on teonet ver. " VERSION "\n";
// Initialize teonet event manager and Read configuration
auto *teo = new teo::Teonet(argc, argv, event_cb);
// Set custom timer interval. See "case EV_K_TIMER" to continue this example
teo->setCustomTimer(2.00);
// Show Hello message
std::cout << "Example started...\n\n";
// Start teonet
teo->run();
delete(teo);
return (EXIT_SUCCESS);
}