summaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/ping/pingclient.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'fnet/src/examples/ping/pingclient.cpp')
-rw-r--r--fnet/src/examples/ping/pingclient.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/fnet/src/examples/ping/pingclient.cpp b/fnet/src/examples/ping/pingclient.cpp
new file mode 100644
index 00000000000..075c0b5df32
--- /dev/null
+++ b/fnet/src/examples/ping/pingclient.cpp
@@ -0,0 +1,93 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#include <vespa/fastos/fastos.h>
+#include <vespa/log/log.h>
+LOG_SETUP("pingclient");
+#include <vespa/fnet/fnet.h>
+#include <examples/ping/packets.h>
+
+
+class PingClient : public FastOS_Application
+{
+public:
+ int Main();
+};
+
+
+int
+PingClient::Main()
+{
+ if (_argc < 2) {
+ printf("usage : pingclient <connectspec>\n");
+ printf("example: pingclient 'tcp/localhost:8000'\n");
+ return 1;
+ }
+
+ FNET_PacketQueue queue;
+ FastOS_ThreadPool pool(65000);
+ PingPacketFactory factory;
+ FNET_SimplePacketStreamer streamer(&factory);
+ FNET_Transport transport;
+ FNET_Connection *conn = transport.Connect(_argv[1], &streamer);
+ FNET_Channel *channels[10];
+ transport.Start(&pool);
+
+ uint32_t channelCnt = 0;
+ for (uint32_t i = 0; i < 10; i++) {
+ channels[i] = (conn == NULL) ? NULL : conn->OpenChannel(&queue, FNET_Context(i));
+ if (channels[i] == 0) {
+ fprintf(stderr, "Could not make channel[%d] to %s\n", i, _argv[1]);
+ break;
+ }
+ channelCnt++;
+ channels[i]->Send(new PingRequest());
+ channels[i]->Sync();
+ fprintf(stderr, "Sent ping in context %d\n", i);
+ }
+
+ FNET_Packet *packet;
+ FNET_Context context;
+ while (channelCnt > 0) {
+ packet = queue.DequeuePacket(5000, &context);
+ if (packet == NULL) {
+ fprintf(stderr, "Timeout\n");
+ for(int c = 0; c < 10; c++) {
+ if (channels[c] != NULL) {
+ channels[c]->Close();
+ channels[c]->Free();
+ channels[c] = NULL;
+ fprintf(stderr, "Closed channel with context %d\n", c);
+ }
+ }
+ break;
+ }
+ if (packet->GetPCODE() == PCODE_PING_REPLY) {
+ fprintf(stderr, "Got ping result in context %d\n",
+ context._value.INT);
+ } else if (packet->IsChannelLostCMD()) {
+ fprintf(stderr, "Lost channel with context %d\n",
+ context._value.INT);
+ }
+ if (channels[context._value.INT] != NULL) {
+ channels[context._value.INT]->Close();
+ channels[context._value.INT]->Free();
+ channels[context._value.INT] = NULL;
+ fprintf(stderr, "Closed channel with context %d\n",
+ context._value.INT);
+ channelCnt--;
+ }
+ packet->Free();
+ }
+ if (conn != NULL)
+ conn->SubRef();
+ transport.ShutDown(true);
+ pool.Close();
+ return 0;
+}
+
+
+int
+main(int argc, char **argv)
+{
+ PingClient myapp;
+ return myapp.Entry(argc, argv);
+}