aboutsummaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/frt/rpc/rpc_client.cpp
blob: 874a5f18b4170baf2c7a1d86c0931e2f036e9731 (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
// Copyright Vespa.ai. 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_client");

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

int
RPCClient::main(int argc, char **argv)
{
    if (argc < 2) {
        printf("usage  : rpc_client <connectspec>\n");
        return 1;
    }
    fnet::frt::StandaloneFRT server;
    FRT_Supervisor & supervisor = server.supervisor();

    FRT_Target *target = supervisor.GetTarget(argv[1]);

    const char *str1 = "abc";
    const char *str2 = "def";
    float float1     =  20.5;
    float float2     =  60.5;
    double double1   =  25.5;
    double double2   =   5.5;

    fprintf(stdout, "\nTesting concat method\n");
    FRT_RPCRequest *req = supervisor.AllocRPCRequest();
    req->SetMethodName("concat");
    req->GetParams()->AddString(str1);
    req->GetParams()->AddString(str2);
    target->InvokeSync(req, 5.0);
    if (req->GetErrorCode() == FRTE_NO_ERROR) {
        fprintf(stdout, "%s + %s = %s\n", str1, str2,
                req->GetReturn()->GetValue(0)._string._str);
    } else {
        fprintf(stdout, "error(%d): %s\n",
                req->GetErrorCode(),
                req->GetErrorMessage());
    }

    fprintf(stdout, "\nTesting addFloat method\n");
    req->internal_subref();
    req = supervisor.AllocRPCRequest();
    req->SetMethodName("addFloat");
    req->GetParams()->AddFloat(float1);
    req->GetParams()->AddFloat(float2);
    target->InvokeSync(req, 5.0);
    if (req->GetErrorCode() == FRTE_NO_ERROR) {
        fprintf(stdout, "%f + %f = %f\n", float1, float2,
                req->GetReturn()->GetValue(0)._float);
    } else {
        fprintf(stdout, "error(%d): %s\n",
                req->GetErrorCode(),
                req->GetErrorMessage());
    }

    fprintf(stdout, "\nTesting addDouble method\n");
    req->internal_subref();
    req = supervisor.AllocRPCRequest();
    req->SetMethodName("addDouble");
    req->GetParams()->AddDouble(double1);
    req->GetParams()->AddDouble(double2);
    target->InvokeSync(req, 5.0);
    if (req->GetErrorCode() == FRTE_NO_ERROR) {
        fprintf(stdout, "%f + %f = %f\n", double1, double2,
                req->GetReturn()->GetValue(0)._double);
    } else {
        fprintf(stdout, "error(%d): %s\n",
                req->GetErrorCode(),
                req->GetErrorMessage());
    }

    req->internal_subref();
    target->internal_subref();
    return 0;
}


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