summaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/frt/rpc/rpc_server.cpp
blob: b05fe15f6635cc747053ce931c8b5b1ab05cd96e (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
// 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>

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

class RPCServer : public FRT_Invokable
{
private:
    FRT_Supervisor *_supervisor;

    RPCServer(const RPCServer &);
    RPCServer &operator=(const RPCServer &);

public:
    RPCServer() : _supervisor(nullptr) {}
    void InitRPC(FRT_Supervisor *s);
    void RPC_concat(FRT_RPCRequest *req);
    void RPC_addFloat(FRT_RPCRequest *req);
    void RPC_addDouble(FRT_RPCRequest *req);
    int Main(int argc, char **argv);
};

void
RPCServer::InitRPC(FRT_Supervisor *s)
{
    FRT_ReflectionBuilder rb(s);
    //-------------------------------------------------------------------
    rb.DefineMethod("concat", "ss", "s", true,
                    FRT_METHOD(RPCServer::RPC_concat), this);
    rb.MethodDesc("Concatenate two strings");
    rb.ParamDesc("string1", "a string");
    rb.ParamDesc("string2", "another string");
    rb.ReturnDesc("ret", "the concatenation of string1 and string2");
    //-------------------------------------------------------------------
    rb.DefineMethod("addFloat", "ff", "f", true,
                    FRT_METHOD(RPCServer::RPC_addFloat), this);
    rb.MethodDesc("Add two floats");
    rb.ParamDesc("float1", "a float");
    rb.ParamDesc("float2", "another float");
    rb.ReturnDesc("ret", "float1 + float2");
    //-------------------------------------------------------------------
    rb.DefineMethod("addDouble", "dd", "d", true,
                    FRT_METHOD(RPCServer::RPC_addDouble), this);
    rb.MethodDesc("Add two doubles");
    rb.ParamDesc("double1", "a double");
    rb.ParamDesc("double2", "another double");
    rb.ReturnDesc("ret", "double1 + double2");
    //-------------------------------------------------------------------
}

void
RPCServer::RPC_concat(FRT_RPCRequest *req)
{
    FRT_Values &params = *req->GetParams();
    FRT_Values &ret    = *req->GetReturn();

    uint32_t len = (params[0]._string._len +
                    params[1]._string._len);
    char *tmp = ret.AddString(len);
    strcpy(tmp, params[0]._string._str);
    strcat(tmp, params[1]._string._str);
}

void
RPCServer::RPC_addFloat(FRT_RPCRequest *req)
{
    FRT_Values &params = *req->GetParams();
    FRT_Values &ret    = *req->GetReturn();

    ret.AddFloat(params[0]._float + params[1]._float);
}

void
RPCServer::RPC_addDouble(FRT_RPCRequest *req)
{
    FRT_Values &params = *req->GetParams();
    FRT_Values &ret    = *req->GetReturn();

    ret.AddDouble(params[0]._double + params[1]._double);
}

int
RPCServer::Main(int argc, char **argv)
{
    FNET_SignalShutDown::hookSignals();
    if (argc < 2) {
        printf("usage  : rpc_server <listenspec>\n");
        return 1;
    }

    _supervisor = new FRT_Supervisor();
    InitRPC(_supervisor);
    _supervisor->Listen(argv[1]);
    FNET_SignalShutDown ssd(*_supervisor->GetTransport());
    _supervisor->Main();
    delete _supervisor;
    return 0;
}


class App : public FastOS_Application
{
private:
    RPCServer _server;

public:
    App() : _server() {}
    int Main() override;
};

int
App::Main()
{
    return _server.Main(_argc, _argv);
}


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