MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
extforce.cc File Reference
#include "mbconfig.h"
#include "dataman.h"
#include "extforce.h"
#include "extedge.h"
#include "except.h"
#include "solver.h"
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstdlib>
#include <unistd.h>
#include <cerrno>
Include dependency graph for extforce.cc:

Go to the source code of this file.

Functions

void ReadExtFileParams (DataManager *pDM, MBDynParser &HP, unsigned int uLabel, mbsleep_t &SleepTime, std::streamsize &Precision)
 
static ExtFileHandlerBaseReadExtSocketHandler (DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
 
static ExtFileHandlerBaseReadExtFileHandler (DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
 
void ReadExtForce (DataManager *pDM, MBDynParser &HP, unsigned int uLabel, ExtFileHandlerBase *&pEFH, bool &bSendAfterPredict, int &iCoupling)
 

Function Documentation

static ExtFileHandlerBase* ReadExtFileHandler ( DataManager pDM,
MBDynParser HP,
unsigned int  uLabel 
)
static

Definition at line 1087 of file extforce.cc.

References IncludeParser::GetFileName(), IncludeParser::GetLineData(), HighParser::IsKeyWord(), MBDYN_EXCEPT_ARGS, mbsleep_init(), ReadExtFileHandlerEDGE(), ReadExtFileParams(), ReadExtSocketHandler(), and SAFENEWWITHCONSTRUCTOR.

Referenced by ReadExtForce().

1090 {
1091  if (HP.IsKeyWord("EDGE")) {
1092  return ReadExtFileHandlerEDGE(pDM, HP, uLabel);
1093 
1094  } else if (HP.IsKeyWord("socket")) {
1095  return ReadExtSocketHandler(pDM, HP, uLabel);
1096  }
1097 
1098  ExtFileHandlerBase *pEFH = 0;
1099 
1100  // default
1101  const char *s = HP.GetFileName();
1102  if (s == 0) {
1103  silent_cerr("ExtForce(" << uLabel << "): "
1104  "unable to get input file name "
1105  "at line " << HP.GetLineData() << std::endl);
1107  }
1108  std::string fin = s;
1109 
1110  bool bUnlinkIn = false;
1111  if (HP.IsKeyWord("unlink")) {
1112  bUnlinkIn = true;
1113  }
1114 
1115  s = HP.GetFileName();
1116  if (s == 0) {
1117  silent_cerr("ExtForce(" << uLabel << "): "
1118  "unable to get output file name "
1119  "at line " << HP.GetLineData() << std::endl);
1121  }
1122  std::string fout = s;
1123 
1124  bool bNoClobberOut = false;
1125  if (HP.IsKeyWord("no" "clobber")) {
1126  bNoClobberOut = true;
1127  }
1128 
1129  mbsleep_t SleepTime = mbsleep_init(1);
1130  std::streamsize Precision = 0;
1131  ReadExtFileParams(pDM, HP, uLabel, SleepTime, Precision);
1132 
1134  ExtFileHandler(fin, bUnlinkIn, fout, bNoClobberOut,
1135  SleepTime, Precision));
1136 
1137  return pEFH;
1138 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
unsigned long mbsleep_t
Definition: mbsleep.h:51
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
Definition: parsinc.cc:673
static ExtFileHandlerBase * ReadExtSocketHandler(DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
Definition: extforce.cc:882
ExtFileHandlerBase * ReadExtFileHandlerEDGE(DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
Definition: extedge.cc:757
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
mbsleep_t mbsleep_init(long t)
Definition: mbsleep.c:38
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
void ReadExtFileParams(DataManager *pDM, MBDynParser &HP, unsigned int uLabel, mbsleep_t &SleepTime, std::streamsize &Precision)
Definition: extforce.cc:845

Here is the call graph for this function:

void ReadExtFileParams ( DataManager pDM,
MBDynParser HP,
unsigned int  uLabel,
mbsleep_t SleepTime,
std::streamsize &  Precision 
)

Definition at line 845 of file extforce.cc.

References HighParser::GetInt(), IncludeParser::GetLineData(), HighParser::GetTimeout(), HighParser::IsKeyWord(), and MBDYN_EXCEPT_ARGS.

Referenced by ReadExtFileHandler(), ReadExtFileHandlerEDGE(), and ReadExtSocketHandler().

850 {
851  mbsleep_t MinSleepTime = SleepTime;
852  if (HP.IsKeyWord("sleep" "time")) {
853  SleepTime = HP.GetTimeout(SleepTime);
854  if (SleepTime < MinSleepTime ) {
855  silent_cerr("ExtForce(" << uLabel << "): "
856  "invalid sleep time " << SleepTime
857  << " less than " << MinSleepTime
858  << " at line " << HP.GetLineData()
859  << std::endl);
861  }
862  }
863 
864  Precision = 6;
865  if (HP.IsKeyWord("precision")) {
866  if (!HP.IsKeyWord("default")) {
867  Precision = HP.GetInt();
868  if (Precision < 0) {
869  silent_cerr("ExtForce(" << uLabel << "): "
870  "invalid precision value "
871  "\"" << Precision << "\""
872  " at line " << HP.GetLineData()
873  << std::endl);
875  }
876  }
877  }
878 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
unsigned long mbsleep_t
Definition: mbsleep.h:51
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
virtual mbsleep_t GetTimeout(const mbsleep_t &DefVal)
Definition: parser.cc:1062
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697

Here is the call graph for this function:

void ReadExtForce ( DataManager pDM,
MBDynParser HP,
unsigned int  uLabel,
ExtFileHandlerBase *&  pEFH,
bool bSendAfterPredict,
int iCoupling 
)

Definition at line 1141 of file extforce.cc.

References ExtForce::COUPLING_LOOSE, ExtForce::COUPLING_NONE, ExtForce::COUPLING_STAGGERED, ExtForce::COUPLING_TIGHT, HighParser::GetInt(), IncludeParser::GetLineData(), HighParser::GetYesNo(), HighParser::IsKeyWord(), MBDYN_EXCEPT_ARGS, and ReadExtFileHandler().

Referenced by ReadModalExtForce(), ReadModalMappingExtForce(), ReadStructExtForce(), and ReadStructMappingExtForce().

1147 {
1148  pEFH = ReadExtFileHandler(pDM, HP, uLabel);
1149 
1150  iCoupling = ExtForce::COUPLING_LOOSE;
1151  if (HP.IsKeyWord("coupling")) {
1152  if (HP.IsKeyWord("none")) {
1153  iCoupling = ExtForce::COUPLING_NONE;
1154 
1155  } else if (HP.IsKeyWord("staggered")) {
1156  iCoupling = ExtForce::COUPLING_STAGGERED;
1157 
1158  } else if (HP.IsKeyWord("loose")) {
1159  iCoupling = ExtForce::COUPLING_LOOSE;
1160 
1161  } else if (HP.IsKeyWord("tight")) {
1162  iCoupling = ExtForce::COUPLING_TIGHT;
1163 
1164  } else {
1165  iCoupling = HP.GetInt();
1166  if (iCoupling < -1) {
1167  silent_cerr("ExtForce(" << uLabel << "): "
1168  "invalid coupling value "
1169  "\"" << iCoupling << "\""
1170  " at line " << HP.GetLineData()
1171  << std::endl);
1173  }
1174  }
1175  }
1176 
1177  if (iCoupling >= ExtForce::COUPLING_LOOSE) {
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 "
1183  "at line " << HP.GetLineData()
1184  << std::endl);
1186  }
1187  }
1188 
1189  } else {
1190  bSendAfterPredict = false;
1191  }
1192 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
static ExtFileHandlerBase * ReadExtFileHandler(DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
Definition: extforce.cc:1087
virtual bool GetYesNo(bool &bRet)
Definition: parser.cc:1022
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697

Here is the call graph for this function:

static ExtFileHandlerBase* ReadExtSocketHandler ( DataManager pDM,
MBDynParser HP,
unsigned int  uLabel 
)
static

Definition at line 882 of file extforce.cc.

References IncludeParser::GetFileName(), HighParser::GetInt(), IncludeParser::GetLineData(), HighParser::GetStringWithDelims(), HighParser::GetYesNo(), host, IPPORT_USERRESERVED, HighParser::IsArg(), HighParser::IsKeyWord(), MBDYN_EXCEPT_ARGS, mbsleep_init(), MSG_NOSIGNAL, path, port, ReadExtFileParams(), and SAFENEWWITHCONSTRUCTOR.

Referenced by ReadExtFileHandler().

885 {
886 #ifdef USE_SOCKET
887  ExtFileHandlerBase *pEFH = 0;
888 
889  bool bGotCreate(false);
890  bool bCreate(false);
891  unsigned short int port = (unsigned short int)-1;
892  std::string host;
893  std::string path;
894 
895  if (HP.IsKeyWord("create")) {
896  bGotCreate = true;
897  if (!HP.GetYesNo(bCreate)) {
898  silent_cerr("ExtSocketHandler"
899  "(" << uLabel << "): "
900  "\"create\" must be either \"yes\" or \"no\" "
901  "at line " << HP.GetLineData()
902  << std::endl);
904  }
905  }
906 
907  if (HP.IsKeyWord("local") || HP.IsKeyWord("path")) {
908  const char *m = HP.GetFileName();
909 
910  if (m == 0) {
911  silent_cerr("ExtSocketHandler"
912  "(" << uLabel << "): "
913  "unable to read local path "
914  "at line " << HP.GetLineData()
915  << std::endl);
917  }
918 
919  path = m;
920  }
921 
922  if (HP.IsKeyWord("port")) {
923  if (!path.empty()) {
924  silent_cerr("ExtSocketHandler"
925  "(" << uLabel << "): "
926  "cannot specify port "
927  "for a local socket "
928  "at line " << HP.GetLineData()
929  << std::endl);
931  }
932 
933  int p = HP.GetInt();
934  /* Da sistemare da qui */
935 #ifdef IPPORT_USERRESERVED
936  if (p <= IPPORT_USERRESERVED) {
937  silent_cerr("ExtSocketHandler"
938  "(" << uLabel << "): "
939  "cannot listen on reserved port "
940  << port << ": less than "
941  "IPPORT_USERRESERVED=" << IPPORT_USERRESERVED
942  << " at line " << HP.GetLineData()
943  << std::endl);
945  }
946  /* if #undef'd, don't bother checking;
947  * the OS will do it for us */
948 #endif /* IPPORT_USERRESERVED */
949 
950  port = p;
951  }
952 
953  if (HP.IsKeyWord("host")) {
954  if (!path.empty()) {
955  silent_cerr("ExtSocketHandler"
956  "(" << uLabel << "): "
957  "cannot specify host for a local socket "
958  "at line " << HP.GetLineData()
959  << std::endl);
961  }
962 
963  const char *h;
964 
965  h = HP.GetStringWithDelims();
966  if (h == 0) {
967  silent_cerr("ExtSocketHandler"
968  "(" << uLabel << "): "
969  "unable to read host "
970  "at line " << HP.GetLineData()
971  << std::endl);
973  }
974 
975  host = h;
976 
977  } else if (path.empty() && !bCreate) {
978  silent_cerr("ExtSocketHandler"
979  "(" << uLabel << "): "
980  "host undefined "
981  "at line " << HP.GetLineData()
982  << std::endl);
984  }
985 
986  int socket_type = SOCK_STREAM;
987  if (HP.IsKeyWord("socket" "type")) {
988  if (HP.IsKeyWord("udp")) {
989  socket_type = SOCK_DGRAM;
990 
991  if (!bGotCreate) {
992  bCreate = true;
993  }
994 
995  } else if (!HP.IsKeyWord("tcp")) {
996  silent_cerr("ExtSocketHandler(" << uLabel << "\"): "
997  "invalid socket type "
998  "at line " << HP.GetLineData() << std::endl);
1000  }
1001  }
1002 
1003  if ((socket_type == SOCK_DGRAM) && !bCreate) {
1004  silent_cerr("ExtSocketHandler(" << uLabel << "\"): "
1005  "socket type=udp incompatible with create=no "
1006  "at line " << HP.GetLineData() << std::endl);
1008  }
1009 
1010  // we want to block until the whole chunk is received
1011  int recv_flags = 0;
1012  int send_flags = 0;
1013 #ifdef MSG_WAITALL
1014  recv_flags |= MSG_WAITALL;
1015 #endif // MSG_WAITALL
1016 
1017  while (HP.IsArg()) {
1018  if (HP.IsKeyWord("signal")) {
1019 #ifdef MSG_NOSIGNAL
1020  recv_flags &= ~MSG_NOSIGNAL;
1021  send_flags &= ~MSG_NOSIGNAL;
1022 #else // ! MSG_NOSIGNAL
1023  silent_cout("ExtSocketHandler"
1024  "(" << uLabel << "): "
1025  "MSG_NOSIGNAL not defined (ignored) "
1026  "at line " << HP.GetLineData()
1027  << std::endl);
1028 #endif // ! MSG_NOSIGNAL
1029 
1030  // not honored by recv(2)
1031  } else if (HP.IsKeyWord("no" "signal")) {
1032 #ifdef MSG_NOSIGNAL
1033  recv_flags |= MSG_NOSIGNAL;
1034  send_flags |= MSG_NOSIGNAL;
1035 #else // ! MSG_NOSIGNAL
1036  silent_cout("ExtSocketHandler"
1037  "(" << uLabel << "): "
1038  "MSG_NOSIGNAL not defined (ignored) "
1039  "at line " << HP.GetLineData()
1040  << std::endl);
1041 #endif // ! MSG_NOSIGNAL
1042 
1043  } else {
1044  break;
1045  }
1046  }
1047 
1048  UseSocket *pUS = 0;
1049  if (path.empty()) {
1050  if (port == (unsigned short int)(-1)) {
1051  silent_cerr("ExtSocketHandler"
1052  "(" << uLabel << "): "
1053  "port missing"
1054  << std::endl);
1056  }
1057  SAFENEWWITHCONSTRUCTOR(pUS, UseInetSocket, UseInetSocket(host, port, socket_type, bCreate));
1058 
1059  } else {
1060  SAFENEWWITHCONSTRUCTOR(pUS, UseLocalSocket, UseLocalSocket(path, socket_type, bCreate));
1061  }
1062 
1063  if (bCreate) {
1064  pDM->RegisterSocketUser(pUS);
1065 
1066  } else {
1067  pUS->Connect();
1068  }
1069 
1070  mbsleep_t SleepTime = mbsleep_init(0);
1071  std::streamsize Precision = 0;
1072  ReadExtFileParams(pDM, HP, uLabel, SleepTime, Precision);
1073  // NOTE: so far, precision is ignored
1074 
1075  SAFENEWWITHCONSTRUCTOR(pEFH, ExtSocketHandler,
1076  ExtSocketHandler(pUS, SleepTime, recv_flags, send_flags));
1077 
1078  return pEFH;
1079 #else // ! USE_SOCKET
1080  silent_cerr("ExtSocketHandler not supported" << std::endl);
1082 #endif // ! USE_SOCKET
1083 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
unsigned long mbsleep_t
Definition: mbsleep.h:51
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
Definition: parsinc.cc:673
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
const char * host
Definition: autopilot.c:142
virtual const char * GetStringWithDelims(enum Delims Del=DEFAULTDELIM, bool escape=true)
Definition: parser.cc:1228
mbsleep_t mbsleep_init(long t)
Definition: mbsleep.c:38
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
#define MSG_NOSIGNAL
Definition: sock.h:69
virtual bool GetYesNo(bool &bRet)
Definition: parser.cc:1022
virtual bool IsArg(void)
Definition: parser.cc:807
#define IPPORT_USERRESERVED
Definition: sock.h:66
unsigned short int port
Definition: autopilot.c:143
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
const char * path
Definition: autopilot.c:141
void ReadExtFileParams(DataManager *pDM, MBDynParser &HP, unsigned int uLabel, mbsleep_t &SleepTime, std::streamsize &Precision)
Definition: extforce.cc:845

Here is the call graph for this function: