0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SshOutputCollector.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2014 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
27 
28 #ifndef Tools_cluster_SshOutputCollector_h
29 #define Tools_cluster_SshOutputCollector_h
30 
31 #include <Common/PageArena.h>
32 #include <Common/Logger.h>
33 
34 #include <boost/iterator/iterator_facade.hpp>
35 
36 #include <cstdlib>
37 #include <string>
38 #include <tuple>
39 #include <vector>
40 
41 namespace Hypertable {
42 
45 
48 
49  public:
50 
52  class Buffer {
53  public:
55  Buffer() { }
59  Buffer(char *buffer, size_t sz=0) : base(buffer), ptr(buffer), size(sz) { }
62  size_t fill() const { return ptr-base; }
65  size_t remain() const { return size-fill(); }
69  void add(const char *data, size_t len) {
70  HT_ASSERT(len <= remain());
71  memcpy(ptr, data, len);
72  ptr += len;
73  }
75  char *base {};
77  char *ptr {};
79  size_t size {};
80  };
81 
83  class iterator : public boost::iterator_facade<iterator, const std::string, boost::forward_traversal_tag> {
84  public:
88  iterator(std::vector<Buffer> &buffers, size_t index=0)
89  : m_buffers(buffers), m_index(index), m_next_index(index) {
90  increment();
91  }
92 
93  private:
95 
98  void increment();
99 
105  bool equal(iterator const& other) const {
106  return this->m_index == other.m_index &&
107  this->m_offset == other.m_offset;
108  }
109 
114  const std::string & dereference() const { return m_line; }
115 
117  std::vector<Buffer> &m_buffers;
119  size_t m_index {};
121  size_t m_offset {};
123  size_t m_next_index {};
125  size_t m_next_offset {};
127  std::string m_line;
128  };
129 
132  SshOutputCollector(size_t buffer_size) : m_buffer_size(buffer_size) {}
133 
137  size_t buffer_size() { return m_buffer_size; }
138 
144  }
145 
148  void add(Buffer buf) {
149  if (buf.fill())
150  m_buffers.push_back(buf);
151  }
152 
158  bool empty() const {
159  if (m_buffers.empty())
160  return true;
161  for (auto & buffer : m_buffers) {
162  if (buffer.fill())
163  return false;
164  }
165  return true;
166  }
167 
175  HT_ASSERT(m_buffers.empty() || m_buffers.back().fill() > 0);
176  return !m_buffers.empty() && *(m_buffers.back().ptr-1) != '\n';
177  }
178 
181  iterator begin() { return iterator(m_buffers, 0); }
182 
185  iterator end() { return iterator(m_buffers, m_buffers.size()); }
186 
187  private:
189  size_t m_buffer_size {};
193  std::vector<Buffer> m_buffers;
194  };
195 
197 
198 } // namespace Hypertable
199 
200 #endif // Tools_cluster_SshOutputCollector_h
void increment()
Increment to next output line.
Collects buffers of output from execution of remote SSH command.
std::vector< Buffer > m_buffers
Vector of buffers.
size_t m_next_offset
Offset within next buffer of start of next line of output.
iterator end()
Returns iterator at end of output collector.
PageArena memory allocator.
iterator begin()
Returns iterator at beginning of output collector.
size_t m_next_index
Index of buffer in m_buffers holding start of next line of output.
CharArena m_arena
Character arena from which buffers are allocated.
size_t remain() const
Returns amount of unused space remaining in buffer.
size_t size
Size of allocated buffer.
bool equal(iterator const &other) const
Equality comparison.
const std::string & dereference() const
Returns next line.
CharT * alloc(size_t sz)
Allocate sz bytes.
Definition: PageArena.h:216
#define HT_ASSERT(_e_)
Definition: Logger.h:396
void add(Buffer buf)
Adds filled buffer to collector.
Fixed-size buffer to hold a portion of output.
Logging routines and macros.
std::string m_line
Current line of output.
The PageArena allocator is simple and fast, avoiding individual mallocs/frees.
Definition: PageArena.h:69
iterator(std::vector< Buffer > &buffers, size_t index=0)
Constructor.
Iterator for traversing output line-by-line.
char * base
Pointer to beginning of buffer memory.
Buffer(char *buffer, size_t sz=0)
Constructor with allocated page argument.
Hypertable definitions
SshOutputCollector(size_t buffer_size)
Constructor.
void add(const char *data, size_t len)
Adds data to buffer.
char * ptr
Pointer to next unused position in buffer memory.
bool empty() const
Returns true if no output has been collected Returns true if there are no collected buffers or if non...
std::vector< Buffer > & m_buffers
Output buffers.
size_t fill() const
Returns amount of buffer filled.
bool last_line_is_partial()
Returns true if last line collected is partial.
size_t m_offset
Offset within current buffer of start of current line of output.
Buffer allocate_buffer()
Allocate a buffer.
size_t buffer_size()
Returns buffer size.
size_t m_index
Index of buffer in m_buffers holding start of current line of output.