void TunTapComponent::rxThreadFunction() { //LOG(LINFO) << "RX thread started, listening on tun/tap device " << x_tunTapDevice; fd_set socketSet; struct timeval selectTimeout; char buffer[MAX_BUF_SIZE]; int nread; try { // read data coming from the kernel while(true) { boost::this_thread::interruption_point(); // reset socket set and add tap descriptor FD_ZERO(&socketSet); FD_SET(tunFd_, &socketSet); // initialize timeout selectTimeout.tv_sec = 1; selectTimeout.tv_usec = 0; // suspend thread until we receive a packet or timeout if (select(tunFd_ + 1, &socketSet, NULL, NULL, &selectTimeout) == 0) { //LOG(LDEBUG) << "Timeout while waiting for incoming packet."; } else { if (FD_ISSET(tunFd_, &socketSet)) { if ((nread = read(tunFd_, buffer, MAX_BUF_SIZE)) < 0) { LOG(LFATAL) << "Error while reading from tun/tap interface."; continue; } LOG(LDEBUG) << "Read " << nread << " bytes from device " << tunName_; // copy received data into new StackDataSet shared_ptr<StackDataSet> packetBuffer(new StackDataSet); packetBuffer->data.assign(buffer, buffer + nread); // send downwards sendDownwards(packetBuffer); } } } // while (true) throw SystemException("Rx thread stopped unexpectedly."); } catch(IrisException& ex) { LOG(LFATAL) << "Error in TunTap component: " << ex.what() << " - TX thread exiting."; } catch(boost::thread_interrupted) { LOG(LINFO) << "Thread " << boost::this_thread::get_id() << " in stack component interrupted."; } }
void FileWriterComponent::processMessageFromAbove(boost::shared_ptr<StackDataSet> set) { LOG(LDEBUG) << "FileWriter::processMessageFromAbove() called."; if(!fromBelow_x) { writeBlock(set); } sendDownwards(set); }
void FileReaderComponent::fileReadingLoop() { boost::this_thread::sleep(boost::posix_time::milliseconds(100)); try { while(running_) { if(enabled_x) { boost::shared_ptr<StackDataSet> readDataBuffer(new StackDataSet); readBlock(readDataBuffer); if(++count_ == packets_x) running_ = false; if(sendBelow_x) { sendDownwards(readDataBuffer); } else { sendUpwards(readDataBuffer); } } else { boost::this_thread::sleep(boost::posix_time::milliseconds(1)); } boost::this_thread::interruption_point(); boost::this_thread::sleep(boost::posix_time::milliseconds(delay_x)); } } catch(IrisException& ex) { LOG(LFATAL) << "Error in FileReader component: " << ex.what() << " - file reading thread exiting."; } }
/*! Process a message from above * * This example just passes data through. */ void FileReaderComponent::processMessageFromAbove(boost::shared_ptr<StackDataSet> set) { sendDownwards(set); }