aboutsummaryrefslogtreecommitdiffstats
path: root/jrt_test/src/jrt-test/simpleserver/simpleserver.cpp
blob: 75defdc3881df42b391e949c949e4990fb00e5fd (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/log/log.h>
LOG_SETUP("simpleserver");
#include <vespa/fastos/fastos.h>
#include <vespa/fnet/frt/frt.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", true,
                        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", true,
                        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", true,
                        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");
	}
    }
};


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


int
App::Main()
{
    if (_argc < 2) {
        printf("usage: %s <listenspec> [ddw]\n", _argv[0]);
        printf("  ddw = disable direct write\n");
        return 1;
    }
    FRT_Supervisor orb;
    if (_argc >= 3 && strcmp(_argv[2], "ddw") == 0) {
        printf("(direct write disabled)\n");
        orb.GetTransport()->SetDirectWrite(false);
    }
    Server server(&orb);
    orb.Listen(_argv[1]);
    orb.Main();
    return 0;
}


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