NCBI C++ ToolKit
automation.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: automation.hpp 89800 2020-04-23 17:05:51Z sadyrovr $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Dmitry Kazimirov
27  *
28  * File Description: Declaration of the automation processor.
29  *
30  */
31 
32 #ifndef AUTOMATION__HPP
33 #define AUTOMATION__HPP
34 
35 #include "ns_cmd_impl.hpp"
36 #include "util.hpp"
37 
39 
40 namespace NAutomation
41 {
42 
44 {
45 public:
46  enum EErrCode {
49  };
50 
51  virtual const char* GetErrCodeString(void) const override
52  {
53  switch (GetErrCode()) {
54  case eInvalidInput:
55  return "eInvalidInput";
57  return "eCommandProcessingError";
58  default:
60  }
61  }
62 
64 };
65 
67 {
70 
71  SInputOutput(const CJsonNode& message) :
72  input(message.Iterate()),
73  reply(CJsonNode::NewArrayNode())
74  {}
75 };
76 
77 class CArgument
78 {
79 public:
81  m_Value(input.GetNode())
82  {}
83 
85  m_Name(name),
88  {}
89 
90  template <typename TValue>
91  CArgument(string name, const TValue& value) :
92  m_Name(name),
95  {}
96 
97  CJsonNode Help();
98  void Exec(const string& name, CJsonIterator& input);
99  string Name() const { return m_Name; }
100  CJsonNode Value() const { return m_Value; }
101 
102  // Shortcuts
103  template <typename TInt = Int8>
104  TInt AsInteger() const { return static_cast<TInt>(m_Value.AsInteger()); }
105  string AsString() const { return m_Value.AsString(); }
106  double AsDouble() const { return m_Value.AsDouble(); }
107  bool AsBoolean() const { return m_Value.AsBoolean(); }
108 
109 private:
110  string m_Name;
114 };
115 
116 typedef initializer_list<CArgument> TArgsInit;
117 
118 struct TArguments : vector<CArgument>
119 {
121  TArguments(TArgsInit args) : vector<CArgument>(args) {}
122 
123  const CArgument& operator[](const char* name) const
124  {
125  auto found = [&](const CArgument& arg) { return name == arg.Name(); };
126  const auto result = find_if(begin(), end(), found);
127 
128  _ASSERT(result != end());
129  return *result;
130  }
131 };
132 
133 struct SCommandImpl;
134 class CCommand;
135 
136 typedef vector<CCommand> TCommands;
137 typedef function<void(const TArguments&, SInputOutput&, void*)> TCommandExecutor;
138 typedef function<void*(const string&, SInputOutput&, void*)> TCommandChecker;
139 typedef pair<TCommands, TCommandChecker> TCommandGroup;
140 
141 class CCommand
142 {
143 public:
144  CCommand(string name, TCommandExecutor exec, TArgsInit args = TArgsInit());
145  CCommand(string name, TCommandExecutor exec, const char * const args);
146  CCommand(string name, TCommands commands);
147  CCommand(string name, TCommandGroup group);
148 
150  void* Check(SInputOutput& io, void* data);
151  bool Exec(SInputOutput& io, void* data);
152 
153 private:
154  string m_Name;
155  shared_ptr<SCommandImpl> m_Impl;
156 };
157 
159 {
160  SServerAddressToJson(int which_part) : m_WhichPart(which_part) {}
161 
162  virtual CJsonNode ExecOn(CNetServer server);
163 
165 };
166 
167 typedef Int8 TObjectID;
168 
169 class CAutomationProc;
170 
172 {
173 public:
174  CAutomationObject(CAutomationProc* automation_proc) :
175  m_AutomationProc(automation_proc)
176  {
177  }
178 
179  void SetID(TObjectID object_id) {m_Id = object_id;}
180 
181  TObjectID GetID() const {return m_Id;}
182 
183  virtual const string& GetType() const = 0;
184 
186 
187  template <class TDerived>
188  static void* CheckCall(const string& name, SInputOutput& io, void* data);
189 
190  template <class TDerived>
191  static void ExecNew(const TArguments& args, SInputOutput& io, void* data);
192 
193  template <class TClass, void(TClass::*Method)(const TArguments&, SInputOutput& io)>
194  static void ExecMethod(const TArguments& args, SInputOutput& io, void* data);
195 
196 protected:
198 };
199 
201 {
203 
204  SNetServiceBase(CAutomationProc* automation_proc) :
205  CAutomationObject(automation_proc)
206  {
207  }
208 
209  virtual CNetService GetService() = 0;
211 
212  void ExecGetName(const TArguments& args, SInputOutput& io);
213  void ExecGetAddress(const TArguments& args, SInputOutput& io);
214 
215  static TCommands CallCommands();
216 };
217 
219 {
221 
222  SNetService(CAutomationProc* automation_proc) :
223  SNetServiceBase(automation_proc)
224  {
225  }
226 
227  void ExecServerInfo(const TArguments& args, SInputOutput& io);
228  void ExecExec(const TArguments& args, SInputOutput& io);
229 
230  static TCommands CallCommands();
231 };
232 
234 
236 {
237 public:
238  virtual void InputMessage(const CJsonNode&) {}
239  virtual void OutputMessage(const CJsonNode&) = 0;
240 
241  virtual ~IMessageSender() {}
242 };
243 
245 {
246 public:
247  CAutomationProc(IMessageSender* message_sender);
248 
249  bool Process(const CJsonNode& message);
250 
251  void SendWarning(const string& warn_msg, TAutomationObjectRef source);
252 
253  void SendError(const CTempString& error_message);
254 
256 
259  CNetServer::TInstance server);
260 
263  CNetServer::TInstance server);
264 
267  CNetServer::TInstance server);
268 
269  static void ExecAllowXSite(const TArguments& args, SInputOutput& io, void* data);
270 
272 
273 private:
274  CJsonNode ProcessMessage(const CJsonNode& message);
275 
277 
278  vector<TAutomationObjectRef> m_ObjectByIndex;
279 
283 
284  static void ExecExit(const TArguments& args, SInputOutput& io, void* data);
285  static void ExecDel(const TArguments& args, SInputOutput& io, void* data);
286  static void ExecVersion(const TArguments& args, SInputOutput& io, void* data);
287  static void ExecWhatIs(const TArguments& args, SInputOutput& io, void* data);
288  static void ExecEcho(const TArguments& args, SInputOutput& io, void* data);
289  static void ExecSleep(const TArguments& args, SInputOutput& io, void* data);
290  static void ExecSetContext(const TArguments& args, SInputOutput& io, void* data);
291 
292  static TCommands Commands();
293  static TCommands CallCommands();
294  static TCommands NewCommands();
295 };
296 
298 {
299  TObjectID new_object_id = m_ObjectByIndex.size();
300  new_object->SetID(new_object_id);
301  m_ObjectByIndex.push_back(new_object);
302  return new_object_id;
303 }
304 
305 template <class TDerived>
306 void* CAutomationObject::CheckCall(const string& name, SInputOutput& io, void* data)
307 {
308  _ASSERT(data);
309 
310  auto that = static_cast<CAutomationProc*>(data);
311  auto& input = io.input;
312 
313  if (!input) {
314  NCBI_THROW_FMT(CAutomationException, eInvalidInput, name << ": insufficient number of arguments");
315  }
316 
317  auto object_id = input.GetNode().AsInteger();
318  auto& object_ref = that->ObjectIdToRef(object_id);
319 
320  if (name != object_ref->GetType()) return nullptr;
321 
322  ++input;
323  return object_ref.GetPointer();
324 }
325 
326 template <class TDerived>
328 {
329  _ASSERT(data);
330 
331  auto that = static_cast<CAutomationProc*>(data);
332  auto& reply = io.reply;
333 
334  CRef<CAutomationObject> new_object;
335 
336  try {
337  new_object.Reset(TDerived::Create(args, that));
338  }
339  catch (CException& e) {
340  NCBI_THROW_FMT(CAutomationException, eCommandProcessingError,
341  "Error in '" << TDerived::kName << "' constructor: " << e.GetMsg());
342  }
343 
344  auto id = that->AddObject(new_object);
345  reply.AppendInteger(id);
346 }
347 
348 template <class TClass, void(TClass::*Method)(const TArguments&, SInputOutput& io)>
350 {
351  _ASSERT(data);
352 
353  auto that = static_cast<TClass*>(data);
354  (that->*Method)(args, io);
355 }
356 
357 }
358 
360 
361 #endif // AUTOMATION__HPP
Iterator for JSON arrays and objects.
JSON node abstraction.
Int8 AsInteger() const
Provided that this is a numeric node (that is, either an integer or a floating point node),...
const string AsString() const
Provided that this is a string node, return the string value of this node.
bool AsBoolean() const
Provided that this is a boolean node, return the boolean value of this node.
ENodeType
JSON node type.
double AsDouble() const
Provided that this is a numeric node (that is, either a floating point or an integer node),...
CNetServiceIterator Iterate(EIterationMode mode=eSortByLoad)
CObject –.
Definition: ncbiobj.hpp:180
CRef –.
Definition: ncbiobj.hpp:618
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
This class is for use by the grid_cli utility only.
double AsDouble() const
Definition: automation.hpp:106
CJsonNode Value() const
Definition: automation.hpp:100
CJsonNode m_TypeOrDefaultValue
Definition: automation.hpp:111
void Exec(const string &name, CJsonIterator &input)
Definition: automation.cpp:112
string AsString() const
Definition: automation.hpp:105
CArgument(string name, CJsonNode::ENodeType type)
Definition: automation.hpp:84
bool AsBoolean() const
Definition: automation.hpp:107
string Name() const
Definition: automation.hpp:99
CArgument(string name, const TValue &value)
Definition: automation.hpp:91
CArgument(CJsonIterator &input)
Definition: automation.hpp:80
TInt AsInteger() const
Definition: automation.hpp:104
NCBI_EXCEPTION_DEFAULT(CAutomationException, CException)
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
Definition: automation.hpp:51
static void ExecNew(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.hpp:327
void SetID(TObjectID object_id)
Definition: automation.hpp:179
static void ExecMethod(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.hpp:349
CAutomationObject(CAutomationProc *automation_proc)
Definition: automation.hpp:174
virtual const string & GetType() const =0
static void * CheckCall(const string &name, SInputOutput &io, void *data)
Definition: automation.hpp:306
CAutomationProc * m_AutomationProc
Definition: automation.hpp:185
static TCommands Commands()
Definition: automation.cpp:437
static void ExecVersion(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.cpp:485
static void ExecDel(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.cpp:474
vector< TAutomationObjectRef > m_ObjectByIndex
Definition: automation.hpp:278
static TCommands NewCommands()
Definition: automation.cpp:423
static void ExecEcho(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.cpp:507
TObjectID AddObject(TAutomationObjectRef new_object)
Definition: automation.hpp:297
static void ExecSleep(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.cpp:513
static TCommands CallCommands()
Definition: automation.cpp:407
void SendWarning(const string &warn_msg, TAutomationObjectRef source)
Definition: automation.cpp:584
TAutomationObjectRef ReturnNetScheduleServerObject(CNetScheduleAPI::TInstance ns_api, CNetServer::TInstance server)
bool Process(const CJsonNode &message)
Definition: automation.cpp:572
void SendError(const CTempString &error_message)
Definition: automation.cpp:595
static void ExecExit(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.cpp:468
static void ExecWhatIs(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.cpp:492
TAutomationObjectRef ReturnNetStorageServerObject(CNetStorageAdmin::TInstance nst_api, CNetServer::TInstance server)
TAutomationObjectRef & ObjectIdToRef(TObjectID object_id)
Definition: automation.cpp:603
TAutomationObjectRef ReturnNetCacheServerObject(CNetICacheClient::TInstance ic_api, CNetServer::TInstance server)
static void ExecAllowXSite(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.cpp:535
CJsonNode ProcessMessage(const CJsonNode &message)
Definition: automation.cpp:542
IMessageSender * m_MessageSender
Definition: automation.hpp:276
CAutomationProc(IMessageSender *message_sender)
Definition: automation.cpp:399
static void ExecSetContext(const TArguments &args, SInputOutput &io, void *data)
Definition: automation.cpp:521
void * Check(SInputOutput &io, void *data)
Definition: automation.cpp:312
shared_ptr< SCommandImpl > m_Impl
Definition: automation.hpp:155
bool Exec(SInputOutput &io, void *data)
Definition: automation.cpp:317
CJsonNode Help(CJsonIterator &input)
Definition: automation.cpp:297
CCommand(string name, TCommandExecutor exec, TArgsInit args=TArgsInit())
Definition: automation.cpp:274
virtual void OutputMessage(const CJsonNode &)=0
virtual void InputMessage(const CJsonNode &)
Definition: automation.hpp:238
#define true
Definition: bool.h:35
#define false
Definition: bool.h:36
char data[12]
Definition: iconv.c:80
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
#define NCBI_THROW_FMT(exception_class, err_code, message)
The same as NCBI_THROW but with message processed as output to ostream.
Definition: ncbiexpt.hpp:719
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static int input()
pair< TCommands, TCommandChecker > TCommandGroup
Definition: automation.hpp:139
initializer_list< CArgument > TArgsInit
Definition: automation.hpp:116
CRef< CAutomationObject > TAutomationObjectRef
Definition: automation.hpp:233
vector< CCommand > TCommands
Definition: automation.hpp:134
function< void(const TArguments &, SInputOutput &, void *)> TCommandExecutor
Definition: automation.hpp:137
function< void *(const string &, SInputOutput &, void *)> TCommandChecker
Definition: automation.hpp:138
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
const CharType(& source)[N]
Definition: pointer.h:1149
CSeq_id_Mapper TInstance
static const bool kName
SInputOutput(const CJsonNode &message)
Definition: automation.hpp:71
void ExecGetName(const TArguments &args, SInputOutput &io)
Definition: automation.cpp:350
void ExecGetAddress(const TArguments &args, SInputOutput &io)
Definition: automation.cpp:356
virtual CNetService GetService()=0
SNetServiceBase(CAutomationProc *automation_proc)
Definition: automation.hpp:204
static TCommands CallCommands()
Definition: automation.cpp:333
SNetService(CAutomationProc *automation_proc)
Definition: automation.hpp:222
void ExecExec(const TArguments &args, SInputOutput &io)
Definition: automation.cpp:389
static TCommands CallCommands()
Definition: automation.cpp:366
void ExecServerInfo(const TArguments &args, SInputOutput &io)
Definition: automation.cpp:383
virtual CJsonNode ExecOn(CNetServer server)
Definition: automation.cpp:322
const CArgument & operator[](const char *name) const
Definition: automation.hpp:123
TArguments(TArgsInit args)
Definition: automation.hpp:121
Definition: type.c:6
#define _ASSERT
else result
Definition: token2.c:20
Modified on Fri Sep 20 14:57:48 2024 by modify_doxy.py rev. 669887