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
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
#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
|
#stop ⇒ Object
17
18
19
|
# File 'lib/redom/dispatcher.rb', line 17
def stop
@pool.stop
end
|