main.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 //#include "3rdParty/cppunit/Exception.h"
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); // app does not have the correct type.
00087         app->exec();
00088         if (app->cppUnitException)
00089             throw std::exception (app->cppUnitException);
00090     }
00091 
00092 private:
00093     //CPPUNIT_NS::Exception cppUnitException;
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     // Interface  ClientHttpSessionFactoryStatus:
00128     void clientHttpSessionFactorySucceeded (const Uri &uri,
00129                                             ClientHttpSession *session)
00130     {
00131         session_ = session;
00132         session_->open(); // Enables reading and writing.
00133         session_->get (uri, &memoryFile);
00134     }
00135 
00136     void clientHttpSessionFactoryFailed (const Uri &)
00137     {
00138         qDebug() << "FAILED to open session!";
00139         done();
00140     }
00141 
00142     // Interface  ClientHttpSessionStatus:
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         //downloader.download ("http://www.calitko.org"); // requires chunked
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         //downloader.download ("http://www.calitko.org"); // requires chunked
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 //  QApplication::setStyle(new QCDEStyle());
00209 //  QApplication::setStyle(new QMotifStyle());
00210     QApplication::setStyle(new QPlastiqueStyle());
00211 //  QApplication::setStyle(new QWindowsXPStyle());
00212     QApplication::setWindowIcon (QIcon ("Icons/Trojan.png"));
00213     MainWindow window;
00214     window.show();
00215     return app.exec();
00216 }
00217