rename easyPVACPP to pvaClientCPP; this means repository and all names;
This commit is contained in:
250
src/pvaClientMonitor.cpp
Normal file
250
src/pvaClientMonitor.cpp
Normal file
@@ -0,0 +1,250 @@
|
||||
/* pvaClientMonitor.cpp */
|
||||
/**
|
||||
* Copyright - See the COPYRIGHT that is included with this distribution.
|
||||
* EPICS pvData is distributed subject to a Software License Agreement found
|
||||
* in file LICENSE that is included with this distribution.
|
||||
*/
|
||||
/**
|
||||
* @author mrk
|
||||
* @date 2015.03
|
||||
*/
|
||||
#define epicsExportSharedSymbols
|
||||
|
||||
#include <sstream>
|
||||
#include <pv/event.h>
|
||||
#include <pv/pvaClient.h>
|
||||
#include <pv/bitSetUtil.h>
|
||||
|
||||
using std::tr1::static_pointer_cast;
|
||||
using namespace epics::pvData;
|
||||
using namespace epics::pvAccess;
|
||||
using namespace std;
|
||||
|
||||
namespace epics { namespace pvaClient {
|
||||
|
||||
|
||||
class ChannelMonitorRequester : public MonitorRequester
|
||||
{
|
||||
PvaClientMonitor * pvaClientMonitor;
|
||||
public:
|
||||
ChannelMonitorRequester(PvaClientMonitor * pvaClientMonitor)
|
||||
: pvaClientMonitor(pvaClientMonitor) {}
|
||||
string getRequesterName()
|
||||
{return pvaClientMonitor->getRequesterName();}
|
||||
void message(string const & message,MessageType messageType)
|
||||
{pvaClientMonitor->message(message,messageType);}
|
||||
void monitorConnect(
|
||||
const Status& status,
|
||||
Monitor::shared_pointer const & monitor,
|
||||
StructureConstPtr const & structure)
|
||||
{pvaClientMonitor->monitorConnect(status,monitor,structure);}
|
||||
void monitorEvent(MonitorPtr const & monitor)
|
||||
{
|
||||
pvaClientMonitor->monitorEvent(monitor);
|
||||
}
|
||||
void unlisten(MonitorPtr const & monitor)
|
||||
{pvaClientMonitor->unlisten();}
|
||||
};
|
||||
|
||||
PvaClientMonitor::PvaClientMonitor(
|
||||
PvaClientPtr const &pvaClient,
|
||||
PvaClientChannelPtr const & pvaClientChannel,
|
||||
Channel::shared_pointer const & channel,
|
||||
PVStructurePtr const &pvRequest)
|
||||
: pvaClient(pvaClient),
|
||||
pvaClientChannel(pvaClientChannel),
|
||||
channel(channel),
|
||||
pvRequest(pvRequest),
|
||||
isDestroyed(false),
|
||||
connectState(connectIdle),
|
||||
userPoll(false),
|
||||
userWait(false)
|
||||
{
|
||||
}
|
||||
|
||||
PvaClientMonitor::~PvaClientMonitor()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
void PvaClientMonitor::checkMonitorState()
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
if(connectState==connectIdle) connect();
|
||||
if(connectState==connected) start();
|
||||
}
|
||||
|
||||
// from MonitorRequester
|
||||
string PvaClientMonitor::getRequesterName()
|
||||
{
|
||||
PvaClientPtr yyy = pvaClient.lock();
|
||||
if(!yyy) throw std::runtime_error("pvaClient was destroyed");
|
||||
return yyy->getRequesterName();
|
||||
}
|
||||
|
||||
void PvaClientMonitor::message(string const & message,MessageType messageType)
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
PvaClientPtr yyy = pvaClient.lock();
|
||||
if(!yyy) throw std::runtime_error("pvaClient was destroyed");
|
||||
yyy->message(message, messageType);
|
||||
}
|
||||
|
||||
void PvaClientMonitor::monitorConnect(
|
||||
const Status& status,
|
||||
Monitor::shared_pointer const & monitor,
|
||||
StructureConstPtr const & structure)
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
connectStatus = status;
|
||||
this->monitor = monitor;
|
||||
if(status.isOK()) {
|
||||
pvaClientData = PvaClientMonitorData::create(structure);
|
||||
pvaClientData->setMessagePrefix(channel->getChannelName());
|
||||
}
|
||||
waitForConnect.signal();
|
||||
|
||||
}
|
||||
|
||||
void PvaClientMonitor::monitorEvent(MonitorPtr const & monitor)
|
||||
{
|
||||
PvaClientMonitorRequesterPtr req = pvaClientMonitorRequester.lock();
|
||||
if(req) req->event(getPtrSelf());
|
||||
if(userWait) waitForEvent.signal();
|
||||
}
|
||||
|
||||
void PvaClientMonitor::unlisten()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
// from PvaClientMonitor
|
||||
void PvaClientMonitor::destroy()
|
||||
{
|
||||
{
|
||||
Lock xx(mutex);
|
||||
if(isDestroyed) return;
|
||||
isDestroyed = true;
|
||||
}
|
||||
if(monitor) monitor->destroy();
|
||||
monitor.reset();
|
||||
}
|
||||
|
||||
void PvaClientMonitor::connect()
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
issueConnect();
|
||||
Status status = waitConnect();
|
||||
if(status.isOK()) return;
|
||||
stringstream ss;
|
||||
ss << "channel " << channel->getChannelName() << " PvaClientMonitor::connect " << status.getMessage();
|
||||
throw std::runtime_error(ss.str());
|
||||
}
|
||||
|
||||
void PvaClientMonitor::issueConnect()
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
if(connectState!=connectIdle) {
|
||||
stringstream ss;
|
||||
ss << "channel " << channel->getChannelName() << " pvaClientMonitor already connected ";
|
||||
throw std::runtime_error(ss.str());
|
||||
}
|
||||
monitorRequester = ChannelMonitorRequester::shared_pointer(new ChannelMonitorRequester(this));
|
||||
connectState = connectActive;
|
||||
monitor = channel->createMonitor(monitorRequester,pvRequest);
|
||||
}
|
||||
|
||||
Status PvaClientMonitor::waitConnect()
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
if(connectState!=connectActive) {
|
||||
stringstream ss;
|
||||
ss << "channel " << channel->getChannelName() << " pvaClientMonitor illegal connect state ";
|
||||
throw std::runtime_error(ss.str());
|
||||
}
|
||||
waitForConnect.wait();
|
||||
if(connectStatus.isOK()){
|
||||
connectState = connected;
|
||||
return Status::Ok;
|
||||
}
|
||||
connectState = connectIdle;
|
||||
return Status(Status::STATUSTYPE_ERROR,connectStatus.getMessage());
|
||||
}
|
||||
|
||||
void PvaClientMonitor::setRequester(PvaClientMonitorRequesterPtr const & pvaClientMonitorrRequester)
|
||||
{
|
||||
this->pvaClientMonitorRequester = pvaClientMonitorrRequester;
|
||||
}
|
||||
|
||||
void PvaClientMonitor::start()
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
if(connectState==monitorStarted) return;
|
||||
if(connectState==connectIdle) connect();
|
||||
if(connectState!=connected) throw std::runtime_error("PvaClientMonitor::start illegal state");
|
||||
connectState = monitorStarted;
|
||||
monitor->start();
|
||||
}
|
||||
|
||||
|
||||
void PvaClientMonitor::stop()
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
if(connectState!=monitorStarted) return;
|
||||
connectState = connected;
|
||||
monitor->stop();
|
||||
}
|
||||
|
||||
bool PvaClientMonitor::poll()
|
||||
{
|
||||
checkMonitorState();
|
||||
if(connectState!=monitorStarted) throw std::runtime_error("PvaClientMonitor::poll illegal state");
|
||||
if(userPoll) throw std::runtime_error("PvaClientMonitor::poll did not release last");
|
||||
monitorElement = monitor->poll();
|
||||
if(!monitorElement) return false;
|
||||
userPoll = true;
|
||||
pvaClientData->setData(monitorElement);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PvaClientMonitor::waitEvent(double secondsToWait)
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
if(connectState!=monitorStarted) throw std::runtime_error("PvaClientMonitor::poll illegal state");
|
||||
if(poll()) return true;
|
||||
userWait = true;
|
||||
if(secondsToWait==0.0) {
|
||||
waitForEvent.wait();
|
||||
} else {
|
||||
waitForEvent.wait(secondsToWait);
|
||||
}
|
||||
userWait = false;
|
||||
return poll();
|
||||
}
|
||||
|
||||
void PvaClientMonitor::releaseEvent()
|
||||
{
|
||||
if(isDestroyed) throw std::runtime_error("pvaClientMonitor was destroyed");
|
||||
if(connectState!=monitorStarted) throw std::runtime_error("PvaClientMonitor::poll illegal state");
|
||||
if(!userPoll) throw std::runtime_error("PvaClientMonitor::releaseEvent did not call poll");
|
||||
userPoll = false;
|
||||
monitor->release(monitorElement);
|
||||
}
|
||||
|
||||
PvaClientMonitorDataPtr PvaClientMonitor::getData()
|
||||
{
|
||||
checkMonitorState();
|
||||
return pvaClientData;
|
||||
}
|
||||
|
||||
PvaClientMonitorPtr PvaClientMonitor::create(
|
||||
PvaClientPtr const &pvaClient,
|
||||
PvaClientChannelPtr const & pvaClientChannel,
|
||||
Channel::shared_pointer const & channel,
|
||||
PVStructurePtr const &pvRequest)
|
||||
{
|
||||
PvaClientMonitorPtr epv(new PvaClientMonitor(pvaClient,pvaClientChannel,channel,pvRequest));
|
||||
return epv;
|
||||
}
|
||||
|
||||
}}
|
||||
Reference in New Issue
Block a user