Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

BufferedFileFactory.C

Go to the documentation of this file.
00001 #include "BufferedFileFactory.h"
00002 #include "Clock.h"
00003 #include <iostream>
00004 BufferedFileFactory::BufferedFileFactory(): descriptorToFile(),tv() {
00005         //descriptorToFile = new BuffVec();
00006         //
00007         //descriptorToFile = new BuffVec();
00008         FD_ZERO(&readSet);
00009         FD_ZERO(&writeSet);
00010         maxFd = -1;
00011 }
00012 BufferedFile * BufferedFileFactory::getNewBufferedFile(int fdesc) {
00013         BufferedFile * bf = new BufferedFile(fdesc);
00014         bf->setBufferedFileFactory(this);
00015         assert(bf!=NULL);
00016         descriptorToFile.push_back(bf);
00017         FD_SET(fdesc,&writeSet);
00018         FD_SET(fdesc,&readSet);
00019         if (fdesc+1 > maxFd) {
00020                 maxFd = fdesc+1;
00021         }
00022         return bf;
00023 }
00024 //If readReady is true and !hasBeenRead DONT
00025 int BufferedFileFactory::process(int microTime) {
00026         BuffVec::iterator iter;
00027         fd_set readingSet;
00028         fd_set readingSet2;
00029         fd_set writingSet;
00030         fd_set writingSet2;
00031         FD_ZERO(&readingSet);
00032         FD_ZERO(&writingSet);
00033         FD_ZERO(&readingSet2);
00034         FD_ZERO(&writingSet2);
00035         for (iter = descriptorToFile.begin(); iter !=  descriptorToFile.end(); iter++) {
00036                 BufferedFile * file = *iter;
00037                 int fd = file->getFileHandle();
00038                 if (file->doesRead()) {
00039                         //if (!file->readReady() || (file->readReady() && file->hasBeenRead())) {
00040                         if (file->readReady() && file->hasBeenRead()) {
00041                               FD_SET(fd,&readingSet2);
00042                         } else {
00043                                 //cerr << "Has Been Read or is not readready: " << fd << " " << Clock::clock << "\n";
00044                               FD_SET(fd,&readingSet);
00045                         }
00046                 }
00047                 if (file->doesWrite()) {
00048                         //if (!file->writeReady() || (file->writeReady() && file->hasBeenWritten())) {
00049                         if (file->writeReady() && file->hasBeenWritten()) {
00050                               FD_SET(fd,&writingSet2);
00051                         } else {
00052                                 //cerr << "Has Been Written or is not writeready: " << fd << " " << Clock::clock << "\n";
00053                         
00054                               FD_SET(fd,&writingSet);
00055                         }
00056                 }
00057         }
00058         //fd_set readingSet = readSet;
00059         //fd_set writingSet = writeSet;
00060         //tv.tv_usec = microTime/2; 
00061         tv.tv_sec = 0;
00062         tv.tv_usec = microTime; 
00063         int read = select(maxFd+1,&readingSet,&writingSet, NULL, &tv);
00064         tv.tv_sec = 0;
00065         tv.tv_usec = microTime; 
00066         read += select(maxFd+1,&readingSet2,&writingSet2, NULL, &tv);
00067         //int read = select(maxFd+1,&readingSet,NULL, NULL, &tv);
00068         //tv.tv_sec = 0;
00069         //tv.tv_usec = microTime/2; 
00070         //int write = select(maxFd+1,NULL,&writingSet, NULL, &tv);
00071         int write = 0;
00072         for (iter = descriptorToFile.begin(); iter !=  descriptorToFile.end(); iter++) {
00073                 BufferedFile * file = *iter;
00074                 int fd = file->getFileHandle();
00075                 if (file->doesRead()) {
00076                         if (FD_ISSET(fd, &readingSet) || FD_ISSET(fd,&readingSet2)) {
00077                                 //cerr << "ReadFD: "<<fd << " " << (int)(file) << "\n";
00078                                 file->setReadReady(true);
00079                         } else {
00080                                 file->setReadReady(false);
00081                         }
00082                 }
00083                 if (file->doesWrite()) {
00084                         if (FD_ISSET(fd, &writingSet) || (FD_ISSET(fd, &writingSet2))) {
00085                                 file->setWriteReady(true);
00086                         } else {
00087                                 file->setWriteReady(false);
00088                         }
00089                 }       
00090         }
00091         return (read+write);
00092 }
00093 void BufferedFileFactory::closeFile(BufferedFile * f) {
00094         int fdesc = f->getFileHandle();
00095         descriptorToFile.remove(f);
00096         FD_CLR(fdesc,&readSet);
00097         FD_CLR(fdesc,&writeSet);
00098         maxFd = 0;
00099         BuffVec::iterator iter;
00100         //for (iter = descriptorToFile.begin(); iter !=  descriptorToFile.end(); iter++) {
00101         //      BufferedFile * r = (*iter);
00102         //      if (f==r) {
00103         //              descriptorToFile.erase(iter);
00104         //      }
00105         //}
00106         for (iter = descriptorToFile.begin(); iter !=  descriptorToFile.end(); iter++) {
00107                 int fd = (*iter)->getFileHandle();
00108                 //if (fd == fdesc) {
00109                 //      descriptorToFile.erase(iter);
00110                 //} else if (fd+1 > maxFd) { 
00111                 if (fd+1 > maxFd) { 
00112                         maxFd = fd+1;
00113                 }
00114         }
00115 }
00116 BufferedFileFactory::~BufferedFileFactory() {
00117         BuffVec::iterator iter;
00118         for (iter = descriptorToFile.begin(); iter !=  descriptorToFile.end(); iter++) {
00119                 (*iter)->setBufferedFileFactory(NULL);
00120                 (*iter)->Close();
00121                 delete *iter;
00122         }
00123         descriptorToFile.clear();
00124         //delete descriptorToFile;
00125 }
00126 void BufferedFileFactory::setBufferedFileRead(BufferedFile * bf,bool mode) {
00127         int fd = bf->getFileHandle();
00128         if (mode) { //READ
00129                 if (FD_ISSET(fd,&readSet)) {
00130 
00131                 } else {
00132                         FD_SET(fd,&readSet);
00133                 }
00134         } else { //NOT READ
00135                 if (FD_ISSET(fd,&readSet)) {
00136                         FD_CLR(fd,&readSet);
00137                 } else {
00138                 }
00139         }
00140 }
00141 void BufferedFileFactory::setBufferedFileWrite(BufferedFile * bf,bool mode) {
00142         int fd = bf->getFileHandle();
00143         if (mode) { //WRITE
00144                 if (FD_ISSET(fd,&writeSet)) {
00145 
00146                 } else {
00147                         FD_SET(fd,&writeSet);
00148                 }
00149         } else { //NOT WRITE
00150                 if (FD_ISSET(fd,&writeSet)) {
00151                         FD_CLR(fd,&writeSet);
00152                 } else {
00153                 }
00154         }
00155 }

Generated on Tue Dec 17 21:14:13 2002 for AUSS_Connector by doxygen1.2.18