MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
ExtFileHandler Class Reference

#include <extforce.h>

Inheritance diagram for ExtFileHandler:
Collaboration diagram for ExtFileHandler:

Public Member Functions

 ExtFileHandler (std::string &fin, bool bRemoveIn, std::string &fout, bool bNoClobberOut, mbsleep_t SleepTime, std::streamsize Precision)
 
virtual ~ExtFileHandler (void)
 
virtual bool Prepare_pre (void)
 
virtual void Prepare_post (bool ok)
 
virtual void AfterPredict (void)
 
virtual bool Send_pre (SendWhen when)
 
virtual void Send_post (SendWhen when)
 
virtual bool Recv_pre (void)
 
virtual bool Recv_post (void)
 
virtual std::ostream * GetOutStream (void)
 
virtual std::istream * GetInStream (void)
 
- Public Member Functions inherited from ExtFileHandlerBase
 ExtFileHandlerBase (mbsleep_t SleepTime, std::streamsize Precision)
 
virtual ~ExtFileHandlerBase (void)
 
virtual Negotiate NegotiateRequest (void) const
 
virtual int GetOutFileDes (void)
 
virtual int GetSendFlags (void) const
 
virtual int GetInFileDes (void)
 
virtual int GetRecvFlags (void) const
 

Protected Attributes

std::string fin
 
std::string fout
 
std::string tmpout
 
bool bRemoveIn
 
bool bNoClobberOut
 
std::ifstream inf
 
std::ofstream outf
 
- Protected Attributes inherited from ExtFileHandlerBase
std::streamsize Precision
 
mbsleep_t SleepTime
 
bool bOK
 

Additional Inherited Members

- Public Types inherited from ExtFileHandlerBase
enum  SendWhen { SEND_FIRST_TIME, SEND_REGULAR, SEND_AFTER_CONVERGENCE }
 
enum  Negotiate { NEGOTIATE_NO, NEGOTIATE_CLIENT, NEGOTIATE_SERVER }
 
enum  OutputFlags {
  OUTPUT_KIN = 0x1000, OUTPUT_DYN = 0x2000, OUTPUT_REF = 0x4000, OUTPUT_REF_KIN = (OUTPUT_REF | OUTPUT_KIN),
  OUTPUT_REF_DYN = (OUTPUT_REF | OUTPUT_DYN)
}
 

Detailed Description

Definition at line 112 of file extforce.h.

Constructor & Destructor Documentation

ExtFileHandler::ExtFileHandler ( std::string &  fin,
bool  bRemoveIn,
std::string &  fout,
bool  bNoClobberOut,
mbsleep_t  SleepTime,
std::streamsize  Precision 
)

Definition at line 116 of file extforce.cc.

References NO_OP.

123 fin(fin), fout(fout), tmpout(fout + ".tmp"),
125 {
126  NO_OP;
127 }
ExtFileHandlerBase(mbsleep_t SleepTime, std::streamsize Precision)
Definition: extforce.cc:50
std::string fin
Definition: extforce.h:114
#define NO_OP
Definition: myassert.h:74
bool bNoClobberOut
Definition: extforce.h:115
std::streamsize Precision
Definition: extforce.h:75
std::string tmpout
Definition: extforce.h:114
bool bRemoveIn
Definition: extforce.h:115
std::string fout
Definition: extforce.h:114
mbsleep_t SleepTime
Definition: extforce.h:76
ExtFileHandler::~ExtFileHandler ( void  )
virtual

Definition at line 129 of file extforce.cc.

References NO_OP.

130 {
131  NO_OP;
132 }
#define NO_OP
Definition: myassert.h:74

Member Function Documentation

void ExtFileHandler::AfterPredict ( void  )
virtual

Implements ExtFileHandlerBase.

Definition at line 147 of file extforce.cc.

References NO_OP.

148 {
149  NO_OP;
150 }
#define NO_OP
Definition: myassert.h:74
std::istream * ExtFileHandler::GetInStream ( void  )
virtual

Reimplemented from ExtFileHandlerBase.

Definition at line 289 of file extforce.cc.

References inf.

290 {
291  return &inf;
292 }
std::ifstream inf
Definition: extforce.h:117
std::ostream * ExtFileHandler::GetOutStream ( void  )
virtual

Reimplemented from ExtFileHandlerBase.

Definition at line 283 of file extforce.cc.

References outf.

284 {
285  return &outf;
286 }
std::ofstream outf
Definition: extforce.h:118
void ExtFileHandler::Prepare_post ( bool  ok)
virtual

Implements ExtFileHandlerBase.

Definition at line 141 of file extforce.cc.

References NO_OP.

142 {
143  NO_OP;
144 }
#define NO_OP
Definition: myassert.h:74
bool ExtFileHandler::Prepare_pre ( void  )
virtual

Implements ExtFileHandlerBase.

Definition at line 135 of file extforce.cc.

136 {
137  return true;
138 }
bool ExtFileHandler::Recv_post ( void  )
virtual

Implements ExtFileHandlerBase.

Definition at line 257 of file extforce.cc.

References bRemoveIn, fin, inf, and MBDYN_EXCEPT_ARGS.

258 {
259  inf.close();
260 
261  if (bRemoveIn) {
262  if (unlink(fin.c_str()) != 0) {
263  int save_errno = errno;
264 
265  switch (save_errno) {
266  case ENOENT:
267  break;
268 
269  default:
270  silent_cerr("unable to delete input file "
271  "\"" << fin.c_str() << "\": "
272  << strerror(save_errno) << std::endl);
274  }
275  }
276  }
277 
278  // NOTE: allow MBDyn to decide when converged
279  return true;
280 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
std::string fin
Definition: extforce.h:114
std::ifstream inf
Definition: extforce.h:117
bool bRemoveIn
Definition: extforce.h:115
bool ExtFileHandler::Recv_pre ( void  )
virtual

Implements ExtFileHandlerBase.

Definition at line 234 of file extforce.cc.

References ExtFileHandlerBase::bOK, fin, inf, mbdyn_stop_at_end_of_iteration(), mbsleep(), and ExtFileHandlerBase::SleepTime.

235 {
236  inf.open(fin.c_str());
237 
238  for (int cnt = 0; !inf; cnt++) {
239  silent_cout("input file \"" << fin.c_str() << "\" missing, "
240  "try #" << cnt << "; "
241  "sleeping " << SleepTime << " s" << std::endl);
242 
244  inf.setstate(std::ios_base::badbit);
245  return (bOK = false);
246  }
247 
248  mbsleep(&SleepTime);
249  inf.clear();
250  inf.open(fin.c_str());
251  }
252 
253  return inf.good();
254 }
int mbsleep(const mbsleep_t *t)
Definition: mbsleep.c:90
std::string fin
Definition: extforce.h:114
int mbdyn_stop_at_end_of_iteration(void)
Definition: solver.cc:172
std::ifstream inf
Definition: extforce.h:117
mbsleep_t SleepTime
Definition: extforce.h:76

Here is the call graph for this function:

void ExtFileHandler::Send_post ( SendWhen  when)
virtual

Implements ExtFileHandlerBase.

Definition at line 219 of file extforce.cc.

References fout, MBDYN_EXCEPT_ARGS, outf, and tmpout.

220 {
221  outf.close();
222  if (rename(tmpout.c_str(), fout.c_str()) != 0) {
223  int save_errno = errno;
224  silent_cerr("ExtFileHandler: unable to rename output file "
225  "\"" << tmpout.c_str() << "\" "
226  "into \"" << fout.c_str() << "\" "
227  "(" << save_errno << ": " << strerror(save_errno) << ")"
228  << std::endl);
230  }
231 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
std::string tmpout
Definition: extforce.h:114
std::ofstream outf
Definition: extforce.h:118
std::string fout
Definition: extforce.h:114
bool ExtFileHandler::Send_pre ( SendWhen  when)
virtual

Implements ExtFileHandlerBase.

Definition at line 153 of file extforce.cc.

References bNoClobberOut, fout, MBDYN_EXCEPT_ARGS, mbdyn_stop_at_end_of_iteration(), mbsleep(), outf, ExtFileHandlerBase::Precision, ExtFileHandlerBase::SleepTime, and tmpout.

154 {
155  if (bNoClobberOut) {
156  bool bKeepGoing(true);
157 
158  for (int cnt = 0; bKeepGoing; cnt++) {
159  struct stat s;
160 
161  if (stat(fout.c_str(), &s) != 0) {
162  int save_errno = errno;
163 
164  switch (save_errno) {
165  case ENOENT:
166  bKeepGoing = false;
167  break;
168 
169  default:
170  silent_cerr("unable to stat output file "
171  "\"" << fout.c_str() << "\": "
172  << strerror(save_errno)
173  << std::endl);
175  }
176 
177  } else {
179  outf.setstate(std::ios_base::badbit);
180  return false;
181  }
182 
183  if (SleepTime > 0) {
184  silent_cout("output file "
185  "\"" << fout.c_str() << "\" "
186  "still present, "
187  "try #" << cnt << "; "
188  "sleeping " << SleepTime << " s"
189  << std::endl);
190  mbsleep(&SleepTime);
191 
192  } else {
193  silent_cout("output file "
194  "\"" << fout.c_str() << "\" "
195  "still present, "
196  "try #" << cnt
197  << std::endl);
198  }
199  }
200  }
201  }
202 
203  outf.open(tmpout.c_str());
204 
205  if (!outf) {
206  silent_cerr("unable to open file \"" << fout.c_str() << "\""
207  << std::endl);
209  }
210 
211  if (Precision != 0) {
212  outf.precision(Precision);
213  }
214  outf.setf(std::ios::scientific);
215  return outf.good();
216 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
int mbsleep(const mbsleep_t *t)
Definition: mbsleep.c:90
int mbdyn_stop_at_end_of_iteration(void)
Definition: solver.cc:172
bool bNoClobberOut
Definition: extforce.h:115
std::streamsize Precision
Definition: extforce.h:75
std::string tmpout
Definition: extforce.h:114
std::ofstream outf
Definition: extforce.h:118
std::string fout
Definition: extforce.h:114
mbsleep_t SleepTime
Definition: extforce.h:76

Here is the call graph for this function:

Member Data Documentation

bool ExtFileHandler::bNoClobberOut
protected

Definition at line 115 of file extforce.h.

Referenced by Send_pre().

bool ExtFileHandler::bRemoveIn
protected

Definition at line 115 of file extforce.h.

Referenced by Recv_post().

std::string ExtFileHandler::fin
protected

Definition at line 114 of file extforce.h.

Referenced by Recv_post(), and Recv_pre().

std::string ExtFileHandler::fout
protected

Definition at line 114 of file extforce.h.

Referenced by Send_post(), and Send_pre().

std::ifstream ExtFileHandler::inf
protected

Definition at line 117 of file extforce.h.

Referenced by GetInStream(), Recv_post(), and Recv_pre().

std::ofstream ExtFileHandler::outf
protected

Definition at line 118 of file extforce.h.

Referenced by GetOutStream(), Send_post(), and Send_pre().

std::string ExtFileHandler::tmpout
protected

Definition at line 114 of file extforce.h.

Referenced by Send_post(), and Send_pre().


The documentation for this class was generated from the following files: