summaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/frt/rpc/rpc_callback_server.cpp
blob: 419b1266d23c0226ded7f8c718dd1e77da961e7e (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/log/log.h>
LOG_SETUP("rpc_callback_server");
#include <vespa/fnet/frt/frt.h>


struct RPC : public FRT_Invokable
{
    void CallBack(FRT_RPCRequest *req);
    void Init(FRT_Supervisor *s);
};

void
RPC::CallBack(FRT_RPCRequest *req)
{
    FNET_Connection *conn = req->GetConnection();
    FRT_RPCRequest *cb = new FRT_RPCRequest();
    cb->SetMethodName(req->GetParams()->GetValue(0)._string._str);
    FRT_Supervisor::InvokeSync(conn->Owner(), conn, cb, 5.0);
    if(cb->IsError()) {
        printf("[error(%d): %s]\n",
               cb->GetErrorCode(),
               cb->GetErrorMessage());
    }
    cb->SubRef();
}

void
RPC::Init(FRT_Supervisor *s)
{
    FRT_ReflectionBuilder rb(s);
    //-------------------------------------------------------------------
    rb.DefineMethod("callBack", "s", "", false,
                    FRT_METHOD(RPC::CallBack), this);
    //-------------------------------------------------------------------
}


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

int
MyApp::Main()
{
    FNET_SignalShutDown::hookSignals();
    if (_argc < 2) {
        printf("usage  : rpc_server <listenspec>\n");
        return 1;
    }
    RPC rpc;
    FRT_Supervisor orb;
    rpc.Init(&orb);
    orb.Listen(_argv[1]);
    FNET_SignalShutDown ssd(*orb.GetTransport());
    orb.Main();
    return 0;
}


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