DataSerializer.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 #include "DataSerializer.h"
00025 #include "BodyEnd.h"
00026 #include "BodyReader.h"
00027 #include "BodyWriter.h"
00028 #include "Protocol.h"
00029 #include "Imports.cpp"
00030 
00032 
00036 DataSerializer::DataSerializer (Protocol *protocol)
00037  :  protocol (protocol), bodyReader (0), bodyWriter (0)
00038 {
00039 }
00040 
00042 
00052 bool DataSerializer::read (Data &data, Transport *transport)
00053 {
00054     if (bodyReader)
00055         return readBody (data, transport);
00056     else
00057         return readHeader (data, transport);
00058 }
00059 
00061 bool DataSerializer::readHeader (Data &data, Transport *transport)
00062 {
00063     // A header is terminated by two new lines (<CR><LF><CR><LF>):
00064     QByteArray rawHeader = transport->readTo ("\r\n\r\n");
00065     if (rawHeader.length() == 0) {
00066         // Also support <LF>-only line ends, i.e. <LF><LF> terminated:
00067         rawHeader = transport->readTo ("\n\n");
00068         if (rawHeader.length() == 0)
00069             return false;
00070     }
00071     Header header = protocol->createHeader (rawHeader);
00072     bodyReader = protocol->createBodyReader (header);
00073     Q_ASSERT (bodyReader != 0);
00074     data = header;
00075     return true;
00076 }
00077 
00079 bool DataSerializer::readBody (Data &data, Transport *transport)
00080 {
00081     Data readData = bodyReader->read (transport);
00082     if (readData == Data())
00083         return false;
00084     else {
00085         if (typeid (*readData) == typeid (BodyEnd)) {
00086             protocol->destroyBodyReader (bodyReader);
00087             bodyReader = 0; // So that next time we'll read the header.
00088         }
00089         data = readData;
00090         return true;
00091     }
00092 }
00093 
00095 
00102 bool DataSerializer::write (const Data &data, Transport *transport)
00103 {
00104     if (bodyWriter)
00105         return writeBody (data, transport);
00106     else
00107         return writeHeader (data, transport);
00108 }
00109 
00111 bool DataSerializer::writeHeader (const Data &data, Transport *transport)
00112 {
00113     Header header = data; // Will throw if types don't match.
00114     bool wroteHeader = transport->write (header->toRawBytes());
00115     if (wroteHeader) {
00116         bodyWriter = protocol->createBodyWriter (header);
00117         Q_ASSERT (bodyWriter != 0);
00118     }
00119     return wroteHeader;
00120 }
00121 
00123 bool DataSerializer::writeBody (const Data &data, Transport *transport)
00124 {
00125     if (!bodyWriter->write (data, transport))
00126         return false;
00127     if (typeid (*data) == typeid (BodyEnd)) {
00128         protocol->destroyBodyWriter (bodyWriter);
00129         bodyWriter = 0; // So that next time we'll write the header.
00130     }
00131     return true;
00132 }