main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <library/cpp/unified_agent_client/client.h>
  2. #include <library/cpp/getopt/opt.h>
  3. #include <util/string/split.h>
  4. using namespace NUnifiedAgent;
  5. class TOptions {
  6. public:
  7. TString Uri;
  8. TString SharedSecretKey;
  9. TString SessionId;
  10. TString SessionMeta;
  11. TOptions(int argc, const char* argv[]) {
  12. NLastGetopt::TOpts opts;
  13. TString logPriorityStr;
  14. opts
  15. .AddLongOption("uri")
  16. .RequiredArgument()
  17. .Required()
  18. .StoreResult(&Uri);
  19. opts
  20. .AddLongOption("shared-secret-key")
  21. .RequiredArgument()
  22. .Optional()
  23. .StoreResult(&SharedSecretKey);
  24. opts
  25. .AddLongOption("session-id")
  26. .RequiredArgument()
  27. .Optional()
  28. .StoreResult(&SessionId);
  29. opts
  30. .AddLongOption("session-meta", "key-value pairs separated by comma, e.g. 'k1=v1,k2=v2'")
  31. .RequiredArgument()
  32. .Optional()
  33. .StoreResult(&SessionMeta);
  34. opts.AddHelpOption();
  35. opts.AddVersionOption();
  36. NLastGetopt::TOptsParseResult res(&opts, argc, argv);
  37. }
  38. };
  39. bool TryParseMeta(const TString& s, THashMap<TString, TString>& meta) {
  40. for (auto& t: StringSplitter(s).Split(',')) {
  41. TString key;
  42. TString value;
  43. if (!StringSplitter(t.Token()).Split('=').TryCollectInto(&key, &value)) {
  44. Cout << "invalid meta, can't extract key-value pair from [" << t.Token() << "]" << Endl;
  45. return false;
  46. }
  47. meta[key] = value;
  48. }
  49. return true;
  50. }
  51. bool TryParseLine(const TString& line, TVector<TString>& lineItems) {
  52. lineItems = StringSplitter(line).Split('|').ToList<TString>();
  53. Y_ABORT_UNLESS(lineItems.size() >= 1);
  54. if (lineItems.size() > 2) {
  55. Cout << "invalid line format, expected 'k1=v1,k2=v2|payload' or just 'payload'" << Endl;
  56. return false;
  57. }
  58. return true;
  59. }
  60. int main(int argc, const char* argv[]) {
  61. TOptions options(argc, argv);
  62. TClientSessionPtr sessionPtr;
  63. {
  64. TLog emptyLog;
  65. auto clientParameters = TClientParameters(options.Uri).SetLog(emptyLog);
  66. if (!options.SharedSecretKey.Empty()) {
  67. clientParameters.SetSharedSecretKey(options.SharedSecretKey);
  68. }
  69. auto clientPtr = MakeClient(clientParameters);
  70. auto sessionParameters = TSessionParameters();
  71. if (!options.SessionId.Empty()) {
  72. sessionParameters.SetSessionId(options.SessionId);
  73. }
  74. if (!options.SessionMeta.empty()) {
  75. THashMap<TString, TString> sessionMeta;
  76. if (!TryParseMeta(options.SessionMeta, sessionMeta)) {
  77. return -1;
  78. }
  79. sessionParameters.SetMeta(sessionMeta);
  80. }
  81. sessionPtr = clientPtr->CreateSession(sessionParameters);
  82. }
  83. TString line;
  84. while (true) {
  85. Cin.ReadLine(line);
  86. if (line.Empty()) {
  87. break;
  88. }
  89. TVector<TString> lineItems;
  90. if (!TryParseLine(line, lineItems)) {
  91. continue;
  92. }
  93. TClientMessage clientMessage;
  94. clientMessage.Payload = lineItems.back();
  95. if (lineItems.size() == 2) {
  96. THashMap<TString, TString> messageMeta;
  97. if (!TryParseMeta(lineItems[0], messageMeta)) {
  98. continue;
  99. }
  100. clientMessage.Meta = std::move(messageMeta);
  101. }
  102. sessionPtr->Send(std::move(clientMessage));
  103. }
  104. sessionPtr->Close();
  105. return 0;
  106. }