Class: Redom::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/redom/dispatcher.rb

Instance Method Summary collapse

Methods included from Utils

#_dispatcher, #_logger, dispatcher=, logger=

Constructor Details

#initialize(opts) ⇒ Dispatcher

Returns a new instance of Dispatcher.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/redom/dispatcher.rb', line 5

def initialize(opts)
  if find_all_connection_classes.size == 0
    _logger.warn 'No Redom::Connection class defined.'
  end

  @opts = opts
  @conns = Hash.new
  @tasks = Hash.new
  @pool = ThreadPool.new(@opts)
  @pool.start
end

Instance Method Details

#connections(filter = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/redom/dispatcher.rb', line 89

def connections(filter = nil)
  if filter
    @conns.values.select { |conn|
      filter === conn
    }
  else
    @conns.values
  end
end

#delete_task(task_id) ⇒ Object



85
86
87
# File 'lib/redom/dispatcher.rb', line 85

def delete_task(task_id)
  @tasks.delete task_id
end

#on_close(ws) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/redom/dispatcher.rb', line 71

def on_close(ws)
  @conns.each { |cid, conn|
    if cid =~ /^#{ws.__id__}@/
      _logger.debug "Connection closed. ID='#{ws.__id__}'"
      run_task(conn, :on_close)
      @conns.delete cid
    end
  }
end

#on_error(ws, err) ⇒ Object



81
82
83
# File 'lib/redom/dispatcher.rb', line 81

def on_error(ws, err)
  _logger.debug "Error occured. ID='#{ws.__id__}'"
end

#on_message(ws, msg) ⇒ Object



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/redom/dispatcher.rb', line 31

def on_message(ws, msg)
  _logger.debug "Message received. ID='#{ws.__id__}'\n#{msg}"
  req = JSON.parse(msg)
  case req[IDX_REQUEST_TYPE]
  when REQ_HANDSHAKE
    if cls = @conn_cls[req[IDX_CONNECTION_CLASS]]
      conn = cls.new._init(ws, @opts)
      @conns["#{ws.__id__}@#{conn.__id__}"] = conn
      run_task(conn, :_on_open)
    else
      _logger.error "Undefined Redom::Connection class '#{req[IDX_CONNECTION_CLASS]}'."
    end
  when REQ_METHOD_INVOCATION
    if conn = @conns[req[IDX_CONNECTION_ID]]
      req[IDX_ARGUMENTS].map! { |arg|
        if Array === arg
          case arg[0]
          when TYPE_PROXY
              arg = Proxy.new(conn, [arg[1]])
          when TYPE_ARRAY
              arg = arg[1]
          end
        end
        arg
      }
      run_task(conn, req[IDX_METHOD_NAME], req[IDX_ARGUMENTS])
    else
      _logger.error "Connection missing. ID='#{req[IDX_CONNECTION_ID]}'"
    end
  when REQ_PROXY_RESULT
    if task = @tasks[req[IDX_TASK_ID]]
      task.run req[IDX_PROXY_RESULT]
    else
      _logger.error "Task missing. ID='#{req[IDX_TASK_ID]}'"
    end
  else
    _logger.error "Undefined request type '#{req[IDX_REQUEST_TYPE]}'."
  end
end

#on_open(ws) ⇒ Object



27
28
29
# File 'lib/redom/dispatcher.rb', line 27

def on_open(ws)
  _logger.debug "Connection established. ID='#{ws.__id__}'"
end

#run_task(conn, name, args = [], blck = nil) ⇒ Object



21
22
23
24
25
# File 'lib/redom/dispatcher.rb', line 21

def run_task(conn, name, args = [], blck = nil)
  task = Task.new(conn, @pool.worker, [name, args, blck])
  @tasks[task._id] = task
  task.run
end

#stopObject



17
18
19
# File 'lib/redom/dispatcher.rb', line 17

def stop
  @pool.stop
end