aboutsummaryrefslogtreecommitdiffstats
path: root/jrt_test/src/jrt-test/simpleserver/simpleserver.cpp
blob: 9feedb8d663d4f9831cc1b5b951a1f8e99e4d5cc (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/rpcrequest.h>
#include <vespa/fnet/transport.h>
#include <vespa/vespalib/util/signalhandler.h>

class Server : public FRT_Invokable
{
private:
    Server(const Server &);
    Server &operator=(const Server &);

public:
    Server(FRT_Supervisor *s)
    {
        FRT_ReflectionBuilder rb(s);
        //---------------------------------------------------------------------
        rb.DefineMethod("inc", "i", "i",
                        FRT_METHOD(Server::rpc_inc), this);
        rb.MethodDesc("Increase an integer value");
        rb.ParamDesc("value", "initial value");
        rb.ReturnDesc("result", "value + 1");
        //---------------------------------------------------------------------
        rb.DefineMethod("blob", "x", "x",
                        FRT_METHOD(Server::rpc_blob), this);
        rb.MethodDesc("Send a copy of a blob back to the client");
        rb.ParamDesc("blob", "the original blob");
        rb.ReturnDesc("blob", "a copy of the original blob");
        //---------------------------------------------------------------------
        rb.DefineMethod("test", "iib", "i",
                        FRT_METHOD(Server::rpc_test), this);
        rb.MethodDesc("Magic test method");
        rb.ParamDesc("value", "the value");
        rb.ParamDesc("error", "error code to set");
        rb.ParamDesc("extra", "if not 0, add an extra return value");
        rb.ReturnDesc("value", "the value");
        //---------------------------------------------------------------------
    }

    void rpc_inc(FRT_RPCRequest *req)
    {
        req->GetReturn()->AddInt32(req->GetParams()->GetValue(0)._intval32 + 1);
    }

    void rpc_blob(FRT_RPCRequest *req)
    {
        req->GetReturn()->AddData(req->GetParams()->GetValue(0)._data._buf,
                                  req->GetParams()->GetValue(0)._data._len);
    }

    void rpc_test(FRT_RPCRequest *req)
    {
	int value = req->GetParams()->GetValue(0)._intval32;
	int error = req->GetParams()->GetValue(1)._intval32;
	int extra = req->GetParams()->GetValue(2)._intval8;

	req->GetReturn()->AddInt32(value);
	if (extra != 0) {
	    req->GetReturn()->AddInt32(value);
	}
	if (error != 0) {
	    req->SetError(error, "Custom error");
	}
    }
};


int main(int argc, char **argv) {
    vespalib::SignalHandler::PIPE.ignore();
    if (argc < 2) {
        printf("usage: %s <listenspec>\n", argv[0]);
        return 1;
    }
    fnet::frt::StandaloneFRT frt;
    Server server(&frt.supervisor());
    frt.supervisor().Listen(argv[1]);
    frt.supervisor().GetTransport()->WaitFinished();
    return 0;
}