module Fix::Engine::ServerConnection

The server connection wrapper, used when accepting a connection

Constants

LOGON_TIMEOUT

Timespan during which a client must send a logon message after connecting

Public Instance Methods

client() click to toggle source

Returns the currently connected client

# File lib/fix/engine/server_connection.rb, line 47
def client
  Client.get(ip, port, self)
end
logon_timeout() click to toggle source

Logs the client out should he fail to authenticate before LOGON_TIMEOUT seconds

# File lib/fix/engine/server_connection.rb, line 36
def logon_timeout
  unless @target_comp_id
  log("Client #{peer} failed to authenticate before timeout, closing connection")
  close_connection_after_writing
  client.delete
  end
end
peer() click to toggle source

The way we refer to our connection peer in various logs and messages

# File lib/fix/engine/server_connection.rb, line 54
def peer
  "<#{client.key}>"
end
post_init() click to toggle source

Run after a client has connected

Calls superclass method Fix::Engine::Connection#post_init
# File lib/fix/engine/server_connection.rb, line 22
def post_init
  super

  @port, @ip  = Socket.unpack_sockaddr_in(get_peername)
  @client     = Client.get(ip, port, self)

  log("Client connected #{peer}, expecting logon message in the next #{LOGON_TIMEOUT}s")

  EM.add_timer(LOGON_TIMEOUT) { logon_timeout }
end
run_message_handler(msg) click to toggle source

We override +FE::Connection#run_message_handlers+ to add some session-related logic

# File lib/fix/engine/server_connection.rb, line 69
def run_message_handler(msg)
  if !@target_comp_id && msg.is_a?(FP::Messages::Logon)
    log("Peer authenticated as <#{msg.username}> with heartbeat interval of <#{msg.heart_bt_int}s> and message sequence number start <#{msg.msg_seq_num}>")
    client.username = msg.username
    @target_comp_id = msg.sender_comp_id
    set_heartbeat_interval(msg.heart_bt_int)

    logon                     = FP::Messages::Logon.new
    logon.username            = msg.username
    logon.target_comp_id      = msg.sender_comp_id
    logon.sender_comp_id      = msg.target_comp_id 
    logon.reset_seq_num_flag  = true

    send_msg(logon)

  elsif @target_comp_id && msg.is_a?(FP::Messages::Logon)
    log("Received second logon message, reset_seq_num_flag <#{msg.reset_seq_num_flag}>")
    if msg.reset_seq_num_flag = 'Y'
      @send_seq_num = 1
      @messages = []
    end

  elsif !@target_comp_id
    peer_error("The session must be started with a logon message", msg.msg_seq_num, target_comp_id: msg.sender_comp_id)

  else
    super(msg)

  end
end
unbind() click to toggle source

Deletes the FE::Client instance after the connection is terminated

Calls superclass method Fix::Engine::Connection#unbind
# File lib/fix/engine/server_connection.rb, line 61
def unbind
  super
  client.delete
end