MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
dataman6.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/dataman6.cc,v 1.27 2017/01/12 14:46:09 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #ifdef USE_SOCKET
35 #include "dataman.h"
36 #include "dataman_.h"
37 
38 #include <stdio.h>
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <sys/un.h>
46 #include <netdb.h>
47 
48 void
49 DataManager::RegisterSocketUser(UseSocket *pUS)
50 {
51  ASSERT(pUS->GetSock() != -1);
52  SocketUsers[pUS->GetSock()] = pUS;
53 }
54 
55 void
56 DataManager::DeleteSocketUsers(void)
57 {
58  for (std::map<int, UseSocket *>::iterator i = SocketUsers.begin();
59  i != SocketUsers.end(); ++i)
60  {
61  delete i->second;
62  }
63  SocketUsers.clear();
64 }
65 
66 void
67 DataManager::WaitSocketUsers(void)
68 {
69  if (SocketUsers.empty()) {
70  return;
71  }
72 
73  time_t finalTime = 0;
74  if (SocketUsersTimeout != 0) {
75  finalTime = time(NULL) + SocketUsersTimeout;
76  }
77 
78  fd_set active_set, read_set;
79  FD_ZERO(&active_set);
80 
81  /* insert registered sockets in set */
82  std::map<int, UseSocket *>::iterator ri;
83  std::map<int, UseSocket *>::const_iterator re = SocketUsers.end();
84  int n;
85  for (n = 0, ri = SocketUsers.begin(); ri != re; ++n, ++ri) {
86  FD_SET(ri->first, &active_set);
87  }
88 
89  /* wait for all registered */
90  while (n > 0) {
91  struct timeval timeout, *timeoutp = 0;
92 
93  if (finalTime != 0) {
94  timeout.tv_sec = time(NULL);
95  if (timeout.tv_sec >= finalTime) {
96 do_timeout:;
97  silent_cerr("DataManager::WaitSocketUsers(): "
98  "timeout " << SocketUsersTimeout
99  << " s exceeded" << std::endl);
101  }
102  timeout.tv_sec = finalTime - timeout.tv_sec;
103  timeout.tv_usec = 0;
104 
105  timeoutp = &timeout;
106  }
107 
108  read_set = active_set;
109  int a = select(FD_SETSIZE, &read_set, 0, 0, timeoutp);
110  switch (a) {
111  case -1: {
112  int save_errno = errno;
113  char *msg = strerror(save_errno);
114 
115  silent_cerr("select() failed: " << save_errno << " "
116  "(" << msg << ")" << std::endl);
118  }
119 
120  case 0:
121  goto do_timeout;
122  }
123 
124  /* loop on active to see what is being connected */
125  for (int i = 0; i < FD_SETSIZE && a > 0; i++) {
126  if (FD_ISSET(i, &read_set)) {
127  UseSocket *pUS = SocketUsers[i];
128  int sock;
129 
130  sock = accept(i, pUS->GetSockaddr(),
131  &pUS->GetSocklen());
132  if (sock < 0) {
133  int save_errno = errno;
134  char *msg = strerror(save_errno);
135 
136  silent_cerr("accept() failed: "
137  << save_errno << " "
138  "(" << msg << ")"
139  << std::endl);
141  }
142 
143  /* remove accepted from set */
144  FD_CLR(i, &active_set);
145  close(i);
146  SocketUsers.erase(i);
147 
148  /* register as connected */
149  pUS->ConnectSock(sock);
150  n--;
151  a--;
152  }
153  }
154  }
155 }
156 
157 #endif // USE_SOCKET
static int timeout
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
#define ASSERT(expression)
Definition: colamd.c:977
struct mbrtai_msg_t msg
static const doublereal a
Definition: hfluid_.h:289