summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/AdminRouter.cpp
blob: b4ce2128540dfb6b931c95cf02ca8da3ff9e8240 (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
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#include <IceGrid/AdminRouter.h>

using namespace std;
using namespace IceGrid;

IceGrid::AdminRouter::AdminRouter(const shared_ptr<TraceLevels>& traceLevels) :
    _traceLevels(traceLevels)
{
}

void
IceGrid::AdminRouter::invokeOnTarget(const shared_ptr<Ice::ObjectPrx>& target,
                                     const pair<const Ice::Byte*, const Ice::Byte*>& inParams,
                                     function<void(bool, const pair<const Ice::Byte*, const Ice::Byte*>&)>&& response,
                                     function<void(exception_ptr)>&& exception,
                                     const Ice::Current& current)
{
    assert(target);

    if(_traceLevels->admin > 0)
    {
        Ice::Trace out(_traceLevels->logger, _traceLevels->adminCat);
        out << "routing operation `" << current.operation << "' to `" << target->ice_toString() << "'";
    }

    target->ice_invokeAsync(current.operation, current.mode, inParams,
                            [response, operation = current.operation, traceLevels = _traceLevels, target]
                            (bool ok, auto bytes)
                            {
                                if(traceLevels->admin > 0)
                                {
                                    Ice::Trace out(traceLevels->logger, traceLevels->adminCat);

                                    out << "operation `" << operation << "' routed to `"
                                        << Ice::identityToString(target->ice_getIdentity())
                                        << " -f " << target->ice_getFacet() << "' is returning ";

                                    if(ok)
                                    {
                                        out << "successfully";
                                    }
                                    else
                                    {
                                        out << "a user exception";
                                    }
                                }

                                response(move(ok), move(bytes));
                            },
                            [exception, operation = current.operation, traceLevels = _traceLevels, target]
                            (exception_ptr exptr)
                            {
                                if(traceLevels->admin > 0)
                                {
                                    Ice::Trace out(traceLevels->logger, traceLevels->adminCat);
                                    try
                                    {
                                        rethrow_exception(exptr);
                                    }
                                    catch(const std::exception& ex)
                                    {
                                        out << "operation `" << operation << "' routed to `"
                                            << Ice::identityToString(target->ice_getIdentity())
                                            << " -f " << target->ice_getFacet() << "' failed with " << ex;
                                    }
                                }

                                exception(exptr);
                            },
                            nullptr,
                            current.ctx);
}