]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/CommandTable.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / common / CommandTable.h
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2016 Red Hat Inc
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#ifndef COMMAND_TABLE_H_
16#define COMMAND_TABLE_H_
17
18#include "messages/MCommand.h"
9f95a23c 19#include "messages/MMgrCommand.h"
7c673cae
FG
20
21class CommandOp
22{
23 public:
24 ConnectionRef con;
25 ceph_tid_t tid;
26
27 std::vector<std::string> cmd;
9f95a23c 28 ceph::buffer::list inbl;
7c673cae 29 Context *on_finish;
9f95a23c 30 ceph::buffer::list *outbl;
7c673cae
FG
31 std::string *outs;
32
9f95a23c
TL
33 MessageRef get_message(const uuid_d &fsid,
34 bool mgr=false) const
7c673cae 35 {
9f95a23c 36 if (mgr) {
f67539c2 37 auto m = ceph::make_message<MMgrCommand>(fsid);
9f95a23c
TL
38 m->cmd = cmd;
39 m->set_data(inbl);
40 m->set_tid(tid);
41 return m;
42 } else {
f67539c2 43 auto m = ceph::make_message<MCommand>(fsid);
9f95a23c
TL
44 m->cmd = cmd;
45 m->set_data(inbl);
46 m->set_tid(tid);
47 return m;
48 }
7c673cae
FG
49 }
50
51 CommandOp(const ceph_tid_t t) : tid(t), on_finish(nullptr),
52 outbl(nullptr), outs(nullptr) {}
53 CommandOp() : tid(0), on_finish(nullptr), outbl(nullptr), outs(nullptr) {}
54};
55
56/**
57 * Hold client-side state for a collection of in-flight commands
58 * to a remote service.
59 */
60template<typename T>
61class CommandTable
62{
63protected:
64 ceph_tid_t last_tid;
65 std::map<ceph_tid_t, T> commands;
66
67public:
68
69 CommandTable()
70 : last_tid(0)
71 {}
72
73 ~CommandTable()
74 {
11fdf7f2 75 ceph_assert(commands.empty());
7c673cae
FG
76 }
77
78 T& start_command()
79 {
80 ceph_tid_t tid = last_tid++;
81 commands.insert(std::make_pair(tid, T(tid)) );
82
83 return commands.at(tid);
84 }
85
86 const std::map<ceph_tid_t, T> &get_commands() const
87 {
88 return commands;
89 }
90
91 bool exists(ceph_tid_t tid) const
92 {
93 return commands.count(tid) > 0;
94 }
95
96 T& get_command(ceph_tid_t tid)
97 {
98 return commands.at(tid);
99 }
100
101 void erase(ceph_tid_t tid)
102 {
103 commands.erase(tid);
104 }
105
106 void clear() {
107 commands.clear();
108 }
109};
110
111#endif
112