File: Reflector.rb

package info (click to toggle)
quickfix 1.15.1%2Bdfsg-4
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 42,080 kB
  • sloc: cpp: 631,686; python: 129,549; ruby: 106,716; xml: 43,737; ansic: 7,668; java: 1,826; cs: 816; makefile: 544; sh: 462; sql: 313
file content (167 lines) | stat: -rw-r--r-- 4,018 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#****************************************************************************
# Copyright (c) 2001-2014
#
# This file is part of the QuickFIX FIX Engine
#
# This file may be distributed under the terms of the quickfixengine.org
# license as defined by quickfixengine.org and appearing in the file
# LICENSE included in the packaging of this file.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# See http://www.quickfixengine.org/LICENSE for licensing information.
#
# Contact ask@quickfixengine.org if any conditions of this licensing are
# not clear to you.
#****************************************************************************

class Reflector < Array

  def identifyMessage(message)
    if [?I, ?E, ?R, ?i, ?e].include?(message[0]) 
      return message[0]
    else 
      return ?X
    end
  end

  def processFile(messages)
    lineNum = 0
    messages.each_line do
      | line |
      lineNum += 1
      line.chomp!
      if line.empty? then
      elsif (/^[IEie]\d{1},/ === line) then
	cid = line[1].ord.to_i - 48
	body = fixify!(fileify!(timeify!(line[3, line.length])))
      else
	cid = 1
	body = fixify!(fileify!(timeify!(line[1, line.length])))
      end

      begin
        processLine(lineNum, line, body, cid)      
      rescue
        errorAction(lineNum, line);
      end
    end
  end
  
  def processLine(lineNum, line, body, cid)
    if line.empty?
    elsif line[0] == ?\#
    elsif identifyMessage(line) == ?I
      initiateAction(body, cid)
    elsif identifyMessage(line) == ?E
      expectedAction(body, cid)
    elsif identifyMessage(line) == ?i
      if body == "CONNECT"
        connectAction(cid)
      elsif body == "DISCONNECT"
        disconnectAction(cid)
      else
        raise "Syntax error: " + body
      end
    elsif identifyMessage(line) == ?e
      if body == "CONNECT"
        waitConnectAction(cid)
      elsif body == "DISCONNECT"
        waitDisconnectAction(cid)
      else
        raise "Syntax error: " + body
      end
    else
        raise "Syntax error: " + body
    end
  end

  def fixify!(message)
    hasLength = (message =~ /[\001]9=.*?[\001]/)
    length = ""

    head = message.slice!(/^8=.*?[\001]/)

    if head == nil
      return message
    end

    checksum = message.slice(/[\001]10=.*[\001]$/)
    if(checksum != nil)
      message.slice!(/[\001]10=.*[\001]$/)
    end
    
    message.chomp!
    if hasLength == nil      
      length = "9=" + message.length.to_s + "\001"
    end
    
    if checksum == nil
      checksumStr = sprintf("%03d", (head + length + message).sum(8));
      checksum = "10=" + checksumStr + "\001"
    end

    message.replace(head + length + message + checksum)
    return message
  end

  def timeify!(message)
    copy = ""
    copy.replace(message)
    t = getTime
    
    strtime = t.strftime("%Y%m%d-%H:%M:%S")
    message.sub!("<TIME>", strtime)
    if( message != copy )
      return timeify!(message)
    end

    pos1 = /\<TIME[+-]\d+\>/ =~ message
    pos2 = /\>/ =~ message
    if( pos1 != nil )
      op = message[pos1 + 5]
      num = message.slice(pos1+6..pos2-1)
      if( op == ?+ )
	t += num.to_i
      else
	t -= num.to_i
      end

      strtime = t.strftime("%Y%m%d-%H:%M:%S")
      exp = Regexp.compile("<TIME[" + op.chr + "]" + num + ">")
      message.sub!(exp, strtime)
      if( message != copy )
	return timeify!(message)
      end
    end

    return message
  end
  
  def fileify!(message)
    copy = ""
    copy.replace(message)
    
    pos1 = /\<FILE:.+>/ =~ message
    pos2 = /\>/ =~ message
    if( pos1 != nil )
      file = message.slice(pos1+6..pos2-1)
      contents = File.open(file, "rb").read.strip
      exp = Regexp.compile("<FILE:#{file}>")
      message.sub!(exp, contents)
      if( message != copy )
        return fileify!(message)
      end
    end

    return message
  end

  def getTime
    t = Time.new
    t = t.gmtime
    return t
  end

end