aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/examples/ua_grpc_client/main.cpp
blob: a9eb423d137176ff7505dc187d7e8e54a595eb02 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <library/cpp/unified_agent_client/client.h>

#include <library/cpp/getopt/opt.h>

#include <util/string/split.h>

using namespace NUnifiedAgent;

class TOptions {
public:
    TString Uri;
    TString SharedSecretKey;
    TString SessionId;
    TString SessionMeta;

    TOptions(int argc, const char* argv[]) {
        NLastGetopt::TOpts opts;
        TString logPriorityStr;

        opts
            .AddLongOption("uri")
            .RequiredArgument()
            .Required()
            .StoreResult(&Uri);
        opts
            .AddLongOption("shared-secret-key")
            .RequiredArgument()
            .Optional()
            .StoreResult(&SharedSecretKey);
        opts
            .AddLongOption("session-id")
            .RequiredArgument()
            .Optional()
            .StoreResult(&SessionId);
        opts
            .AddLongOption("session-meta", "key-value pairs separated by comma, e.g. 'k1=v1,k2=v2'")
            .RequiredArgument()
            .Optional()
            .StoreResult(&SessionMeta);

        opts.AddHelpOption();
        opts.AddVersionOption();
        NLastGetopt::TOptsParseResult res(&opts, argc, argv);
    }
};

bool TryParseMeta(const TString& s, THashMap<TString, TString>& meta) {
    for (auto& t: StringSplitter(s).Split(',')) {
        TString key;
        TString value;
        if (!StringSplitter(t.Token()).Split('=').TryCollectInto(&key, &value)) {
            Cout << "invalid meta, can't extract key-value pair from [" << t.Token() << "]" << Endl;
            return false;
        }
        meta[key] = value;
    }
    return true;
}

bool TryParseLine(const TString& line, TVector<TString>& lineItems) {
    lineItems = StringSplitter(line).Split('|').ToList<TString>();
    Y_VERIFY(lineItems.size() >= 1);
    if (lineItems.size() > 2) {
        Cout << "invalid line format, expected 'k1=v1,k2=v2|payload' or just 'payload'" << Endl;
        return false;
    }
    return true;
}

int main(int argc, const char* argv[]) {
    TOptions options(argc, argv);

    TClientSessionPtr sessionPtr;
    {
        TLog emptyLog;
        auto clientParameters = TClientParameters(options.Uri).SetLog(emptyLog);
        if (!options.SharedSecretKey.Empty()) {
            clientParameters.SetSharedSecretKey(options.SharedSecretKey);
        }
        auto clientPtr = MakeClient(clientParameters);
        auto sessionParameters = TSessionParameters();
        if (!options.SessionId.Empty()) {
            sessionParameters.SetSessionId(options.SessionId);
        }
        if (!options.SessionMeta.empty()) {
            THashMap<TString, TString> sessionMeta;
            if (!TryParseMeta(options.SessionMeta, sessionMeta)) {
                return -1;
            }
            sessionParameters.SetMeta(sessionMeta);
        }
        sessionPtr = clientPtr->CreateSession(sessionParameters);
    }

    TString line;
    while (true) {
        Cin.ReadLine(line);
        if (line.Empty()) {
            break;
        }

        TVector<TString> lineItems;
        if (!TryParseLine(line, lineItems)) {
            continue;
        }

        TClientMessage clientMessage;
        clientMessage.Payload = lineItems.back();
        if (lineItems.size() == 2) {
            THashMap<TString, TString> messageMeta;
            if (!TryParseMeta(lineItems[0], messageMeta)) {
                continue;
            }
            clientMessage.Meta = std::move(messageMeta);
        }
        sessionPtr->Send(std::move(clientMessage));
    }

    sessionPtr->Close();

    return 0;
}