fix8  version 1.4.0
Open Source C++ FIX Framework
FIX8::ReliableClientSession< T > Class Template Reference

Reliable Client wrapper. This client attempts to recover from disconnects and login rejects. More...

#include <sessionwrapper.hpp>

Inheritance diagram for FIX8::ReliableClientSession< T >:
FIX8::ClientSession< T > FIX8::ClientSessionBase FIX8::SessionConfig FIX8::Configuration

Public Types

enum  { no_servers_configured =0xffff }
 
using ReliableClientSession_ptr = std::unique_ptr< ReliableClientSession< T >>
 Convenient scoped pointer for your session. More...
 
- Public Types inherited from FIX8::ClientSession< T >
using ClientSession_ptr = std::unique_ptr< ClientSession< T >>
 Convenient scoped pointer for your session. More...
 
using session_type = T
 
- Public Types inherited from FIX8::Configuration
enum  group_types {
  g_sessions, g_persisters, g_loggers, g_server_group,
  g_ssl_context, g_schedules, g_logins, g_client_group,
  g_count
}
 
enum  Logtype { session_log, protocol_log }
 

Public Member Functions

 ReliableClientSession (const F8MetaCntx &ctx, const std::string &conf_file, const std::string &session_name)
 Ctor. Prepares session for connection as an initiator. More...
 
bool has_given_up () const
 
virtual ~ReliableClientSession ()
 Dtor. More...
 
virtual void start (bool wait, unsigned send_seqnum=0, unsigned recv_seqnum=0, const f8String davi=f8String())
 
size_t get_attempts_cnt () const
 
size_t get_server_cnt () const
 
const Serverget_server (unsigned idx=no_servers_configured) const
 
int operator() ()
 
f8_thread_cancellation_tokencancellation_token ()
 
- Public Member Functions inherited from FIX8::ClientSession< T >
 ClientSession (const F8MetaCntx &ctx, const std::string &conf_file, const std::string &session_name, bool init_con_later=false)
 Ctor. Prepares session for connection as an initiator. More...
 
virtual ~ClientSession ()
 Dtor. More...
 
T * session_ptr ()
 
- Public Member Functions inherited from FIX8::ClientSessionBase
 ClientSessionBase (const F8MetaCntx &ctx, const std::string &conf_file, const std::string &session_name)
 
virtual ~ClientSessionBase ()
 Dtor. More...
 
- Public Member Functions inherited from FIX8::SessionConfig
 SessionConfig (const F8MetaCntx &ctx, const std::string &conf_file, const std::string &session_name)
 Ctor. Loads configuration, obtains session details, sets up logfile flags. More...
 
virtual ~SessionConfig ()
 Dtor. More...
 
const XmlElementget_session_element () const
 
- Public Member Functions inherited from FIX8::Configuration
const XmlElementfind_group (group_types type, const std::string &tag) const
 
 Configuration (const std::string &xmlfile, bool do_process=false)
 
 Configuration (std::istream &istr, bool do_process=false)
 
virtual ~Configuration ()
 Dtor. More...
 
F8API int process ()
 
const XmlElementget_session (const unsigned num) const
 
F8API Connection::Role get_role (const XmlElement *from) const
 
F8API size_t get_addresses (const XmlElement *from, std::vector< Server > &target) const
 
F8API Poco::Net::SocketAddress get_address (const XmlElement *from) const
 
F8API Poco::Net::IPAddress get_ip (const XmlElement *from) const
 
template<typename T >
get_logflags (const std::string &tag, const std::vector< std::string > &names, const XmlElement *from, Logger::LogPositions *positions=nullptr) const
 
F8API std::string & get_logname (const XmlElement *from, std::string &to, const SessionID *sid=nullptr) const
 
unsigned get_connect_timeout (const XmlElement *from, const unsigned def=defaults::connect_timeout) const
 
unsigned get_retry_interval (const XmlElement *from, const unsigned def=defaults::retry_interval) const
 
unsigned get_retry_count (const XmlElement *from, const int def=defaults::login_retries) const
 
unsigned get_tcp_recvbuf_sz (const XmlElement *from, const unsigned def=0) const
 
unsigned get_tcp_sendbuf_sz (const XmlElement *from, const unsigned def=0) const
 
unsigned get_version (const XmlElement *from, const unsigned def=0) const
 
unsigned get_tabsize (const XmlElement *from, const unsigned def=defaults::tabsize) const
 
unsigned get_logfile_rotation (const XmlElement *from, const unsigned def=defaults::log_rotation) const
 
unsigned get_heartbeat_interval (const XmlElement *from, const unsigned def=defaults::hb_interval) const
 
bool get_tcp_nodelay (const XmlElement *from, const bool def=true) const
 
bool get_tcp_keepalive (const XmlElement *from, const bool def=false) const
 
bool get_tcp_reuseaddr (const XmlElement *from, const bool def=false) const
 
int get_tcp_linger (const XmlElement *from, const int def=-1) const
 
bool get_silent_disconnect (const XmlElement *from, const bool def=false) const
 
bool get_enforce_compids_flag (const XmlElement *from, const bool def=true) const
 
bool get_ignore_logon_sequence_check_flag (const XmlElement *from, const bool def=false) const
 
bool get_no_chksum_flag (const XmlElement *from, const bool def=false) const
 
bool get_permissive_mode_flag (const XmlElement *from, const bool def=false) const
 
bool get_reset_sequence_number_flag (const XmlElement *from, const bool def=false) const
 
bool get_always_seqnum_assign (const XmlElement *from, const bool def=false) const
 
F8API ProcessModel get_process_model (const XmlElement *from) const
 
default_appl_ver_id get_default_appl_ver_id (const XmlElement *from) const
 
sender_comp_id get_sender_comp_id (const XmlElement *from) const
 
target_comp_id get_target_comp_id (const XmlElement *from) const
 
F8API Persistercreate_persister (const XmlElement *from, const SessionID *sid=nullptr, bool flag=false) const
 
F8API Loggercreate_logger (const XmlElement *from, const Logtype ltype, const SessionID *sid=nullptr) const
 
F8API Schedule create_schedule (const XmlElement *from) const
 
F8API Clients create_clients (const XmlElement *from) const
 
F8API Schedule create_login_schedule (const XmlElement *from) const
 
F8API Session_Schedulecreate_session_schedule (const XmlElement *from) const
 
F8API unsigned get_all_sessions (std::vector< const XmlElement * > &target, const Connection::Role role=Connection::cn_unknown) const
 
const XmlElementget_root () const
 
template<typename T >
get_logflags (const string &tag, const vector< string > &names, const XmlElement *from, Logger::LogPositions *positions) const
 

Private Attributes

f8_thread< ReliableClientSession< T > > _thread
 
unsigned _send_seqnum = 0
 
unsigned _recv_seqnum = 0
 
unsigned _current = 0
 
unsigned _attempts = 0
 
f8_atomic< bool > _giving_up
 
std::vector< Server_servers
 
const size_t _failover_cnt
 
f8_thread_cancellation_token _cancellation_token
 

Additional Inherited Members

- Public Attributes inherited from FIX8::SessionConfig
const F8MetaCntx_ctx
 
const XmlElement_ses
 
LoginParameters _loginParameters
 
const std::string _session_name
 
- Protected Member Functions inherited from FIX8::Configuration
bool from_or_default (const XmlElement *from, const f8String &tag, f8String &target) const
 
template<typename T >
T & get_string_field (const XmlElement *from, const std::string &tag, T &to) const
 
Tickval::ticks get_time_field (const XmlElement *from, const std::string &tag, bool timeonly=false) const
 
template<typename T >
find_or_default (const XmlElement *from, const std::string &tag, const T def) const
 
- Protected Attributes inherited from FIX8::ClientSession< T >
sender_comp_id _sci
 
target_comp_id _tci
 
const SessionID _id
 
Logger_log
 
Logger_plog
 
Persister_persist
 
T * _session
 
Poco::Net::StreamSocket * _sock = nullptr
 
Poco::Net::SocketAddress _addr
 
ClientConnection_cc = nullptr
 

Detailed Description

template<typename T>
class FIX8::ReliableClientSession< T >

Reliable Client wrapper. This client attempts to recover from disconnects and login rejects.

Template Parameters
Tspecialised with your derived session class

Definition at line 283 of file sessionwrapper.hpp.

Member Typedef Documentation

template<typename T >
using FIX8::ReliableClientSession< T >::ReliableClientSession_ptr = std::unique_ptr<ReliableClientSession<T>>

Convenient scoped pointer for your session.

Definition at line 491 of file sessionwrapper.hpp.

Member Enumeration Documentation

template<typename T >
anonymous enum
Enumerator
no_servers_configured 

Definition at line 302 of file sessionwrapper.hpp.

Constructor & Destructor Documentation

template<typename T >
FIX8::ReliableClientSession< T >::ReliableClientSession ( const F8MetaCntx ctx,
const std::string &  conf_file,
const std::string &  session_name 
)
inline

Ctor. Prepares session for connection as an initiator.

Definition at line 294 of file sessionwrapper.hpp.

References FIX8::SessionConfig::_loginParameters, and FIX8::LoginParameters::_reliable.

295  : ClientSession<T>(ctx, conf_file, session_name, true), _thread(std::ref(*this)),
297  {
298  _giving_up = false;
299  this->_loginParameters._reliable = true;
300  }
std::vector< Server > _servers
LoginParameters _loginParameters
F8API size_t get_addresses(const XmlElement *from, std::vector< Server > &target) const
const F8MetaCntx & ctx()
Compiler generated metadata object, accessed through this function.
const XmlElement * _ses
f8_thread< ReliableClientSession< T > > _thread
template<typename T >
virtual FIX8::ReliableClientSession< T >::~ReliableClientSession ( )
inlinevirtual

Dtor.

Definition at line 309 of file sessionwrapper.hpp.

References FIX8::_f8_threadcore::join(), and FIX8::f8_thread< T >::request_stop().

310  {
311  _thread.request_stop();
312  _thread.join();
313  }
f8_thread< ReliableClientSession< T > > _thread

Member Function Documentation

template<typename T >
f8_thread_cancellation_token& FIX8::ReliableClientSession< T >::cancellation_token ( )
inline

Definition at line 488 of file sessionwrapper.hpp.

References FIX8::ReliableClientSession< T >::_cancellation_token.

488 { return _cancellation_token; }
f8_thread_cancellation_token _cancellation_token
template<typename T >
size_t FIX8::ReliableClientSession< T >::get_attempts_cnt ( ) const
inline

Get the number of attempts made so far

Returns
number of attempts

Definition at line 332 of file sessionwrapper.hpp.

References FIX8::ReliableClientSession< T >::_attempts.

332 { return _attempts; }
template<typename T >
const Server* FIX8::ReliableClientSession< T >::get_server ( unsigned  idx = no_servers_configured) const
inline

Get the Server object for a given server index

Parameters
idxof server desired, if not given return current
Returns
ptr to server, 0 if not found

Definition at line 341 of file sessionwrapper.hpp.

References FIX8::ReliableClientSession< T >::_current, and FIX8::ReliableClientSession< T >::no_servers_configured.

template<typename T >
size_t FIX8::ReliableClientSession< T >::get_server_cnt ( ) const
inline

Get the number of configured failover servers

Returns
number of servers

Definition at line 336 of file sessionwrapper.hpp.

References FIX8::ReliableClientSession< T >::_failover_cnt.

336 { return _failover_cnt; }
template<typename T >
bool FIX8::ReliableClientSession< T >::has_given_up ( ) const
inlinevirtual

If reliable, determine if the maximum no. of reties has been reached

Returns
true if maximum no. of reties reached

Reimplemented from FIX8::ClientSessionBase.

Definition at line 306 of file sessionwrapper.hpp.

References FIX8::ReliableClientSession< T >::_giving_up.

306 { return _giving_up; }
template<typename T >
int FIX8::ReliableClientSession< T >::operator() ( )
inline

The reliability thread entry point.

Returns
0 on success

Definition at line 349 of file sessionwrapper.hpp.

References FIX8::ReliableClientSession< T >::_attempts, FIX8::ClientSession< T >::_cc, FIX8::ReliableClientSession< T >::_current, FIX8::LoginParameters::_davi, FIX8::ReliableClientSession< T >::_failover_cnt, FIX8::LoginParameters::_hb_int, FIX8::LoginParameters::_login_retries, FIX8::LoginParameters::_login_retry_interval, FIX8::SessionConfig::_loginParameters, FIX8::ReliableClientSession< T >::_recv_seqnum, FIX8::LoginParameters::_reset_sequence_numbers, FIX8::ClientSession< T >::_sock, FIX8::Logger::Error, FIX8::hypersleep< h_milliseconds >(), FIX8::Connection::start(), FIX8::Logger::Warn, and FIX8::f8Exception::what().

350  {
351  while(!_cancellation_token)
352  {
353  ++_attempts;
354 
355  bool excepted(_failover_cnt == 0);
356  try
357  {
358  if (_failover_cnt)
359  {
360  std::ostringstream ostr;
361  ostr << "Trying " << _servers[_current]._hostname << '(' << (1 + _servers[_current]._retries) << "), "
362  << _attempts << " attempts so far";
363  this->_session->log(ostr.str(), Logger::Warn);
364  this->_loginParameters._reset_sequence_numbers = _servers[_current]._reset_sequence_numbers; // permit override
365  }
366 
367  //std::cout << "operator()():try" << std::endl;
368 #ifdef FIX8_HAVE_OPENSSL
369  bool secured(this->_ssl.is_secure());
370  this->_sock = secured
371  ? new Poco::Net::SecureStreamSocket(this->_ssl._context)
372  : new Poco::Net::StreamSocket;
373 #else
374  bool secured(false);
375  this->_sock = new Poco::Net::StreamSocket;
376 #endif
377  this->_cc = new ClientConnection(this->_sock, _failover_cnt ? this->_addr = _servers[_current]._addr : this->_addr,
378  *this->_session, this->_loginParameters._hb_int, this->get_process_model(this->_ses), true, secured);
379  this->_session->set_login_parameters(this->_loginParameters);
380  this->_session->set_session_config(this);
381  this->_session->start(this->_cc, true, _send_seqnum, _recv_seqnum, this->_loginParameters._davi());
382  _send_seqnum = _recv_seqnum = 0; // only set seqnums for the first time round
383  }
384  catch (Poco::TimeoutException& e)
385  {
386  this->_session->log(e.what(), Logger::Error);
387  excepted = true;
388  }
389  catch (f8Exception& e)
390  {
391  // std::cerr << e.what() << std::endl;
392  this->_session->log(e.what(), Logger::Error);
393  excepted = true;
394  }
395  catch (Poco::Net::InvalidAddressException& e)
396  {
397  this->_session->log(e.what(), Logger::Error);
398  excepted = true;
399  }
400 #if POCO_VERSION >= 0x01040000
401  catch (Poco::Net::InvalidSocketException& e)
402  {
403  this->_session->log(e.what(), Logger::Error);
404  excepted = true;
405  }
406 #endif
407  catch (Poco::Net::ServiceNotFoundException& e)
408  {
409  //std::cerr << e.what() << std::endl;
410  this->_session->log(e.what(), Logger::Error);
411  excepted = true;
412  }
413  catch (Poco::Net::ConnectionRefusedException& e)
414  {
415  this->_session->log(e.what(), Logger::Error);
416  excepted = true;
417  }
418  catch (Poco::Net::DNSException& e)
419  {
420  this->_session->log(e.what(), Logger::Error);
421  excepted = true;
422  }
423  catch (Poco::Net::InterfaceNotFoundException& e)
424  {
425  this->_session->log(e.what(), Logger::Error);
426  excepted = true;
427  }
428  catch (Poco::Net::NetException& e) // catch all other NetExceptions
429  {
430  this->_session->log(e.what(), Logger::Error);
431  excepted = true;
432  }
433  catch (std::exception& e)
434  {
435  //std::cout << "process:: std::exception" << endl;
436  this->_session->log(e.what(), Logger::Error);
437  excepted = true;
438  }
439 
440  //std::cout << "operator()():out of try" << std::endl;
441  try
442  {
443  this->_session->stop();
444  }
445  catch (f8Exception& e)
446  {
447  this->_session->log(e.what(), Logger::Error);
448  }
449  catch (Poco::Net::NetException& e)
450  {
451  this->_session->log(e.what(), Logger::Error);
452  }
453  catch (std::exception& e)
454  {
455  this->_session->log(e.what(), Logger::Error);
456  }
457 
458  delete this->_cc;
459  this->_cc = nullptr;
460  delete this->_sock;
461  this->_sock = nullptr;
462 
463  if (!excepted || (_failover_cnt == 0
464  && this->_loginParameters._login_retries > 0 && _attempts > this->_loginParameters._login_retries))
465  break;
466 
467  if (_failover_cnt)
468  {
469  ++_servers[_current]._retries;
470 
471  while (!_cancellation_token) // FIXME possible endless loop condition
472  {
473  if (_servers[_current]._max_retries && _servers[_current]._retries < _servers[_current]._max_retries)
474  break;
475  _servers[_current]._retries = 0; // reset for next time
476  ++_current %= _failover_cnt;
477  }
478  }
479 
481  }
482 
483  _giving_up = true;
484 
485  return 0;
486  }
std::vector< Server > _servers
LoginParameters _loginParameters
unsigned _login_retry_interval
Definition: session.hpp:353
default_appl_ver_id _davi
Definition: session.hpp:357
f8_thread_cancellation_token _cancellation_token
Poco::Net::StreamSocket * _sock
int hypersleep< h_milliseconds >(unsigned amt)
Definition: hypersleep.hpp:105
ClientConnection * _cc
unsigned _login_retries
Definition: session.hpp:353
Poco::Net::SocketAddress _addr
template<typename T >
virtual void FIX8::ReliableClientSession< T >::start ( bool  wait,
unsigned  send_seqnum = 0,
unsigned  recv_seqnum = 0,
const f8String  davi = f8String() 
)
inlinevirtual

Start the session - initiate the connection, logon and start heartbeating.

Parameters
waitif true wait till session finishes before returning
send_seqnumnext send seqnum
recv_seqnumnext recv seqnum
davidefault appl version id (FIXT)

Reimplemented from FIX8::ClientSession< T >.

Definition at line 320 of file sessionwrapper.hpp.

References FIX8::ReliableClientSession< T >::_recv_seqnum, and FIX8::f8_thread< T >::start().

321  {
322  _send_seqnum = send_seqnum;
323  _recv_seqnum = recv_seqnum;
324  if (!wait)
325  _thread.start();
326  else
327  (*this)();
328  }
f8_thread< ReliableClientSession< T > > _thread

Member Data Documentation

template<typename T >
unsigned FIX8::ReliableClientSession< T >::_attempts = 0
private
template<typename T >
f8_thread_cancellation_token FIX8::ReliableClientSession< T >::_cancellation_token
private
template<typename T >
unsigned FIX8::ReliableClientSession< T >::_current = 0
private
template<typename T >
const size_t FIX8::ReliableClientSession< T >::_failover_cnt
private
template<typename T >
f8_atomic<bool> FIX8::ReliableClientSession< T >::_giving_up
private

Definition at line 287 of file sessionwrapper.hpp.

Referenced by FIX8::ReliableClientSession< T >::has_given_up().

template<typename T >
unsigned FIX8::ReliableClientSession< T >::_recv_seqnum = 0
private
template<typename T >
unsigned FIX8::ReliableClientSession< T >::_send_seqnum = 0
private

Definition at line 286 of file sessionwrapper.hpp.

template<typename T >
std::vector<Server> FIX8::ReliableClientSession< T >::_servers
private

Definition at line 288 of file sessionwrapper.hpp.

template<typename T >
f8_thread<ReliableClientSession<T> > FIX8::ReliableClientSession< T >::_thread
private

Definition at line 285 of file sessionwrapper.hpp.


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