| 489 | } |
| 490 | |
| 491 | void |
| 492 | jalib::JMultiSocketProgram::monitorSockets(double dblTimeout) |
| 493 | { |
| 494 | struct timeval tmptime = { 0, 0 }; |
| 495 | struct timeval timeoutBuf; |
| 496 | struct timeval *timeout; |
| 497 | |
| 498 | setTimeoutInterval(dblTimeout); |
| 499 | |
| 500 | timeoutBuf = timeoutInterval; |
| 501 | timeout = timeoutEnabled ? &timeoutBuf : NULL; |
| 502 | |
| 503 | dmtcp::set<int> closedFds; |
| 504 | dmtcp::vector<struct pollfd>fds; |
| 505 | size_t i; |
| 506 | for (;;) { |
| 507 | closedFds.clear(); |
| 508 | fds.clear(); |
| 509 | |
| 510 | if (timeout == NULL && timeoutEnabled) { |
| 511 | timeoutBuf = timeoutInterval; |
| 512 | timeout = &timeoutBuf; |
| 513 | } else if (timeout != NULL && !timeoutEnabled) { |
| 514 | timeout = NULL; |
| 515 | } |
| 516 | |
| 517 | struct pollfd socketFd = { 0 }; |
| 518 | |
| 519 | // collect listen fds in rfds, clean up dead sockets |
| 520 | for (i = 0; i < _listenSockets.size(); ++i) { |
| 521 | if (_listenSockets[i].isValid()) { |
| 522 | socketFd.fd = _listenSockets[i].sockfd(); |
| 523 | socketFd.events = POLLIN; |
| 524 | fds.push_back(socketFd); |
| 525 | } else { |
| 526 | _listenSockets[i].close(); |
| 527 | |
| 528 | // socket is dead... remove it |
| 529 | JTRACE("listen socket failure") (i); |
| 530 | |
| 531 | // swap with last |
| 532 | _listenSockets[i] = _listenSockets[_listenSockets.size() - 1]; |
| 533 | _listenSockets.pop_back(); |
| 534 | i--; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // collect data fds in rfds, clean up dead sockets |
| 539 | for (i = 0; i < _dataSockets.size(); ++i) { |
| 540 | if (!_dataSockets[i]->hadError()) { |
| 541 | socketFd.fd = _dataSockets[i]->socket().sockfd(); |
| 542 | socketFd.events = POLLIN; |
| 543 | fds.push_back(socketFd); |
| 544 | } else { |
| 545 | JReaderInterface *dsock = _dataSockets[i]; |
| 546 | closedFds.insert(dsock->socket().sockfd()); |
| 547 | |
| 548 | // socket is dead... remove it |