GenericSession.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 "GenericSession.h"
00025 #include "Imports.cpp"
00026 
00028 
00043 GenericSession::GenericSession (Transport *transport_,
00044                                 DataSerializer *serializer_,
00045                                 DataQueue *queue_,
00046                                 SessionStatus *status_)
00047  :  transport (transport_),
00048     dataSerializer (serializer_),
00049     dataQueue (queue_),
00050     status (status_),
00051     state (Closed)
00052 {
00053     Q_ASSERT (transport != 0 && dataSerializer != 0
00054               && dataQueue != 0 && status != 0);
00055 }
00056 
00058 GenericSession::~GenericSession()
00059 {
00060 }
00061 
00063 
00068 void GenericSession::open()
00069 {
00070     Q_ASSERT (state == Closed);
00071     state = Opened;
00072     transportReadyRead (transport);
00073 }
00074 
00076 
00086 void GenericSession::send (const Data &data)
00087 {
00088     Q_ASSERT (state == Opened);
00089     status->sessionSendingData (this, data);
00090     if (!dataQueue->isEmpty() || !dataSerializer->write (data, transport))
00091         dataQueue->enqueue (data);
00092 }
00093 
00095 
00100 void GenericSession::abort()
00101 {
00102     state = Closing;
00103     status->sessionClosing (this);
00104     // \todo Clear buffer?
00105     transport->abort();
00106     Q_ASSERT (state == Closed);
00107 }
00108 
00110 
00115 void GenericSession::close()
00116 {
00117     Q_ASSERT (state == Opened);
00118     state = Closing;
00119     status->sessionClosing (this);
00120     if (dataQueue->isEmpty())
00121         transport->disconnectFromNode();
00122 }
00123 
00125 
00131 void GenericSession::transportConnected (Transport *)
00132 {
00133     Q_ASSERT (false);
00134 }
00135 
00137 void GenericSession::transportReadyRead (Transport *)
00138 {
00139     Q_ASSERT (state != Closed);
00140     Data data;
00141     // The handler for sessionReceivedData() may call close() or abort()
00142     // so we should check our state before trying again to read anything:
00143     while (state != Closed && dataSerializer->read (data, transport))
00144         status->sessionReceivedData (this, data);
00145 }
00146 
00148 void GenericSession::transportReadyWrite (Transport *)
00149 {
00150     Q_ASSERT (state != Closed);
00151     while (!dataQueue->isEmpty())
00152         if (dataSerializer->write (dataQueue->next(), transport))
00153             dataQueue->dequeue();
00154         else
00155             break;
00156     if (state == Closing)
00157         transport->disconnectFromNode();
00158 }
00159 
00161 void GenericSession::transportDisconnected (Transport *)
00162 {
00163     Q_ASSERT (state != Closed);
00164     state = Closed;
00165     status->sessionClosed (this);
00166 }