Class: AgentFIX::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_fix/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, connection_type) ⇒ Agent

Returns a new instance of Agent.



13
14
15
16
17
18
19
20
# File 'lib/agent_fix/agent.rb', line 13

def initialize name, connection_type
  @name = name
  @connection_type = connection_type
  @logged_on = false
  @bookmark = 0
  @all_messages = MessageCache.new
  @logger = Java::org.slf4j.LoggerFactory.getLogger("AgentFIX.Agent")
end

Instance Attribute Details

#bookmarkObject

Returns the value of attribute bookmark.



11
12
13
# File 'lib/agent_fix/agent.rb', line 11

def bookmark
  @bookmark
end

#connection_typeObject (readonly)

Returns the value of attribute connection_type.



8
9
10
# File 'lib/agent_fix/agent.rb', line 8

def connection_type
  @connection_type
end

#defaultObject

Returns the value of attribute default.



9
10
11
# File 'lib/agent_fix/agent.rb', line 9

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/agent_fix/agent.rb', line 8

def name
  @name
end

#sessionObject

Returns the value of attribute session.



9
10
11
# File 'lib/agent_fix/agent.rb', line 9

def session
  @session
end

Instance Method Details

#fromAdmin(message, sessionId) ⇒ Object



66
67
68
69
# File 'lib/agent_fix/agent.rb', line 66

def fromAdmin(message, sessionId)
  @logger.debug "#{@name} fromAdmin #{sessionId.to_s}: #{message.to_s.gsub("","|")}"
  @all_messages.add_message(message:message,sent:false,admin:true)
end

#fromApp(message, sessionId) ⇒ Object



56
57
58
59
# File 'lib/agent_fix/agent.rb', line 56

def fromApp(message, sessionId)
  @logger.debug "#{@name} fromApp #{sessionId.to_s}: #{message.to_s.gsub("","|")}"
  @all_messages.add_message(message:message,sent:false,admin:false)
end

#history(opts = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/agent_fix/agent.rb', line 98

def history opts={}
  opts[:since] ||= 0
  opts[:received_only] ||= false
  opts[:include_session]||= AgentFIX::include_session_level?

  indexed_msgs = []
  @all_messages.messages.each_with_index { |m,i| indexed_msgs << m.merge(index:i) }
  indexed_msgs = indexed_msgs.slice(opts[:since], indexed_msgs.length)

  if opts[:received_only]
    indexed_msgs = indexed_msgs.find_all {|m| !m[:sent]}
  end

  unless opts[:include_session]
    indexed_msgs = indexed_msgs.find_all {|m| !m[:admin]}
  end

  indexed_msgs
end

#initObject



22
23
24
25
26
27
28
29
# File 'lib/agent_fix/agent.rb', line 22

def init
  parse_settings
  @connector = case @connection_type
    when :acceptor then quickfix.SocketAcceptor.new(self, @storeFactory, @settings, @logFactory, @messageFactory)
    when :initiator then quickfix.SocketInitiator.new(self, @storeFactory, @settings, @logFactory, @messageFactory)
    else nil
  end
end

#loggedOn?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/agent_fix/agent.rb', line 71

def loggedOn?
  lock.synchronize do
    return @logged_on
  end
end

#messages_received(opts = {}) ⇒ Object



118
119
120
# File 'lib/agent_fix/agent.rb', line 118

def messages_received opts = {}
  history opts.merge(:received_only=>true)
end

#onCreate(sessionId) ⇒ Object



31
32
33
# File 'lib/agent_fix/agent.rb', line 31

def onCreate(sessionId)
  @default_session = sessionId
end

#onLogon(sessionId) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/agent_fix/agent.rb', line 35

def onLogon(sessionId)
  @logger.debug "#{@name} onLogon: #{sessionId.to_s}"

  lock.synchronize do
    @logged_on = true
  end
end

#onLogout(sessionId) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/agent_fix/agent.rb', line 43

def onLogout(sessionId)
  @logger.debug "#{@name} onLogout: #{sessionId.to_s}"

  lock.synchronize do
    @logged_on = false
  end
end

#resetObject



85
86
87
# File 'lib/agent_fix/agent.rb', line 85

def reset
  clear_state!
end

#sendToTarget(msg) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/agent_fix/agent.rb', line 77

def sendToTarget msg
  msg.getHeader.setField(quickfix.field.BeginString.new(@default_session.getBeginString))
  msg.getHeader.setField(quickfix.field.TargetCompID.new(@default_session.getTargetCompID))
  msg.getHeader.setField(quickfix.field.SenderCompID.new(@default_session.getSenderCompID))

  quickfix.Session.sendToTarget(msg)
end

#startObject



89
90
91
# File 'lib/agent_fix/agent.rb', line 89

def start
  @connector.start
end

#stop(force = false) ⇒ Object



93
94
95
96
# File 'lib/agent_fix/agent.rb', line 93

def stop force=false
  @connector.stop force
  clear_state!
end

#toAdmin(message, sessionId) ⇒ Object



61
62
63
64
# File 'lib/agent_fix/agent.rb', line 61

def toAdmin(message, sessionId)
  @logger.debug "#{@name} toAdmin #{sessionId.to_s}: #{message.to_s.gsub("","|")}"
  @all_messages.add_message(message:message,sent:true,admin:true)
end

#toApp(message, sessionId) ⇒ Object



51
52
53
54
# File 'lib/agent_fix/agent.rb', line 51

def toApp(message, sessionId) 
  @logger.debug "#{@name} toApp #{sessionId.to_s}: #{message.to_s.gsub("","|")}"
  @all_messages.add_message(message:message,sent:true,admin:false)
end