aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/tests/startsome/rpc_info.cpp
blob: be6d59f6a8129fc8c800ce426dd6fd13a58e5679 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/signalhandler.h>
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/rpcrequest.h>
#include <vespa/fnet/frt/target.h>


class RPCInfo
{
public:

  void GetReq(FRT_RPCRequest **req, FRT_Supervisor *supervisor)
  {
    if ((*req) != nullptr)
      (*req)->internal_subref();
    (*req) = supervisor->AllocRPCRequest();
  }

  void FreeReqs(FRT_RPCRequest *r1, FRT_RPCRequest *r2)
  {
    if (r1 != nullptr)
      r1->internal_subref();
    if (r2 != nullptr)
      r2->internal_subref();
  }

  void DumpMethodInfo(const char *indent, FRT_RPCRequest *info,
                      const char *name)
  {
    if (info->IsError()) {
      printf("%sMETHOD %s\n", indent, name);
      printf("%s  [error(%d): %s]\n\n", indent,
             info->GetErrorCode(),
             info->GetErrorMessage());
      return;
    }

    const char      *desc    = info->GetReturn()->GetValue(0)._string._str;
    const char      *arg     = info->GetReturn()->GetValue(1)._string._str;
    const char      *ret     = info->GetReturn()->GetValue(2)._string._str;
    uint32_t         argCnt  = strlen(arg);
    uint32_t         retCnt  = strlen(ret);
    FRT_StringValue *argName = info->GetReturn()->GetValue(3)._string_array._pt;
    FRT_StringValue *argDesc = info->GetReturn()->GetValue(4)._string_array._pt;
    FRT_StringValue *retName = info->GetReturn()->GetValue(5)._string_array._pt;
    FRT_StringValue *retDesc = info->GetReturn()->GetValue(6)._string_array._pt;

    printf("%sMETHOD %s\n", indent, name);
    printf("%s  DESCRIPTION:\n"
           "%s    %s\n", indent, indent, desc);

    if (argCnt > 0) {
      printf("%s  PARAMS:\n", indent);
      for (uint32_t a = 0; a < argCnt; a++)
        printf("%s    [%c][%s] %s\n", indent, arg[a], argName[a]._str,
               argDesc[a]._str);
    }

    if (retCnt > 0) {
      printf("%s  RETURN:\n", indent);
      for (uint32_t r = 0; r < retCnt; r++)
        printf("%s    [%c][%s] %s\n", indent, ret[r], retName[r]._str,
               retDesc[r]._str);
    }
    printf("\n");
  }


  int main(int argc, char **argv)
  {
    if (argc < 2) {
      printf("usage : rpc_info <connectspec> [verbose]\n");
      return 1;
    }

    bool verbose = (argc > 2 && strcmp(argv[2], "verbose") == 0);
    fnet::frt::StandaloneFRT server;
    FRT_Supervisor & supervisor = server.supervisor();
    FRT_Target     *target = supervisor.GetTarget(argv[1]);
    FRT_RPCRequest *m_list = nullptr;
    FRT_RPCRequest *info   = nullptr;

    GetReq(&info, &supervisor);
    info->SetMethodName("frt.rpc.ping");
    target->InvokeSync(info, 5.0);
    if (info->IsError()) {
      fprintf(stderr, "Error talking to %s\n", argv[1]);
      FreeReqs(m_list, info);
      return 1;
    }

    GetReq(&m_list, &supervisor);
    m_list->SetMethodName("frt.rpc.getMethodList");
    target->InvokeSync(m_list, 5.0);

    if (!m_list->IsError()) {

      uint32_t numMethods      = m_list->GetReturn()->GetValue(0)._string_array._len;
      FRT_StringValue *methods = m_list->GetReturn()->GetValue(0)._string_array._pt;
      FRT_StringValue *arglist = m_list->GetReturn()->GetValue(1)._string_array._pt;
      FRT_StringValue *retlist = m_list->GetReturn()->GetValue(2)._string_array._pt;

      for (uint32_t m = 0; m < numMethods; m++) {

        if (verbose) {

          GetReq(&info, &supervisor);
          info->SetMethodName("frt.rpc.getMethodInfo");
          info->GetParams()->AddString(methods[m]._str);
          target->InvokeSync(info, 5.0);
          DumpMethodInfo("", info, methods[m]._str);

        } else {

          printf("METHOD [%s] <- %s <- [%s]\n",
                 retlist[m]._str, methods[m]._str, arglist[m]._str);
        }
      }
    } else {
      fprintf(stderr, "  [error(%d): %s]\n",
              m_list->GetErrorCode(),
              m_list->GetErrorMessage());
    }
    FreeReqs(m_list, info);
    target->internal_subref();
    return 0;
  }
};


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