main.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
00021
00022
00023 #include "Qt.h"
00024
00025 #include "MainWindow.h"
00026 #include <QCDEStyle>
00027 #include <QMotifStyle>
00028 #include <QPlastiqueStyle>
00029 #include <QWindowsXPStyle>
00030
00031
00032 #include "Protocols/Http/ClientHttpSession.h"
00033 #include "Protocols/Http/ClientHttpSessionFactory.h"
00034 #include "Protocols/Http/ClientHttpSessionStatus.h"
00035 #include "Protocols/Http/HeaderBase.h"
00036 #include "Protocols/Http/ResponseHeader.h"
00037 #include "Protocols/Generics/IpResolvingTransportFactory.h"
00038 #include "Protocols/Generics/TcpTransportFactory.h"
00039 #include "Protocols/Generics/QtNameResolver.h"
00040
00041 #include <iostream>
00042
00043 class MemoryFile : public Protocols::Http::File
00044 {
00045 public:
00046 bool write (const QByteArray &bytes, bool = true)
00047 {
00048 bytes_ += bytes;
00049 return true;
00050 }
00051
00052 QByteArray bytes_;
00053 };
00054
00055 using Protocols::Generics::Data;
00056 using Protocols::Generics::IpResolvingTransportFactory;
00057 using Protocols::Generics::TcpTransportFactory;
00058 using Protocols::Generics::QtNameResolver;
00059 using Protocols::Http::ClientHttpSession;
00060 using Protocols::Http::ClientHttpSessionFactory;
00061 using Protocols::Http::HeaderBase;
00062 using Protocols::Http::ResponseHeader;
00063 using Utils::Uri;
00064
00065
00066 #include <exception>
00067
00068 class TesterApp : QCoreApplication
00069 {
00071
00082 static int exec()
00083 {
00084 TesterApp *app = dynamic_cast <TesterApp *> (
00085 QCoreApplication::instance());
00086 Q_ASSERT (app != 0);
00087 app->exec();
00088 if (app->cppUnitException)
00089 throw std::exception (app->cppUnitException);
00090 }
00091
00092 private:
00093
00094 std::exception cppUnitException;
00095 };
00096
00097 class HttpDownloader : public Protocols::Http::ClientHttpSessionFactoryStatus,
00098 public Protocols::Http::ClientHttpSessionStatus
00099 {
00100 QtNameResolver qtNameResolver_;
00101 TcpTransportFactory tcpTransportFactory_;
00102 IpResolvingTransportFactory ipResolvingTransportFactory_;
00103 ClientHttpSessionFactory clientHttpSessionFactory_;
00104 ClientHttpSession *session_;
00105 MemoryFile memoryFile;
00106
00107 public:
00108 HttpDownloader()
00109 : qtNameResolver_(),
00110 tcpTransportFactory_(),
00111 ipResolvingTransportFactory_ (&tcpTransportFactory_, &qtNameResolver_),
00112 clientHttpSessionFactory_ (&ipResolvingTransportFactory_),
00113 session_ (0),
00114 memoryFile()
00115 {
00116 }
00117
00118 ~HttpDownloader()
00119 {
00120 }
00121
00122 void download (const QByteArray &url)
00123 {
00124 clientHttpSessionFactory_.createSession (Uri::fromUnencoded (url), this, this);
00125 }
00126
00127
00128 void clientHttpSessionFactorySucceeded (const Uri &uri,
00129 ClientHttpSession *session)
00130 {
00131 session_ = session;
00132 session_->open();
00133 session_->get (uri, &memoryFile);
00134 }
00135
00136 void clientHttpSessionFactoryFailed (const Uri &)
00137 {
00138 qDebug() << "FAILED to open session!";
00139 done();
00140 }
00141
00142
00143 void clientHttpSessionResponseStarted (const ResponseHeader &header)
00144 {
00145 qDebug() << "Response Header:";
00146 qDebug() << header.toRawBytes();
00147 }
00148
00149 void clientHttpSessionResponseFinished (const HeaderBase &header)
00150 {
00151 qDebug() << "FINISHED";
00152 qDebug() << header.toRawBytes();
00153 qDebug() << "Response DATA:";
00154 std::cout.write (memoryFile.bytes_.constData(), memoryFile.bytes_.size());
00155 session_->close();
00156 }
00157
00158 void clientHttpSessionInvaidResponse (const Data &)
00159 {
00160 qDebug() << "INVALID RESPONSE";
00161 done();
00162 }
00163
00164 void clientHttpSessionClosing()
00165 {
00166 qDebug() << "Closing session...";
00167 }
00168
00169 void clientHttpSessionClosed()
00170 {
00171 qDebug() << "Session closed.";
00172 done();
00173 }
00174
00175 private:
00176 void done()
00177 {
00178 if (session_ != 0)
00179 clientHttpSessionFactory_.destroySession (session_);
00180 QCoreApplication::exit(0);
00181 }
00182 };
00183
00184 int main (int argc, char *argv[])
00185 {
00186 QCoreApplication app (argc, argv);
00187 {
00188 HttpDownloader downloader;
00189
00190 downloader.download ("http://bzr.calitko.org/developers/peter/trac-0.10.3-changeset.request.parsing.patch");
00191 int ret = app.exec();
00192 qDebug() << "app.exec() returned" << ret;
00193 }
00194 qDebug() << "before the second QCoreApplication ctor";
00195 {
00196 HttpDownloader downloader;
00197
00198 downloader.download ("http://bzr.calitko.org/developers/peter/trac-0.10.3-changeset.request.parsing.patch");
00199 int ret = app.exec();
00200 qDebug() << "app.exec() returned" << ret;
00201 }
00202 return 0;
00203 }
00204
00205 int main_old(int argc, char *argv[])
00206 {
00207 QApplication app(argc, argv);
00208
00209
00210 QApplication::setStyle(new QPlastiqueStyle());
00211
00212 QApplication::setWindowIcon (QIcon ("Icons/Trojan.png"));
00213 MainWindow window;
00214 window.show();
00215 return app.exec();
00216 }
00217