Skip to content

Commit 4c06b94

Browse files
committed
populate all api messages to remotely connected device
1 parent 89fc9cb commit 4c06b94

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

bitdust/chat/message_keeper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from bitdust.logs import lg
4343

4444
from bitdust.interface import api_web_socket
45+
from bitdust.interface import api_device
4546

4647
from bitdust.crypt import my_keys
4748

@@ -155,6 +156,7 @@ def store_message(data, message_id, sender_id, recipient_id, message_type=None,
155156
lg.warn('message %r was not stored' % message_id)
156157
return message_json
157158
api_web_socket.on_stream_message(message_json)
159+
api_device.on_stream_message(message_json)
158160
if _Debug:
159161
lg.out(_DebugLevel, 'message_keeper.store_message [%s]:%s from %r to %r' % (message_type, message_id, sender_id, recipient_id))
160162
return message_json

bitdust/interface/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ def enable_model_listener(model_name, request_all=False):
240240
lg.args(_DebugLevel, m=model_name, request_all=request_all)
241241
from bitdust.main import listeners
242242
from bitdust.interface import api_web_socket
243+
from bitdust.interface import api_device
243244
listeners.add_listener(api_web_socket.on_model_changed, model_name)
245+
listeners.add_listener(api_device.on_model_changed, model_name)
244246
if request_all:
245247
return request_model_data(model_name)
246248
return OK()
@@ -257,6 +259,7 @@ def disable_model_listener(model_name):
257259
lg.args(_DebugLevel, m=model_name)
258260
from bitdust.main import listeners
259261
from bitdust.interface import api_web_socket
262+
from bitdust.interface import api_device
260263
if model_name == 'key':
261264
listeners.populate_later('key', stop=True)
262265
elif model_name == 'conversation':
@@ -276,6 +279,7 @@ def disable_model_listener(model_name):
276279
elif model_name == 'shared_location':
277280
listeners.populate_later('shared_location', stop=True)
278281
listeners.remove_listener(api_web_socket.on_model_changed, model_name)
282+
listeners.remove_listener(api_device.on_model_changed, model_name)
279283
return OK()
280284

281285

bitdust/interface/api_device.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
_Debug = True
3838
_DebugLevel = 10
3939

40+
_APILogFileEnabled = False
41+
4042
#------------------------------------------------------------------------------
4143

4244
import os
@@ -81,6 +83,8 @@
8183

8284
def init():
8385
global _AllAPIMethods
86+
global _APILogFileEnabled
87+
_APILogFileEnabled = settings.config.conf().getBool('logs/api-enabled')
8488
from bitdust.interface import api
8589
encrypted_web_socket.SetIncomingAPIMessageCallback(do_process_incoming_message)
8690
routed_web_socket.SetIncomingAPIMessageCallback(do_process_incoming_message)
@@ -96,6 +100,7 @@ def init():
96100
'RESULT',
97101
'_Debug',
98102
'_DebugLevel',
103+
'_APILogFileEnabled',
99104
'strng',
100105
'sys',
101106
'time',
@@ -428,6 +433,7 @@ def reset_authorization(device_name):
428433

429434
def do_process_incoming_message(device_object, json_data):
430435
global _AllAPIMethods
436+
global _APILogFileEnabled
431437
from bitdust.interface import api
432438
command = json_data.get('command')
433439
if command == 'api_call':
@@ -460,8 +466,8 @@ def do_process_incoming_message(device_object, json_data):
460466
if _Debug:
461467
lg.out(_DebugLevel, '*** %s API WS IN %s(%r)' % (call_id, method, kwargs))
462468

463-
# if _APILogFileEnabled:
464-
# lg.out(0, '*** %s WS IN %s(%r)' % (call_id, method, kwargs), log_name='api', showtime=True)
469+
if _APILogFileEnabled:
470+
lg.out(0, '*** %s WS IN %s %s(%r)' % (device_object.device_name, call_id, method, kwargs), log_name='api', showtime=True)
465471

466472
func = getattr(api, method)
467473
try:
@@ -520,42 +526,38 @@ def _eb(err):
520526

521527

522528
def on_event(evt):
523-
for inst in instances().values():
524-
inst.on_outgoing_message({
525-
'cmd': 'push',
526-
'type': 'event',
527-
'payload': {
528-
'event_id': evt.event_id,
529-
'data': evt.data,
530-
},
531-
})
529+
push({
530+
'cmd': 'push',
531+
'type': 'event',
532+
'payload': {
533+
'event_id': evt.event_id,
534+
'data': evt.data,
535+
},
536+
})
532537

533538

534539
def on_stream_message(message_json):
535-
for inst in instances().values():
536-
inst.on_outgoing_message({
537-
'cmd': 'push',
538-
'type': 'stream_message',
539-
'payload': message_json,
540-
})
540+
push({
541+
'cmd': 'push',
542+
'type': 'stream_message',
543+
'payload': message_json,
544+
})
541545

542546

543547
def on_online_status_changed(status_info):
544-
for inst in instances().values():
545-
inst.on_outgoing_message({
546-
'cmd': 'push',
547-
'type': 'online_status',
548-
'payload': status_info,
549-
})
548+
push({
549+
'cmd': 'push',
550+
'type': 'online_status',
551+
'payload': status_info,
552+
})
550553

551554

552555
def on_model_changed(snapshot_object):
553-
for inst in instances().values():
554-
inst.on_outgoing_message({
555-
'cmd': 'push',
556-
'type': 'model',
557-
'payload': snapshot_object.to_json(),
558-
})
556+
push({
557+
'cmd': 'push',
558+
'type': 'model',
559+
'payload': snapshot_object.to_json(),
560+
})
559561

560562

561563
def on_device_client_code_input_received(device_name, client_code):
@@ -566,6 +568,17 @@ def on_device_client_code_input_received(device_name, client_code):
566568
inst.on_client_code_input_received(client_code)
567569

568570

571+
#------------------------------------------------------------------------------
572+
573+
574+
def push(json_data):
575+
global _APILogFileEnabled
576+
for inst in instances().values():
577+
inst.on_outgoing_message(json_data)
578+
if _APILogFileEnabled:
579+
lg.out(0, '*** WS PUSH %s : %r' % (inst.device_name, json_data), log_name='api', showtime=True)
580+
581+
569582
#------------------------------------------------------------------------------
570583

571584
if __name__ == '__main__':

bitdust/interface/routed_web_socket.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ def doWaitClientCodeInput(self, *args, **kwargs):
812812
"""
813813
Action method.
814814
"""
815+
self.server_code = None
815816
events.send('web-socket-handshake-proceeding', data=self.to_json())
816817
BITDUST_WEB_SOCKET_CLIENT_CODE_INPUT = os.environ.get('BITDUST_WEB_SOCKET_CLIENT_CODE_INPUT', None)
817818
if BITDUST_WEB_SOCKET_CLIENT_CODE_INPUT:

0 commit comments

Comments
 (0)