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

Public Member Functions

virtual void getSocketStreamOutParam (DataManager *pDM, MBDynParser &HP, unsigned int uLabel, StreamContent::Type type, SocketStreamOutputDataTmp &socketStreamOutputDataTmp)
 
virtual ElemcreateSocketStreamOutElem (DataManager *pDM, MBDynParser &HP, unsigned int uLabel, StreamContent::Type type, SocketStreamOutputDataTmp &socketStreamOutputDataTmp)
 

Detailed Description

Definition at line 234 of file socketstream_out_elem.cc.

Member Function Documentation

Elem * SocketStreamOutputElemCreator::createSocketStreamOutElem ( DataManager pDM,
MBDynParser HP,
unsigned int  uLabel,
StreamContent::Type  type,
SocketStreamOutputDataTmp socketStreamOutputDataTmp 
)
virtual

Definition at line 450 of file socketstream_out_elem.cc.

References SocketStreamOutputDataTmp::bAbortIfBroken, SocketStreamOutputDataTmp::bCreate, SocketStreamOutputDataTmp::bIsRTAI, SocketStreamOutputDataTmp::bNonBlocking, SocketStreamOutputDataTmp::bNoSignal, SocketStreamOutputDataTmp::bSendFirst, DEFAULT_PORT, SocketStreamOutputDataTmp::flags, IncludeParser::GetLineData(), DataManager::GetLogFile(), host, SocketStreamOutputDataTmp::host, HighParser::IsArg(), MBDYN_EXCEPT_ARGS, MSG_NOSIGNAL, SocketStreamOutputDataTmp::name, SocketStreamOutputDataTmp::OutputEvery, SocketStreamOutputDataTmp::path, SocketStreamOutputDataTmp::port, SocketStreamOutputDataTmp::pSC, SocketStreamOutputDataTmp::pSOE, SAFENEWWITHCONSTRUCTOR, SocketStreamOutputDataTmp::socket_type, and WriteStreamContentLogOutput().

Referenced by ReadSocketStreamElem().

450  {
451  /* Se non c'e' il punto e virgola finale */
452  if (HP.IsArg()) {
453  silent_cerr("SocketStreamElem(" << uLabel << "): "
454  "semicolon expected "
455  "at line " << HP.GetLineData() << std::endl);
457  }
458 
459  Elem *pEl = 0;
460 
461  // .log file output
462  std::ostream& out = pDM->GetLogFile();
463  out
464  << "outputelement: " << uLabel
465  << " stream";
466 
467  if (socketStreamOutputDataTmp.bIsRTAI) {
468 #ifdef USE_RTAI
469  if (socketStreamOutputDataTmp.pSOE != 0) {
470  silent_cerr("SocketStreamElem(" << uLabel << "): "
471  "echo ignored in RTAI mode"
472  << std::endl);
473  }
474 
475  unsigned long node = (unsigned long)-1;
476 #if defined(HAVE_GETADDRINFO)
477  socketStreamOutputDataTmp.hints = {0};
478  socketStreamOutputDataTmp.res = null;
479 
480  socketStreamOutputDataTmp.hints.ai_family = AF_INET;
481  socketStreamOutputDataTmp.hints.ai_socktype = SOCK_STREAM; // FIXME: what about SOCK_DGRAM?
482  socketStreamOutputDataTmp.rc = getaddrinfo(socketStreamOutputDataTmp.host.c_str(), NULL, &(socketStreamOutputDataTmp.hints), &(socketStreamOutputDataTmp.res));
483  if (socketStreamOutputDataTmp.rc == 0) {
484  node = ((struct sockaddr_in *)(socketStreamOutputDataTmp.res)->ai_addr)->sin_addr.s_addr;
485  freeaddrinfo(socketStreamOutputDataTmp.res);
486  }
487 #elif defined(HAVE_GETHOSTBYNAME)
488  socketStreamOutputDataTmp.he = gethostbyname(socketStreamOutputDataTmp.host.c_str());
489  if (socketStreamOutputDataTmp.he != NULL) {
490  node = ((unsigned long *)(socketStreamOutputDataTmp.he)->h_addr_list[0])[0];
491  }
492 #elif defined(HAVE_INET_ATON)
493  if (inet_aton(socketStreamOutputDataTmp.host.c_str(), &(socketStreamOutputDataTmp.addr))) {
494  node = socketStreamOutputDataTmp.addr.s_addr;
495  }
496 #else // ! HAVE_GETADDRINFO && ! HAVE_GETHOSTBYNAME && ! HAVE_INET_ATON
497  silent_cerr("SocketStreamElem(" << uLabel << "): "
498  "host (RTAI RPC) not supported "
499  "at line " << HP.GetLineData() << std::endl);
501 #endif // ! HAVE_GETADDRINFO && ! HAVE_GETHOSTBYNAME && ! HAVE_INET_ATON
502 
503  if (node == (unsigned long)-1) {
504  silent_cerr("RTMBDynInDrive(" << uLabel << "): "
505  "unable to convert host \"" << host << "\" to node" << std::endl);
507  }
508 
509  silent_cerr("starting RTMBDynOutputElement(" << uLabel << ")..."
510  << std::endl);
512  RTMBDynOutElem(uLabel,
513  socketStreamOutputDataTmp.name, socketStreamOutputDataTmp.host, node,
514  socketStreamOutputDataTmp.bCreate, socketStreamOutputDataTmp.pSC, socketStreamOutputDataTmp.bNonBlocking));
515 
516  out
517  << " " << "RTAI"
518  << " " << name
519  << " " << host
520  << " " << node
521  << " " << bCreate;
522 
523  WriteStreamContentLogOutput(pSC, out);
524 
525 #endif // USE_RTAI
526 
527  } else {
528  socketStreamOutputDataTmp.flags = 0;
529 
530 #ifdef USE_SOCKET
531  /* node creation */
532  UseSocket *pUS = 0;
533  if (socketStreamOutputDataTmp.path.empty()) {
534  if (socketStreamOutputDataTmp.port == (unsigned short int)(-1)) {
535  socketStreamOutputDataTmp.port = DEFAULT_PORT;
536  silent_cerr("SocketStreamElem(" << uLabel << "): "
537  "port undefined; using default port "
538  << socketStreamOutputDataTmp.port << " at line "
539  << HP.GetLineData() << std::endl);
540  }
541 
542  SAFENEWWITHCONSTRUCTOR(pUS, UseInetSocket, UseInetSocket(socketStreamOutputDataTmp.host.c_str(),
543  socketStreamOutputDataTmp.port, socketStreamOutputDataTmp.socket_type, socketStreamOutputDataTmp.bCreate));
544 
545  // .log file output
546  out
547  << " " << "INET"
548  << " " << socketStreamOutputDataTmp.name
549  << " " << socketStreamOutputDataTmp.host
550  << " " << socketStreamOutputDataTmp.port;
551  if (socketStreamOutputDataTmp.socket_type == sock_dgram) {
552  out << " udp";
553  } else {
554  out << " tcp";
555  }
556  out << " " << socketStreamOutputDataTmp.bCreate;
557 
558 
559  } else {
560  SAFENEWWITHCONSTRUCTOR(pUS, UseLocalSocket, UseLocalSocket(socketStreamOutputDataTmp.path.c_str(),
561  socketStreamOutputDataTmp.socket_type, socketStreamOutputDataTmp.bCreate));
562 
563  // .log file output
564  out
565  << " " << "UNIX"
566  << " " << socketStreamOutputDataTmp.name
567  << " " << socketStreamOutputDataTmp.path;
568  if (socketStreamOutputDataTmp.socket_type == sock_dgram) {
569  out << " udp";
570  } else {
571  out << " tcp";
572  }
573  out << " " << socketStreamOutputDataTmp.bCreate;
574  }
575 
576  if (socketStreamOutputDataTmp.bCreate) {
577  pDM->RegisterSocketUser(pUS);
578 
579  } else {
580  pUS->Connect();
581  }
582 
583 #ifdef MSG_NOSIGNAL
584  if (socketStreamOutputDataTmp.bNoSignal) {
585  // NOTE: we assume MSG_NOSIGNAL is a macro...
586  socketStreamOutputDataTmp.flags |= MSG_NOSIGNAL;
587 
588  } else {
589  socketStreamOutputDataTmp.flags &= ~MSG_NOSIGNAL;
590  }
591 #else // !MSG_NOSIGNAL
592  silent_cerr("SocketStreamElem(" << uLabel << "): "
593  "MSG_NOSIGNAL undefined; "
594  "your mileage may vary" << std::endl);
595 #endif // !MSG_NOSIGNAL
596 
597 #ifdef MSG_DONTWAIT
598  // NOTE: we assume MSG_DONTWAIT is a macro...
599  if (socketStreamOutputDataTmp.bNonBlocking) {
600  socketStreamOutputDataTmp.flags |= MSG_DONTWAIT;
601 
602  } else {
603  socketStreamOutputDataTmp.flags &= ~MSG_DONTWAIT;
604  }
605 #else // !MSG_DONTWAIT
606  silent_cerr("SocketStreamElem(" << uLabel << "): "
607  "MSG_DONTWAIT undefined; "
608  "your mileage may vary" << std::endl);
609 #endif // !MSG_DONTWAIT
610 
611  silent_cerr("starting SocketStreamElem(" << uLabel << ")..."
612  << std::endl);
613  SAFENEWWITHCONSTRUCTOR(pEl, SocketStreamElem,
614  SocketStreamElem(uLabel, socketStreamOutputDataTmp.name, socketStreamOutputDataTmp.OutputEvery,
615  pUS, socketStreamOutputDataTmp.pSC, socketStreamOutputDataTmp.flags,
616  socketStreamOutputDataTmp.bSendFirst, socketStreamOutputDataTmp.bAbortIfBroken,
617  socketStreamOutputDataTmp.pSOE));
618 
619  out
620  << " " << (!socketStreamOutputDataTmp.bNoSignal)
621  << " " << (!socketStreamOutputDataTmp.bNonBlocking)
622  << " " << socketStreamOutputDataTmp.bSendFirst
623  << " " << socketStreamOutputDataTmp.bAbortIfBroken
624  << " " << socketStreamOutputDataTmp.OutputEvery;
625 
626  WriteStreamContentLogOutput(socketStreamOutputDataTmp.pSC, out);
627 
628 #endif // USE_SOCKET
629  }
630 
631  return pEl;
632 }
void WriteStreamContentLogOutput(const StreamContent *pSC, std::ostream &out)
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
static const int sock_dgram
const char * host
Definition: autopilot.c:142
#define DEFAULT_PORT
Definition: streamoutelem.h:40
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
#define MSG_NOSIGNAL
Definition: sock.h:69
virtual bool IsArg(void)
Definition: parser.cc:807
Definition: elem.h:75
std::ostream & GetLogFile(void) const
Definition: dataman.h:326
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697

Here is the call graph for this function:

void SocketStreamOutputElemCreator::getSocketStreamOutParam ( DataManager pDM,
MBDynParser HP,
unsigned int  uLabel,
StreamContent::Type  type,
SocketStreamOutputDataTmp socketStreamOutputDataTmp 
)
virtual

Definition at line 251 of file socketstream_out_elem.cc.

References SocketStreamOutputDataTmp::bAbortIfBroken, SocketStreamOutputDataTmp::bCreate, SocketStreamOutputDataTmp::bIsRTAI, SocketStreamOutputDataTmp::bNonBlocking, SocketStreamOutputDataTmp::bNoSignal, SocketStreamOutputDataTmp::bSendFirst, DEFAULT_HOST, DEFAULT_PORT, IncludeParser::GetFileName(), HighParser::GetInt(), IncludeParser::GetLineData(), HighParser::GetStringWithDelims(), HighParser::GetYesNo(), SocketStreamOutputDataTmp::host, HighParser::IsArg(), HighParser::IsKeyWord(), MBDYN_EXCEPT_ARGS, SocketStreamOutputDataTmp::name, SocketStreamOutputDataTmp::OutputEvery, SocketStreamOutputDataTmp::path, SocketStreamOutputDataTmp::port, sock_dgram, sock_stream, and SocketStreamOutputDataTmp::socket_type.

Referenced by ReadSocketStreamElem().

251  {
252  socketStreamOutputDataTmp.bIsRTAI = false;//bool bIsRTAI(false);
253 #ifdef USE_RTAI
254  if (::rtmbdyn_rtai_task != 0) {
255  socketStreamOutputDataTmp.bIsRTAI = true;
256  }
257 #endif // USE_RTAI
258 #ifndef USE_SOCKET
259  if (!socketStreamOutputDataTmp.bIsRTAI) {
260  silent_cerr("SocketStreamElem(" << uLabel << "): "
261  "not allowed because apparently the current "
262  "architecture does not support sockets "
263  "at line " << HP.GetLineData() << std::endl);
265  }
266 #endif // ! USE_SOCKET
267 
268  socketStreamOutputDataTmp.port = (unsigned short int)(-1);
269  socketStreamOutputDataTmp.bCreate = false;
270  bool bGotCreate(false);
271 
272  if (HP.IsKeyWord("name") || HP.IsKeyWord("stream" "name")) {
273  const char *m = HP.GetStringWithDelims();
274  if (m == 0) {
275  silent_cerr("SocketStreamElem(" << uLabel << "): "
276  "unable to read stream name "
277  "at line " << HP.GetLineData() << std::endl);
279 
280  } else if (socketStreamOutputDataTmp.bIsRTAI && strlen(m) != 6) {
281  silent_cerr("SocketStreamElem(" << uLabel << "): "
282  "illegal stream name \"" << m << "\" "
283  "(must be exactly 6 chars) "
284  "at line " << HP.GetLineData() << std::endl);
286  }
287 
288  socketStreamOutputDataTmp.name = m;
289 
290  } else {
291  silent_cerr("SocketStreamElem(" << uLabel << "): "
292  "missing stream name "
293  "at line " << HP.GetLineData() << std::endl);
294  if (socketStreamOutputDataTmp.bIsRTAI) {
296  }
297  }
298 
299  if (HP.IsKeyWord("create")) {
300  bGotCreate = true;
301  if (!HP.GetYesNo(socketStreamOutputDataTmp.bCreate)) {
302  silent_cerr("SocketStreamElem(" << uLabel << "):"
303  "\"create\" must be either "
304  "\"yes\" or \"no\" "
305  "at line " << HP.GetLineData()
306  << std::endl);
308  }
309  }
310 
311  if (HP.IsKeyWord("local") || HP.IsKeyWord("path")) {
312  const char *m = HP.GetFileName();
313 
314  if (m == 0) {
315  silent_cerr("SocketStreamElem(" << uLabel << "): "
316  "unable to read local path "
317  "at line " << HP.GetLineData() << std::endl);
319  }
320 
321  socketStreamOutputDataTmp.path = m;
322  }
323 
324  if (HP.IsKeyWord("port")) {
325  if (!socketStreamOutputDataTmp.path.empty()) {
326  silent_cerr("SocketStreamElem(" << uLabel << "): "
327  "cannot specify a port for a local socket "
328  "at line " << HP.GetLineData() << std::endl);
330  }
331 
332  int p = HP.GetInt();
333 
334  if (p <= 0) {
335  silent_cerr("SocketStreamElem(" << uLabel << "): "
336  "illegal port " << p << " "
337  "at line " << HP.GetLineData() << std::endl);
339  }
340 
341  socketStreamOutputDataTmp.port = p;
342  }
343 
344  if (HP.IsKeyWord("host")) {
345  if (!socketStreamOutputDataTmp.path.empty()) {
346  silent_cerr("SocketStreamElem(" << uLabel << "): "
347  "cannot specify an allowed host "
348  "for a local socket "
349  "at line " << HP.GetLineData() << std::endl);
351  }
352 
353  const char *h;
354 
355  h = HP.GetStringWithDelims();
356  if (h == 0) {
357  silent_cerr("SocketStreamElem(" << uLabel << "): "
358  "unable to read host "
359  "at line " << HP.GetLineData() << std::endl);
361  }
362 
363  socketStreamOutputDataTmp.host = h;
364 
365  } else if (socketStreamOutputDataTmp.path.empty() && !socketStreamOutputDataTmp.bCreate) {
366  /* INET sockets (!path) must be created if host is missing */
367  silent_cerr("SocketStreamElem(" << uLabel << "): "
368  "host undefined "
369  "at line " << HP.GetLineData() << std::endl);
370  socketStreamOutputDataTmp.host = DEFAULT_HOST;
371  silent_cerr("SocketStreamElem(" << uLabel << "): "
372  "using default host: "
373  << socketStreamOutputDataTmp.host << ":"
374  << (socketStreamOutputDataTmp.port == (unsigned short int)(-1) ? DEFAULT_PORT : socketStreamOutputDataTmp.port)
375  << std::endl);
376  }
377 
378  socketStreamOutputDataTmp.socket_type = sock_stream;
379  if (HP.IsKeyWord("socket" "type")) {
380  if (HP.IsKeyWord("udp")) {
381  socketStreamOutputDataTmp.socket_type = sock_dgram;
382 
383  if (!bGotCreate) {
384  socketStreamOutputDataTmp.bCreate = true;
385  }
386 
387  } else if (!HP.IsKeyWord("tcp")) {
388  silent_cerr("SocketStreamElem(" << uLabel << "): "
389  "invalid socket type "
390  "at line " << HP.GetLineData() << std::endl);
392  }
393  }
394 
395  if (socketStreamOutputDataTmp.socket_type == sock_dgram && socketStreamOutputDataTmp.bCreate) {
396  silent_cerr("SocketStreamElem(" << uLabel << "): "
397  "socket type=udp incompatible with create=yes "
398  "at line " << HP.GetLineData() << std::endl);
400  }
401 
402  socketStreamOutputDataTmp.bNonBlocking = false;
403  socketStreamOutputDataTmp.bNoSignal = false;
404  socketStreamOutputDataTmp.bSendFirst = true;
405  socketStreamOutputDataTmp.bAbortIfBroken = false;
406  while (HP.IsArg()) {
407  if (HP.IsKeyWord("no" "signal")) {
408  socketStreamOutputDataTmp.bNoSignal = true;
409 
410  } else if (HP.IsKeyWord("signal")) {
411  socketStreamOutputDataTmp.bNoSignal = false;
412 
413  } else if (HP.IsKeyWord("blocking")) {
414  socketStreamOutputDataTmp.bNonBlocking = false;
415 
416  } else if (HP.IsKeyWord("non" "blocking")) {
417  socketStreamOutputDataTmp.bNonBlocking = true;
418 
419  } else if (HP.IsKeyWord("no" "send" "first")) {
420  socketStreamOutputDataTmp.bSendFirst = false;
421 
422  } else if (HP.IsKeyWord("send" "first")) {
423  socketStreamOutputDataTmp.bSendFirst = true;
424 
425  } else if (HP.IsKeyWord("abort" "if" "broken")) {
426  socketStreamOutputDataTmp.bAbortIfBroken = true;
427 
428  } else if (HP.IsKeyWord("do" "not" "abort" "if" "broken")) {
429  socketStreamOutputDataTmp.bAbortIfBroken = false;
430 
431  } else {
432  break;
433  }
434  }
435 
436  socketStreamOutputDataTmp.OutputEvery = 1;
437  if (HP.IsKeyWord("output" "every")) {
438  int i = HP.GetInt();
439  if (i <= 0) {
440  silent_cerr("SocketStreamElem(" << uLabel << "): "
441  "invalid output every value " << i << " "
442  "at line " << HP.GetLineData() << std::endl);
444  }
445  socketStreamOutputDataTmp.OutputEvery = (unsigned int)i;
446  }
447 
448 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
Definition: parsinc.cc:673
static const int sock_dgram
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
virtual const char * GetStringWithDelims(enum Delims Del=DEFAULTDELIM, bool escape=true)
Definition: parser.cc:1228
#define DEFAULT_PORT
Definition: streamoutelem.h:40
virtual bool GetYesNo(bool &bRet)
Definition: parser.cc:1022
virtual bool IsArg(void)
Definition: parser.cc:807
#define DEFAULT_HOST
Definition: streamoutelem.h:41
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
static const int sock_stream

Here is the call graph for this function:


The documentation for this struct was generated from the following file: