aboutsummaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/frt/rpc/rpc_client.cpp
blob: f5e5a9fe1e2a66f45e0836339041ab390e25bcb4 (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
// 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_client");
#include <vespa/fnet/frt/frt.h>


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

int
RPCClient::Main()
{
    if (_argc < 2) {
        printf("usage  : rpc_client <connectspec>\n");
        return 1;
    }
    FRT_Supervisor supervisor;

    supervisor.Start();
    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->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->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->SubRef();
    target->SubRef();
    supervisor.ShutDown(true);
    return 0;
}


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