Class: DIY::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/diy/controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, server, offline, strategy) ⇒ Controller

Returns a new instance of Controller.



6
7
8
9
10
11
12
13
14
# File 'lib/diy/controller.rb', line 6

def initialize( client, server, offline, strategy)
  @client = client
  @server = server
  @offline = offline
  @strategy = strategy
  @before_send = nil
  @timeout = nil
  @error_on_stop = nil
end

Instance Method Details

#before_send(&block) ⇒ Object



146
147
148
# File 'lib/diy/controller.rb', line 146

def before_send(&block)
  @before_send = block
end

#client_send(client, pkts) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/diy/controller.rb', line 122

def client_send(client, pkts)
  if @before_send
    pkts = pkts.collect do |pkt| 
      content = pkt.content
      begin
        pkt.content = @before_send.call(content)
      rescue Exception => e
        DIY::Logger.warn("UserError Catch: " + error = BeforeSendCallError.new(e) )
        error.backtrace.each do |msg|
          DIY::Logger.info(msg)
        end
        raise error
      end
      pkt
    end
  end
  begin
    client.inject(pkts)
  rescue FFI::PCap::LibError =>e
    DIY::Logger.warn("SendPacketError Catch: " + e )
    raise e
  end
end

#do_trapObject



71
72
73
74
75
76
77
# File 'lib/diy/controller.rb', line 71

def do_trap
  Signal.trap("INT") do
    DIY::Logger.info "bye.."
    stop
    exit 0
  end    
end

#error_on_stopObject



154
155
156
# File 'lib/diy/controller.rb', line 154

def error_on_stop(*)
  @error_on_stop = true
end

#offline_resultObject



163
164
165
# File 'lib/diy/controller.rb', line 163

def offline_result
  sprintf "%4d files, %8d packets", @offline.files_size, @offline.now_size
end

#one_round(client, server, pkts) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/diy/controller.rb', line 84

def one_round( client, server, pkts )
  @error_flag = nil
  @round_count = 0 unless @round_count
  @round_count += 1
  DIY::Logger.info "round #{@round_count}: (c:#{client.__drburi} / s:#{server.__drburi}) #{pkts[0].pretty_print}:(queue= #{pkts.size})"
  if pkts.size >= 10
    DIY::Logger.info "queue size too big: #{pkts.size}, maybe something error"
  end
  
  recv_pkt_proc_set( pkts )
  @ready = false
  server.ready(&@recv_pkt_proc)
  @ready = true
  client_send(client, pkts)
  wait_recv_ok(pkts)
  server.terminal
end

#recv_pkt_proc_set(queue) ⇒ Object

设置回调入口, 由 worker 通过DRb 远程调用



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/diy/controller.rb', line 103

def recv_pkt_proc_set(queue)
  @queue_keeper = queue
  # 不重新赋值, 防止 DRb 回收

  @recv_pkt_proc ||= lambda do |recv_pkt|
    begin
      next unless @ready # strip this recv pkt unless it's ready to.

      next if @error_flag # error accur waiting other thread do with it

      @recv_pkt_keeper = Packet.new(recv_pkt)
      @strategy.call(@queue_keeper.first, @recv_pkt_keeper, @queue_keeper)
    rescue DIY::UserError =>e
      DIY::Logger.warn("UserError Catch: " + e.inspect)
      e.backtrace.each do |msg|
        DIY::Logger.info(msg)
      end
      @error_flag = e
    end        
  end
end

#runObject



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
# File 'lib/diy/controller.rb', line 16

def run
  do_trap
  client = @client
  server = @server
  
  @fail_count = 0
  start_time = Time.now
  #clear

  client.terminal
  server.terminal
  
  loop do
    begin
      pkts, where = @offline.nexts
      case where
      when :A
        client, server = @client, @server
      when :B
        client, server = @server, @client
      end
      one_round( client, server, pkts )
    rescue HopePacketTimeoutError, UserError, FFI::PCap::LibError => e
      DIY::Logger.warn( "Timeout: Hope packet is #{pkts[0].pretty_print} ") if e.kind_of?(HopePacketTimeoutError)
      @fail_count += 1
      if @error_on_stop and e.kind_of?(HopePacketTimeoutError)
        client.terminal
        server.terminal
        DIY::Logger.info "Error_on_stop flag opened, stopping..."
        DIY::Logger.info "Dump mac learn table(size is #{@offline.mac_learner.size})... "
        DIY::Logger.info @offline.mac_learner.dump
        break
      end
      begin
        @offline.next_pcap
        client.terminal
        server.terminal
      rescue EOFError
        client.terminal
        server.terminal
        break
      end
      #~ client,server = @client, @server

    rescue EOFError
      client.terminal
      server.terminal
      break
    ensure
      #~ client, server = server, client

    end
  end
  DRb.stop_service
  end_time = Time.now
  stats_result( end_time - start_time, @fail_count )
end

#stats_result(cost_time, fail_count) ⇒ Object



158
159
160
161
# File 'lib/diy/controller.rb', line 158

def stats_result( cost_time, fail_count )
  DIY::Logger.info " Finished in #{cost_time} seconds"
  DIY::Logger.info " #{offline_result}, #{fail_count} failures"
end

#stopObject



79
80
81
82
# File 'lib/diy/controller.rb', line 79

def stop
  @client.terminal
  @server.terminal
end

#timeout(timeout) ⇒ Object



150
151
152
# File 'lib/diy/controller.rb', line 150

def timeout(timeout)
  @timeout = timeout
end

#wait_recv_ok(pkts) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/diy/controller.rb', line 167

def wait_recv_ok(pkts)
  wait_until(@timeout ||= 10) do
    if @error_flag
      raise @error_flag
    end
    pkts.empty?
  end
end

#wait_until(timeout = 10, &block) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/diy/controller.rb', line 176

def wait_until( timeout = 10, &block )
  Timeout.timeout(timeout, DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{timeout} seconds") ) do
    loop do
      break if block.call
      sleep 0.01
    end
  end
end