Class: OMF::Web::Rack::WebsocketHandler

Inherits:
Rack::WebSocket::Application
  • Object
show all
Extended by:
Common::Loggable
Includes:
Common::Loggable
Defined in:
lib/omf-web/rack/websocket_handler.rb

Instance Method Summary collapse

Methods included from Common::Loggable

_logger, debug, error, fatal, info, init_log, logger, set_environment, warn

Instance Method Details

#find_data_source(args) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/omf-web/rack/websocket_handler.rb', line 106

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



42
43
44
45
46
47
48
49
50
# File 'lib/omf-web/rack/websocket_handler.rb', line 42

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



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
# File 'lib/omf-web/rack/websocket_handler.rb', line 16

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
  #puts "message processed"      
end

#on_open(env) ⇒ Object



12
13
14
# File 'lib/omf-web/rack/websocket_handler.rb', line 12

def on_open(env)
  #puts ">>>>> OPEN"
end

#on_register_data_source(args, context) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/omf-web/rack/websocket_handler.rb', line 52

def on_register_data_source(args, context)
  dsp = find_data_source(args)
  return unless dsp  # should define appropriate exception
  debug "Received registration for datasource proxy '#{dsp}'"
  dsp.on_changed(args['offset']) do |action, rows|
    msg = {
      type: 'datasource_update',
      datasource: dsp.name,
      rows: rows,
      action: action
      #offset: offset
    }
    send_data(msg.to_json)
  end
  send_data({type: 'reply', status: 'ok'}.to_json)
end

#on_request_slice(args, context) ⇒ Object

args “col_value”=>“e8…”, “ds_name”=>“individual_link”}}



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
# File 'lib/omf-web/rack/websocket_handler.rb', line 70

def on_request_slice(args, context)
  dsp = find_data_source(args)
  return unless dsp  # should define appropriate exception
  
  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
      #offset: offset
    }
    send_data(msg.to_json)
#         
    # do |new_rows, offset|
    # msg = {
      # type: 'datasource_update',
      # datasource: args['ds_name'],
      # rows: new_rows,
      # offset: offset
    # }
    # send_data(msg.to_json)
  end
end