Files
pvxs/documentation/sharedpv.rst
T
Michael Davidsaver 7debb1fae3 update Config handling
rename from_env() -> fromEnv()
add fromDefs()
2020-07-26 20:46:12 -07:00

2.0 KiB

SharedPV and StaticSource

A SharedPV is a single data value which may be accessed by multiple clients through a Server. It is "shared" in the sense that all clients are manipulating a single variable.

#include <pvxs/sharedpv.h>
namespace pvxs { namespace server { ... } }

Each SharedPV instance has (after open() ) an associated data structure which is later manipulated with post().

A simple usage is:

using namespace pvxs;

auto initial = nt::NTScalar{TypeCode::Float64}.create();
initial["value"] = 42.0;

auto src(server::StaticSource::build());

auto pv(server::SharedPV::buildMailbox());
pv.open(initial);

src.add(argv[1], pv);

auto serv = server::Server::Config::fromEnv()
        .build()
        .addSource("box", src.source());

serv.run();

In this context "mailbox" refers to the default onPut() handler, which simply post()s whatever the client sends.

An example of a SharedPV with a custom Put handler

auto pv(server::SharedPV::buildMailbox());

pv.opPut([](server::SharedPV& pv,
            std::unique_ptr<server::ExecOp>&& op,
            Value&& top)
{
    // We decide that .value will be present with .open()
    auto val = top["value"];
    // is client trying to change .value ?
    if(val.isMarked(true, true)) {
        auto val = top["value"].as<double>();

        // clip to range [0, 10]
        top["value"] = std::max(0.0, std::min(val, 10.0));
    }

    // update and send to subscribers
    pv.post(std::move(top));
    // notify client of success  (or call op->error() if not)
    op->reply(); 
});

auto initial = nt::NTScalar{TypeCode::Float64}.create();
initial["value"] = 42.0;

auto src(server::StaticSource::build());

pv.open(initial);

pvxs::server::SharedPV

pvxs::server::StaticSource