aboutsummaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/ping/pingclient.cpp
blob: b4f098e829ec4e6e002f981c716f756cff24365d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fnet/transport.h>
#include <vespa/fnet/simplepacketstreamer.h>
#include <vespa/fnet/channel.h>
#include <vespa/fnet/connection.h>
#include <examples/ping/packets.h>
#include <vespa/vespalib/util/signalhandler.h>

#include <vespa/log/log.h>
LOG_SETUP("pingclient");

class PingClient
{
public:
    int main(int argc, char **argv);
};


int
PingClient::main(int argc, char **argv)
{
    if (argc < 2) {
        printf("usage  : pingclient <connectspec> <timeout>\n");
        printf("example: pingclient 'tcp/localhost:8000'\n");
        return 1;
    }

    FNET_PacketQueue           queue;
    PingPacketFactory          factory;
    FNET_SimplePacketStreamer  streamer(&factory);
    FNET_Transport             transport;
    FNET_Connection           *conn = transport.Connect(argv[1], &streamer);
    uint32_t                   timeout_ms=5000;
    FNET_Channel              *channels[10];

    if (argc == 3) {
        timeout_ms = atof(argv[2]) * 1000;
    }
    transport.Start();

    uint32_t channelCnt = 0;
    for (uint32_t i = 0; i < 10; i++) {
        channels[i] = (conn == nullptr) ? nullptr : 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(timeout_ms, &context);
        if (packet == nullptr) {
            fprintf(stderr, "Timeout\n");
            for(int c = 0; c < 10; c++) {
                if (channels[c] != nullptr) {
                    channels[c]->Close();
                    channels[c]->Free();
                    channels[c] = nullptr;
                    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] != nullptr) {
            channels[context._value.INT]->Close();
            channels[context._value.INT]->Free();
            channels[context._value.INT] = nullptr;
            fprintf(stderr, "Closed channel with context %d\n",
                    context._value.INT);
            channelCnt--;
        }
        packet->Free();
    }
    if (conn != nullptr)
        conn->internal_subref();
    transport.ShutDown(true);
    return 0;
}


int main(int argc, char **argv) {
    vespalib::SignalHandler::PIPE.ignore();
    PingClient myapp;
    return myapp.main(argc, argv);
}