pvAccessCPP 7.1.8
Loading...
Searching...
No Matches
Simple Client Monitor Example

The shortest possible PVA monitor() example.

/*
* Copyright information and license terms for this software can be
* found in the file LICENSE that is included with the distribution
*/
// The simplest possible PVA monitor
#include <iostream>
#if !defined(_WIN32)
#include <signal.h>
#define USE_SIGNAL
#endif
#include <epicsEvent.h>
#include <pva/client.h>
static volatile bool done;
#ifdef USE_SIGNAL
static pvac::MonitorSync * volatile subscription;
static
void handler(int num)
{
(void)num;
done = true;
pvac::MonitorSync *mon = subscription;
if(mon)
mon->wake();
}
#endif
int main(int argc, char *argv[])
{
try {
if(argc<=1) {
std::cerr<<"Usage: "<<argv[0]<<" <pvname>\n";
return 1;
}
pvac::ClientProvider provider("pva");
pvac::ClientChannel channel(provider.connect(argv[1]));
pvac::MonitorSync mon(channel.monitor());
#ifdef USE_SIGNAL
subscription = &mon;
signal(SIGINT, handler);
signal(SIGTERM, handler);
signal(SIGQUIT, handler);
#endif
int ret = 0;
while(!done) {
if(!mon.wait()) // updates mon.event
continue;
switch(mon.event.event) {
// Subscription network/internal error
std::cerr<<mon.name()<<" : Error : "<<mon.event.message<<"\n";
ret = 1;
done = true;
break;
// explicit call of 'mon.cancel' or subscription dropped
std::cout<<mon.name()<<" <Cancel>\n";
done = true;
break;
// Underlying channel becomes disconnected
std::cout<<mon.name()<<" <Disconnect>\n";
break;
// Data queue becomes not-empty
// We drain event FIFO completely
while(mon.poll()) {
std::cout<<mon.name()<<" : "<<mon.root;
}
// check to see if more events might be sent
if(mon.complete()) {
done = true;
std::cout<<mon.name()<<" : <Complete>\n";
}
break;
}
}
#ifdef USE_SIGNAL
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
subscription = 0;
#endif
return ret;
}catch(std::exception& e){
std::cerr<<"Error: "<<e.what()<<"\n";
return 1;
}
}
ostream cerr
ostream cout
virtual const char * what() const noexcept
Represents a single channel.
Definition client.h:267
Central client context.
Definition client.h:518
@ Fail
subscription ends in an error
Definition client.h:187
@ Cancel
subscription ends in cancellation
Definition client.h:188
@ Data
Data queue not empty. Call Monitor::poll()
Definition client.h:190
@ Disconnect
subscription interrupted due to loss of communication
Definition client.h:189
std::string message
set for event=Fail
Definition client.h:192
bool complete() const
true if all events received.
bool poll()
updates root, changed, overrun
std::string name() const
Channel name.
epics::pvData::PVStructure::const_shared_pointer root
Monitor update data.
Definition client.h:160
Subscription usable w/o callbacks.
Definition client.h:207
bool wait()
wait for new event
void wake()
Abort one call to wait(), either concurrent or future.
MonitorEvent event
most recent event updated only during wait() or poll()
Definition client.h:231