This example demonstrates a client which sets up a persistent Monitor operation.
#if !defined(_WIN32)
#include <signal.h>
#define USE_SIGNAL
#endif
#include <epicsEvent.h>
#include <epicsMutex.h>
#include <epicsGuard.h>
#include <epicsGetopt.h>
#include <pv/configuration.h>
#include <pv/caProvider.h>
#include <pv/reftrack.h>
#include <pv/thread.h>
#include <pva/client.h>
namespace pvd = epics::pvData;
namespace pva = epics::pvAccess;
namespace {
typedef epicsGuard<epicsMutex> Guard;
typedef epicsGuardRelease<epicsMutex> UnGuard;
struct Worker {
virtual ~Worker() {}
};
struct WorkQueue : public epicsThreadRunable {
epicsMutex mutex;
typedef std::tr1::shared_ptr<Worker> value_type;
typedef std::tr1::weak_ptr<Worker> weak_type;
queue_t queue;
epicsEvent event;
bool running;
pvd::Thread worker;
WorkQueue()
:running(true)
,worker(pvd::Thread::Config()
.name("Monitor handler")
.autostart(true)
.run(this))
{}
~WorkQueue() {close();}
void close()
{
{
Guard G(mutex);
running = false;
}
event.signal();
worker.exitWait();
}
{
bool wake;
{
Guard G(mutex);
if(!running) return;
wake = queue.empty();
}
if(wake)
event.signal();
}
virtual void run() OVERRIDE FINAL
{
Guard G(mutex);
while(running) {
if(queue.empty()) {
UnGuard U(G);
event.wait();
} else {
queue_t::value_type ent(queue.front());
value_type cb(ent.first.lock());
queue.pop_front();
if(!cb) continue;
try {
UnGuard U(G);
cb->process(ent.second);
}
}
}
}
};
WorkQueue monwork;
epicsMutex mutex;
epicsEvent done;
volatile size_t waitingFor;
#ifdef USE_SIGNAL
void sigdone(int num)
{
(void)num;
waitingFor = 0;
done.signal();
}
#endif
public Worker,
public std::tr1::enable_shared_from_this<MonTracker>
{
POINTER_DEFINITIONS(MonTracker);
virtual ~MonTracker() {mon.
cancel();}
{
monwork.push(shared_from_this(), evt);
}
{
switch(evt.event) {
break;
break;
break;
{
unsigned n;
for(n=0; n<2 && mon.
poll(); n++) {
pvd::PVField::const_shared_pointer fld(mon.
root->getSubField(
"value"));
if(!fld)
<<" Changed:"<<mon.changed
<<" overrun:"<<mon.overrun<<"\n";
}
if(n==2) {
monwork.push(shared_from_this(), evt);
} else if(n==0) {
std::cerr<<
"Spurious Data event "<<name<<
"\n";
}
}
break;
}
}
};
}
int main(int argc, char *argv[]) {
try {
epics::RefMonitor refmon;
double waitTime = -1.0;
requestStr("field()");
pvs_t pvs;
int opt;
while((opt = getopt(argc, argv, "hRp:w:r:")) != -1) {
switch(opt) {
case 'R':
refmon.start(5.0);
break;
case 'p':
providerName = optarg;
break;
case 'w':
waitTime = pvd::castUnsafe<double, std::string>(optarg);
break;
case 'r':
requestStr = optarg;
break;
case 'h':
std::cout<<
"Usage: "<<argv[0]<<
" [-p <provider>] [-w <timeout>] [-r <request>] [-R] <pvname> ...\n";
return 0;
default:
std::cerr<<
"Unknown argument: "<<(char)opt<<
"\n";
return -1;
}
}
for(int i=optind; i<argc; i++)
pvs.push_back(argv[i]);
#ifdef USE_SIGNAL
signal(SIGINT, sigdone);
signal(SIGTERM, sigdone);
signal(SIGQUIT, sigdone);
#endif
pvd::PVStructure::shared_pointer pvReq(pvd::createRequest(requestStr));
pva::Configuration::shared_pointer conf(pva::ConfigurationBuilder()
.push_env()
.build());
std::cout<<
"Use provider: "<<providerName<<
"\n";
{
Guard G(mutex);
waitingFor = pvs.size();
}
for(pvs_t::const_iterator it=pvs.begin(); it!=pvs.end(); ++it) {
MonTracker::shared_pointer mon(new MonTracker(pv));
mon->mon = chan.monitor(mon.get(), pvReq);
}
int ret = 0;
{
Guard G(mutex);
while(waitingFor) {
UnGuard U(G);
if(waitTime<0.0) {
done.wait();
} else if(!done.wait(waitTime)) {
ret = 1;
break;
}
}
}
if(refmon.running()) {
refmon.stop();
refmon.current();
}
monwork.close();
return ret;
return 2;
}
}