summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/MetricsObserverI.cpp
blob: 2e031e89b8c920899e0c9b027f2cce5278b0f320 (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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// **********************************************************************
//
// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Ice/MetricsObserverI.h>
#include <Ice/MetricsAdminI.h>

#include <Ice/Connection.h>
#include <Ice/Endpoint.h>

using namespace std;
using namespace Ice;
using namespace IceMX;
using namespace IceMX::Ice;

namespace 
{


class ConnectionHelper : public ObjectHelperT<ConnectionMetricsObject>
{
public:
    
    ConnectionHelper(const ConnectionPtr& con) : _connection(con)
    {
    }

    virtual string operator()(const string&) const;

private:

    friend class ConnectionAttributeResolver;

    ::Ice::ConnectionInfo*
    getConnectionInfo() const
    {
        return _connection->getInfo().get();
    }

    ::Ice::EndpointInfo*
    getEndpointInfo() const
    {
        return _connection->getEndpoint()->getInfo().get();
    }
    
    ::Ice::ConnectionPtr _connection;
};


class ConnectionAttributeResolver : public ObjectAttributeResolverT<ConnectionHelper>
{
public:

    ConnectionAttributeResolver()
    {
        //add("connectionId")

        add("incoming", &ConnectionHelper::getConnectionInfo, &ConnectionInfo::incoming);
        add("adapterName", &ConnectionHelper::getConnectionInfo, &ConnectionInfo::adapterName);

        add("localHost", &ConnectionHelper::getConnectionInfo, &IPConnectionInfo::localAddress);
        add("localPort", &ConnectionHelper::getConnectionInfo, &IPConnectionInfo::localPort);
        add("remoteHost", &ConnectionHelper::getConnectionInfo, &IPConnectionInfo::remoteAddress);
        add("remotePort", &ConnectionHelper::getConnectionInfo, &IPConnectionInfo::remotePort);

        add("mcastHost", &ConnectionHelper::getConnectionInfo, &UDPConnectionInfo::mcastAddress);
        add("mcastPort", &ConnectionHelper::getConnectionInfo, &UDPConnectionInfo::mcastPort);

        add("endpointType", &ConnectionHelper::getEndpointInfo, &EndpointInfo::type);
        add("endpointIsDatagram", &ConnectionHelper::getEndpointInfo, &EndpointInfo::datagram);
        add("endpointIsSecure", &ConnectionHelper::getEndpointInfo, &EndpointInfo::secure);
        add("endpointProtocolVersion", &ConnectionHelper::getEndpointInfo, &EndpointInfo::protocol);
        add("endpointEncodingVersion", &ConnectionHelper::getEndpointInfo, &EndpointInfo::encoding);
        add("endpointTimeout", &ConnectionHelper::getEndpointInfo, &EndpointInfo::timeout);
        add("endpointCompress", &ConnectionHelper::getEndpointInfo, &EndpointInfo::compress);

        add("endpointHost", &ConnectionHelper::getEndpointInfo, &IPEndpointInfo::host);
        add("endpointPort", &ConnectionHelper::getEndpointInfo, &IPEndpointInfo::port);
    }
};
ConnectionAttributeResolver connectionAttributes;

string
ConnectionHelper::operator()(const string& name) const
{
    return connectionAttributes(this, name);
}

}

void
ConnectionObserverI::attach()
{
    struct Attach 
    {
        void operator()(const ConnectionMetricsObjectPtr& v)
        {
            ++v->total;
            ++v->current;
            ++v->initializing;
        }
    };
    forEach(Attach());
}

void
ConnectionObserverI::detach()
{
    struct Detach 
    {
        void operator()(const ConnectionMetricsObjectPtr& v)
        {
            --v->current;
            --v->closed;
        }
    };
    forEach(Detach());
}

void
ConnectionObserverI::stateChanged(ObserverConnectionState oldState, ObserverConnectionState newState)
{
    struct StateChanged 
    {
        StateChanged(ObserverConnectionState oldState, ObserverConnectionState newState) : 
            oldState(oldState), newState(newState)
        {
        }

        void operator()(const ConnectionMetricsObjectPtr& v)
        {
            --(v.get()->*getObserverConnectionStateMetric(oldState));
            ++(v.get()->*getObserverConnectionStateMetric(newState));
        }

        int ConnectionMetricsObject::*
        getObserverConnectionStateMetric(ObserverConnectionState s)
        {
            switch(s)
            {
            case ObserverConnectionStateInitializing:
                return &ConnectionMetricsObject::initializing;
            case ObserverConnectionStateActive:
                return &ConnectionMetricsObject::active;
            case ObserverConnectionStateHolding:
                return &ConnectionMetricsObject::holding;
            case ObserverConnectionStateClosing:
                return &ConnectionMetricsObject::closing;
            case ObserverConnectionStateClosed:
                return &ConnectionMetricsObject::closed;
            }
        }    

        ObserverConnectionState oldState;
        ObserverConnectionState newState;
    }
    forEach(StateChanged(oldState, newState));
}

void 
ConnectionObserverI::sentBytes(Int num, Long duration)
{
    forEach(chain(add(&ConnectionMetricsObject::sentBytes, num), 
                  add(&ConnectionMetricsObject::sentTime, duration)));
}

void 
ConnectionObserverI::receivedBytes(Int num, Long duration)
{
    forEach(chain(add(&ConnectionMetricsObject::receivedBytes, num),
                  add(&ConnectionMetricsObject::receivedTime, duration)));
}

ObserverResolverI::ObserverResolverI(const MetricsAdminIPtr& metrics) : _metrics(metrics)
{
}

void
ObserverResolverI::setObserverUpdater(const ObserverUpdaterPtr& updater)
{

    _metrics->addUpdater("Connection", newUpdater(updater, &ObserverUpdater::updateConnectionObservers, _connections));
    //_metrics->addUpdater("Thread", new Updater(updater, &ObserverUpdater::updateThreadObservers));
    //_metrics->addUpdater("ThreadPoolThread", new Updater(updater, &ObserverUpdater::updateThreadPoolThreadObservers));
}

ConnectionObserverPtr 
ObserverResolverI::getConnectionObserver(const ConnectionPtr& con, const ConnectionObserverPtr& old)
{
    return _connections.getObserver(_metrics->getMatching("Connection", ConnectionHelper(con)), old.get());
}

ObjectObserverPtr 
ObserverResolverI::getThreadObserver(const string&, const string&, const ObjectObserverPtr&)
{
    return 0;
}

ThreadPoolThreadObserverPtr 
ObserverResolverI::getThreadPoolThreadObserver(const string&, const string&, const ThreadPoolThreadObserverPtr&)
{
    return 0;
}

RequestObserverPtr 
ObserverResolverI::getInvocationObserver(const ObjectPrx&, const string&)
{
    return 0;
}

RequestObserverPtr 
ObserverResolverI::getDispatchObserver(const ObjectPtr&, const Current&)
{
    return 0;
}