aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/vespa/slobrok/server/monitor.cpp
blob: 50ca367a6ec41032b8b586d6f538a2da82854e40 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "monitor.h"
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/target.h>
#include <vespa/fnet/channel.h>

namespace slobrok {

//-----------------------------------------------------------------------------

Monitor::Monitor(IMonitoredServer &server, FRT_Supervisor &supervisor)
    : FNET_Task(supervisor.GetScheduler()),
      _monitoredServer(server),
      _channel(NULL),
      _enabled(false)
{
}


Monitor::~Monitor()
{
    Kill(); // will deadlock if called from task
    disconnect();
}


void
Monitor::enable(FRT_Target *monitorTarget)
{
    assert(monitorTarget != NULL);
    Unschedule();
    disconnect();
    _enabled = true;
    FNET_Connection *conn = monitorTarget->GetConnection();
    if (conn != NULL) {
        _channel = conn->OpenChannel(this, FNET_Context());
    }
    if (_channel == NULL) {
        ScheduleNow();
    } else {
        _channel->SetContext(FNET_Context(_channel));
    }
}


void
Monitor::PerformTask()
{
    if (_enabled) {
        _monitoredServer.notifyDisconnected();
    }
}


void
Monitor::disable()
{
    _enabled = false;
    disconnect();
}


void
Monitor::disconnect()
{
    if (_channel != NULL) {
        _channel->SetContext(FNET_Context((FNET_Channel *)0));
        if (_channel->GetConnection()->GetState() <= FNET_Connection::FNET_CONNECTED) {
            _channel->CloseAndFree();
        }
        _channel = NULL;
    }
}


FNET_IPacketHandler::HP_RetCode
Monitor::HandlePacket(FNET_Packet *packet,
                      FNET_Context context)
{
    if (context._value.CHANNEL == 0) {
        packet->Free();
        return FNET_FREE_CHANNEL;
    }
    if (!packet->IsChannelLostCMD()) {
        packet->Free();
        return FNET_KEEP_CHANNEL;
    }
    _channel = NULL;
    ScheduleNow();
    return FNET_FREE_CHANNEL;
}

//-----------------------------------------------------------------------------

} // namespace slobrok