Protocols::Generics::SocketTransport Class Reference

#include <SocketTransport.h> [code]

Inherits Protocols::Generics::Transport, and Protocols::Generics::SocketStatus.

Inherited by Protocols::Generics::TcpTransport.

Inheritance diagram for Protocols::Generics::SocketTransport:

Inheritance graph
[legend]
Collaboration diagram for Protocols::Generics::SocketTransport:

Collaboration graph
[legend]
List of all members.

Detailed Description

Implements the Transport interface on top of Socket and SocketBuffer.

Object of this class could be combined with different implementations of Socket and SocketBuffer to consistently provide the Transport interface.

SocketTransport ignores certain sequences of Socket status notifications in order to guarantee that the connected() notification is sent only once, for example. SocketTransport also delegates reading/writing from/to the Socket object to the SocketBuffer object. This allows the user to create a SocketTransport object implementing different buffering policies by providing a different SocketBuffer implementation to the SocketTransport ctor.

Definition at line 48 of file SocketTransport.h.

Public Member Functions

 SocketTransport ()
 SocketTransport (Socket *socketToUse, SocketBuffer *socketBufferToUse, TransportStatus *status)
 Constructs a SocketTransport object.
 ~SocketTransport ()
 Destructor.
void connectToNode (const Uri &)
 Tries to establish a connection to nodeAddress.
void disconnectFromNode ()
 Disconnects from the remote node.
void abort ()
 Drops buffered data and closes the connection.
bool canRead (int count) const
QByteArray peek (int count) const
QByteArray peekAtMost (int count) const
QByteArray peekTo (const QByteArray &delimiter) const
QByteArray peekAll () const
QByteArray read (int count)
QByteArray readAtMost (int count)
QByteArray readTo (const QByteArray &delimiter)
QByteArray readAll ()
bool canWrite (int count) const
bool write (const QByteArray &bytes, bool flush)
void setTransportStatus (TransportStatus *)
void socketConnected ()
 Handler for the SocketStatus::socketConnected() message.
void socketDisconnected ()
 Handler for the SocketStatus::socketDisconnected() message.
void socketError ()
 Handler for the SocketStatus::socketError() message.
void socketRead ()
 Handler for the SocketStatus::socketRead() message.
void socketWritten ()
 Handler for the SocketStatus::socketWritten() message.

Private Types

enum  State {
  DisconnectedState,
  ConnectingState,
  ConnectedState,
  DisconnectingState
}

Private Member Functions

CALITKO_TESTABLE SocketTransport (const SocketTransport &)
SocketTransportoperator= (const SocketTransport &)
void tryWriteBufferToSocket (bool canSendReadyWrite)
void tryReadBufferFromSocket (bool canSendReadyRead)

Private Attributes

Socketsocket
SocketBuffersocketBuffer
TransportStatusstatus
State state
bool hasCalledWriteBufferToSocket
bool hasCalledReadBufferFromSocket


Member Enumeration Documentation

enum Protocols::Generics::SocketTransport::State [private]
 

Enumerator:
DisconnectedState 
ConnectingState 
ConnectedState 
DisconnectingState 

Definition at line 99 of file SocketTransport.h.


Constructor & Destructor Documentation

CALITKO_TESTABLE Protocols::Generics::SocketTransport::SocketTransport const SocketTransport  )  [private]
 

Protocols::Generics::SocketTransport::SocketTransport  ) 
 

SocketTransport::SocketTransport Socket socketToUse,
SocketBuffer socketBufferToUse,
TransportStatus statusReceiver
 

Constructs a SocketTransport object.

socketToUse the socket object we should bind to. socketBufferToUse the socket object we would use to buffer incoming and outgoing data from and to socketToUse. statusReceiver is the object which receives our status notifications.

Definition at line 34 of file SocketTransport.cpp.

SocketTransport::~SocketTransport  ) 
 

Destructor.

Destroys SocketTransport object.

Definition at line 50 of file SocketTransport.cpp.


Member Function Documentation

void SocketTransport::abort  )  [virtual]
 

Drops buffered data and closes the connection.

The disconnected() status is sent immediately.

Implements Protocols::Generics::Connection.

Definition at line 99 of file SocketTransport.cpp.

bool SocketTransport::canRead int  count  )  const [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 180 of file SocketTransport.cpp.

bool SocketTransport::canWrite int  count  )  const [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 242 of file SocketTransport.cpp.

void SocketTransport::connectToNode const Uri nodeAddress  )  [virtual]
 

Tries to establish a connection to nodeAddress.

A connection attempt is started in background. Success is indicated by sending the connected() status. The disconnected() status is sent to to indicate failure.

Precondition:
The internal socket object is disconnected.

The given nodeAddress contains valid IP address and port.

Todo:
Should we require that the scheme of nodeAddress determines what kind of socket to use (UDP or TCP)? In this case we could assert that socket is of the correct type.

Implements Protocols::Generics::Connection.

Definition at line 67 of file SocketTransport.cpp.

void SocketTransport::disconnectFromNode  )  [virtual]
 

Disconnects from the remote node.

The disconnection is initiated and the status disconnected() will be sent later when it is completed.

Precondition:
The TcpConnection object is connected.

Implements Protocols::Generics::Connection.

Definition at line 87 of file SocketTransport.cpp.

SocketTransport& Protocols::Generics::SocketTransport::operator= const SocketTransport  )  [private]
 

QByteArray SocketTransport::peek int  count  )  const [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 186 of file SocketTransport.cpp.

QByteArray SocketTransport::peekAll  )  const [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 204 of file SocketTransport.cpp.

QByteArray SocketTransport::peekAtMost int  count  )  const [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 192 of file SocketTransport.cpp.

QByteArray SocketTransport::peekTo const QByteArray &  delimiter  )  const [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 198 of file SocketTransport.cpp.

QByteArray SocketTransport::read int  count  )  [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 210 of file SocketTransport.cpp.

QByteArray SocketTransport::readAll  )  [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 234 of file SocketTransport.cpp.

QByteArray SocketTransport::readAtMost int  count  )  [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 218 of file SocketTransport.cpp.

QByteArray SocketTransport::readTo const QByteArray &  delimiter  )  [virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 226 of file SocketTransport.cpp.

void SocketTransport::setTransportStatus TransportStatus  )  [virtual]
 

Implements Protocols::Generics::Transport.

Definition at line 298 of file SocketTransport.cpp.

void SocketTransport::socketConnected  )  [virtual]
 

Handler for the SocketStatus::socketConnected() message.

Makes sure that the connected() status message is sent only once.

Implements Protocols::Generics::SocketStatus.

Definition at line 111 of file SocketTransport.cpp.

void SocketTransport::socketDisconnected  )  [virtual]
 

Handler for the SocketStatus::socketDisconnected() message.

Makes sure that the disconnected() status message is only sent once.

Implements Protocols::Generics::SocketStatus.

Definition at line 124 of file SocketTransport.cpp.

void SocketTransport::socketError  )  [virtual]
 

Handler for the SocketStatus::socketError() message.

An established connection is directly abort()-ed in case of an error.

Implements Protocols::Generics::SocketStatus.

Definition at line 137 of file SocketTransport.cpp.

void SocketTransport::socketRead  )  [virtual]
 

Handler for the SocketStatus::socketRead() message.

Try to read something from the socket and send the readyRead() status notification.

Note:
The buffer may not actually be able to read any new bytes from the socket but we send readyRead() anyway.

Implements Protocols::Generics::SocketStatus.

Definition at line 159 of file SocketTransport.cpp.

void SocketTransport::socketWritten  )  [virtual]
 

Handler for the SocketStatus::socketWritten() message.

Try to write something to the socket and send the readyWrite() socket notification.

Note:
tryWriteTo() may not succeed to write anything to the socket by there might be some free space in the buffer anyway, so we send readyWrite() anyway.

Implements Protocols::Generics::SocketStatus.

Definition at line 174 of file SocketTransport.cpp.

void SocketTransport::tryReadBufferFromSocket bool  canSendReadyRead  )  [private]
 

Definition at line 284 of file SocketTransport.cpp.

void SocketTransport::tryWriteBufferToSocket bool  canSendReadyWrite  )  [private]
 

Definition at line 270 of file SocketTransport.cpp.

bool SocketTransport::write const QByteArray &  bytes,
bool  flush
[virtual]
 

Implements Protocols::Generics::Buffer.

Definition at line 248 of file SocketTransport.cpp.


Member Data Documentation

bool Protocols::Generics::SocketTransport::hasCalledReadBufferFromSocket [private]
 

Definition at line 112 of file SocketTransport.h.

bool Protocols::Generics::SocketTransport::hasCalledWriteBufferToSocket [private]
 

Definition at line 111 of file SocketTransport.h.

Socket* Protocols::Generics::SocketTransport::socket [private]
 

Reimplemented in Protocols::Generics::TcpTransport.

Definition at line 107 of file SocketTransport.h.

SocketBuffer* Protocols::Generics::SocketTransport::socketBuffer [private]
 

Reimplemented in Protocols::Generics::TcpTransport.

Definition at line 108 of file SocketTransport.h.

State Protocols::Generics::SocketTransport::state [private]
 

Definition at line 110 of file SocketTransport.h.

TransportStatus* Protocols::Generics::SocketTransport::status [private]
 

Definition at line 109 of file SocketTransport.h.


The documentation for this class was generated from the following files: