-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_status.py
More file actions
66 lines (45 loc) · 1.83 KB
/
Copy pathclient_status.py
File metadata and controls
66 lines (45 loc) · 1.83 KB
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
# -*- coding: utf-8 -*-
import time
threshold = 3
class ClientStatus:
'''
This class is mainly used to maintain the status of each client.
The client will send 'heart beat' package ("-^-^-pyHB-^-^-") to the server
There are two benefits of doing this:
1. the heart beat package will allow the server to be aware of whether the client is ALIVE or not
2. the heart beat package also serve as PASSWORD to maintain a connection with the server,
so that the connection which is NOT raised from our client program will be rejected(closed by the server)
'''
__client_has_logged_in = False
# record how long the user keeps online this time
__online_duration = None
__last_online_time_stamp = None
__login_time_stamp = None
# record the time stamp that a heart beat signal came in
__last_check_in_time_stamp = None
def __init__(self):
self.update_client_online_status()
def client_login(self):
self.__client_has_logged_in = True
self.__login_time_stamp = time.time()
def client_logout(self):
self.__client_has_logged_in = False
def client_has_login_or_not(self):
return self.__client_has_logged_in
def update_client_online_status(self):
self.__last_check_in_time_stamp = time.time()
def is_client_offline(self):
delta = time.time() - self.__last_check_in_time_stamp
if delta <= threshold:
return False
else:
return True
def get_last_checked_in_time(self):
return self.__last_check_in_time_stamp
def get_client_online_duration(self):
self.__online_duration = time.time() - self.__login_time_stamp
return self.__online_duration
def get_client_login_time_stamp(self):
return self.__login_time_stamp
if __name__ == '__main__':
pass