summaryrefslogtreecommitdiffstats
path: root/config/src/apps/vespa-configproxy-cmd/proxycmd.cpp
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@yahoo-inc.com>2017-06-09 11:27:37 +0000
committerTor Egge <Tor.Egge@yahoo-inc.com>2017-06-09 11:57:29 +0000
commit320ee6096be743346e9ec520ec6b5092e43ba02d (patch)
tree018893552f8adb69cb757b5f82cd5be1c9ee976d /config/src/apps/vespa-configproxy-cmd/proxycmd.cpp
parentcb8f250c5d3771510d95bc31269e8b8d6d47ba00 (diff)
Rename config programs to have vespa- prefix.
Temporarily add symlinks from old name to new name.
Diffstat (limited to 'config/src/apps/vespa-configproxy-cmd/proxycmd.cpp')
-rw-r--r--config/src/apps/vespa-configproxy-cmd/proxycmd.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/config/src/apps/vespa-configproxy-cmd/proxycmd.cpp b/config/src/apps/vespa-configproxy-cmd/proxycmd.cpp
new file mode 100644
index 00000000000..df37333f514
--- /dev/null
+++ b/config/src/apps/vespa-configproxy-cmd/proxycmd.cpp
@@ -0,0 +1,101 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "proxycmd.h"
+#include <vespa/vespalib/util/stringfmt.h>
+#include <vespa/fnet/frt/supervisor.h>
+#include <vespa/fnet/frt/target.h>
+#include <vespa/fnet/frt/rpcrequest.h>
+
+#include <iostream>
+
+Flags::Flags(const Flags &) = default;
+Flags & Flags::operator=(const Flags &) = default;
+Flags::Flags()
+ : method("cache"),
+ args(),
+ hostname("localhost"),
+ portnumber(19090)
+{ }
+Flags::~Flags() { }
+
+ProxyCmd::ProxyCmd(const Flags& flags)
+ : _supervisor(NULL),
+ _target(NULL),
+ _req(NULL),
+ _flags(flags)
+{ }
+
+ProxyCmd::~ProxyCmd() { }
+
+void ProxyCmd::initRPC() {
+ _supervisor = new FRT_Supervisor();
+ _req = _supervisor->AllocRPCRequest();
+ _supervisor->Start();
+}
+
+void ProxyCmd::invokeRPC() {
+ if (_req == NULL) return;
+ _target->InvokeSync(_req, 65.0);
+}
+
+void ProxyCmd::finiRPC() {
+ if (_req != NULL) {
+ _req->SubRef();
+ _req = NULL;
+ }
+ if (_target != NULL) {
+ _target->SubRef();
+ _target = NULL;
+ }
+ if (_supervisor != NULL) {
+ _supervisor->ShutDown(true);
+ delete _supervisor;
+ _supervisor = NULL;
+ }
+}
+
+void ProxyCmd::printArray(FRT_Values *rvals) {
+ FRT_Value &lines = rvals->GetValue(0);
+ for (size_t i = 0; i < lines._string_array._len; ++i) {
+ std::cout << lines._string_array._pt[i]._str << std::endl;
+ }
+}
+
+vespalib::string ProxyCmd::makeSpec() {
+ return vespalib::make_string("tcp/%s:%d", _flags.hostname.c_str(), _flags.portnumber);
+}
+
+void ProxyCmd::autoPrint() {
+ if (_req->IsError()) {
+ std::cerr << "FAILURE ["<< _req->GetMethodName() <<"]: " << _req->GetErrorMessage() << std::endl;
+ return;
+ }
+ vespalib::string retspec = _req->GetReturnSpec();
+ FRT_Values *rvals = _req->GetReturn();
+ if (retspec == "S") {
+ printArray(rvals);
+ } else if (retspec == "s") {
+ std::cout << rvals->GetValue(0)._string._str << std::endl;
+ } else if (retspec == "i") {
+ std::cout << rvals->GetValue(0)._intval32 << std::endl;
+ } else {
+ _req->Print();
+ }
+}
+
+int ProxyCmd::action() {
+ int errors = 0;
+ initRPC();
+ vespalib::string spec = makeSpec();
+ _target = _supervisor->GetTarget(spec.c_str());
+ _req->SetMethodName(_flags.method.c_str());
+ FRT_Values &params = *_req->GetParams();
+ for (size_t i = 0; i < _flags.args.size(); ++i) {
+ params.AddString(_flags.args[i].c_str(), _flags.args[i].size());
+ }
+ invokeRPC();
+ if (_req->IsError()) ++errors;
+ autoPrint();
+ finiRPC();
+ return errors;
+}