kget
authenticationmonitor.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "authenticationmonitor.h"
00021 #include <math.h>
00022 #include <unistd.h>
00023 #ifndef Q_WS_WIN
00024 #include <sys/poll.h>
00025 #else
00026 #include <util/mingw.h>
00027 #endif
00028 #include <util/functions.h>
00029 #include <util/log.h>
00030 #include <mse/streamsocket.h>
00031 #include "authenticatebase.h"
00032 #include <kdebug.h>
00033
00034
00035 namespace bt
00036 {
00037 AuthenticationMonitor AuthenticationMonitor::self;
00038
00039 AuthenticationMonitor::AuthenticationMonitor()
00040 {}
00041
00042
00043 AuthenticationMonitor::~AuthenticationMonitor()
00044 {
00045
00046 }
00047
00048 void AuthenticationMonitor::clear()
00049 {
00050 std::list<AuthenticateBase*>::iterator itr = auths.begin();
00051 while (itr != auths.end())
00052 {
00053 AuthenticateBase* ab = *itr;
00054 ab->deleteLater();
00055 itr++;
00056 }
00057 auths.clear();
00058 }
00059
00060
00061 void AuthenticationMonitor::add(AuthenticateBase* s)
00062 {
00063 auths.push_back(s);
00064 }
00065
00066 void AuthenticationMonitor::remove(AuthenticateBase* s)
00067 {
00068 auths.remove(s);
00069 }
00070
00071 void AuthenticationMonitor::update()
00072 {
00073 if (auths.size() == 0)
00074 return;
00075
00076 int i = 0;
00077
00078 std::list<AuthenticateBase*>::iterator itr = auths.begin();
00079 while (itr != auths.end())
00080 {
00081 AuthenticateBase* ab = *itr;
00082 if (!ab || ab->isFinished())
00083 {
00084 if (ab)
00085 ab->deleteLater();
00086
00087 itr = auths.erase(itr);
00088 }
00089 else
00090 {
00091 ab->setPollIndex(-1);
00092 if (ab->getSocket() && ab->getSocket()->fd() >= 0)
00093 {
00094 int fd = ab->getSocket()->fd();
00095 if (i >= fd_vec.size())
00096 {
00097 struct pollfd pfd = {-1,0,0};
00098 fd_vec.push_back(pfd);
00099 }
00100
00101 struct pollfd & pfd = fd_vec[i];
00102 pfd.fd = fd;
00103 pfd.revents = 0;
00104 if (!ab->getSocket()->connecting())
00105 pfd.events = POLLIN;
00106 else
00107 pfd.events = POLLOUT;
00108 ab->setPollIndex(i);
00109 i++;
00110 }
00111 itr++;
00112 }
00113 }
00114
00115 #ifndef Q_WS_WIN
00116 if (poll(&fd_vec[0],i,1) > 0)
00117 #else
00118 if (mingw_poll(&fd_vec[0],i,1) > 0)
00119 #endif
00120 {
00121 handleData();
00122 }
00123 }
00124
00125 void AuthenticationMonitor::handleData()
00126 {
00127 std::list<AuthenticateBase*>::iterator itr = auths.begin();
00128 while (itr != auths.end())
00129 {
00130 AuthenticateBase* ab = *itr;
00131 if (ab && ab->getSocket() && ab->getSocket()->fd() >= 0 && ab->getPollIndex() >= 0)
00132 {
00133 int pi = ab->getPollIndex();
00134 if (fd_vec[pi].revents & POLLIN)
00135 {
00136 ab->onReadyRead();
00137 }
00138 else if (fd_vec[pi].revents & POLLOUT)
00139 {
00140 ab->onReadyWrite();
00141 }
00142 }
00143
00144 if (!ab || ab->isFinished())
00145 {
00146 ab->deleteLater();
00147 itr = auths.erase(itr);
00148 }
00149 else
00150 itr++;
00151 }
00152 }
00153
00154 }