Class: Redom::Dispatcher
- Inherits:
-
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
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
93
94
95
96
97
98
99
100
101
|
# File 'lib/redom/dispatcher.rb', line 93
def connections(filter = nil)
if filter
@conns.values.select { |conn|
filter === conn
}
else
@conns.values
end
end
|
#delete_task(task_id) ⇒ Object
89
90
91
|
# File 'lib/redom/dispatcher.rb', line 89
def delete_task(task_id)
@tasks.delete task_id
end
|
#on_close(ws) ⇒ Object
75
76
77
78
79
80
81
82
83
|
# File 'lib/redom/dispatcher.rb', line 75
def on_close(ws)
_logger.debug "Connection closed. ID='#{ws.__id__}'"
if conn = @conns[ws.__id__]
task = Task.new(conn, @pool.worker, [:on_close, []])
@tasks[task._id] = task
task.run
@conns.delete ws.__id__
end
end
|
#on_error(ws, err) ⇒ Object
85
86
87
|
# File 'lib/redom/dispatcher.rb', line 85
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
70
71
72
73
|
# 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
task = Task.new(conn, @pool.worker, [:on_open, []])
@tasks[task._id] = task
task.run
else
_logger.error "Undefined Redom::Connection class '#{req[IDX_CONNECTION_CLASS]}'."
end
when REQ_METHOD_INVOCATION
if conn = @conns[ws.__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
}
task = Task.new(conn, @pool.worker, req[IDX_METHOD_NAME..-1])
@tasks[task._id] = task
task.run
else
_logger.error "Connection missing. ID='#{ws.__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) ⇒ Object
21
22
23
24
25
|
# File 'lib/redom/dispatcher.rb', line 21
def run_task(conn, name, args, blck)
task = Task.new(conn, @pool.worker, [name, args, blck])
@tasks[task._id] = task
task.run
end
|
#stop ⇒ Object
17
18
19
|
# File 'lib/redom/dispatcher.rb', line 17
def stop
@pool.stop
end
|