From 0419fc6a3868e6797a604cd69cc9e3665ec57f79 Mon Sep 17 00:00:00 2001 From: Matej Sekoranja Date: Mon, 2 Jun 2014 21:40:02 +0200 Subject: [PATCH] testChannelConnect: made a cmd-line tool out of it --- testApp/remote/testChannelConnect.cpp | 96 ++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/testApp/remote/testChannelConnect.cpp b/testApp/remote/testChannelConnect.cpp index cbb10de..068d35c 100644 --- a/testApp/remote/testChannelConnect.cpp +++ b/testApp/remote/testChannelConnect.cpp @@ -5,6 +5,10 @@ #include #include +#include +#include +#include +#include #include #include #include @@ -15,13 +19,15 @@ using namespace epics::pvData; using namespace epics::pvAccess; using namespace std; -#define N_CHANNELS 1000 +#define N_CHANNELS_DEFAULT 1000 +#define N_RUNS_DEFAULT 1 class ChannelRequesterImpl : public ChannelRequester { public: - ChannelRequesterImpl(Event& event) : count(0), g_event(event) {} + ChannelRequesterImpl(size_t channels, Event& event) : total(channels), count(0), g_event(event) {} private: + size_t total; int count; Event& g_event; @@ -50,7 +56,7 @@ private: if (connectionState == Channel::CONNECTED) { cout << c->getChannelName() << " CONNECTED: " << (count+1) << endl; - if (++count == N_CHANNELS) + if (static_cast(++count) == total) g_event.signal(); } else if (connectionState == Channel::DISCONNECTED) @@ -64,29 +70,83 @@ private: } }; - - -int main() +void usage (void) { + fprintf (stderr, "\nUsage: testChannelConnect [options]\n\n" + " -h: Help: Print this message\n" + "options:\n" + " -i : number of iterations per each run (< 0 means forever), default is '%d'\n" + " -c : number of channels, default is '%d'\n\n" + , N_RUNS_DEFAULT, N_CHANNELS_DEFAULT); +} + + +int main (int argc, char *argv[]) +{ + size_t nChannels = N_CHANNELS_DEFAULT; + int runs = N_RUNS_DEFAULT; + + int opt; // getopt() current option + setvbuf(stdout,NULL,_IOLBF,BUFSIZ); // Set stdout to line buffering + + while ((opt = getopt(argc, argv, ":hr:w:i:c:s:l:bf:v")) != -1) { + switch (opt) { + case 'h': // Print usage + usage(); + return 0; + case 'i': // iterations + runs = atoi(optarg); + break; + case 'c': // channels + { + int tmp = atoi(optarg); + if (tmp < 0) tmp = 1; + // note: possible overflow (size_t is not always unsigned int) + nChannels = static_cast(tmp); + break; + } + case '?': + fprintf(stderr, + "Unrecognized option: '-%c'. ('testChannelConnect -h' for help.)\n", + optopt); + return 1; + case ':': + fprintf(stderr, + "Option '-%c' requires an argument. ('testChannelConnect -h' for help.)\n", + optopt); + return 1; + default : + usage(); + return 1; + } + } + { Event g_event; ClientFactory::start(); ChannelProvider::shared_pointer provider = getChannelProviderRegistry()->getProvider("pva"); - ChannelRequester::shared_pointer channelRequester(new ChannelRequesterImpl(g_event)); - Channel::shared_pointer channels[N_CHANNELS]; - char buf[16]; - for (int i = 0; i < N_CHANNELS; i++) + int run = 0; + + while (runs < 0 || run++ < runs) { - sprintf(buf, "test%d", (i+1)); - channels[i] = provider->createChannel(buf, channelRequester); - } - - g_event.wait(); - - cout << "connected to all" << endl; - + vector channels(nChannels); + + ChannelRequester::shared_pointer channelRequester(new ChannelRequesterImpl(nChannels, g_event)); + + char buf[16]; + for (size_t i = 0; i < nChannels; i++) + { + sprintf(buf, "test%zu", (i+1)); + channels.push_back(provider->createChannel(buf, channelRequester)); + } + + g_event.wait(); + + cout << "connected to all" << endl; + } + ClientFactory::stop(); }