Class: EventMachine::Breakout::Browser

Inherits:
Connection
  • Object
show all
Defined in:
lib/em-breakout/browser.rb

Instance Attribute Summary collapse

Attributes inherited from Connection

#grid, #grid_name, #is_closing

Instance Method Summary collapse

Methods inherited from Connection

#cid, #close_websocket, #log

Instance Attribute Details

#bidObject (readonly)

browser id



6
7
8
# File 'lib/em-breakout/browser.rb', line 6

def bid
  @bid
end

#message_queueObject (readonly)

browser id



6
7
8
# File 'lib/em-breakout/browser.rb', line 6

def message_queue
  @message_queue
end

#notifyObject (readonly)

browser id



6
7
8
# File 'lib/em-breakout/browser.rb', line 6

def notify
  @notify
end

#routeObject (readonly)

browser id



6
7
8
# File 'lib/em-breakout/browser.rb', line 6

def route
  @route
end

#wipObject

true from when a worker gets sent a message until the worker sends :done_work, false all other times



5
6
7
# File 'lib/em-breakout/browser.rb', line 5

def wip
  @wip
end

Instance Method Details

#breakout(debug = false) ⇒ Object



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
# File 'lib/em-breakout/browser.rb', line 11

def breakout(debug=false)

  onopen do
    @grid_name = request["path"].split('?').first.gsub('/','')
    @grid = GRIDS[@grid_name]
    @route = request["query"]["route"]
    @bid = request["query"]["bid"]
    @notify = request["query"]["notify"] == "true" ? true : false
    @e = request["query"]["e"].to_i
    @gat = request["query"]["gat"]

    log(%|grid_name: #{@grid_name}\nroute: #{@route}\nbid: #{@bid}\ne: #{@e}\n| +
        %|gat: #{@gat}\nnotify: #{@notify}|) if debug

    catch :break do
      if !@grid
        close_websocket "unknown grid"
        throw :break
      end

      if @grid.browsers.has_key?(@bid) or @grid.disconnected_browsers.has_key?(@bid)
        close_websocket "bid in use"
        throw :break
      end

      if @e < Time.now.to_i
        close_websocket "expired"
        throw :break
      end
      
      unless @gat == ::Breakout.grid_access_token(@route, @bid, @e, @notify, @grid.grid_key)
        close_websocket "invalid url"
        throw :break
      end

      @message_queue = []
      @grid.browsers[@bid] = self
      if @notify
        @message_queue << "/open"
        @grid.work_queue[self] = true
        EventMachine.next_tick { @grid.try_work }
      end
    end
  end

  onmessage do |msg|
    log("msg: #{msg}") if debug

    unless @is_closing

      if @message_queue.empty? && !@wip
        @grid.work_queue[self] = true
        EventMachine.next_tick { @grid.try_work }
      end

      @message_queue << msg
    end
  end 

  onclose do
    log("closing") if debug

    if @message_queue
      @grid.browsers.delete @bid

      if @notify
        @grid.disconnected_browsers[@bid] = self
        @message_queue = ["/close"]
        unless @wip
          @grid.work_queue[self] = true
          EventMachine.next_tick { @grid.try_work }
        end
      else
        @grid.work_queue.delete self
      end
    end
  end

  onerror do |reason|
    log reason.pretty
  end
end