[go: up one dir, main page]

File: test.cpp

package info (click to toggle)
drbd-utils 9.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,388 kB
  • sloc: ansic: 43,698; xml: 15,968; cpp: 7,783; sh: 3,699; makefile: 1,353; perl: 353
file content (110 lines) | stat: -rw-r--r-- 3,759 bytes parent folder | download | duplicates (2)
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
#include <cstdlib>
#include <ios>
#include <iostream>
#include <cstdio>
#include <stdexcept>
#include <DrbdResource.h>
#include <StringTokenizer.h>
#include <QTree.h>

#include "comparators.h"
#include "DrbdMon.h"
#include "EventsSourceSpawner.h"
#include "EventsIo.h"

const int BUFSZ = 1000;

int main(int argc, char* argv[])
{
    int rc = 0;
    EventsSourceSpawner* evsp {nullptr};
    EventsIo* evio {nullptr};
    char* buffer {nullptr};
    try
    {
        buffer = new char[BUFSZ];
        evsp = new EventsSourceSpawner();
        int evfd = evsp->spawn_source();
        std::cerr << "Source spawned, pid: " << static_cast<unsigned long long> (evsp->get_process_id()) <<
                  " fd: " << evfd << std::endl;
        if (evfd != -1)
        {
            evio = new EventsIo(evfd);
            bool shutdown = false;
            while (!shutdown)
            {
                EventsIo::event event_id = evio->wait_event();
                switch (event_id)
                {
                    case EventsIo::event::EVENT_LINE:
                        {
                            std::string* event_line = evio->get_event_line();
                            if (event_line != nullptr)
                            {
                                std::cout << "EVENT_LINE ready (" << *event_line << ")" << std::endl;
                            }
                            else
                            {
                                std::cerr << "EVENT_LINE ready, but get_event_line() == nullptr" << std::endl;
                            }
                        }
                        break;
                    case EventsIo::event::SIGNAL:
                        {
                            int signal_id = evio->get_signal();
                            switch (signal_id)
                            {
                                case SIGHUP:
                                    // fall-through
                                case SIGINT:
                                    // fall-through
                                case SIGTERM:
                                    std::cerr << "Received terminate notification" << std::endl;
                                    shutdown = true;
                                    break;
                                case SIGWINCH:
                                    std::cerr << "Received window size change notification" << std::endl;
                                    break;
                                default:
                                    std::cerr << "Received unexpected signal #" << signal_id << std::endl;
                                    break;
                            }
                        }
                        break;
                    case EventsIo::event::STDIN:
                        {
                            char c = std::fgetc(stdin);
                            std::fprintf(stdout, "STDIN ready (%c)\n", c);
                        }
                        break;
                    case EventsIo::event::NONE:
                        // fall-through, not supposed to happen
                    default:
                        // ignore any other events
                        break;
                }
            }
        }
    }
    catch (std::bad_alloc& out_of_memory_exc)
    {
        std::cerr << "Out of memory\n";
        rc = 1;
    }
    catch (EventsSourceException&)
    {
        std::cerr << "caught EventsSourceException" << std::endl;
    }
    catch (EventsIoException&)
    {
        std::cerr << "caught EventsIoException" << std::endl;
    }

    delete evio;
    delete evsp;
    delete[] buffer;

    std::cout << "test: Finished, rc=" << rc << std::endl;

    return rc;
}