summaryrefslogtreecommitdiffstats
path: root/fnet/src/examples/ping/pingclient.cpp
blob: fb834b9108b27733af9b99fe64980a0f3b900d41 (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
// 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() override;
};


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 == 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(5000, &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->SubRef();
    transport.ShutDown(true);
    pool.Close();
    return 0;
}


int
main(int argc, char **argv)
{
    PingClient myapp;
    return myapp.Entry(argc, argv);
}