42 #include <sys/types.h>
51 std::streamsize Precision)
52 : Precision(Precision), SleepTime(SleepTime), bOK(
true)
121 std::streamsize Precision)
123 fin(fin), fout(fout), tmpout(fout +
".tmp"),
124 bRemoveIn(bRemoveIn), bNoClobberOut(bNoClobberOut)
156 bool bKeepGoing(
true);
158 for (
int cnt = 0; bKeepGoing; cnt++) {
161 if (stat(
fout.c_str(), &s) != 0) {
162 int save_errno = errno;
164 switch (save_errno) {
170 silent_cerr(
"unable to stat output file "
171 "\"" <<
fout.c_str() <<
"\": "
172 << strerror(save_errno)
179 outf.setstate(std::ios_base::badbit);
184 silent_cout(
"output file "
185 "\"" <<
fout.c_str() <<
"\" "
187 "try #" << cnt <<
"; "
193 silent_cout(
"output file "
194 "\"" <<
fout.c_str() <<
"\" "
206 silent_cerr(
"unable to open file \"" <<
fout.c_str() <<
"\""
214 outf.setf(std::ios::scientific);
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) <<
")"
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);
244 inf.setstate(std::ios_base::badbit);
245 return (
bOK =
false);
262 if (unlink(
fin.c_str()) != 0) {
263 int save_errno = errno;
265 switch (save_errno) {
270 silent_cerr(
"unable to delete input file "
271 "\"" <<
fin.c_str() <<
"\": "
272 << strerror(save_errno) << std::endl);
301 ExtSocketHandler::u2cmd(
unsigned u)
const
314 static const char *ESCmd2str[] = {
321 "REGULAR_DATA_AND_GOTO_NEXT_STEP",
332 return ESCmd2str[cmd];
335 ExtSocketHandler::ExtSocketHandler(UseSocket *pUS,
mbsleep_t SleepTime,
336 int recv_flags,
int send_flags)
338 pUS(pUS), recv_flags(recv_flags), send_flags(send_flags),
344 ExtSocketHandler::~ExtSocketHandler(
void)
346 if (pUS->GetSock() >= 0) {
350 (void)send(pUS->GetSock(), (
void *)&u,
sizeof(u), send_flags);
356 ExtSocketHandler::NegotiateRequest(
void)
const
367 ExtSocketHandler::Prepare_pre(
void)
372 switch (NegotiateRequest()) {
376 rc = send(pUS->GetSock(), (
void *)&u,
sizeof(u), send_flags);
378 int save_errno = errno;
379 silent_cerr(
"ExtSocketHandler: negotiation request send() failed "
380 "(" << save_errno <<
": " << strerror(save_errno) <<
")"
382 return (bOK =
false);
384 }
else if (rc !=
sizeof(u)) {
385 silent_cerr(
"ExtSocketHandler: negotiation request send() failed "
386 "(sent " << rc <<
" bytes "
387 "instead of " <<
sizeof(u) <<
")"
389 return (bOK =
false);
394 rc = recv(pUS->GetSock(), (
void *)&u,
sizeof(u), recv_flags);
396 return (bOK =
false);
398 }
else if (rc !=
sizeof(u)) {
399 return (bOK =
false);
403 silent_cerr(
"ExtSocketHandler: negotiation request recv() failed"
405 return (bOK =
false);
417 ExtSocketHandler::Prepare_post(
bool ok)
419 if (NegotiateRequest()) {
422 ssize_t rc = send(pUS->GetSock(), (
void *)&u,
sizeof(u),
425 int save_errno = errno;
426 silent_cerr(
"ExtSocketHandler: negotiation response send() failed "
427 "(" << save_errno <<
": " << strerror(save_errno) <<
")"
431 }
else if (rc !=
sizeof(u)) {
432 silent_cerr(
"ExtSocketHandler: negotiation response send() failed "
433 "(sent " << rc <<
" bytes "
434 "instead of " <<
sizeof(u) <<
")"
441 ssize_t rc = recv(pUS->GetSock(), (
void *)&u,
sizeof(u),
444 int save_errno = errno;
445 silent_cerr(
"ExtSocketHandler: negotiation response recv() failed "
446 "(" << save_errno <<
": " << strerror(save_errno) <<
")"
450 }
else if (rc !=
sizeof(u)) {
451 silent_cerr(
"ExtSocketHandler: negotiation response recv() failed "
452 "(received " << rc <<
" bytes "
453 "instead of " <<
sizeof(u) <<
")"
466 ExtSocketHandler::AfterPredict(
void)
468 bLastReadForce =
false;
473 ExtSocketHandler::Send_pre(SendWhen when)
475 if (!bReadForces || !bOK) {
480 if (when == SEND_AFTER_CONVERGENCE) {
485 ssize_t rc = send(pUS->GetSock(), (
void *)&u,
sizeof(u), send_flags);
487 int save_errno = errno;
488 silent_cerr(
"ExtSocketHandler: send() failed "
489 "(" << save_errno <<
": " << strerror(save_errno) <<
")"
492 return (bOK =
false);
494 }
else if (rc !=
sizeof(u)) {
495 silent_cerr(
"ExtSocketHandler: send() failed "
496 "(sent " << rc <<
" bytes "
497 "instead of " <<
sizeof(u) <<
")"
500 return (bOK =
false);
507 ExtSocketHandler::Send_post(SendWhen when)
510 if (when == SEND_AFTER_CONVERGENCE) {
518 ExtSocketHandler::Recv_pre(
void)
526 if (SleepTime != 0) {
530 rc = recv(pUS->GetSock(), (
void *)&u,
sizeof(u),
531 recv_flags | MSG_DONTWAIT);
536 int save_errno = errno;
538 if (errno != EAGAIN) {
539 silent_cerr(
"ExtSocketHandler: "
540 "recv() failed (" << save_errno <<
": "
541 << strerror(save_errno) <<
")"
544 return (bOK =
false);
548 return (bOK =
false);
556 ssize_t rc = recv(pUS->GetSock(), (
void *)&u,
sizeof(u), recv_flags);
558 int save_errno = errno;
560 if (errno != EAGAIN) {
561 silent_cerr(
"ExtSocketHandler: "
562 "recv() failed (" << save_errno <<
": "
563 << strerror(save_errno) <<
")"
566 return (bOK =
false);
569 }
else if (rc !=
sizeof(u)) {
570 silent_cerr(
"ExtSocketHandler: "
571 "recv()=" << rc <<
" (expected " <<
sizeof(u) <<
")"
574 return (bOK =
false);
581 silent_cerr(
"ExtSocketHandler: "
582 "received unknown code (" <<
unsigned(u) <<
")"
585 return (bOK =
false);
596 silent_cout(
"ExtSocketHandler: peer requested end of simulation"
604 bLastReadForce =
true;
615 ExtSocketHandler::Recv_post(
void)
617 if (bLastReadForce) {
625 ExtSocketHandler::GetOutFileDes(
void)
627 return pUS->GetSock();
631 ExtSocketHandler::GetSendFlags(
void)
const
637 ExtSocketHandler::GetInFileDes(
void)
639 return pUS->GetSock();
643 ExtSocketHandler::GetRecvFlags(
void)
const
680 bool bSendAfterPredict,
685 c(iCoupling > COUPLING_LOOSE ? pDM : NULL),
687 bSendAfterPredict(bSendAfterPredict),
688 iCoupling(iCoupling),
849 std::streamsize& Precision)
854 if (SleepTime < MinSleepTime ) {
855 silent_cerr(
"ExtForce(" << uLabel <<
"): "
856 "invalid sleep time " << SleepTime
857 <<
" less than " << MinSleepTime
869 silent_cerr(
"ExtForce(" << uLabel <<
"): "
870 "invalid precision value "
871 "\"" << Precision <<
"\""
889 bool bGotCreate(
false);
891 unsigned short int port = (
unsigned short int)-1;
898 silent_cerr(
"ExtSocketHandler"
899 "(" << uLabel <<
"): "
900 "\"create\" must be either \"yes\" or \"no\" "
911 silent_cerr(
"ExtSocketHandler"
912 "(" << uLabel <<
"): "
913 "unable to read local path "
924 silent_cerr(
"ExtSocketHandler"
925 "(" << uLabel <<
"): "
926 "cannot specify port "
927 "for a local socket "
935 #ifdef IPPORT_USERRESERVED
937 silent_cerr(
"ExtSocketHandler"
938 "(" << uLabel <<
"): "
939 "cannot listen on reserved port "
940 << port <<
": less than "
955 silent_cerr(
"ExtSocketHandler"
956 "(" << uLabel <<
"): "
957 "cannot specify host for a local socket "
967 silent_cerr(
"ExtSocketHandler"
968 "(" << uLabel <<
"): "
969 "unable to read host "
977 }
else if (path.empty() && !bCreate) {
978 silent_cerr(
"ExtSocketHandler"
979 "(" << uLabel <<
"): "
986 int socket_type = SOCK_STREAM;
989 socket_type = SOCK_DGRAM;
996 silent_cerr(
"ExtSocketHandler(" << uLabel <<
"\"): "
997 "invalid socket type "
1003 if ((socket_type == SOCK_DGRAM) && !bCreate) {
1004 silent_cerr(
"ExtSocketHandler(" << uLabel <<
"\"): "
1005 "socket type=udp incompatible with create=no "
1014 recv_flags |= MSG_WAITALL;
1015 #endif // MSG_WAITALL
1017 while (HP.
IsArg()) {
1022 #else // ! MSG_NOSIGNAL
1023 silent_cout(
"ExtSocketHandler"
1024 "(" << uLabel <<
"): "
1025 "MSG_NOSIGNAL not defined (ignored) "
1028 #endif // ! MSG_NOSIGNAL
1031 }
else if (HP.
IsKeyWord(
"no" "signal")) {
1035 #else // ! MSG_NOSIGNAL
1036 silent_cout(
"ExtSocketHandler"
1037 "(" << uLabel <<
"): "
1038 "MSG_NOSIGNAL not defined (ignored) "
1041 #endif // ! MSG_NOSIGNAL
1050 if (port == (
unsigned short int)(-1)) {
1051 silent_cerr(
"ExtSocketHandler"
1052 "(" << uLabel <<
"): "
1064 pDM->RegisterSocketUser(pUS);
1071 std::streamsize Precision = 0;
1076 ExtSocketHandler(pUS, SleepTime, recv_flags, send_flags));
1079 #else // ! USE_SOCKET
1080 silent_cerr(
"ExtSocketHandler not supported" << std::endl);
1082 #endif // ! USE_SOCKET
1089 unsigned int uLabel)
1103 silent_cerr(
"ExtForce(" << uLabel <<
"): "
1104 "unable to get input file name "
1108 std::string fin = s;
1110 bool bUnlinkIn =
false;
1117 silent_cerr(
"ExtForce(" << uLabel <<
"): "
1118 "unable to get output file name "
1122 std::string fout = s;
1124 bool bNoClobberOut =
false;
1126 bNoClobberOut =
true;
1130 std::streamsize Precision = 0;
1135 SleepTime, Precision));
1143 unsigned int uLabel,
1145 bool& bSendAfterPredict,
1166 if (iCoupling < -1) {
1167 silent_cerr(
"ExtForce(" << uLabel <<
"): "
1168 "invalid coupling value "
1169 "\"" << iCoupling <<
"\""
1178 bSendAfterPredict =
true;
1179 if (HP.
IsKeyWord(
"send" "after" "predict")) {
1180 if (!HP.
GetYesNo(bSendAfterPredict)) {
1181 silent_cerr(
"ExtForce(" << uLabel <<
"): "
1182 "invalud \"send after predict\" value "
1190 bSendAfterPredict =
false;
virtual bool Send_pre(SendWhen when)
virtual std::ostream * GetOutStream(void)
virtual SubVectorHandler & InitialAssRes(SubVectorHandler &WorkVec, const VectorHandler &XCurr)
virtual std::istream * GetInStream(void)
virtual void AfterPredict(void)=0
#define MBDYN_EXCEPT_ARGS
virtual int GetInFileDes(void)
virtual int GetOutFileDes(void)
virtual void ResizeReset(integer)
virtual integer GetInt(integer iDefval=0)
virtual void Prepare_post(bool ok)
virtual bool Recv_post(void)
virtual ~ExtFileHandlerBase(void)
int mbsleep(const mbsleep_t *t)
ExtFileHandlerBase(mbsleep_t SleepTime, std::streamsize Precision)
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
virtual void Set(Converged::State s)
ExtFileHandler(std::string &fin, bool bRemoveIn, std::string &fout, bool bNoClobberOut, mbsleep_t SleepTime, std::streamsize Precision)
std::vector< Hint * > Hints
virtual bool Recv_pre(void)=0
virtual std::istream * GetInStream(void)
ExtForce(unsigned int uL, DataManager *pDM, ExtFileHandlerBase *pEFH, bool bSendAfterPredict, int iCoupling, flag fOut)
int mbdyn_stop_at_end_of_iteration(void)
virtual Negotiate NegotiateRequest(void) const
static ExtFileHandlerBase * ReadExtSocketHandler(DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
virtual ~ExtFileHandler(void)
virtual bool Prepare_pre(void)
virtual bool Prepare(ExtFileHandlerBase *pEFH)=0
ExtFileHandlerBase * ReadExtFileHandlerEDGE(DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
virtual bool IsKeyWord(const char *sKeyWord)
virtual bool Send_pre(SendWhen when)=0
void Send(ExtFileHandlerBase::SendWhen when)
virtual int GetRecvFlags(void) const
ExtFileHandlerBase * pEFH
virtual void SetValue(DataManager *pDM, VectorHandler &X, VectorHandler &XP, SimulationEntity::Hints *h=0)
virtual const char * GetStringWithDelims(enum Delims Del=DEFAULTDELIM, bool escape=true)
mbsleep_t mbsleep_init(long t)
std::streamsize Precision
void mbdyn_set_stop_at_end_of_iteration(void)
static const char * cmd2str(int cmd)
static ExtFileHandlerBase * ReadExtFileHandler(DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
virtual mbsleep_t GetTimeout(const mbsleep_t &DefVal)
#define ASSERT(expression)
virtual std::ostream * GetOutStream(void)
virtual bool Recv_post(void)=0
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
virtual void Send_post(SendWhen when)
virtual bool Prepare_pre(void)=0
static std::stack< cleanup * > c
virtual void InitialWorkSpaceDim(integer *piNumRows, integer *piNumCols) const
virtual bool GetYesNo(bool &bRet)
virtual void Send_post(SendWhen when)=0
virtual bool Recv_pre(void)
virtual void Prepare_post(bool ok)=0
virtual int GetSendFlags(void) const
#define IPPORT_USERRESERVED
virtual void Update(const VectorHandler &XCurr, const VectorHandler &XPrimeCurr)
virtual void AfterPredict(void)
virtual HighParser::ErrOut GetLineData(void) const
virtual VariableSubMatrixHandler & InitialAssJac(VariableSubMatrixHandler &WorkMat, const VectorHandler &XCurr)
void ReadExtForce(DataManager *pDM, MBDynParser &HP, unsigned int uLabel, ExtFileHandlerBase *&pEFH, bool &bSendAfterPredict, int &iCoupling)
void mbdyn_set_stop_at_end_of_time_step(void)
void ReadExtFileParams(DataManager *pDM, MBDynParser &HP, unsigned int uLabel, mbsleep_t &SleepTime, std::streamsize &Precision)
virtual void AfterConvergence(const VectorHandler &X, const VectorHandler &XP)
virtual void AfterPredict(VectorHandler &X, VectorHandler &XP)