Class: Fit::FitServer

Inherits:
Object
  • Object
show all
Defined in:
lib/fit/fit_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countsObject (readonly)

Returns the value of attribute counts.



15
16
17
# File 'lib/fit/fit_server.rb', line 15

def counts
  @counts
end

#hostObject

Returns the value of attribute host.



14
15
16
# File 'lib/fit/fit_server.rb', line 14

def host
  @host
end

#portObject

Returns the value of attribute port.



14
15
16
# File 'lib/fit/fit_server.rb', line 14

def port
  @port
end

#reason_for_bad_connectionObject (readonly)

Returns the value of attribute reason_for_bad_connection.



15
16
17
# File 'lib/fit/fit_server.rb', line 15

def reason_for_bad_connection
  @reason_for_bad_connection
end

#test_ticketObject

Returns the value of attribute test_ticket.



14
15
16
# File 'lib/fit/fit_server.rb', line 14

def test_ticket
  @test_ticket
end

#verboseObject

Returns the value of attribute verbose.



14
15
16
# File 'lib/fit/fit_server.rb', line 14

def verbose
  @verbose
end

Instance Method Details

#args(arg_list) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fit/fit_server.rb', line 35

def args(arg_list)
  begin
    arg_count = arg_list.size
    if arg_count == 4 and  arg_list.shift == '-v'
      @verbose = true
    elsif arg_count != 3
      return false
    end
    @host = arg_list.shift
    @port = arg_list.shift.to_i
    return false if @port.zero?;
    @test_ticket = arg_list.shift
    return true
  rescue => exception
    return false
  end
end

#build_requestObject



58
59
60
# File 'lib/fit/fit_server.rb', line 58

def build_request
  return "GET /?responder=socketCatcher&ticket=" + @test_ticket + " HTTP/1.1\r\n\r\n"
end

#close_connectionObject



62
63
64
# File 'lib/fit/fit_server.rb', line 62

def close_connection
  @socket.close
end

#establish_connectionObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fit/fit_server.rb', line 66

def establish_connection
  say "Connecting to " + @host.to_s + ":" + @port.to_s
  @socket = TCPSocket.new(@host, @port)
  @socket.print(build_request)
  @socket.flush
  status = FitProtocol.read_size(@socket)
  unless status.zero?
    @reason_for_bad_connection = FitProtocol.read(status, @socket)
    say "\t> failed to connect: " + @reason_for_bad_connection
    return false
  else
    say "\t> connected"
    return true
  end
end

#exception(e) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fit/fit_server.rb', line 127

def exception(e)
  say "Exception occured!"
  say "\t> " + e.to_s
  parse = ParseHolder.create("span",  "Exception occurred: ", nil, nil)
  if @fixture.nil?
    new_fixture
  end
  @fixture.exception(parse, e)
  @counts.exceptions = @counts.exceptions + 1
  @fixture_listener.table_finished(parse)
  @fixture_listener.tables_finished(@fixture.counts)
end

#exit_codeObject



119
120
121
# File 'lib/fit/fit_server.rb', line 119

def exit_code()
  return @counts.wrong + @counts.exceptions
end

#finishObject



114
115
116
117
# File 'lib/fit/fit_server.rb', line 114

def finish
  say "Final counts: " + @counts.to_s
  say "Exiting with code " + exit_code().to_s
end

#good_connection?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/fit/fit_server.rb', line 110

def good_connection?
  return reason_for_bad_connection.nil?
end

#new_fixtureObject



105
106
107
108
# File 'lib/fit/fit_server.rb', line 105

def new_fixture
  @fixture = Fixture.new
  @fixture.listener = @fixture_listener
end

#processObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fit/fit_server.rb', line 82

def process
  @fixture_listener = TablePrintingFixtureListener.new(@socket)
  @counts = Counts.new
  document_size = 0
  begin
    while ((document_size = FitProtocol.read_size(@socket)) != 0)
      begin
        say "Processing document with " + document_size.to_s + " bytes"
        document = FitProtocol.read(document_size, @socket)
        tables = Parse.new(document, ['table', 'tr', 'td'])
        new_fixture
        @fixture.do_tables(tables)
        say "\t> " + @fixture.counts.to_s
        @counts.tally(@fixture.counts)
      rescue Fit::ParseException => e
        exception(e)
      end
    end
  rescue => e
    exception(e)
  end
end

#run(args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fit/fit_server.rb', line 17

def run(args)
  if args(args)
    connected = establish_connection
    if not connected
      puts "Could not connect. " + @reason_for_bad_connection
      return -1
    else
      process
      close_connection
      finish
      return exit_code
    end
  else
    usage
    return -1
  end    
end

#say(message) ⇒ Object



123
124
125
# File 'lib/fit/fit_server.rb', line 123

def say(message)
  puts message if @verbose
end

#usageObject



53
54
55
56
# File 'lib/fit/fit_server.rb', line 53

def usage
  puts "Usage: ruby FitServer.rb [options] <host> <port> <test_ticket>"
  puts "  -v: verbose"
end