summaryrefslogtreecommitdiff
path: root/src/cls/lua/cls_lua_client.cc
blob: f949d4ccf28b18a54fb9c78ae52dbc3bc44b6dd7 (plain)
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
#include <errno.h>
#include <string>
#include <vector>
#include "include/encoding.h"
#include "include/rados.h"
#include "include/rados/librados.h"
#include "include/types.h"
#include "cls_lua.h"
#include "cls_lua_client.hpp"

using std::string;
using std::vector;
using librados::IoCtx;
using librados::bufferlist;

namespace cls_lua_client {

  int exec(IoCtx& ioctx, const string& oid, const string& script,
      const string& handler, bufferlist& input, bufferlist& output,
      vector<string> *log)
  {
    struct clslua_cmd cmd;
    struct clslua_reply reply;
    bufferlist outbl, inbl;
    int ret;

    cmd.script = script;
    cmd.funcname = handler;
    cmd.input = input;
    ::encode(cmd, inbl);

    /*
     * TODO: we need to encapsulate the return value as well. for example,
     * -ENOTSUPP is returned if the class is not found, but we also return
     * -ENOTSUPP if a handler isn't found. In the later case we still get a
     * valid reply, in the former not so much.
     */
    ret = ioctx.exec(oid, "lua", "eval", inbl, outbl);
    if (ret < 0)
      return ret;

    try {
      ::decode(reply, outbl);
    } catch (const buffer::error &err) {
      return -EBADMSG;
    }

    output = reply.output;
    if (log)
      log->swap(reply.log);

    return ret;
  }

}