rpcServiceExample.cpp: more robust, up-to-date code
This commit is contained in:
@@ -10,33 +10,46 @@
|
|||||||
using namespace epics::pvData;
|
using namespace epics::pvData;
|
||||||
using namespace epics::pvAccess;
|
using namespace epics::pvAccess;
|
||||||
|
|
||||||
|
static Structure::const_shared_pointer resultStructure =
|
||||||
|
getFieldCreate()->createFieldBuilder()->
|
||||||
static StructureConstPtr createResultField()
|
add("c", pvDouble)->
|
||||||
{
|
createStructure();
|
||||||
FieldCreatePtr fieldCreate = getFieldCreate();
|
|
||||||
|
|
||||||
StringArray fieldNames;
|
|
||||||
fieldNames.push_back("c");
|
|
||||||
FieldConstPtrArray fields;
|
|
||||||
fields.push_back(fieldCreate->createScalar(pvDouble));
|
|
||||||
return fieldCreate->createStructure(fieldNames, fields);
|
|
||||||
}
|
|
||||||
|
|
||||||
static StructureConstPtr resultStructure = createResultField();
|
|
||||||
|
|
||||||
class SumServiceImpl :
|
class SumServiceImpl :
|
||||||
public RPCService
|
public RPCService
|
||||||
{
|
{
|
||||||
PVStructure::shared_pointer request(PVStructure::shared_pointer const & args)
|
PVStructure::shared_pointer request(PVStructure::shared_pointer const & pvArguments)
|
||||||
throw (RPCRequestException)
|
throw (RPCRequestException)
|
||||||
{
|
{
|
||||||
// TODO error handling
|
// NTURI support
|
||||||
double a = atof(args->getStringField("a")->get().c_str());
|
PVStructure::shared_pointer args(
|
||||||
double b = atof(args->getStringField("b")->get().c_str());
|
(pvArguments->getStructure()->getID() == "uri:ev4:nt/2012/pwd:NTURI") ?
|
||||||
|
pvArguments->getStructureField("query") :
|
||||||
|
pvArguments
|
||||||
|
);
|
||||||
|
|
||||||
|
// get fields and check their existence
|
||||||
|
PVScalar::shared_pointer af = args->getSubField<PVScalar>("a");
|
||||||
|
PVScalar::shared_pointer bf = args->getSubField<PVScalar>("b");
|
||||||
|
if (!af || !bf)
|
||||||
|
throw RPCRequestException(Status::STATUSTYPE_ERROR, "scalar 'a' and 'b' fields are required");
|
||||||
|
|
||||||
|
// get the numbers (and convert if neccessary)
|
||||||
|
double a, b;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
a = af->getAs<double>();
|
||||||
|
b = bf->getAs<double>();
|
||||||
|
}
|
||||||
|
catch (std::exception &e)
|
||||||
|
{
|
||||||
|
throw RPCRequestException(Status::STATUSTYPE_ERROR,
|
||||||
|
std::string("failed to convert arguments to double: ") + e.what());
|
||||||
|
}
|
||||||
|
|
||||||
|
// create return structure and set data
|
||||||
PVStructure::shared_pointer result = getPVDataCreate()->createPVStructure(resultStructure);
|
PVStructure::shared_pointer result = getPVDataCreate()->createPVStructure(resultStructure);
|
||||||
result->getDoubleField("c")->put(a+b);
|
result->getSubField<PVDouble>("c")->put(a+b);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user