00001 #ifndef TTY_UNIX_HAS_BEEN_INCLUDED
00002 #define TTY_UNIX_HAS_BEEN_INCLUDED
00003
00004 #include "std/support.H"
00005 #include "dev/tty.H"
00006
00007
00008 class UNIX_MANAGER : public FD_MANAGER {
00009 public:
00010 class tty_to_id {
00011 public:
00012 FD_EVENT *_fd;
00013 tty_to_id() : _fd(0) { }
00014 tty_to_id(FD_EVENT *fd) : _fd(fd) { }
00015 int operator == (const tty_to_id &i) { return _fd == i._fd; }
00016 };
00017
00018 protected :
00019 ARRAY<tty_to_id> _ids;
00020
00021 public :
00022 UNIX_MANAGER() { }
00023
00024 virtual void loop(int infinite = 1) {
00025 fd_set selectFDs;
00026 fd_set exceptFDs;
00027 do {
00028 struct timeval tval, *to = (_timeouts.num()||!infinite) ? &tval : 0;
00029 tval.tv_sec = 0; tval.tv_usec = 10;
00030
00031 FD_ZERO(&selectFDs);
00032 FD_ZERO(&exceptFDs);
00033 int max = -1, fd, num = 0, readyFDs;
00034
00035 int i;
00036 for (i = _ids.num() - 1; i >= 0; i--) {
00037 fd = _ids[i]._fd->fd();
00038 if (fd >= 0) {
00039 FD_SET(fd, &selectFDs);
00040 FD_SET(fd, &exceptFDs);
00041 if (fd > max) max = fd;
00042 } else {
00043 cerr << "UNIX_MANAGER::loop - removing bad FD_EVENT" << endl;
00044 rem(_ids[i]._fd);
00045 }
00046 }
00047
00048 readyFDs = select(max + 1, &selectFDs, 0, &exceptFDs, to);
00049 if (readyFDs == 0 && _timeouts.num())
00050 for (i = 0; i < _timeouts.num(); i++) _timeouts[i]->timeout();
00051 for (i = 0; i < _ids.num() && (num < readyFDs); i++) {
00052 if (FD_ISSET(_ids[i]._fd->fd(), &selectFDs)) {
00053 _ids[i]._fd->sample();
00054 num++;
00055 }
00056 if (FD_ISSET(_ids[i]._fd->fd(), &exceptFDs)) {
00057 _ids[i]._fd->except();
00058 num++;
00059 }
00060 }
00061 } while (infinite);
00062 }
00063 virtual void add(FD_EVENT *fd) {
00064 _ids += tty_to_id(fd);
00065 }
00066
00067 virtual void rem(FD_EVENT *fd) {
00068 int ind = _ids.get_index(tty_to_id(fd));
00069 if (ind != BAD_IND) {
00070 _ids -= tty_to_id(fd);
00071 }
00072 }
00073 };
00074 #endif