summaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/frt/rpc/rpc_proxy.cpp
blob: e73f556b9d8c477505d7f93285bfef1cdf970c54 (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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fnet/frt/frt.h>
#include <vespa/fastos/app.h>
//-----------------------------------------------------------------------------

struct Session
{
    FNET_Connection *client;
    FRT_Target      *server;
    uint32_t         id;
    uint32_t         finiCnt;

    Session(uint32_t xid) : client(nullptr), server(nullptr), id(xid), finiCnt(0) {}
    ~Session() { assert(client == nullptr && server == nullptr && finiCnt == 2); }

private:
    Session(const Session &);
    Session &operator=(const Session &);
};

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

class RPCProxy : public FRT_Invokable
{
private:
    FRT_Supervisor &_supervisor;
    const char     *_spec;
    bool            _verbose;
    uint32_t        _currID;
    char            _prefixStr[256];

    RPCProxy(const RPCProxy &);
    RPCProxy &operator=(const RPCProxy &);

public:
    RPCProxy(FRT_Supervisor &supervisor,
             const char *spec,
             bool verbose)
        : _supervisor(supervisor),
          _spec(spec),
          _verbose(verbose),
          _currID(0),
          _prefixStr() {}

    bool IsVerbose() const { return _verbose; }
    const char *GetPrefix(FRT_RPCRequest *req);
    void PrintMethod(FRT_RPCRequest *req, const char *desc);
    void Done(FRT_RPCRequest *req);
    void HOOK_Mismatch(FRT_RPCRequest *req);
    void HOOK_Init(FRT_RPCRequest *req);
    void HOOK_Down(FRT_RPCRequest *req);
    void HOOK_Fini(FRT_RPCRequest *req);
    static Session *GetSession(FRT_RPCRequest *req)
    {
        return (Session *) req->GetConnection()->GetContext()._value.VOIDP;
    }
};

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

class ReqDone : public FRT_IRequestWait
{
private:
    RPCProxy &_proxy;

public:
    ReqDone(RPCProxy &proxy) : _proxy(proxy) {}
    void RequestDone(FRT_RPCRequest *req) override;
};

void
ReqDone::RequestDone(FRT_RPCRequest *req)
{
    _proxy.Done(req);
}

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

const char *
RPCProxy::GetPrefix(FRT_RPCRequest *req)
{
    FastOS_Time t;
    tm currTime;
    tm *currTimePt;

    t.SetNow();
    time_t secs = t.Secs();
    currTimePt = localtime_r(&secs, &currTime);
    assert(currTimePt == &currTime);
    (void) currTimePt;

    char rid[32];
    if (req->GetContext()._value.CHANNEL != nullptr) {
        sprintf(rid, "[rid=%u]", req->GetContext()._value.CHANNEL->GetID());
    } else {
        rid[0] = '\0';
    }

    sprintf(_prefixStr, "[%04d-%02d-%02d %02d:%02d:%02d:%03d][sid=%u]%s",
            currTime.tm_year + 1900,
            currTime.tm_mon + 1,
            currTime.tm_mday,
            currTime.tm_hour,
            currTime.tm_min,
            currTime.tm_sec,
            (int)(t.GetMicroSeconds() / 1000),
            GetSession(req)->id,
            rid);

    return _prefixStr;
}


void
RPCProxy::PrintMethod(FRT_RPCRequest *req, const char *desc)
{
    fprintf(stdout, "%s %s: %s\n", GetPrefix(req), desc,
            req->GetMethodName());
}


void
RPCProxy::Done(FRT_RPCRequest *req)
{
    PrintMethod(req, "RETURN");
    if (IsVerbose()) {
        req->GetReturn()->Print(8);
    }
    req->Return();
}


void
RPCProxy::HOOK_Mismatch(FRT_RPCRequest *req)
{
    PrintMethod(req, "INVOKE");
    if (IsVerbose()) {
        req->GetParams()->Print(8);
    }
    req->Detach();
    req->SetError(FRTE_NO_ERROR, "");
    if (req->GetConnection()->IsServer() && GetSession(req)->server != nullptr)
    {
        GetSession(req)->server->InvokeAsync(req, 60.0, &req->getStash().create<ReqDone>(*this));
    } else if (req->GetConnection()->IsClient() && GetSession(req)->client != nullptr)
    {
        FRT_Supervisor::InvokeAsync(GetSession(req)->client->Owner(), GetSession(req)->client,
                                    req, 60.0, &req->getStash().create<ReqDone>(*this));
    } else {
        req->SetError(FRTE_RPC_CONNECTION);
        req->Return();
    }
}


void
RPCProxy::HOOK_Init(FRT_RPCRequest *req)
{
    if (req->GetConnection()->IsClient()) {
        return;
    }
    Session *session = new Session(_currID++);
    session->client = req->GetConnection();
    session->server =
        _supervisor.Get2WayTarget(_spec,
                                  FNET_Context((void *) session));
    session->client->SetContext(FNET_Context((void *) session));
    if (session->server->GetConnection() == nullptr ||
        session->server->GetConnection()->GetState()
        > FNET_Connection::FNET_CONNECTED)
    {
        session->finiCnt = 1;
        session->client->Owner()->Close(session->client);
    }
    fprintf(stdout, "%s INIT\n", GetPrefix(req));
}


void
RPCProxy::HOOK_Down(FRT_RPCRequest *req)
{
    Session *session = GetSession(req);
    if (req->GetConnection()->IsClient()) {
        if (session->client != nullptr) {
            session->client->Owner()->Close(session->client);
        }
    } else {
        session->server->SubRef();
        session->client = nullptr;
        session->server = nullptr;
    }
}


void
RPCProxy::HOOK_Fini(FRT_RPCRequest *req)
{
    if (++GetSession(req)->finiCnt == 2) {
        fprintf(stdout, "%s FINI\n", GetPrefix(req));
        delete GetSession(req);
    }
}

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

class App : public FastOS_Application
{
public:
    int Main() override;
};

int
App::Main()
{
    FNET_SignalShutDown::hookSignals();
    // would like to turn off FNET logging somehow
    if (_argc < 3) {
        fprintf(stderr, "usage: %s <listenspec> <connectspec> [verbose]\n", _argv[0]);
        return 1;
    }
    bool verbose = (_argc > 3) && (strcmp(_argv[3], "verbose") == 0);

    FRT_Supervisor supervisor;
    RPCProxy       proxy(supervisor, _argv[2], verbose);

    supervisor.GetReflectionManager()->Reset();
    supervisor.SetSessionInitHook(FRT_METHOD(RPCProxy::HOOK_Init), &proxy);
    supervisor.SetSessionDownHook(FRT_METHOD(RPCProxy::HOOK_Down), &proxy);
    supervisor.SetSessionFiniHook(FRT_METHOD(RPCProxy::HOOK_Fini), &proxy);
    supervisor.SetMethodMismatchHook(FRT_METHOD(RPCProxy::HOOK_Mismatch),
                                     &proxy);
    supervisor.Listen(_argv[1]);
    FNET_SignalShutDown ssd(*supervisor.GetTransport());
    supervisor.Main();
    return 0;
}


int
main(int argc, char **argv)
{
    App app;
    return app.Entry(argc, argv);
}