File: testRPC.cpp

package info (click to toggle)
epics-base 7.0.8.1%2Bdfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,532 kB
  • sloc: cpp: 130,870; ansic: 115,274; perl: 10,647; makefile: 3,477; yacc: 1,307; python: 594; lex: 236; sh: 108; csh: 36
file content (157 lines) | stat: -rw-r--r-- 5,327 bytes parent folder | download | duplicates (3)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

#include <pv/epicsException.h>
#include <pv/valueBuilder.h>

#include <pv/clientFactory.h>
#include <pv/rpcClient.h>
#include <pv/rpcServer.h>
#include <pv/rpcService.h>

#include <epicsUnitTest.h>
#include <testMain.h>

namespace pvd = epics::pvData;
namespace pva = epics::pvAccess;

namespace {

pvd::StructureConstPtr reply_type(pvd::getFieldCreate()->createFieldBuilder()
                                  ->add("value", pvd::pvDouble)
                                  ->createStructure());

struct SumService : public pva::RPCService
{
    virtual epics::pvData::PVStructure::shared_pointer request(
        epics::pvData::PVStructure::shared_pointer const & args
    ) OVERRIDE FINAL
    {
        testDiag("request()");
        pvd::PVScalarPtr lhs(args->getSubField<pvd::PVScalar>("query.lhs")),
                         rhs(args->getSubField<pvd::PVScalar>("query.rhs"));
        if(!lhs || !rhs)
            throw pva::RPCRequestException("Missing query.lhs and/or query.rhs");

        double a = lhs->getAs<double>(),
               b = rhs->getAs<double>();

        testDiag("Add %f + %f", a, b);
        pvd::PVStructure::shared_pointer reply(pvd::getPVDataCreate()->createPVStructure(reply_type));
        reply->getSubFieldT<pvd::PVDouble>("value")->put(a+b);
        return reply;
    }
};

void testSum(const pva::ChannelProvider::shared_pointer& cli_prov)
{
    pva::RPCClient client("sum", pvd::createRequest("field()"), cli_prov);

    pvd::ValueBuilder args("epics:nt/NTURI:1.0");
    args.add<pvd::pvString>("scheme", "pva")
        .add<pvd::pvString>("path", "sum");

    pvd::PVStructurePtr reply;

    testDiag("Request");
    reply = client.request(args.addNested("query")
                               .add<pvd::pvDouble>("lhs", 5.0)
                               .add<pvd::pvDouble>("rhs", 3.0)
                           .endNested()
                           .buildPVStructure());

    pvd::int32 value = reply->getSubFieldT<pvd::PVScalar>("value")->getAs<pvd::int32>();
    testOk(value==8, "Reply value = %d", (unsigned)value);

    testDiag("Wait for connect (already connected)");
    testOk1(client.waitConnect());

}



struct FailService : public pva::RPCService
{
    virtual epics::pvData::PVStructure::shared_pointer request(
        epics::pvData::PVStructure::shared_pointer const & args
    ) OVERRIDE FINAL
    {
        testDiag("failing()");
        throw std::runtime_error("oops");
    }
};

void testRPCFail(const pva::ChannelProvider::shared_pointer& cli_prov)
{

    testDiag("Fail");

    pva::RPCClient client("fail", pvd::createRequest("field()"), cli_prov);

    pvd::ValueBuilder args("epics:nt/NTURI:1.0");
    args.add<pvd::pvString>("scheme", "pva")
        .add<pvd::pvString>("path", "fail");


    testDiag("Request");
    try{
        (void)client.request(args.addNested("query")
                             .add<pvd::pvDouble>("lhs", 5.0)
                             .add<pvd::pvDouble>("rhs", 3.0)
                             .endNested()
                             .buildPVStructure());
        testFail("Missing expected exception");
    }catch(pva::RPCRequestException& e){
        testPass("caught expected rpc exception: %s", e.what());
    }catch(std::exception& e){
        testFail("caught un-expected exception: %s", e.what());
    }
}

} // namespace

MAIN(testRPC)
{
    testPlan(3);
    try {
        pva::Configuration::shared_pointer conf(pva::ConfigurationBuilder()
                                                //.push_env()
                                                //.add("EPICS_PVA_DEBUG", "3")
                                                .add("EPICS_PVAS_INTF_ADDR_LIST", "127.0.0.1")
                                                .add("EPICS_PVA_ADDR_LIST", "127.0.0.1")
                                                .add("EPICS_PVA_AUTO_ADDR_LIST","0")
                                                .add("EPICS_PVA_SERVER_PORT", "0")
                                                .add("EPICS_PVA_BROADCAST_PORT", "0")
                                                .push_map()
                                                .build());

        testDiag("Server Setup");
        pva::RPCServer serv(conf);
        testDiag("TestServer on ports TCP=%u UDP=%u",
                 serv.getServer()->getServerPort(),
                 serv.getServer()->getBroadcastPort());

        {
            std::tr1::shared_ptr<pva::RPCService> service(new SumService);
            serv.registerService("sum", service);
        }
        {
            std::tr1::shared_ptr<pva::RPCService> service(new FailService);
            serv.registerService("fail", service);
        }

        testDiag("Client Setup");
        pva::ClientFactory::start();
        pva::ChannelProvider::shared_pointer cli_prov(pva::ChannelProviderRegistry::clients()->createProvider("pva",
                                                                                                              serv.getServer()->getCurrentConfig()));
        if(!cli_prov)
            testAbort("No pva provider");
        testDiag("Client Ready");

        testSum(cli_prov);
        testRPCFail(cli_prov);

    }catch(std::exception& e){
        PRINT_EXCEPTION(e);
        testAbort("Unexpected exception: %s", e.what());
    }
    return testDone();
}