Add example of record which supports RPC and a client

Record has x and y-coordinate fields and a timestamp and also provides
a service which sets (x,y) to a sequence of values.

An RPC client application (move) sends positions as an array.
This commit is contained in:
Dave Hickin
2015-12-08 12:20:43 +00:00
parent a99b08fd02
commit bc3335d4f9
33 changed files with 1015 additions and 0 deletions

43
exampleRPC/src/Makefile Normal file
View File

@@ -0,0 +1,43 @@
TOP=..
include $(TOP)/configure/CONFIG
#----------------------------------------
# ADD MACRO DEFINITIONS AFTER THIS LINE
#=============================
#==================================================
# Build an IOC support library
#
DBD += exampleRPC.dbd
INC += exampleRPC.h
LIBRARY_IOC += exampleRPC
exampleRPC_SRCS += exampleRPC.cpp
exampleRPC_SRCS += exampleRPCRegister.cpp
exampleRPC_LIBS += pvDatabase
exampleRPC_LIBS += pvAccess
exampleRPC_LIBS += pvData
exampleRPC_LIBS += Com
exampleRPC_LIBS += $(EPICS_BASE_IOC_LIBS)
PROD_HOST += exampleRPCMain
exampleRPCMain_SRCS += exampleRPCMain.cpp
exampleRPCMain_LIBS += exampleRPC
exampleRPCMain_LIBS += pvDatabase
exampleRPCMain_LIBS += pvAccess
exampleRPCMain_LIBS += pvData
exampleRPCMain_LIBS += Com
PROD_HOST += move
move_SRCS += positionClient.cpp
move_LIBS += pvAccess pvData Com
#===========================
include $(TOP)/configure/RULES
#----------------------------------------
# ADD RULES AFTER THIS LINE

View File

@@ -0,0 +1,213 @@
/* exampleRPC.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 dgh
* @date 2015.12.08
*/
#include <pv/standardField.h>
#define epicsExportSharedSymbols
#include <pv/exampleRPC.h>
#include <pv/standardField.h>
#include <pv/standardPVField.h>
#include <epicsThread.h>
using namespace epics::pvData;
using namespace epics::pvDatabase;
using std::tr1::static_pointer_cast;
using std::string;
namespace epics { namespace exampleRPC {
PVStructurePtr ExampleRPCService::request(
PVStructure::shared_pointer const & args
) throw (epics::pvAccess::RPCRequestException)
{
bool haveControl = pvRecord->takeControl();
if (!haveControl)
throw pvAccess::RPCRequestException(Status::STATUSTYPE_ERROR,
"I'm busy");
FieldCreatePtr fieldCreate = getFieldCreate();
PVDataCreatePtr pvDataCreate = getPVDataCreate();
PVStructureArrayPtr valueField = args->getSubField<PVStructureArray>("value");
if (valueField.get() == 0)
throw pvAccess::RPCRequestException(Status::STATUSTYPE_ERROR,
"No structure array value field");
StructureConstPtr valueFieldStructure = valueField->
getStructureArray()->getStructure();
ScalarConstPtr xField = valueFieldStructure->getField<Scalar>("x");
if (xField.get() == 0 || xField->getScalarType() != pvDouble)
throw pvAccess::RPCRequestException(Status::STATUSTYPE_ERROR,
"value field's structure has no double field x");
ScalarConstPtr yField = valueFieldStructure->getField<Scalar>("y");
if (xField.get() == 0 || xField->getScalarType() != pvDouble)
throw pvAccess::RPCRequestException(Status::STATUSTYPE_ERROR,
"value field's structure has no double field y");
PVStructureArray::const_svector vals = valueField->view();
for (PVStructureArray::const_svector::const_iterator it = vals.begin();
it != vals.end(); ++it)
{
double x = (*it)->getSubFieldT<PVDouble>("x")->get();
double y = (*it)->getSubFieldT<PVDouble>("y")->get();
pvRecord->put(x,y);
epicsThreadSleep(1.0);
}
StructureConstPtr topStructure = fieldCreate->createFieldBuilder()->
createStructure();
PVStructurePtr returned = pvDataCreate->createPVStructure(topStructure);
pvRecord->releaseControl();
return returned;
}
void ExampleRPCServiceAsync::request(
epics::pvData::PVStructurePtr const & args,
epics::pvAccess::RPCResponseCallback::shared_pointer const & callback)
{
bool haveControl = pvRecord->takeControl();
if (!haveControl)
throw pvAccess::RPCRequestException(Status::STATUSTYPE_ERROR,
"I'm busy");
FieldCreatePtr fieldCreate = getFieldCreate();
PVDataCreatePtr pvDataCreate = getPVDataCreate();
PVStructureArrayPtr valueField = args->getSubField<PVStructureArray>("value");
if (valueField.get() == 0)
throw pvAccess::RPCRequestException(Status::STATUSTYPE_ERROR,
"No structure array value field");
StructureConstPtr valueFieldStructure = valueField->
getStructureArray()->getStructure();
ScalarConstPtr xField = valueFieldStructure->getField<Scalar>("x");
if (xField.get() == 0 || xField->getScalarType() != pvDouble)
throw pvAccess::RPCRequestException(Status::STATUSTYPE_ERROR,
"value field's structure has no double field x");
ScalarConstPtr yField = valueFieldStructure->getField<Scalar>("y");
if (yField.get() == 0 || yField->getScalarType() != pvDouble)
throw pvAccess::RPCRequestException(Status::STATUSTYPE_ERROR,
"value field's structure has no double field y");
PVStructureArray::const_svector vals = valueField->view();
for (PVStructureArray::const_svector::const_iterator it = vals.begin();
it != vals.end(); ++it)
{
double x = (*it)->getSubFieldT<PVDouble>("x")->get();
double y = (*it)->getSubFieldT<PVDouble>("y")->get();
pvRecord->put(x,y);
epicsThreadSleep(1.0);
}
StructureConstPtr topStructure = fieldCreate->createFieldBuilder()->
createStructure();
PVStructurePtr returned = pvDataCreate->createPVStructure(topStructure);
pvRecord->releaseControl();
callback->requestDone(Status::Ok, returned);
}
ExampleRPCPtr ExampleRPC::create(
string const & recordName)
{
StandardFieldPtr standardField = getStandardField();
FieldCreatePtr fieldCreate = getFieldCreate();
PVDataCreatePtr pvDataCreate = getPVDataCreate();
StructureConstPtr topStructure = fieldCreate->createFieldBuilder()->
add("x",pvDouble)->
add("y",pvDouble)->
add("timeStamp",standardField->timeStamp()) ->
createStructure();
PVStructurePtr pvStructure = pvDataCreate->createPVStructure(topStructure);
ExampleRPCPtr pvRecord(
new ExampleRPC(recordName,pvStructure));
if(!pvRecord->init()) pvRecord.reset();
return pvRecord;
}
ExampleRPC::ExampleRPC(
string const & recordName,
PVStructurePtr const & pvStructure)
: PVRecord(recordName,pvStructure)
{
}
ExampleRPC::~ExampleRPC()
{
}
void ExampleRPC::destroy()
{
PVRecord::destroy();
}
bool ExampleRPC::init()
{
initPVRecord();
service = ExampleRPCService::create(
std::tr1::dynamic_pointer_cast<ExampleRPC>(
shared_from_this()));
PVFieldPtr pvField;
pvTimeStamp.attach(getPVStructure()->getSubField("timeStamp"));
return true;
}
epics::pvAccess::Service::shared_pointer ExampleRPC::getService(
PVStructurePtr const & /*pvRequest*/)
{
return service;
}
bool ExampleRPC::takeControl()
{
return taskMutex.tryLock();
}
void ExampleRPC::releaseControl()
{
taskMutex.unlock();
}
void ExampleRPC::put(double x, double y)
{
lock();
beginGroupPut();
getPVStructure()->getSubField<PVDouble>("x")->put(x);
getPVStructure()->getSubField<PVDouble>("y")->put(y);
endGroupPut();
process();
unlock();
}
void ExampleRPC::process()
{
timeStamp.getCurrent();
pvTimeStamp.set(timeStamp);
}
}}

126
exampleRPC/src/exampleRPC.h Normal file
View File

@@ -0,0 +1,126 @@
/* exampleRPC.h */
/**
* 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 dgh
* @date 2015.12.08
*/
#ifndef EXAMPLERPC_H
#define EXAMPLERPC_H
#ifdef epicsExportSharedSymbols
# define exampleRPCEpicsExportSharedSymbols
# undef epicsExportSharedSymbols
#endif
#include <pv/pvDatabase.h>
#include <pv/timeStamp.h>
#include <pv/pvTimeStamp.h>
#ifdef exampleRPCEpicsExportSharedSymbols
# define epicsExportSharedSymbols
# undef exampleRPCEpicsExportSharedSymbols
#endif
#include <shareLib.h>
namespace epics { namespace exampleRPC {
class ExampleRPCService;
typedef std::tr1::shared_ptr<ExampleRPCService> ExampleRPCServicePtr;
class ExampleRPCServiceAsync;
typedef std::tr1::shared_ptr<ExampleRPCServiceAsync> ExampleRPCServiceAsyncPtr;
class ExampleRPC;
typedef std::tr1::shared_ptr<ExampleRPC> ExampleRPCPtr;
class epicsShareClass ExampleRPCService :
public virtual epics::pvAccess::RPCService
{
public:
POINTER_DEFINITIONS(ExampleRPCService);
static ExampleRPCService::shared_pointer create(ExampleRPCPtr const & pvRecord)
{
return ExampleRPCServicePtr(new ExampleRPCService(pvRecord));
}
~ExampleRPCService() {};
epics::pvData::PVStructurePtr request(
epics::pvData::PVStructure::shared_pointer const & args
) throw (epics::pvAccess::RPCRequestException);
private:
ExampleRPCService(ExampleRPCPtr const & pvRecord)
: pvRecord(pvRecord)
{
}
ExampleRPCPtr pvRecord;
};
class ExampleRPCServiceAsync :
public epics::pvAccess::RPCServiceAsync
{
public:
POINTER_DEFINITIONS(ExampleRPCServiceAsync);
static ExampleRPCServiceAsync::shared_pointer create(ExampleRPCPtr const & pvRecord)
{
return ExampleRPCServiceAsyncPtr(new ExampleRPCServiceAsync(pvRecord));
}
void request(epics::pvData::PVStructurePtr const & args,
epics::pvAccess::RPCResponseCallback::shared_pointer const & callback);
private:
ExampleRPCServiceAsync(ExampleRPCPtr const & pvRecord)
: pvRecord(pvRecord)
{
}
ExampleRPCPtr pvRecord;
};
class ExampleRPC;
typedef std::tr1::shared_ptr<ExampleRPC> ExampleRPCPtr;
class epicsShareClass ExampleRPC :
public epics::pvDatabase::PVRecord
{
public:
POINTER_DEFINITIONS(ExampleRPC);
static ExampleRPCPtr create(
std::string const & recordName);
virtual ~ExampleRPC();
virtual void destroy();
virtual bool init();
virtual void process();
virtual epics::pvAccess::Service::shared_pointer getService(
epics::pvData::PVStructurePtr const & pvRequest);
void put(double x, double y);
bool takeControl();
void releaseControl();
private:
ExampleRPC(std::string const & recordName,
epics::pvData::PVStructurePtr const & pvStructure);
epics::pvData::PVTimeStamp pvTimeStamp;
epics::pvData::TimeStamp timeStamp;
epics::pvData::Mutex taskMutex;
epics::pvAccess::Service::shared_pointer service;
};
}}
#endif /* EXAMPLERPC_H */

View File

@@ -0,0 +1 @@
registrar("exampleRPCRegister")

View File

@@ -0,0 +1,48 @@
/*ExampleRPCMain.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
*/
/* Author: Marty Kraimer */
#include <string>
#include <iostream>
#include <pv/exampleRPC.h>
#include <pv/channelProviderLocal.h>
using namespace std;
using namespace epics::pvData;
using namespace epics::pvAccess;
using namespace epics::pvDatabase;
using namespace epics::exampleRPC;
int main(int argc,char *argv[])
{
PVDatabasePtr master = PVDatabase::getMaster();
PVRecordPtr pvRecord;
bool result = false;
string recordName;
recordName = "mydevice";
pvRecord = ExampleRPC::create(recordName);
result = master->addRecord(pvRecord);
if(!result) cout<< "record " << recordName << " not added" << endl;
ContextLocal::shared_pointer contextLocal = ContextLocal::create();
contextLocal->start();
PVStringArrayPtr pvNames = master->getRecordNames();
shared_vector<const string> names = pvNames->view();
for(size_t i=0; i<names.size(); ++i) cout << names[i] << endl;
contextLocal->waitForExit();
return 0;
}

View File

@@ -0,0 +1,71 @@
/*exampleRPCRegister.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 2013.07.24
*/
/* Author: Marty Kraimer */
#include <cstddef>
#include <cstdlib>
#include <cstddef>
#include <string>
#include <cstdio>
#include <memory>
#include <iostream>
#include <cantProceed.h>
#include <epicsStdio.h>
#include <epicsMutex.h>
#include <epicsEvent.h>
#include <epicsThread.h>
#include <iocsh.h>
#include <pv/pvIntrospect.h>
#include <pv/pvData.h>
#include <pv/pvAccess.h>
#include <pv/pvDatabase.h>
#include <epicsExport.h>
#include <pv/exampleRPC.h>
using namespace epics::pvData;
using namespace epics::pvAccess;
using namespace epics::pvDatabase;
using namespace epics::exampleRPC;
using std::cout;
using std::endl;
static const iocshArg testArg0 = { "recordName", iocshArgString };
static const iocshArg *testArgs[] = {
&testArg0};
static const iocshFuncDef exampleRPCFuncDef = {
"exampleRPCCreateRecord", 1, testArgs};
static void exampleRPCCallFunc(const iocshArgBuf *args)
{
PVDatabasePtr master = PVDatabase::getMaster();
char *recordName = args[0].sval;
ExampleRPCPtr record = ExampleRPC::create(recordName);
bool result = master->addRecord(record);
if(!result) cout << "recordname" << " not added" << endl;
}
static void exampleRPCRegister(void)
{
static int firstTime = 1;
if (firstTime) {
firstTime = 0;
iocshRegister(&exampleRPCFuncDef, exampleRPCCallFunc);
}
}
extern "C" {
epicsExportRegistrar(exampleRPCRegister);
}

View File

@@ -0,0 +1 @@
registrar("exampleRPCRegister")

View File

@@ -0,0 +1,126 @@
/**
* Copyright - See the COPYRIGHT that is included with this distribution.
* EPICS exampleCPP is distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
*/
#include <pv/pvData.h>
#include <pv/rpcService.h>
#include <pv/clientFactory.h>
#include <pv/rpcClient.h>
#include <string>
#include <iostream>
using namespace epics::pvData;
static StructureConstPtr makeDeviceStructure()
{
static StructureConstPtr deviceStructure;
if (deviceStructure.get() == 0)
{
FieldCreatePtr fieldCreate = getFieldCreate();
deviceStructure = fieldCreate->createFieldBuilder()->
add("x",pvDouble)->
add("y",pvDouble)->
createStructure();
}
return deviceStructure;
}
static StructureConstPtr makeArgumentStructure()
{
static StructureConstPtr requestStructure;
if (requestStructure.get() == 0)
{
FieldCreatePtr fieldCreate = getFieldCreate();
requestStructure = fieldCreate->createFieldBuilder()->
addArray("value", makeDeviceStructure())->
createStructure();
}
return requestStructure;
}
// Set a pvAccess connection timeout, after which the client gives up trying
// to connect to server.
const static double REQUEST_TIMEOUT = 3.0;
const static std::string DEVICE_NAME = "mydevice";
const static std::string APP_NAME = "move";
void usage()
{
std::cout << "Usage: " << APP_NAME << " [x_1 y_1] ... [x_n y_n]\n"
<< "Sequentially sets the values of the x and y fields of "
<< DEVICE_NAME << " to (x_i,y_i).\n"
<< "Returns on completion."
<< std::endl;
}
/**
*/
int main (int argc, char *argv[])
{
for (int i = 1; i < argc; ++i)
{
std::string arg(argv[i]);
if (arg == "-h" || arg == "--help")
{
usage();
return 0;
}
}
if ((argc % 2) != 1)
{
std::cerr << APP_NAME << " requires an even number of arguments."
<< std::endl;
usage();
return 1;
}
// Start the pvAccess client side.
epics::pvAccess::ClientFactory::start();
try
{
PVStructurePtr arguments(getPVDataCreate()->createPVStructure(makeArgumentStructure()));
PVStructureArray::svector values;
for (int i = 1; i < argc; )
{
PVStructurePtr point(getPVDataCreate()->createPVStructure(makeDeviceStructure()));
point->getSubField<PVDouble>("x")->put(atof(argv[i++]));
point->getSubField<PVDouble>("y")->put(atof(argv[i++]));
values.push_back(point);
}
arguments->getSubField<PVStructureArray>("value")->replace(freeze(values));
epics::pvAccess::RPCClient::shared_pointer client
= epics::pvAccess::RPCClient::create(DEVICE_NAME);
PVStructurePtr response = client->request(arguments,
REQUEST_TIMEOUT + 1.0 * (argc/2));
std::cout << "Done" << std::endl;
}
catch (epics::pvAccess::RPCRequestException & ex)
{
std::cerr << "Operation failed. RPCException:" << std::endl;
std::cerr << ex.what() << std::endl;
}
catch (...)
{
// Catch any other exceptions so we always call ClientFactory::stop().
std::cerr << "Unexpected exception." << std::endl;
}
// Stop pvAccess client, so that this application exits cleanly.
epics::pvAccess::ClientFactory::stop();
return 0;
}