pva2pva 1.4.2
Loading...
Searching...
No Matches
gwmain.cpp
1
2#include <iostream>
3#include <fstream>
4#include <stdexcept>
5#include <map>
6
7#if !defined(_WIN32)
8#include <signal.h>
9#define USE_SIGNAL
10#endif
11
12#include <epicsStdlib.h>
13#include <epicsGetopt.h>
14#include <iocsh.h>
15#include <epicsTimer.h>
16#include <libComRegister.h>
17
18#include <pv/json.h>
19
20#include <pv/pvAccess.h>
21#include <pv/clientFactory.h>
22#include <pv/configuration.h>
23#include <pv/serverContext.h>
24#include <pv/reftrack.h>
25#include <pv/iocreftrack.h>
26#include <pv/iocshelper.h>
27#include <pv/logger.h>
28
29#include "server.h"
30#include "pva2pva.h"
31
32namespace pvd = epics::pvData;
33namespace pva = epics::pvAccess;
34
35extern int p2pReadOnly;
36
37namespace {
38
39pvd::StructureConstPtr schema(pvd::getFieldCreate()->createFieldBuilder()
40 ->add("version", pvd::pvUInt)
41 ->add("readOnly", pvd::pvBoolean)
42 ->addNestedStructureArray("clients")
43 ->add("name", pvd::pvString)
44 ->add("provider", pvd::pvString)
45 ->add("addrlist", pvd::pvString)
46 ->add("autoaddrlist", pvd::pvBoolean)
47 ->add("serverport", pvd::pvUShort)
48 ->add("bcastport", pvd::pvUShort)
49 ->endNested()
50 ->addNestedStructureArray("servers")
51 ->add("name", pvd::pvString)
52 ->addArray("clients", pvd::pvString)
53 ->add("interface", pvd::pvString)
54 ->add("addrlist", pvd::pvString)
55 ->add("autoaddrlist", pvd::pvBoolean)
56 ->add("serverport", pvd::pvUShort)
57 ->add("bcastport", pvd::pvUShort)
58 ->add("control_prefix", pvd::pvString)
59 ->endNested()
60 ->createStructure());
61
62
63void usage(const char *me)
64{
65 std::cerr<<"Usage: "<<me<<" [-vhiIC] <config file>\n";
66}
67
68void getargs(ServerConfig& arg, int argc, char *argv[])
69{
70 int opt;
71 bool checkonly = false;
72
73 while( (opt=getopt(argc, argv, "qvhiIC"))!=-1)
74 {
75 switch(opt) {
76 case 'h':
77 usage(argv[0]);
78 exit(0);
79 case 'q':
80 arg.debug--;
81 break;
82 case 'v':
83 arg.debug++;
84 break;
85 case 'I':
86 arg.interactive = true;
87 break;
88 case 'i':
89 arg.interactive = false;
90 break;
91 case 'C':
92 checkonly = true;
93 break;
94 default:
95 std::cerr<<"Unknown argument -"<<char(opt)<<"\n";
96 usage(argv[0]);
97 exit(1);
98 }
99 }
100
101 if(optind!=argc-1) {
102 std::cerr<<"Exactly one positional argument expected\n";
103 usage(argv[0]);
104 exit(1);
105 }
106
107 arg.conf = pvd::getPVDataCreate()->createPVStructure(schema);
108 std::ifstream strm(argv[optind]);
109 pvd::parseJSON(strm, arg.conf);
110
111 p2pReadOnly = arg.conf->getSubFieldT<pvd::PVScalar>("readOnly")->getAs<pvd::boolean>();
112
113 unsigned version = arg.conf->getSubFieldT<pvd::PVUInt>("version")->get();
114 if(version==0) {
115 std::cerr<<"Warning: config file missing \"version\" key. Assuming 1\n";
116 } else if(version!=1) {
117 std::cerr<<"config file version mis-match. expect 1 found "<<version<<"\n";
118 exit(1);
119 }
120 if(arg.conf->getSubFieldT<pvd::PVStructureArray>("clients")->view().empty()) {
121 std::cerr<<"No clients configured\n";
122 exit(1);
123 }
124 if(arg.conf->getSubFieldT<pvd::PVStructureArray>("servers")->view().empty()) {
125 std::cerr<<"No servers configured\n";
126 exit(1);
127 }
128
129 if(checkonly) {
130 std::cerr<<"Config file OK\n";
131 exit(0);
132 }
133}
134
135GWServerChannelProvider::shared_pointer configure_client(ServerConfig& arg, const pvd::PVStructurePtr& conf)
136{
137 std::string name(conf->getSubFieldT<pvd::PVString>("name")->get());
138 std::string provider(conf->getSubFieldT<pvd::PVString>("provider")->get());
139
140 LOG(pva::logLevelInfo, "Configure client '%s' with provider '%s'", name.c_str(), provider.c_str());
141
142 pva::Configuration::shared_pointer C(pva::ConfigurationBuilder()
143 .add("EPICS_PVA_ADDR_LIST", conf->getSubFieldT<pvd::PVString>("addrlist")->get())
144 .add("EPICS_PVA_AUTO_ADDR_LIST", conf->getSubFieldT<pvd::PVScalar>("autoaddrlist")->getAs<std::string>())
145 .add("EPICS_PVA_SERVER_PORT", conf->getSubFieldT<pvd::PVScalar>("serverport")->getAs<pvd::uint16>())
146 .add("EPICS_PVA_BROADCAST_PORT", conf->getSubFieldT<pvd::PVScalar>("bcastport")->getAs<pvd::uint16>())
147 .add("EPICS_PVA_DEBUG", arg.debug>=5 ? 5 : 0)
148 .push_map()
149 .build());
150
151 pva::ChannelProvider::shared_pointer base(pva::ChannelProviderRegistry::clients()->createProvider(provider, C));
152 if(!base)
153 throw std::runtime_error("Can't create ChannelProvider");
154
155 GWServerChannelProvider::shared_pointer ret(new GWServerChannelProvider(base));
156 return ret;
157}
158
159pva::ServerContext::shared_pointer configure_server(ServerConfig& arg, const pvd::PVStructurePtr& conf)
160{
161 std::string name(conf->getSubFieldT<pvd::PVString>("name")->get());
162
163 LOG(pva::logLevelInfo, "Configure server '%s'", name.c_str());
164
165 pva::Configuration::shared_pointer C(pva::ConfigurationBuilder()
166 .add("EPICS_PVAS_INTF_ADDR_LIST", conf->getSubFieldT<pvd::PVString>("interface")->get())
167 .add("EPICS_PVAS_BEACON_ADDR_LIST", conf->getSubFieldT<pvd::PVString>("addrlist")->get())
168 .add("EPICS_PVAS_AUTO_BEACON_ADDR_LIST", conf->getSubFieldT<pvd::PVScalar>("autoaddrlist")->getAs<std::string>())
169 .add("EPICS_PVAS_SERVER_PORT", conf->getSubFieldT<pvd::PVScalar>("serverport")->getAs<pvd::uint16>())
170 .add("EPICS_PVAS_BROADCAST_PORT", conf->getSubFieldT<pvd::PVScalar>("bcastport")->getAs<pvd::uint16>())
171 .add("EPICS_PVA_DEBUG", arg.debug>=5 ? 5 : 0)
172 .push_map()
173 .build());
174
175 pvd::PVStringArray::shared_pointer clients(conf->getSubFieldT<pvd::PVStringArray>("clients"));
176 pvd::PVStringArray::const_svector names(clients->view());
177 std::vector<pva::ChannelProvider::shared_pointer> providers;
178
179 for(pvd::PVStringArray::const_svector::const_iterator it(names.begin()), end(names.end()); it!=end; ++it)
180 {
181 ServerConfig::clients_t::const_iterator it2(arg.clients.find(*it));
182 if(it2==arg.clients.end())
183 throw std::runtime_error("Server references non-existant client");
184 providers.push_back(it2->second);
185 }
186
187 pva::ServerContext::shared_pointer ret(pva::ServerContext::create(pva::ServerContext::Config()
188 .config(C)
189 .providers(providers)));
190 return ret;
191}
192
193volatile int quit;
194epicsEvent done;
195
196#ifdef USE_SIGNAL
197void sigdone(int num)
198{
199 (void)num;
200 quit = 1;
201 done.signal();
202}
203#endif
204
205ServerConfig* volatile theserver;
206
207void iocsh_drop(const char *client, const char *channel)
208{
209 if(!theserver)
210 return;
211 try {
212 theserver->drop(client, channel);
213 }catch(std::exception& e){
214 std::cout<<"Error: "<<e.what()<<"\n";
215 }
216}
217
218void gwsr(int lvl, const char *server)
219{
220 if(!theserver)
221 return;
222 try {
223 theserver->status_server(lvl, server);
224 }catch(std::exception& e){
225 std::cout<<"Error: "<<e.what()<<"\n";
226 }
227}
228
229void gwcr(int lvl, const char *client, const char *channel)
230{
231 if(!theserver)
232 return;
233 try {
234 theserver->status_client(lvl, client, channel);
235 }catch(std::exception& e){
236 std::cout<<"Error: "<<e.what()<<"\n";
237 }
238}
239
240}// namespace
241
242int main(int argc, char *argv[])
243{
244 try {
245 pva::refTrackRegistrar();
246
247 epics::iocshRegister<const char*, const char*, &iocsh_drop>("drop", "client", "channel");
248 epics::iocshRegister<int, const char*, &gwsr>("gwsr", "level", "channel");
249 epics::iocshRegister<int, const char*, const char*, &gwcr>("gwcr", "level", "client", "channel");
250
251 libComRegister();
252 registerReadOnly();
253 epics::registerRefCounter("ChannelCacheEntry", &ChannelCacheEntry::num_instances);
254 epics::registerRefCounter("ChannelCacheEntry::CRequester", &ChannelCacheEntry::CRequester::num_instances);
255 epics::registerRefCounter("GWChannel", &GWChannel::num_instances);
256 epics::registerRefCounter("MonitorCacheEntry", &MonitorCacheEntry::num_instances);
257 epics::registerRefCounter("MonitorUser", &MonitorUser::num_instances);
258
259 ServerConfig arg;
260 theserver = &arg;
261 getargs(arg, argc, argv);
262
263 if(arg.debug>0)
264 std::cout<<"Notice: This p2p gateway prototype has been superceded by the p4p.gw gateway\n"
265 " which has exciting new features including granular access control,\n"
266 " and status PVs including bandwidth usage reports.\n"
267 " p2p is considered deprecated by its author, and will receive\n"
268 " minimal maintainance effort going forward.\n"
269 " Users are encouraged to migrate to p4p.gw.\n"
270 "\n"
271 " https://mdavidsaver.github.io/p4p/gw.html\n"
272 "\n";
273
274 pva::pvAccessLogLevel lvl;
275 if(arg.debug<0)
276 lvl = pva::logLevelError;
277 else if(arg.debug==0)
278 lvl = pva::logLevelWarn;
279 else if(arg.debug==1)
280 lvl = pva::logLevelInfo;
281 else if(arg.debug==2)
282 lvl = pva::logLevelDebug;
283 else if(arg.debug==3)
284 lvl = pva::logLevelTrace;
285 else // arg.debug>=4
286 lvl = pva::logLevelAll;
287 SET_LOG_LEVEL(lvl);
288
289 pva::ClientFactory::start();
290
291 pvd::PVStructureArray::const_svector arr;
292
293 arr = arg.conf->getSubFieldT<pvd::PVStructureArray>("clients")->view();
294
295 for(size_t i=0; i<arr.size(); i++) {
296 if(!arr[i]) continue;
297 const pvd::PVStructurePtr& client = arr[i];
298
299 std::string name(client->getSubFieldT<pvd::PVString>("name")->get());
300 if(name.empty())
301 throw std::runtime_error("Client with empty name not allowed");
302
303 ServerConfig::clients_t::const_iterator it(arg.clients.find(name));
304 if(it!=arg.clients.end())
305 throw std::runtime_error(std::string("Duplicate client name not allowed : ")+name);
306
307 arg.clients[name] = configure_client(arg, client);
308 }
309
310 arr = arg.conf->getSubFieldT<pvd::PVStructureArray>("servers")->view();
311
312 for(size_t i=0; i<arr.size(); i++) {
313 if(!arr[i]) continue;
314 const pvd::PVStructurePtr& server = arr[i];
315
316 std::string name(server->getSubFieldT<pvd::PVString>("name")->get());
317 if(name.empty())
318 throw std::runtime_error("Server with empty name not allowed");
319
320 ServerConfig::servers_t::const_iterator it(arg.servers.find(name));
321 if(it!=arg.servers.end())
322 throw std::runtime_error(std::string("Duplicate server name not allowed : ")+name);
323
324 arg.servers[name] = configure_server(arg, server);
325 }
326
327 int ret = 0;
328 if(arg.interactive) {
329 ret = iocsh(NULL);
330 } else {
331#ifdef USE_SIGNAL
332 signal(SIGINT, sigdone);
333 signal(SIGTERM, sigdone);
334 signal(SIGQUIT, sigdone);
335#endif
336
337 while(!quit) {
338 done.wait();
339 }
340 }
341
342 theserver = 0;
343
344 return ret;
345 }catch(std::exception& e){
346 std::cerr<<"Fatal Error : "<<e.what()<<"\n";
347 return 1;
348 }
349}