-
Notifications
You must be signed in to change notification settings - Fork 3
/
contact_status.py
483 lines (401 loc) · 14.2 KB
/
contact_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
107
108
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/python
# contact_status.py
#
#
# Copyright (C) 2008-2016 Veselin Penev, http://bitdust.io
#
# This file (contact_status.py) is part of BitDust Software.
#
# BitDust is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BitDust Software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with BitDust Software. If not, see <http://www.gnu.org/licenses/>.
#
# Please contact us if you have any questions at bitdust.io@gmail.com
#
#
#
#
"""
.. module:: contact_status.
.. raw:: html
<a href="http://bitdust.io/automats/contact_status/contact_status.png" target="_blank">
<img src="http://bitdust.io/automats/contact_status/contact_status.png" style="max-width:100%;">
</a>
A state machine and several extra methods to keep track of current users's online state.
To do p2p communications need to know who is available and who is not at the moment.
This simple state machine is used to detect connection status with remote user.
The situation when remote user replies to a packet sent to him
means that he is currently available over the network.
A one instance of ``contact_status()`` machine is created for
every remote contact and monitor his status.
EVENTS:
* :red:`file-sent`
* :red:`inbox-packet`
* :red:`outbox-packet`
* :red:`sent-done`
* :red:`sent-failed`
* :red:`timer-20sec`
"""
#------------------------------------------------------------------------------
_Debug = True
_DebugLevel = 6
#------------------------------------------------------------------------------
import os
import sys
import time
try:
from twisted.internet import reactor
except:
sys.exit('Error initializing twisted.internet.reactor in contact_status.py')
#------------------------------------------------------------------------------
from logs import lg
from lib import nameurl
from main import settings
from contacts import contactsdb
from automats import automat
from p2p import commands
from transport import callback
import ratings
from userid import my_id
#------------------------------------------------------------------------------
_StatusLabels = {
'CONNECTED': 'online',
'ACK?': 'responding',
'PING': 'checking',
'OFFLINE': 'offline',
}
_StatusIcons = {
'CONNECTED': 'icons/online-user01.png',
'ACK?': 'icons/ackwait-user01.png',
'PING': 'icons/ping-user01.png',
'OFFLINE': 'icons/offline-user01.png',
}
#------------------------------------------------------------------------------
_ContactsStatusDict = {}
_ShutdownFlag = False
#------------------------------------------------------------------------------
def init():
"""
Needs to be called before other methods here.
"""
lg.out(4, 'contact_status.init')
callback.insert_inbox_callback(-1, Inbox)
callback.add_outbox_callback(Outbox)
callback.add_queue_item_status_callback(OutboxStatus)
def shutdown():
"""
Called from top level code when the software is finishing.
"""
lg.out(4, 'contact_status.shutdown')
global _ShutdownFlag
global _ContactsStatusDict
for A in _ContactsStatusDict.values():
A.destroy()
_ContactsStatusDict.clear()
_ShutdownFlag = True
def isKnown(idurl):
"""
Return `True` if state machine contact_status() already exists for this
user.
"""
if idurl in [None, 'None', '']:
return False
global _ContactsStatusDict
return idurl in _ContactsStatusDict.keys()
def isOnline(idurl):
"""
Return True if given contact's state is ONLINE.
"""
global _ShutdownFlag
if _ShutdownFlag:
return False
if idurl in [None, 'None', '']:
return False
global _ContactsStatusDict
if idurl not in _ContactsStatusDict.keys():
A(idurl)
if _Debug:
lg.out(_DebugLevel, 'contact_status.isOnline contact %s is not found, made a new instance' % idurl)
return A(idurl).state == 'CONNECTED'
def isOffline(idurl):
"""
Return True if given contact's state is OFFLINE.
"""
global _ShutdownFlag
if _ShutdownFlag:
return True
if idurl in [None, 'None', '']:
return True
global _ContactsStatusDict
if idurl not in _ContactsStatusDict.keys():
A(idurl)
if _Debug:
lg.out(_DebugLevel, 'contact_status.isOffline contact %s is not found, made a new instance' % idurl)
return A(idurl).state == 'OFFLINE'
def isCheckingNow(idurl):
"""
Return True if given contact's state is PING or ACK?.
"""
global _ShutdownFlag
if _ShutdownFlag:
return False
if idurl in [None, 'None', '']:
return False
global _ContactsStatusDict
if idurl not in _ContactsStatusDict.keys():
A(idurl)
if _Debug:
lg.out(_DebugLevel, 'contact_status.isCheckingNow contact %s is not found, made a new instance' % idurl)
st = A(idurl).state
return st == 'PING' or st == 'ACK?'
def getStatusLabel(idurl):
"""
Return some text description about the current state of that user.
"""
global _ShutdownFlag
if _ShutdownFlag:
return '?'
if idurl in [None, 'None', '']:
return '?'
global _ContactsStatusDict
if idurl not in _ContactsStatusDict.keys():
A(idurl)
if _Debug:
lg.out(_DebugLevel, 'contact_status.getStatusLabel contact %s is not found, made a new instance' % idurl)
global _StatusLabels
return _StatusLabels.get(A(idurl).state, '?')
def getStatusIcon(idurl):
"""
Return an icon name depending on current state of that user.
"""
global _ShutdownFlag
if _ShutdownFlag:
return '?'
if idurl in [None, 'None', '']:
return '?'
global _ContactsStatusDict
if idurl not in _ContactsStatusDict.keys():
A(idurl)
if _Debug:
lg.out(_DebugLevel, 'contact_status.getStatusIcon contact %s is not found, made a new instance' % idurl)
global _StatusIcons
return _StatusIcons.get(A(idurl).state, '?')
def listOfflineSuppliers(customer_idurl=None):
"""
Loops all suppliers and check their state, return a list of those with
state OFFLINE.
"""
result = []
for idurl in contactsdb.suppliers(customer_idurl=customer_idurl):
if not idurl:
result.append(idurl)
elif isOffline(idurl):
result.append(idurl)
return result
def listOfflineCustomers():
"""
Loops all customers and check their state, return a list of those with
state OFFLINE.
"""
result = []
for idurl in contactsdb.customers():
if not idurl:
result.append(idurl)
elif isOffline(idurl):
result.append(idurl)
return result
def countOfflineAmong(idurls_list):
"""
Loops all IDs in ``idurls_list`` and count how many is OFFLINE.
"""
num = 0
for idurl in idurls_list:
if isOffline(idurl):
num += 1
return num
def countOnlineAmong(idurls_list):
"""
Loops all IDs in ``idurls_list`` and count how many is ONLINE.
"""
num = 0
for idurl in idurls_list:
if idurl:
if isOnline(idurl):
num += 1
return num
#------------------------------------------------------------------------------
def A(idurl, event=None, arg=None):
"""
Access method to interact with a state machine created for given contact.
"""
global _ShutdownFlag
global _ContactsStatusDict
if idurl not in _ContactsStatusDict:
if _ShutdownFlag:
return None
_ContactsStatusDict[idurl] = ContactStatus(
idurl, 'status_%s' % nameurl.GetName(idurl), 'OFFLINE', 8)
if event is not None:
_ContactsStatusDict[idurl].automat(event, arg)
return _ContactsStatusDict[idurl]
class ContactStatus(automat.Automat):
"""
A class to keep track of user's online status.
"""
fast = True
timers = {
'timer-20sec': (20.0, ['PING', 'ACK?']),
}
def __init__(self, idurl, name, state, debug_level):
self.idurl = idurl
self.time_connected = None
automat.Automat.__init__(self, name, state, debug_level)
if _Debug:
lg.out(_DebugLevel + 2, 'contact_status.ContactStatus %s %s %s' % (name, state, idurl))
def state_changed(self, oldstate, newstate, event, arg):
if _Debug:
lg.out(_DebugLevel - 2, '%s : [%s]->[%s]' % (nameurl.GetName(self.idurl), oldstate, newstate))
def A(self, event, arg):
#---CONNECTED---
if self.state == 'CONNECTED':
if event == 'sent-failed' and self.Fails >= 3 and self.isDataPacket(arg):
self.state = 'OFFLINE'
self.doRepaint(arg)
elif event == 'sent-failed' and self.isDataPacket(arg) and self.Fails < 3:
self.Fails += 1
#---OFFLINE---
elif self.state == 'OFFLINE':
if event == 'outbox-packet' and self.isPingPacket(arg):
self.state = 'PING'
self.AckCounter = 0
self.doRepaint(arg)
elif event == 'inbox-packet':
self.state = 'CONNECTED'
self.Fails = 0
self.doRememberTime(arg)
self.doRepaint(arg)
#---PING---
elif self.state == 'PING':
if event == 'sent-done':
self.state = 'ACK?'
self.AckCounter = 0
elif event == 'inbox-packet':
self.state = 'CONNECTED'
self.Fails = 0
self.doRememberTime(arg)
self.doRepaint(arg)
elif event == 'file-sent':
self.AckCounter += 1
elif event == 'sent-failed' and self.AckCounter > 1:
self.AckCounter -= 1
elif event == 'timer-20sec' or (event == 'sent-failed' and self.AckCounter == 1):
self.state = 'OFFLINE'
self.doRepaint(arg)
#---ACK?---
elif self.state == 'ACK?':
if event == 'inbox-packet':
self.state = 'CONNECTED'
self.Fails = 0
self.doRememberTime(arg)
self.doRepaint(arg)
elif event == 'timer-20sec':
self.state = 'OFFLINE'
elif event == 'outbox-packet' and self.isPingPacket(arg):
self.state = 'PING'
self.AckCounter = 0
self.doRepaint(arg)
return None
def isPingPacket(self, arg):
"""
Condition method.
"""
pkt_out = arg
return pkt_out.outpacket and pkt_out.outpacket.Command == commands.Identity() and pkt_out.wide is True
def isDataPacket(self, arg):
"""
Condition method.
"""
pkt_out, status, error = arg
return pkt_out.outpacket.Command not in [commands.Identity(), commands.Ack()]
def doRememberTime(self, arg):
"""
Action method.
"""
self.time_connected = time.time()
def doRepaint(self, arg):
"""
Action method.
"""
if not settings.NewWebGUI():
from web import webcontrol
webcontrol.OnAliveStateChanged(self.idurl)
else:
from web import control
control.request_update([('contact', self.idurl)])
#------------------------------------------------------------------------------
def OutboxStatus(pkt_out, status, error=''):
"""
This method is called when raised a status report after sending a packet to
remote peer.
If packet sending was failed - user seems to be OFFLINE.
"""
if pkt_out.remote_idurl == my_id.getLocalID():
return False
if pkt_out.outpacket.CreatorID != my_id.getLocalID():
return False
if status == 'finished':
A(pkt_out.remote_idurl, 'sent-done', (pkt_out, status, error))
else:
if _Debug:
lg.out(_DebugLevel, 'contact_status.OutboxStatus %s: [%s] with %s' % (status, pkt_out, pkt_out.outpacket))
A(pkt_out.remote_idurl, 'sent-failed', (pkt_out, status, error))
return False
def Inbox(newpacket, info, status, message):
"""
This is called when some ``packet`` was received from remote peer - user seems to be ONLINE.
"""
if newpacket.OwnerID == my_id.getLocalID():
return False
if newpacket.RemoteID != my_id.getLocalID():
return False
A(newpacket.OwnerID, 'inbox-packet', (newpacket, info, status, message))
ratings.remember_connected_time(newpacket.OwnerID)
return False
def Outbox(pkt_out):
"""
Called when some ``packet`` is placed in the sending queue.
This packet can be our Identity packet - this is a sort of PING operation
to try to connect with that man.
"""
if pkt_out.outpacket.RemoteID == my_id.getLocalID():
return False
if pkt_out.outpacket.CreatorID != my_id.getLocalID():
return False
A(pkt_out.outpacket.RemoteID, 'outbox-packet', pkt_out)
return False
def FileSent(workitem, args):
"""
This is called when transport_control starts the file transfer to some
peer.
Used to count how many times you PING him.
"""
if workitem.remoteid == my_id.getLocalID():
return
A(workitem.remoteid, 'file-sent', (workitem, args))
def PacketSendingTimeout(remoteID, packetID):
"""
Called from ``p2p.io_throttle`` when some packet is timed out.
Right now this do nothing, state machine ignores that event.
"""
if remoteID == my_id.getLocalID():
return
A(remoteID, 'sent-timeout', packetID)