Class: OMF::Web::Rack::WebsocketHandler
- Inherits:
-
Rack::WebSocket::Application
- Object
- Rack::WebSocket::Application
- OMF::Web::Rack::WebsocketHandler
show all
- Extended by:
- Common::Loggable
- Includes:
- Common::Loggable
- Defined in:
- lib/omf-web/rack/websocket_handler.rb
Constant Summary
collapse
- MESSAGE_DELAY =
0.5
Instance Method Summary
collapse
_logger, debug, error, fatal, info, init_log, logger, set_environment, warn
Instance Method Details
#find_data_source(args) ⇒ Object
145
146
147
148
149
150
151
152
153
|
# File 'lib/omf-web/rack/websocket_handler.rb', line 145
def find_data_source(args)
ds_name = args['ds_name']
unless dsp = OMF::Web::DataSourceProxy[ds_name]
warn "Request for unknown datasource '#{ds_name}'."
send_data({type: 'reply', status: 'error', err_msg: "Unknown datasource '#{ds_name}'"}.to_json)
return nil
end
dsp
end
|
#on_close(env) ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/omf-web/rack/websocket_handler.rb', line 45
def on_close(env)
begin
debug "client disconnected"
@tab_inst.on_ws_close(self, @sub_path) if @tab_inst
@tab_inst = nil
rescue Exception => ex
error(ex)
end
end
|
#on_message(env, msg_s) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/omf-web/rack/websocket_handler.rb', line 20
def on_message(env, msg_s)
begin
req = ::Rack::Request.new(env)
sid = req.params['sid']
Thread.current["sessionID"] = sid
msg = JSON.parse(msg_s)
debug("Msg(#{sid}): #{msg.inspect}")
msg_type = msg['type']
args = msg['args']
msg_handler = "on_#{msg_type}".to_sym
unless respond_to? msg_handler
warn "Received unknonw request '#{msg_type}'"
send_data({type: 'reply', status: 'error', err_msg: "Unknown message type '#{msg_type}'"}.to_json)
return
end
context = OMF::Web::SessionStore[args['name'], :ws] ||= {}
send(msg_handler, args, context)
rescue Exception => ex
error ex
debug "#{ex.backtrace.join("\n")}"
send_data({type: 'reply', status: 'exception', err_msg: ex.to_s}.to_json)
end
end
|
#on_open(env) ⇒ Object
Delay in pushing action message to browser after receiving a ‘on_change’ from monitored data proxy
16
17
18
|
# File 'lib/omf-web/rack/websocket_handler.rb', line 16
def on_open(env)
end
|
#on_register_data_source(args, context) ⇒ Object
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/omf-web/rack/websocket_handler.rb', line 55
def on_register_data_source(args, context)
dsp = find_data_source(args)
return unless dsp debug "Received registration for datasource proxy '#{dsp}'"
send_data({type: 'reply', status: 'ok'}.to_json)
mutex = Mutex.new
semaphore = ConditionVariable.new
action_queue = {}
dsp.on_changed(args['offset']) do |action, rows|
mutex.synchronize do
(action_queue[action] ||= []).concat(rows)
semaphore.signal
end
end
Thread.new do
begin
loop do
mutex.synchronize do
action_queue.each do |action, rows|
next if rows.empty?
debug "Sending '#{action}' message with #{rows.length} rows"
msg = {
type: 'datasource_update',
datasource: dsp.name,
rows: rows,
action: action
}
send_data(msg.to_json)
rows.clear
end
semaphore.wait(mutex)
end
sleep MESSAGE_DELAY
end
rescue Exception => ex
error "on_register_data_source - #{ex}"
debug "#{ex.backtrace.join("\n")}"
end
end
end
|
#on_request_slice(args, context) ⇒ Object
args “col_value”=>“e8…”, “ds_name”=>“individual_link”}}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/omf-web/rack/websocket_handler.rb', line 109
def on_request_slice(args, context)
dsp = find_data_source(args)
return unless dsp
sargs = args['slice']
col_name = sargs['col_name']
col_value = sargs['col_value']
debug "Creating slice '#{col_name}:#{col_value}' data source '#{dsp}'"
if old_sdsp = context[:sliced_datasource]
return if old_sdsp[:column_name] == col_name
old_sdsp[:dsp].release
end
sdsp = dsp.create_slice(col_name, col_value)
context[:sliced_datasource] = {:col_name => col_name, :dsp => sdsp}
sdsp.on_changed(0) do |action, rows|
msg = {
type: 'datasource_update',
datasource: args['ds_name'],
rows: rows,
action: action
}
send_data(msg.to_json)
end
end
|