Class: BullClientController

Inherits:
Object
  • Object
show all
Includes:
MNotification
Defined in:
lib/bull/client.rb

Constant Summary collapse

@@ticket =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MNotification

notification_controller=, #notifications, notifications, #notify_error, #notify_info, #notify_ok

Constructor Details

#initializeBullClientController

Returns a new instance of BullClientController.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bull/client.rb', line 24

def initialize
    @user_id = nil
    @password = nil
    @watch = {}
    @promises = {}
    @ws = nil
    @app_rendered = false
    @files = {}

    @connection = RVar.new 'disconnected'
    reactive(@connection) do
        if @connection.value == 'disconnected'
            notify_error 'disconnected', 1
        else
            notify_ok 'connected', 1
        end
    end
end

Instance Attribute Details

#app_renderedObject

include MRelogin



18
19
20
# File 'lib/bull/client.rb', line 18

def app_rendered
  @app_rendered
end

#connectionObject (readonly)

Returns the value of attribute connection.



20
21
22
# File 'lib/bull/client.rb', line 20

def connection
  @connection
end

#wsObject

Returns the value of attribute ws.



19
20
21
# File 'lib/bull/client.rb', line 19

def ws
  @ws
end

Instance Method Details

#delete(table, id) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bull/client.rb', line 108

def delete(table, id)
    #prom = rpc('delete', table, id).then do |count|
    rpc('delete', table, id).then do |count|
        if count == 0
            #$notifications.add ['error', 'data not deleted', 0] if $notifications
            notify_error 'data not deleted', 0
        elsif count == 1
            #$notifications.add ['ok', 'data deleted', 0] if $notifications
            notify_ok 'data deleted', 0
        end
        #count
    end
    #prom
end

#file(command, *args) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/bull/client.rb', line 71

def file(command, *args)
    id = get_ticket
    promise = Promise.new
    send 'file_'+command, id, *args
    @promises[id] = promise
    @files[id] = ""
    promise
end

#get_watchObject



54
55
56
# File 'lib/bull/client.rb', line 54

def get_watch
    @watch
end

#insert(table, hsh) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bull/client.rb', line 80

def insert(table, hsh)
    prom = rpc('insert', table, value: hsh).then do |response|
        if response.nil?
            #$notifications.add ['error', 'data not inserted', 0] if $notifications
            notify_error 'data not inserted', 0
        else
            #$notifications.add ['ok', 'data inserted', 0] if $notifications
          notify_ok 'data inserted', 0
        end
        response
    end
    prom
end

#login(user, password) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bull/client.rb', line 123

def  user, password
    rpc('login', user, password).then do |response|
        if response
            @user_id = user
            @password = password
            @roles = response
            notify_ok 'logged', 0
        else
            notify_error 'incorrect user or password', 0
        end
        response
    end
end

#logoutObject



155
156
157
158
# File 'lib/bull/client.rb', line 155

def logout
    rpc('logout')
    clear
end

#notify(msg) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/bull/client.rb', line 160

def notify msg
  begin
    msg = JSON.parse(msg)
    data = msg['data'] || msg['result']
    if data.instance_of?(String) && msg['times'] && msg['times'][0] == 'result'
        data = Time.parse data
    else
        resolve_times data, msg['times']
    end
    if msg['response'] == 'watch'
        handle_watch_data msg['id'], data
    elsif msg['response'] == 'rpc'
        handle_rpc_result msg['id'], data
    elsif msg['response'] == 'file'
        handle_file_data msg['id'], data, msg['end']
    end
  rescue Exception => e
      #print e.message
      #print e.backtrace.inspect
  end
end

#reloginObject

password



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/bull/client.rb', line 143

def relogin #password
    (@user_id, @password).then do |response|
        if response
            #show_relogin false
            notify_ok 'relogged', 0
            rewatch
        else
            notify_error 'password incorrect', 0
        end
    end
end

#rewatchObject



137
138
139
140
141
# File 'lib/bull/client.rb', line 137

def rewatch
    @watch.each do |id, value|
        send 'watch_' + value[:name], id, *value[:args]
    end
end

#rpc(command, *args) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/bull/client.rb', line 63

def rpc(command, *args)
    id = get_ticket
    promise = Promise.new
    send 'rpc_'+command, id, *args
    @promises[id] = promise
    promise
end

#start(app) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/bull/client.rb', line 182

def start(app)
    begin
        controller = self
        url = 'wss://' + `document.location.hostname` + ':3000'
        @ws = Browser::Socket.new url do
            on :open do |e|
                controller.connection.value = 'connected'
                if !controller.app_rendered
                    $document.ready do
                      React.render(React.create_element(app), `document.getElementById('container')`)
                      controller.app_rendered = true
                    end
                else
                    if @user_id
                        #controller.show_relogin true
                        controller.relogin
                    else
                        controller.rewatch
                    end
                end
            end
            on :message do |e|
                begin
                    controller.notify e.data
                rescue Exception => e
                    #print e.message
                    #print e.backtrace.inspect
                end
            end
            on :close do |e|
                #print e.message
                #print e.backtrace.inspect
                controller.connection.value = 'disconnected'
                $window.after(5) {controller.reset}

            end
        end            
    rescue Exception => e  
        #print e.message
        #print e.backtrace.inspect
        $window.after(5) {reset}
    end
end

#stop_watch(id) ⇒ Object



58
59
60
61
# File 'lib/bull/client.rb', line 58

def stop_watch(id)
    @watch.delete(id)
    send('stop_watch', id)
end

#task(name, *args) ⇒ Object



50
51
52
# File 'lib/bull/client.rb', line 50

def task(name, *args)
    send 'task_' + name, -1, *args
end

#update(table, id, hsh) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bull/client.rb', line 94

def update(table, id, hsh)
    prom = rpc('update', table, id, value: hsh).then do |count|
        if count == 0
            #$notifications.add ['error', 'data not updated', 0] if $notifications
            notify_error 'data not updated', 0
        elsif count == 1
            #$notifications.add ['ok', 'data updated', 0] if $notifications
            notify_ok 'data updated', 0
        end
        count
    end
    prom
end

#watch(name, *args, &block) ⇒ Object



43
44
45
46
47
48
# File 'lib/bull/client.rb', line 43

def watch(name, *args, &block)
    id = get_ticket
    @watch[id] = {who: block, name: name, args: args}
    send('watch_' + name, id, *args)
    id
end