aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/tests/startsome/tstdst.cpp
blob: 8a6b3901cc0ab190a5a869e7f2f19b18cecd3cad (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/host_name.h>
#include <vespa/vespalib/util/signalhandler.h>
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/invoker.h>
#include <vespa/fnet/transport.h>
#include <vespa/fnet/frt/target.h>
#include <sstream>
#include <unistd.h>

#include <vespa/log/log.h>
LOG_SETUP("testrpcserver");

class FNET_Transport;
class FNET_Scheduler;
class FRT_Supervisor;

namespace testrpcserver {

class RPCHooks;

class TstEnv : public FRT_IRequestWait
{
private:
    FNET_Transport    *_transport;
    FRT_Supervisor    *_supervisor;

    int                _myport;
    int                _sbport;
    RPCHooks          *_rpcHooks;

    FNET_Transport *getTransport() { return _transport; }
    FRT_Supervisor *getSupervisor() { return _supervisor; }

    TstEnv(const TstEnv &);            // Not used
    TstEnv &operator=(const TstEnv &); // Not used
public:
    const char * const _id;

    explicit TstEnv(int sbp, int myp, const char *n);
    virtual ~TstEnv();

    int MainLoop();

    void shutdown() { getTransport()->ShutDown(false); }
    void RequestDone(FRT_RPCRequest* req) override {
        if (req->IsError()) {
            LOG(error, "registration failed: %s", req->GetErrorMessage());
        } else {
            LOG(debug, "registered");
        }
    }
};


class RPCHooks : public FRT_Invokable
{
private:
    TstEnv &_env;

    RPCHooks(const RPCHooks &);            // Not used
    RPCHooks &operator=(const RPCHooks &); // Not used
public:
    explicit RPCHooks(TstEnv &env);
    virtual ~RPCHooks();

    void initRPC(FRT_Supervisor *supervisor);
private:
    void rpc_listNamesServed(FRT_RPCRequest *req);

    void rpc_stop(FRT_RPCRequest *req);
};


RPCHooks::RPCHooks(TstEnv &env)
    : _env(env)
{
}
RPCHooks::~RPCHooks()
{
}


void
RPCHooks::initRPC(FRT_Supervisor *supervisor)
{

    FRT_ReflectionBuilder rb(supervisor);
    //-------------------------------------------------------------------------
    rb.DefineMethod("slobrok.callback.listNamesServed", "", "S",
                    FRT_METHOD(RPCHooks::rpc_listNamesServed), this);
    rb.MethodDesc("Look up a rpcserver");
    rb.ReturnDesc("names", "The rpcserver names on this server");
    //-------------------------------------------------------------------------
    rb.DefineMethod("system.stop", "", "",
                    FRT_METHOD(RPCHooks::rpc_stop), this);
    rb.MethodDesc("Shut down the application");
    //-------------------------------------------------------------------------
}


void
RPCHooks::rpc_listNamesServed(FRT_RPCRequest *req)
{
    std::vector<const char *> rpcsrvlist;
    rpcsrvlist.push_back("testrpcsrv/17");
    rpcsrvlist.push_back("testrpcsrv/191");
    rpcsrvlist.push_back(_env._id);

    FRT_Values &dst = *req->GetReturn();

    FRT_StringValue *names = dst.AddStringArray(rpcsrvlist.size());

    for (uint32_t i = 0; i < rpcsrvlist.size(); ++i) {
        dst.SetString(&names[i], rpcsrvlist[i]);
    }
    if (rpcsrvlist.size() < 1) {
        req->SetError(FRTE_RPC_METHOD_FAILED, "no rpcserver names");
    }
}


// System API methods
void
RPCHooks::rpc_stop(FRT_RPCRequest *req)
{
    (void) req;
    LOG(debug, "RPC: Shutdown");
    _env.shutdown();
}


TstEnv::TstEnv(int sbp, int myp, const char *n)
    : _transport(new FNET_Transport()),
      _supervisor(new FRT_Supervisor(_transport)),
      _myport(myp),
      _sbport(sbp),
      _rpcHooks(NULL),
      _id(n)
{
    _rpcHooks = new RPCHooks(*this);
    _rpcHooks->initRPC(getSupervisor());
}


TstEnv::~TstEnv()
{
    delete _rpcHooks;
}


int
TstEnv::MainLoop()
{
    srandom(time(NULL));

    if (! getSupervisor()->Listen(_myport)) {
        LOG(error, "TestRpcServer: unable to listen to port %d", _myport);
        return 1;
    }

    std::string myrpcsrv = _id;

    std::ostringstream tmp;
    tmp << "tcp/" << vespalib::HostName::get() << ":" << _myport;
    std::string myspec = tmp.str();
    tmp.str("");
    tmp << "tcp/" << vespalib::HostName::get() << ":" << _sbport;
    std::string sbspec = tmp.str();

    FRT_RPCRequest *req = getSupervisor()->AllocRPCRequest();
    req->SetMethodName("slobrok.registerRpcServer");
    req->GetParams()->AddString(myrpcsrv.c_str());
    req->GetParams()->AddString(myspec.c_str());
    FRT_Target *slobrok = getSupervisor()->GetTarget(sbspec.c_str());
    slobrok->InvokeAsync(req, 5.0, this);
    getTransport()->Main();
    getTransport()->WaitFinished();
    return 0;
}


class App
{
public:
    int main(int argc, char **argv) {
        int sbport = 2773;
        int myport = 2774;
        const char *rpcsrvname = "testrpcsrv/17";

        int c;
        while ((c = getopt(argc, argv, "n:p:s:")) != -1) {
            switch (c) {
            case 'p':
                myport = atoi(optarg);
                break;
            case 's':
                sbport = atoi(optarg);
                break;
            case 'n':
                rpcsrvname = optarg;
                break;
            default:
                LOG(error, "unknown option letter '%c'", c);
                return 1;
            }
        }

        TstEnv *mainobj = new TstEnv(sbport, myport, rpcsrvname);
        int res = mainobj->MainLoop();
        delete mainobj;
        return res;
    }
};

} // namespace testrpcserver

int main(int argc, char **argv) {
    vespalib::SignalHandler::PIPE.ignore();
    testrpcserver::App tstdst;
    return tstdst.main(argc, argv);
}