aboutsummaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/frt/rpc/rpc_callback_client.cpp
blob: c52d48f24eb36a88791c153c081a6f2f7feaaf51 (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
// 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/target.h>
#include <vespa/fnet/frt/rpcrequest.h>
#include <vespa/vespalib/util/signalhandler.h>

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

struct RPC : public FRT_Invokable
{
    uint32_t invokeCnt;
    RPC() : invokeCnt(0) {}
    void Prod(FRT_RPCRequest *req);
    void Init(FRT_Supervisor *s);
};

void
RPC::Prod(FRT_RPCRequest *req)
{
    (void) req;
    ++invokeCnt;
}

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


class MyApp
{
public:
    int main(int argc, char **argv);
};

int
MyApp::main(int argc, char **argv)
{
    if (argc < 2) {
        printf("usage  : rpc_server <connectspec>\n");
        return 1;
    }
    bool ok = true;
    RPC rpc;
    fnet::frt::StandaloneFRT server;
    FRT_Supervisor & orb = server.supervisor();
    rpc.Init(&orb);

    FRT_Target *target = orb.Get2WayTarget(argv[1]);
    FRT_RPCRequest *req = orb.AllocRPCRequest();

    printf("invokeCnt: %d\n", rpc.invokeCnt);

    req->SetMethodName("callBack");
    req->GetParams()->AddString("prod");
    target->InvokeSync(req, 10.0);

    if(req->IsError()) {
        printf("[error(%d): %s]\n",
               req->GetErrorCode(),
               req->GetErrorMessage());
        ok = false;
    }

    printf("invokeCnt: %d\n", rpc.invokeCnt);

    req = orb.AllocRPCRequest(req);
    req->SetMethodName("callBack");
    req->GetParams()->AddString("prod");
    target->InvokeSync(req, 10.0);

    if(req->IsError()) {
        printf("[error(%d): %s]\n",
               req->GetErrorCode(),
               req->GetErrorMessage());
        ok = false;
    }

    printf("invokeCnt: %d\n", rpc.invokeCnt);

    req = orb.AllocRPCRequest(req);
    req->SetMethodName("callBack");
    req->GetParams()->AddString("prod");
    target->InvokeSync(req, 10.0);

    if(req->IsError()) {
        printf("[error(%d): %s]\n",
               req->GetErrorCode(),
               req->GetErrorMessage());
        ok = false;
    }

    printf("invokeCnt: %d\n", rpc.invokeCnt);
    if (rpc.invokeCnt != 3) {
        ok = false;
    }

    req->internal_subref();
    target->internal_subref();
    return ok ? 0 : 1;
}


int main(int argc, char **argv) {
    vespalib::SignalHandler::PIPE.ignore();
    MyApp myapp;
    return myapp.main(argc, argv);
}