SocketTransport.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-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 #ifndef PROTOCOLS__GENERICS__SOCKET_TRANSPORT_H
00024 #define PROTOCOLS__GENERICS__SOCKET_TRANSPORT_H
00025 
00026 #include "Transport.h"
00027 #include "TransportStatus.h"
00028 #include "Socket.h"
00029 #include "SocketStatus.h"
00030 #include "SocketBuffer.h"
00031 
00032 namespace Protocols {
00033 namespace Generics {
00034 
00036 
00048 class SocketTransport : public Transport, public SocketStatus
00049 {
00050     CALITKO_TESTABLE
00051     REFERENCE_OBJECT (SocketTransport)
00052 
00053 public:
00054                     SocketTransport();
00055                     SocketTransport (Socket *socketToUse,
00056                                      SocketBuffer *socketBufferToUse,
00057                                      TransportStatus *status);
00058                     ~SocketTransport();
00059 
00060     // Implementation for the Transport::Connection interface:
00061     void            connectToNode (const Uri &);
00062     void            disconnectFromNode();
00063     void            abort();
00064 
00065     //NodeAddress       ownNodeAddress() const;
00066     //NodeAddress       remoteNodeAddress() const;
00067 
00068     // Implementation for the Transport::Buffer interface:
00069     bool            canRead (int count) const;
00070 
00071     QByteArray      peek (int count) const;
00072     QByteArray      peekAtMost (int count) const;
00073     QByteArray      peekTo (const QByteArray &delimiter) const;
00074     QByteArray      peekAll() const;
00075 
00076     QByteArray      read (int count);
00077     QByteArray      readAtMost (int count);
00078     QByteArray      readTo (const QByteArray &delimiter);
00079     QByteArray      readAll();
00080 
00081     bool            canWrite (int count) const;
00082 
00083     bool            write (const QByteArray &bytes, bool flush);
00084 
00085     // Implementation for the Transport interface:
00086     void            setTransportStatus (TransportStatus *);
00087 
00088     // Implementation for the SocketStatus interface:
00089     void            socketConnected();
00090     void            socketDisconnected();
00091     void            socketError();
00092     void            socketRead();
00093     void            socketWritten();
00094 
00095 private:
00096     void            tryWriteBufferToSocket (bool canSendReadyWrite);
00097     void            tryReadBufferFromSocket (bool canSendReadyRead);
00098 
00099     enum State
00100     {
00101         DisconnectedState,
00102         ConnectingState,
00103         ConnectedState,
00104         DisconnectingState
00105     };
00106 
00107     Socket                      *socket;
00108     SocketBuffer                *socketBuffer;
00109     TransportStatus             *status;
00110     State                       state;
00111     bool                        hasCalledWriteBufferToSocket;
00112     bool                        hasCalledReadBufferFromSocket;
00113 };
00114 
00115 } // namespace Generics
00116 } // namespace Protocols
00117 
00118 #endif // PROTOCOLS__GENERICS__SOCKET_TRANSPORT_H