From 19f3dc5211615a678dfc14d3168dc44484352bfc Mon Sep 17 00:00:00 2001 From: Matej Sekoranja Date: Fri, 18 Jan 2013 16:12:34 +0100 Subject: [PATCH] eget NTURI support --- testApp/remote/eget.cpp | 68 +++++++++++++++++++++++++++++++++-- testApp/remote/testServer.cpp | 14 ++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/testApp/remote/eget.cpp b/testApp/remote/eget.cpp index 57246ac..230c5cb 100644 --- a/testApp/remote/eget.cpp +++ b/testApp/remote/eget.cpp @@ -490,6 +490,52 @@ void formatNTNameValue(std::ostream& o, PVStructurePtr const & pvStruct) } +void formatNTURI(std::ostream& o, PVStructurePtr const & pvStruct) +{ + PVStringPtr scheme = dynamic_pointer_cast(pvStruct->getStringField("scheme")); + if (scheme.get() == 0) + { + std::cerr << "no string 'scheme' field in NTURI" << std::endl; + } + + PVStringPtr authority = dynamic_pointer_cast(pvStruct->getSubField("authority")); + + PVStringPtr path = dynamic_pointer_cast(pvStruct->getStringField("path")); + if (path.get() == 0) + { + std::cerr << "no string 'path' field in NTURI" << std::endl; + return; + } + + PVStructurePtr query = dynamic_pointer_cast(pvStruct->getSubField("query")); + + o << scheme->get() << "://"; + if (authority.get()) o << authority->get(); + o << '/' << path->get(); + + // query + if (query.get()) + { + PVFieldPtrArray fields = query->getPVFields(); + size_t numColumns = fields.size(); + + if (numColumns > 0) + { + o << '?'; + + for (size_t i = 0; i < numColumns; i++) + { + if (i) + o << '&'; + + // TODO encode value!!! + o << fields[i]->getFieldName() << '=' << *(fields[i].get()); + } + } + } + + o << std::endl; +} typedef void(*NTFormatterFunc)(std::ostream& o, PVStructurePtr const & pvStruct); typedef map NTFormatterLUTMap; @@ -503,6 +549,7 @@ void initializeNTFormatterLUT() ntFormatterLUT["uri:ev4:nt/2012/pwd:NTMatrix"] = formatNTMatrix; ntFormatterLUT["uri:ev4:nt/2012/pwd:NTAny"] = formatNTAny; ntFormatterLUT["uri:ev4:nt/2012/pwd:NTNameValue"] = formatNTNameValue; + ntFormatterLUT["uri:ev4:nt/2012/pwd:NTURI"] = formatNTURI; // // TODO remove: smooth transition @@ -514,6 +561,7 @@ void initializeNTFormatterLUT() ntFormatterLUT["NTMatrix"] = formatNTMatrix; ntFormatterLUT["NTAny"] = formatNTAny; ntFormatterLUT["NTNameValue"] = formatNTNameValue; + ntFormatterLUT["NTURI"] = formatNTURI; // StandardPV "support" ntFormatterLUT["scalar_t"] = formatNTScalar; @@ -962,7 +1010,7 @@ int main (int argc, char *argv[]) bool serviceRequest = false; bool onlyQuery = false; string service; - string urlEncodedRequest; + //string urlEncodedRequest; vector< pair > parameters; setvbuf(stdout,NULL,_IOLBF,BUFSIZ); /* Set stdout to line buffering */ @@ -995,11 +1043,13 @@ int main (int argc, char *argv[]) return 1; } parameters.push_back(pair(param.substr(0, eqPos), param.substr(eqPos+1, string::npos))); + /* if (urlEncodedRequest.size()) urlEncodedRequest += '&'; char* encoded = url_encode(optarg); urlEncodedRequest += encoded; free(encoded); + */ break; } case 's': /* Service name */ @@ -1176,6 +1226,8 @@ int main (int argc, char *argv[]) // service RPC mode else { + String authority; + URI uri; bool validURI = URI::parse(service, uri); if (validURI) @@ -1187,6 +1239,8 @@ int main (int argc, char *argv[]) return 1; } + authority = uri.host; + if (uri.path.length() <= 1) { std::cerr << "invalid URI, empty path" << std::endl; @@ -1262,11 +1316,15 @@ int main (int argc, char *argv[]) StringArray uriFieldNames; + uriFieldNames.push_back("scheme"); + if (!authority.empty()) uriFieldNames.push_back("authority"); uriFieldNames.push_back("path"); uriFieldNames.push_back("query"); FieldConstPtrArray uriFields; uriFields.push_back(getFieldCreate()->createScalar(pvString)); + if (!authority.empty()) uriFields.push_back(getFieldCreate()->createScalar(pvString)); + uriFields.push_back(getFieldCreate()->createScalar(pvString)); uriFields.push_back(queryStructure); Structure::const_shared_pointer uriStructure( @@ -1283,6 +1341,8 @@ int main (int argc, char *argv[]) getPVDataCreate()->createPVStructure(uriStructure) ); + request->getStringField("scheme")->put("pva"); + if (!authority.empty()) request->getStringField("authority")->put(authority); request->getStringField("path")->put(service); PVStructure::shared_pointer query = request->getStructureField("query"); for (vector< pair >::iterator iter = parameters.begin(); @@ -1304,7 +1364,11 @@ int main (int argc, char *argv[]) ChannelProvider::shared_pointer provider = getChannelAccess()->getProvider("pvAccess"); shared_ptr channelRequesterImpl(new ChannelRequesterImpl()); - Channel::shared_pointer channel = provider->createChannel(service, channelRequesterImpl); + Channel::shared_pointer channel = + authority.empty() ? + provider->createChannel(service, channelRequesterImpl) : + provider->createChannel(service, channelRequesterImpl, + ChannelProvider::PRIORITY_DEFAULT, authority); if (channelRequesterImpl->waitUntilConnected(timeOut)) { diff --git a/testApp/remote/testServer.cpp b/testApp/remote/testServer.cpp index 1065087..264dc63 100644 --- a/testApp/remote/testServer.cpp +++ b/testApp/remote/testServer.cpp @@ -1031,6 +1031,20 @@ class MockChannelRPC : public ChannelRPC m_channelRPCRequester->requestDone(Status::Ok, result); } + } + else if (m_channelName == "testNTURI") + { + if (pvArgument->getStructure()->getID() != "uri:ev4:nt/2012/pwd:NTURI") + { + PVStructure::shared_pointer nullPtr; + Status errorStatus(Status::STATUSTYPE_ERROR, "argument is not a NTURI structure"); + m_channelRPCRequester->requestDone(errorStatus, nullPtr); + } + else + { + // return argument as result + m_channelRPCRequester->requestDone(Status::Ok, pvArgument); + } } else {