FREEDM DGI
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
freedm::broker::CProtocolSR Class Reference

A reliable connection protocol with sweet as expirations.

#include <CProtocolSR.hpp>

Inheritance diagram for freedm::broker::CProtocolSR:
freedm::broker::IProtocol

Public Member Functions

 CProtocolSR (std::string uuid, boost::asio::ip::udp::endpoint endpoint)
 Initializes the protocol with the underlying connection. More...
 
virtual void ChangePhase (bool)
 Handles the change phase even. More...
 
int GetReliability () const
 Get the connection reliability for DCUSTOMNETWORK. More...
 
boost::asio::ip::udp::socket & GetSocket ()
 Get a socket connected to a single peer DGI. More...
 
bool GetStopped ()
 Handles checking to see if the connection is stopped. More...
 
std::string GetUUID () const
 Gets the uuid: More...
 
void OnReceive ()
 Writes the window (with acks on message receipt) More...
 
bool Receive (const ProtocolMessage &msg)
 deterimines if a messageshould be given to the dispatcher More...
 
void ReceiveACK (const ProtocolMessage &msg)
 Public facing function that handles marking down ACKs for sent messages. More...
 
void Send (const ModuleMessage &msg)
 Public facing send function that sends a message. More...
 
void SendACK (const ProtocolMessage &msg)
 Handles Writing an ack for the input message to the channel. More...
 
void SendSYN ()
 Sends a synchronizer. More...
 
void SetReliability (int r)
 Set the connection reliability for DCUSTOMNETWORK. More...
 
void SetStopped (bool v)
 Handles setting the stopped variable. More...
 
void Stop ()
 Stops the timers. More...
 
void Write (ProtocolMessageWindow &msg)
 Handles writing the message to the underlying connection. More...
 
void WriteWindow ()
 Writes a whole window to the channel. More...
 

Protected Member Functions

virtual void WriteCallback (const boost::system::error_code &)
 Callback for when a write completes. More...
 

Private Member Functions

void Resend (const boost::system::error_code &err)
 Resend outstanding messages. More...
 

Private Attributes

std::deque< ProtocolMessage > m_ack_window
 
unsigned int m_dropped
 The number that have been dropped. More...
 
unsigned int m_inresyncs
 Counts the number of times this one has been resynced. More...
 
unsigned int m_inseq
 The expected next in sequence number. More...
 
bool m_insync
 Marks if this has been synced. More...
 
boost::posix_time::ptime m_insynctime
 Time the last accepted sync was. More...
 
unsigned int m_outseq
 The next number to assign to an outgoing message. More...
 
bool m_outsync
 Marks if we've sent the outsync for this connection. More...
 
google::protobuf::uint64 m_outsynchash
 Keeps track of the last resync that we've seen. More...
 
unsigned int m_sendkill
 The hash to... MURDER. More...
 
bool m_sendkills
 Marks if we should send the kill hash. More...
 
boost::asio::deadline_timer m_timeout
 Timeout for resends. More...
 
bool m_timer_active
 Indicates if the timer is active. More...
 
std::deque< ProtocolMessage > m_window
 The window. More...
 

Static Private Attributes

static const unsigned int MAX_DROPPED_MSGS = 3
 The number of messages that have to be dropped before the connection is dead. More...
 
static const unsigned int REFIRE_TIME = 10
 Refire time in MS. More...
 
static const unsigned int SEQUENCE_MODULO = 1024
 Sequence modulo. More...
 

Constructor & Destructor Documentation

freedm::broker::CProtocolSR::CProtocolSR ( std::string  uuid,
boost::asio::ip::udp::endpoint  endpoint 
)
explicit

CProtocolSR::CProtocolSR

Description:
Constructor for the CProtocolSR class.
Precondition:
The object is uninitialized.
Postcondition:
The object is initialized: m_killwindow is empty, the connection is marked as unsynced, It won't be sending kill statuses. Its first message will be numbered as 0 for outgoing and the timer is not set.
Parameters
uuidThe peer this connection is made to.
endpointThe endpoint that will be the destination for sent messages

Member Function Documentation

virtual void freedm::broker::IProtocol::ChangePhase ( bool  )
inlinevirtualinherited
int freedm::broker::IProtocol::GetReliability ( ) const
inherited

IProtocol::GetReliability

Description:
Get the connection reliability for DCUSTOMNETWORK
Precondition:
None
Postcondition:
None
Returns
percentage of packets that are allowed through
boost::asio::ip::udp::socket& freedm::broker::IProtocol::GetSocket ( )
inherited
bool freedm::broker::IProtocol::GetStopped ( )
inlineinherited
std::string freedm::broker::IProtocol::GetUUID ( ) const
inherited

IProtocol::GetUUID

Description:
Gets the UUID of the DGI on the other end of this connection.
Precondition:
None
Postcondition:
None
Returns
the peer's UUID
void freedm::broker::CProtocolSR::OnReceive ( )
virtual

CProtocolSR::OnReceive

Description:
When a message is received, write the window to the channel, then flush the ack queue.
Precondition:
None
Postcondition:
There are no acks queued and the message has been written to the channel.

Implements freedm::broker::IProtocol.

bool freedm::broker::CProtocolSR::Receive ( const ProtocolMessage &  msg)
virtual

CProtocolSR::Receive

Description:
Accepts a message into the protocol, if that message should be accepted. If this function returns true, the message is passed to the dispatcher. Since this message accepts SYNs there might be times when processing and state changes but the message is marked as "rejected" this is normal.
Precondition:
Accept logic can be complicated, there are several scenarios that should be addressed. 1) A bad request has been received 2) A SYN message is received for the first time 3) A SYN message is received as a duplicate. 4) A Message has been received before the connection has been synced. 5) A Message has been received with the expected sequenceno with or without a kill flag. 6) A message has been received with a kill flag. The kill is greater than the expected sequence number 7) A message has been received with a kill flag. The kill is less than the expected sequence number. However, the message's number is less than the expected sequence number 8) A message has been received with a kill flag. The kill is less than the expected sequence number and the message's sequence number is greater than the expected sequence number.
Postcondition:
Cases are handled as follows: 1) The connection is resynced. 2) The message is ACKed, the send time of the sync is noted, and the connection is synced. 3) The SYN is ignored. 4) A bad request message is generated and sent to the source. 5) The message is accepted. 6) The message is rejected. Kills should only ever be less than the expected sequence number unless the message is arrived out of order 7) The message is simply old but still arriving at the socket, and can be rejected. 8) The message should be accepted because one or more message expired in the gap of sequence numbers.
Parameters
msgThe received message
Returns
True if the message is accepted, false otherwise.

Implements freedm::broker::IProtocol.

void freedm::broker::CProtocolSR::ReceiveACK ( const ProtocolMessage &  msg)
virtual

CProtocolSR::ReceiveACK

Description:
Marks a message as acknowledged by the receiver and moves to transmit the next message.
Precondition:
A message has been sent.
Postcondition:
If the ACK corresponds to the head of window by a match of sequence number and the message hash, the the message is popped and the killable flag is set to false, since the head of the window has never been sent. If the there is still an message in the window to send, the resend function is called.
Parameters
msgThe received ACK message

Implements freedm::broker::IProtocol.

void freedm::broker::CProtocolSR::Resend ( const boost::system::error_code &  err)
private

CProtocolSR::CProtocolSR

Description:
Handles refiring ACKs and Sent Messages.
Precondition:
The connection has received or sent at least one message.
Postcondition:
One of the following conditions or combination of states is upheld: 1) An ack for a message that has not yet expired has been resent and a timer to call resend has been set. 2) Message(s) has/have expired and are removed from the queue. The flag to send kills is set. 3) The window is empty and no message is set to the channel, the timer is not re-set. 4) A message expired and then next message will cause the sequence numbers to wrap, (or they have wrapped since the last time a message was successfully sent) so a sync is inserted at the front of the queue to skip that case on the receiver side. The sendkill flag is cleared and the sendkill value is cleared. 5) If there is still a message to resend, the timer is reset.
Parameters
errThe timer error code. If the err is 0 then the timer expired

We use static pointer cast to convert the IPROTOCOL pointer to this derived type

void freedm::broker::CProtocolSR::Send ( const ModuleMessage &  msg)
virtual

CProtocolSR::CProtocolSR

Description:
Send function for the CProtocolSR. Sending using this protocol involves an alternating bit scheme. Messages can expire and delivery won't be attempted after the deadline is passed. Killed messages are noted in the next outgoing message. The receiver tracks the killed messages and uses them to help maintain ordering.
Precondition:
The protocol is intialized.
Postcondition:
At least one message is in the channel and actively being resent. The send window is greater than or equal to one. The timer for the resend is freshly set or is currently running for a resend. If a message is written to the channel, the m_killable flag is set.
Parameters
msgThe message to write to the channel.

Implements freedm::broker::IProtocol.

void freedm::broker::CProtocolSR::SendACK ( const ProtocolMessage &  msg)
virtual

CProtocolSR::SendACK

Description:
Composes an ack and PrepareAndWrites it to the channel. ACKS are saved to the protocol's state and are written again during resends to try and maximize througput.
Parameters
msgThe message to ACK.
Precondition:
A message has been accepted.
Postcondition:
The m_currentack member is set to the ack and the message will be resent during resend until it expires.

Implements freedm::broker::IProtocol.

void freedm::broker::CProtocolSR::SendSYN ( )

CProtocolSR::SendSYN

Description:
Composes an SYN and writes it to the channel.
Precondition:
A message has been accepted.
Postcondition:
A syn has been written to the channel
void freedm::broker::IProtocol::SetReliability ( int  r)
inherited

IProtocol::SetReliability

Description:
Set the connection reliability for DCUSTOMNETWORK. 0 all packets areartifically dropped. 100 means no packets are artifically dropped.
Precondition:
None
Postcondition:
Sets the reliabilty of the protocol to r
Parameters
rbetween 0 and 100
void freedm::broker::IProtocol::SetStopped ( bool  v)
inlineinherited
void freedm::broker::CProtocolSR::Stop ( )
inlinevirtual
void freedm::broker::CProtocolSR::Write ( ProtocolMessageWindow &  msg)
virtual

CProtocolSR::Write

Description:
Writes the message to the connected peer. Also, timestamps the message, if a timestamp does not already exist.
Precondition:
None
Postcondition:
Writes the message to the channel.
Parameters
msgthe message write.

Reimplemented from freedm::broker::IProtocol.

virtual void freedm::broker::IProtocol::WriteCallback ( const boost::system::error_code &  )
inlineprotectedvirtualinherited
void freedm::broker::CProtocolSR::WriteWindow ( )

CProtocolSR::WriteWindow

Description:
Creates a message bundle of outstanding messages to write to the channel.
Precondition:
None
Postcondition:
Writes the window to the channel.

Member Data Documentation

std::deque<ProtocolMessage> freedm::broker::CProtocolSR::m_ack_window
private
unsigned int freedm::broker::CProtocolSR::m_dropped
private
unsigned int freedm::broker::CProtocolSR::m_inresyncs
private
unsigned int freedm::broker::CProtocolSR::m_inseq
private
bool freedm::broker::CProtocolSR::m_insync
private
boost::posix_time::ptime freedm::broker::CProtocolSR::m_insynctime
private
unsigned int freedm::broker::CProtocolSR::m_outseq
private
bool freedm::broker::CProtocolSR::m_outsync
private
google::protobuf::uint64 freedm::broker::CProtocolSR::m_outsynchash
private
unsigned int freedm::broker::CProtocolSR::m_sendkill
private
bool freedm::broker::CProtocolSR::m_sendkills
private
boost::asio::deadline_timer freedm::broker::CProtocolSR::m_timeout
private
bool freedm::broker::CProtocolSR::m_timer_active
private
std::deque<ProtocolMessage> freedm::broker::CProtocolSR::m_window
private
const unsigned int freedm::broker::CProtocolSR::MAX_DROPPED_MSGS = 3
staticprivate
const unsigned int freedm::broker::CProtocolSR::REFIRE_TIME = 10
staticprivate
const unsigned int freedm::broker::CProtocolSR::SEQUENCE_MODULO = 1024
staticprivate

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