ivi-main-loop
exampleMainLoop.cpp
1 #include "ivi-main-loop.h"
2 #include "ivi-main-loop-log.h"
3 
4 #include <sys/socket.h>
5 #include <fcntl.h>
6 #include "unistd.h"
7 
8 void fd_set_non_blocking(int fd)
9 {
10  int flags = fcntl(fd, F_GETFL, 0);
11  fcntl(fd, F_SETFL, flags | O_NONBLOCK);
12 }
13 
14 using namespace ivi;
15 
16 /**
17  * Basic example using the main loop abstraction
18  */
19 int main(int argc, const char * *argv)
20 {
21  DefaultDispatcherType mainLoop;
22 
23  int pipes[2];
24  if (pipe(pipes) != 0) {
25  log_error() << "Can not create pipe";
26  exit(-1);
27  }
28 
29  int &pipeIn = pipes[0];
30  int &pipeOut = pipes[1];
31  fd_set_non_blocking(pipeIn);
32  fd_set_non_blocking(pipeOut);
33 
34  TimeOutEventSource *timeOutSource = mainLoop.newTimeOutEventSource([&]() {
35  log_debug() << "Writing to pipe ";
36 
37  char bytes[64] = {};
38  if (write(pipeOut, bytes, sizeof(bytes)) != sizeof(bytes)) {
39  log_error() << "Can not write data";
40  exit(-1);
41  }
42 
44  }, 1000);
45  timeOutSource->enable();
46 
47  TimeOutEventSource *timeOutSourceClose = mainLoop.newTimeOutEventSource([&]() {
48  log_debug() << "Closing pipe";
49  close(pipeOut);
51  }, 5000);
52  timeOutSourceClose->enable();
53 
54  IdleEventSource *idleSource = mainLoop.newIdleEventSource([&]() {
55  static int i = 0;
56  i++;
57  if (i % 500000 == 0) {
58  log_debug() << "idle called " << i << " times";
59  }
60 
62  });
63  idleSource->enable();
64 
65  TimeOutEventSource *timeOutSource3 = mainLoop.newTimeOutEventSource([&]() {
66  log_debug() << "Stopping idle source ";
67  idleSource->disable();
69  }, 2000);
70  timeOutSource3->enable();
71 
72  auto *fdInputSource = mainLoop.newChannelWatchEventSource([&](
74  log_debug() << "Data received ";
75 
76  char bytes[64];
77 
78  auto r = read(pipeIn, bytes, sizeof(bytes));
79  if (r < 0) {
80  log_error() << "Can not read data";
81  exit(-1);
82  } else {
83  log_debug() << r << " bytes read";
84  }
85 
87 
89  fdInputSource->enable();
90 
91  auto *fdHangUpSource = mainLoop.newChannelWatchEventSource([&](
93  log_debug() << "Hang up detected => exit main loop";
94  mainLoop.quit();
97  fdHangUpSource->enable();
98 
99  // Run the main loop. The method will return after the quit() method has been called
100  mainLoop.run();
101 
102  delete timeOutSource;
103  delete fdInputSource;
104  delete fdHangUpSource;
105 
106  log_debug() << "Terminated";
107 
108  return 0;
109 }
virtual void disable()=0
Disables the source.
Returning that value from a source's callback function causes the source to be disabled.
A timeout source is triggered after a certain amount of time.
Returning that value from a source's callback function causes the source to remain enabled...
virtual void enable()=0
Enables the source.
An idle event source can be used to be notified whenever a dispatcher has no non-idle to trigger...
Some data is available from the channel.
The channel has been close, which means no data can be read from the corresponding file descriptor...