summaryrefslogtreecommitdiff
path: root/src/monitor.cpp
blob: deab9e0fb4024cf75a1b017ef2b150f34b92c0a5 (plain)
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
/*
    Copyright (c) 2012 250bpm s.r.o.
    Copyright (c) 2012 Other contributors as noted in the AUTHORS file

    This file is part of Crossroads I/O project.

    Crossroads I/O is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Crossroads is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "monitor.hpp"
#include "io_thread.hpp"
#include "options.hpp"
#include "random.hpp"
#include "err.hpp"

xs::monitor_t::monitor_t (xs::io_thread_t *io_thread_) :
    own_t (io_thread_, options_t ()),
    io_object_t (io_thread_),
    timer (NULL)
{
}

xs::monitor_t::~monitor_t ()
{
}

void xs::monitor_t::start ()
{
    send_plug (this);
}

void xs::monitor_t::stop ()
{
    send_stop ();
}

void xs::monitor_t::log (int sid_, const char *text_)
{
    sync.lock ();
    text = text_;
    sync.unlock ();
}

void xs::monitor_t::process_plug ()
{
    //  Schedule sending of the first snapshot.
    timer = add_timer (500 + (generate_random () % 1000));
}

void xs::monitor_t::process_stop ()
{
    rm_timer (timer);
    timer = NULL;
    send_done ();
    delete this;
}

void xs::monitor_t::timer_event (handle_t handle_)
{
    xs_assert (handle_ == timer);

    //  Send the snapshot here!
    sync.lock ();
    publish_logs (text.c_str ());
    sync.unlock ();

    //  Wait before sending next snapshot.
    timer = add_timer (500 + (generate_random () % 1000));
}