File: rpcServiceAsyncExample.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 (87 lines) | stat: -rw-r--r-- 2,622 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
/**
 * Copyright - See the COPYRIGHT that is included with this distribution.
 * pvAccessCPP is distributed subject to a Software License Agreement found
 * in file LICENSE that is included with this distribution.
 */

#include <pv/pvData.h>
#include <pv/rpcServer.h>

using namespace epics::pvData;
using namespace epics::pvAccess;

static Structure::const_shared_pointer resultStructure =
    getFieldCreate()->createFieldBuilder()->
    add("c", pvDouble)->
    createStructure();

// s1 starts with s2 check
static bool starts_with(const std::string& s1, const std::string& s2) {
    return s2.size() <= s1.size() && s1.compare(0, s2.size(), s2) == 0;
}

class SumServiceImpl :
    public RPCServiceAsync
{
    void request(PVStructure::shared_pointer const & pvArguments,
                 RPCResponseCallback::shared_pointer const & callback)
    {
        // NTURI support
        PVStructure::shared_pointer args(
            (starts_with(pvArguments->getStructure()->getID(), "epics:nt/NTURI:1.")) ?
            pvArguments->getSubField<PVStructure>("query") :
            pvArguments
        );

        // get fields and check their existence
        PVScalar::shared_pointer af = args->getSubField<PVScalar>("a");
        PVScalar::shared_pointer bf = args->getSubField<PVScalar>("b");
        if (!af || !bf)
        {
            callback->requestDone(
                Status(
                    Status::STATUSTYPE_ERROR,
                    "scalar 'a' and 'b' fields are required"
                ),
                PVStructure::shared_pointer());
            return;
        }

        // get the numbers (and convert if neccessary)
        double a, b;
        try
        {
            a = af->getAs<double>();
            b = bf->getAs<double>();
        }
        catch (std::exception &e)
        {
            callback->requestDone(
                Status(
                    Status::STATUSTYPE_ERROR,
                    std::string("failed to convert arguments to double: ") + e.what()
                ),
                PVStructure::shared_pointer());
            return;
        }

        // create return structure and set data
        PVStructure::shared_pointer result = getPVDataCreate()->createPVStructure(resultStructure);
        result->getSubField<PVDouble>("c")->put(a+b);

        callback->requestDone(Status::Ok, result);
    }
};

int main()
{
    RPCServer server;

    server.registerService("sum", RPCServiceAsync::shared_pointer(new SumServiceImpl()));
    // you can register as many services as you want here ...

    server.printInfo();
    server.run();

    return 0;
}