kopete/protocols/messenger/libpapillon
httpconnection.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "Papillon/Http/Connection"
00016
00017
00018 #include <QtCore/QQueue>
00019 #include <QtDebug>
00020
00021
00022 #include "Papillon/Network/IpEndpointConnector"
00023 #include "Papillon/Network/NetworkStream"
00024 #include "Papillon/Http/CoreProtocol"
00025 #include "Papillon/Http/Transfer"
00026
00027 namespace Papillon
00028 {
00029
00030 class HttpConnection::Private
00031 {
00032 public:
00033 Private()
00034 : connector(0)
00035 {}
00036
00037 ~Private()
00038 {
00039 delete connector;
00040 }
00041
00042 IpEndpointConnector *connector;
00043 HttpCoreProtocol protocol;
00044 QQueue<HttpTransfer*> transferQueue;
00045 QString cookie;
00046 };
00047
00048 HttpConnection::HttpConnection(QObject *parent)
00049 : QObject(parent), d(new Private)
00050 {
00051 d->connector = new IpEndpointConnector(true, this);
00052
00053 connect(d->connector, SIGNAL(connected()), this, SIGNAL(connected()));
00054 connect(d->connector, SIGNAL(closed()), this, SIGNAL(disconnected()));
00055 connect(d->connector->networkStream(), SIGNAL(readyRead()), this, SLOT(streamReadyRead()));
00056
00057 connect(&d->protocol, SIGNAL(outgoingData(QByteArray)), this, SLOT(protocolOutgoingData(QByteArray)));
00058 connect(&d->protocol, SIGNAL(incomingData()), this, SLOT(protocolIncomingData()));
00059 }
00060
00061 HttpConnection::~HttpConnection()
00062 {
00063 delete d;
00064 }
00065
00066 void HttpConnection::setCookie(const QString &cookie)
00067 {
00068 d->cookie = cookie;
00069 }
00070
00071 QString HttpConnection::cookie() const
00072 {
00073 return d->cookie;
00074 }
00075
00076 void HttpConnection::connectToServer(const QString &server)
00077 {
00078 d->connector->connectWithAddressInfo(server, 443);
00079 }
00080
00081 void HttpConnection::disconnectFromServer()
00082 {
00083 d->connector->close();
00084 }
00085
00086 HttpTransfer *HttpConnection::read()
00087 {
00088 if( d->transferQueue.isEmpty() )
00089 return 0;
00090 else
00091 return d->transferQueue.dequeue();
00092 }
00093
00094 void HttpConnection::write(HttpTransfer *transfer)
00095 {
00096 qDebug() << Q_FUNC_INFO << "Sending an HttpTransfer on the server.";
00097
00098 d->protocol.outgoingTransfer(transfer);
00099 }
00100
00101 void HttpConnection::streamReadyRead()
00102 {
00103 d->protocol.addIncomingData( d->connector->networkStream()->readAll() );
00104 }
00105
00106 void HttpConnection::protocolOutgoingData(const QByteArray &data)
00107 {
00108 d->connector->networkStream()->write(data);
00109 }
00110
00111 void HttpConnection::protocolIncomingData()
00112 {
00113 HttpTransfer *incoming = d->protocol.incomingTransfer();
00114 if( incoming )
00115 {
00116 d->transferQueue.enqueue( incoming );
00117 emit readyRead();
00118 }
00119 }
00120
00121 }
00122
00123 #include "httpconnection.moc"