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

Go to the SVN repository for this file.

1 #ifndef CONNECT_SERVICES__REMOTE_APP_MB_HPP
2 #define CONNECT_SERVICES__REMOTE_APP_MB_HPP
3 
4 /* $Id: remote_app.hpp 86218 2019-04-18 16:59:46Z sadyrovr $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Maxim Didneko, Dmitry Kazimirov
30  *
31  * File Description:
32  *
33  */
34 
37 
38 #include <connect/connect_export.h>
39 
40 #include <corelib/ncbimisc.hpp>
41 #include <corelib/ncbiobj.hpp>
42 #include <corelib/ncbistre.hpp>
43 
44 #include <vector>
45 
47 
48 const size_t kMaxBlobInlineSize = 500;
49 
53 };
54 
56 {
57 public:
59  string& data, size_t& data_size) :
60  m_Storage(storage), m_Data(&data), m_DataSize(&data_size)
61  {
62  }
63 
65 
66  CNcbiOstream& GetOStream(const string& fname = "",
68  size_t max_inline_size = kMaxBlobInlineSize);
69 
70  CNcbiIstream& GetIStream(string* fname = NULL,
72  void Reset();
73 
74 protected:
75  static int x_GetTypeAndName(CNcbiIstream& istream, string& name);
76 
77 private:
81  string* m_Data;
82  size_t* m_DataSize;
83 };
84 
85 
86 /// Remote Application Request (both client side and application executor side)
87 ///
88 /// It is used by a client application which wants to run a remote application
89 /// through NetSchedule infrastructure and should be used in conjunction with
90 /// CGridJobSubmitter class
91 ///
92 /// It is also used by a grid worker node to get parameters
93 /// for the remote application.
94 ///
96 {
97 public:
99  size_t max_inline_size = kMaxBlobInlineSize);
100 
102 
103  /// Set the command line for the remote application.
104  /// The command line must not contain the remote program
105  /// name -- only its arguments.
106  void SetCmdLine(const string& cmdline) { m_CmdLine = cmdline; }
107  /// Get the command line of the remote application.
108  const string& GetCmdLine() const { return m_CmdLine; }
109 
110  void SetAppRunTimeout(unsigned int sec) { m_AppRunTimeout = sec; }
111  unsigned int GetAppRunTimeout() const { return m_AppRunTimeout; }
112 
113  /// Transfer a file to an application executor side.
114  /// It only makes sense to transfer a file if its name also mentioned in
115  /// the command line for the remote application. When the file is transfered
116  /// the the executor side it gets stored to a temporary directory and then its
117  /// original name in the command line will be replaced with the new temporary name.
118  void AddFileForTransfer(const string& fname,
120  {
121  m_Files[fname] = tt;
122  }
123  const string& GetWorkingDir() const { return m_TmpDirName; }
124 
125  /// Get an output stream to write data to a remote application stdin
127  {
128  return m_StdIn.GetOStream("", eBlobStorage, m_MaxInlineSize);
129  }
130  /// Get the stdin stream of the remote application.
132  {
133  return m_StdIn.GetIStream();
134  }
135 
136  void SetExclusiveMode(bool on_off) { m_ExlusiveMode = on_off; }
137  bool IsExclusiveMode() const { return m_ExlusiveMode; }
138 
139 
140  void SetStdOutErrFileNames(const string& stdout_fname,
141  const string& stderr_fname,
143  {
144  m_StdOutFileName = stdout_fname;
145  m_StdErrFileName = stderr_fname;
146  m_StorageType = type;
147  }
148 
149  const string& GetStdOutFileName() const { return m_StdOutFileName; }
150  const string& GetStdErrFileName() const { return m_StdErrFileName; }
152  { return m_StorageType; }
153 
154  const string& GetInBlobIdOrData() const { return m_InBlobIdOrData; }
155 
156  void SetMaxInlineSize(size_t max_inline_size) { m_MaxInlineSize = max_inline_size; }
157 
158  /// Serialize a request to a given stream. After call to this method the instance
159  /// cleans itself an it can be reused.
160  void Send(CNcbiOstream& os);
161  void Deserialize(CNcbiIstream& is) { x_Deserialize(is); }
162 
163  void Reset();
164 
165 protected:
167 
168  CNetCacheAPI& GetNetCacheAPI() { return m_NetCacheAPI; }
169  const TFiles& GetFileNames() const { return m_Files; }
170 
171  void x_CreateWDir();
172  void x_RemoveWDir();
173 
175  bool x_Deserialize(CNcbiIstream& is, TStoredFiles* files = NULL);
176 
177 private:
179 
181  string m_CmdLine;
182  unsigned int m_AppRunTimeout;
183 
184  string m_TmpDirPath;
185  string m_TmpDirName;
187 
190 
192 
198 };
199 
200 /// Remote Application Result (both client side and application executor side)
201 ///
202 /// It is used by a grid worker node to send results of a
203 /// finished remote application to the client.
204 ///
205 /// It is also used by the client application to get the job results
206 /// and should be used in conjunction with CGridJobStatus
207 ///
209 {
210 public:
212  size_t max_inline_size = kMaxBlobInlineSize) :
213  m_NetCacheAPI(netcache_api),
214  m_RetCode(-1),
215  m_StdOut(netcache_api, m_OutBlobIdOrData, m_OutBlobSize),
216  m_OutBlobSize(0),
217  m_StdErr(netcache_api, m_ErrBlobIdOrData, m_ErrBlobSize),
218  m_ErrBlobSize(0),
219  m_StorageType(eBlobStorage),
220  m_MaxInlineSize(max_inline_size)
221  {
222  }
223  ~CRemoteAppResult();
224 
225  /// Get a stream to put remote application's stdout to
227  {
228  return m_StdOut.GetOStream(m_StdOutFileName,
229  m_StorageType, m_MaxInlineSize);
230  }
231  /// Get a remote application stdout
233  {
234  return m_StdOut.GetIStream(&m_StdOutFileName, &m_StorageType);
235  }
236 
238  {
239  return m_StdErr.GetOStream(m_StdErrFileName,
240  m_StorageType, m_MaxInlineSize);
241  }
242  /// Get a remote application stderr
244  {
245  return m_StdErr.GetIStream(&m_StdErrFileName, &m_StorageType);
246  }
247 
248  void SetRetCode(int ret_code) { m_RetCode = ret_code; }
249  int GetRetCode() const { return m_RetCode; }
250 
251  void Serialize(CNcbiOstream& os);
252  /// Deserialize a request from a given stream.
253  void Receive(CNcbiIstream& is);
254 
255  void Reset();
256 
257  void SetStdOutErrFileNames(const string& stdout_fname,
258  const string& stderr_fname,
260  {
261  m_StdOutFileName = stdout_fname;
262  m_StdErrFileName = stderr_fname;
263  m_StorageType = type;
264  }
265  const string& GetStdOutFileName() const { return m_StdOutFileName; }
266  const string& GetStdErrFileName() const { return m_StdErrFileName; }
268  { return m_StorageType; }
269  const string& GetOutBlobIdOrData() const { return m_OutBlobIdOrData; }
270  const string& GetErrBlobIdOrData() const { return m_ErrBlobIdOrData; }
271 
272  void SetMaxInlineSize(size_t max_inline_size)
273  { m_MaxInlineSize = max_inline_size; }
274 
275 private:
278 
283 
290 };
291 
292 
294 void TokenizeCmdLine(const string& cmdline, vector<string>& args);
295 
297 string JoinCmdLine(const vector<string>& args);
298 
299 
301 
302 #endif // CONNECT_SERVICES__REMOTE_APP_MB_HPP
CAtomicCounter –.
Definition: ncbicntr.hpp:71
SGridRead m_GridRead
Definition: remote_app.hpp:79
size_t * m_DataSize
Definition: remote_app.hpp:82
CNetCacheAPI m_Storage
Definition: remote_app.hpp:78
SGridWrite m_GridWrite
Definition: remote_app.hpp:80
CBlobStreamHelper(CNetCacheAPI::TInstance storage, string &data, size_t &data_size)
Definition: remote_app.hpp:58
Client API for NetCache server.
Remote Application Request (both client side and application executor side)
Definition: remote_app.hpp:96
CNetCacheAPI m_NetCacheAPI
Definition: remote_app.hpp:180
CNcbiIstream & GetStdInForRead()
Get the stdin stream of the remote application.
Definition: remote_app.hpp:131
EStdOutErrStorageType m_StorageType
Definition: remote_app.hpp:195
const TFiles & GetFileNames() const
Definition: remote_app.hpp:169
CNetCacheAPI & GetNetCacheAPI()
Definition: remote_app.hpp:168
bool IsExclusiveMode() const
Definition: remote_app.hpp:137
const string & GetWorkingDir() const
Definition: remote_app.hpp:123
void SetExclusiveMode(bool on_off)
Definition: remote_app.hpp:136
void SetMaxInlineSize(size_t max_inline_size)
Definition: remote_app.hpp:156
void SetStdOutErrFileNames(const string &stdout_fname, const string &stderr_fname, EStdOutErrStorageType type)
Definition: remote_app.hpp:140
void AddFileForTransfer(const string &fname, EStdOutErrStorageType tt=eBlobStorage)
Transfer a file to an application executor side.
Definition: remote_app.hpp:118
const string & GetStdOutFileName() const
Definition: remote_app.hpp:149
CBlobStreamHelper m_StdIn
Definition: remote_app.hpp:188
const string & GetInBlobIdOrData() const
Definition: remote_app.hpp:154
map< string, EStdOutErrStorageType > TFiles
Definition: remote_app.hpp:166
static CAtomicCounter sm_DirCounter
Definition: remote_app.hpp:178
void Deserialize(CNcbiIstream &is)
Definition: remote_app.hpp:161
void SetAppRunTimeout(unsigned int sec)
Definition: remote_app.hpp:110
EStdOutErrStorageType GetStdOutErrStorageType() const
Definition: remote_app.hpp:151
const string & GetCmdLine() const
Get the command line of the remote application.
Definition: remote_app.hpp:108
CNcbiOstream & GetStdIn()
Get an output stream to write data to a remote application stdin.
Definition: remote_app.hpp:126
const string & GetStdErrFileName() const
Definition: remote_app.hpp:150
unsigned int GetAppRunTimeout() const
Definition: remote_app.hpp:111
unsigned int m_AppRunTimeout
Definition: remote_app.hpp:182
map< string, string > TStoredFiles
Definition: remote_app.hpp:174
void SetCmdLine(const string &cmdline)
Set the command line for the remote application.
Definition: remote_app.hpp:106
Remote Application Result (both client side and application executor side)
Definition: remote_app.hpp:209
CBlobStreamHelper m_StdErr
Definition: remote_app.hpp:284
void SetRetCode(int ret_code)
Definition: remote_app.hpp:248
CNcbiOstream & GetStdOutForWrite()
Get a stream to put remote application's stdout to.
Definition: remote_app.hpp:226
const string & GetErrBlobIdOrData() const
Definition: remote_app.hpp:270
string m_ErrBlobIdOrData
Definition: remote_app.hpp:285
int GetRetCode() const
Definition: remote_app.hpp:249
CNcbiIstream & GetStdErr()
Get a remote application stderr.
Definition: remote_app.hpp:243
CNetCacheAPI m_NetCacheAPI
Definition: remote_app.hpp:276
const string & GetStdOutFileName() const
Definition: remote_app.hpp:265
EStdOutErrStorageType GetStdOutErrStorageType() const
Definition: remote_app.hpp:267
size_t m_MaxInlineSize
Definition: remote_app.hpp:289
CNcbiIstream & GetStdOut()
Get a remote application stdout.
Definition: remote_app.hpp:232
void SetStdOutErrFileNames(const string &stdout_fname, const string &stderr_fname, EStdOutErrStorageType type)
Definition: remote_app.hpp:257
string m_OutBlobIdOrData
Definition: remote_app.hpp:280
CRemoteAppResult(CNetCacheAPI::TInstance netcache_api, size_t max_inline_size=kMaxBlobInlineSize)
Definition: remote_app.hpp:211
const string & GetStdErrFileName() const
Definition: remote_app.hpp:266
const string & GetOutBlobIdOrData() const
Definition: remote_app.hpp:269
string m_StdErrFileName
Definition: remote_app.hpp:287
string m_StdOutFileName
Definition: remote_app.hpp:282
void SetMaxInlineSize(size_t max_inline_size)
Definition: remote_app.hpp:272
EStdOutErrStorageType m_StorageType
Definition: remote_app.hpp:288
CNcbiOstream & GetStdErrForWrite()
Definition: remote_app.hpp:237
CBlobStreamHelper m_StdOut
Definition: remote_app.hpp:279
static int type
Definition: getdata.c:31
char data[12]
Definition: iconv.c:80
#define NULL
Definition: ncbistd.hpp:225
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
#define NCBI_XCONNECT_EXPORT
Miscellaneous common-use basic types and functionality.
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
NetCache client specs.
const size_t kMaxBlobInlineSize
Definition: remote_app.hpp:48
EStdOutErrStorageType
Definition: remote_app.hpp:50
@ eLocalFile
Definition: remote_app.hpp:51
@ eBlobStorage
Definition: remote_app.hpp:52
string JoinCmdLine(const vector< string > &args)
Definition: remote_app.cpp:412
void TokenizeCmdLine(const string &cmdline, vector< string > &args)
Definition: remote_app.cpp:381
Definition: type.c:6
void Serialize(CNcbiOstream &, const CRawScoreVector< Key, Score > &)
Generics These throw an exception; we must implement serialization for each type.
unique_ptr< CObjectIStream > GetIStream(string path, ESerialDataFormat serial_format)
Modified on Wed Apr 24 14:13:35 2024 by modify_doxy.py rev. 669887