-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncmanager.py
More file actions
57 lines (54 loc) · 1.38 KB
/
asyncmanager.py
File metadata and controls
57 lines (54 loc) · 1.38 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
from hashlib import md5
#for the future use
import asyncio
class Topic:
def __init__(self,name):
self.uname = name
self.users = []
def send(self,msg):
for x in self.users:
x['sock'].write(msg)
def add(self,client):
if not self.is_in(client['sock']):
self.users.append(client)
return True
else:return False
def remove(self,client):
if self.is_in(client['sock']):
self.users.remove(client)
return True
return False
def is_in(self,obj):
return any([x['sock'] == obj for x in self.users])
class Manager:
def __init__(self,t):
self.threads = {'base':Topic('base'),'base1':Topic('base1')}
for x in t:
self.threads[x['name']] = Topic(x['name'])
def send(self,client,msg):
thread = client.get('thread')
if not thread:
self.threads['base'].add(client)
client['thread'] = 'base'
self.threads['base'].send(msg)
else:
self.threads[thread].send(msg)
def subscribe(self,client,t):
thread = self.threads.get(t)
if thread:
if client.get('thread'):
try:
self.threads[client['thread']].remove(client)
except KeyError:
pass
client['thread'] = thread.uname
return thread.add(client)
return False
def unsubscribe(self,client):
return self.subscribe(client,'base')
#possible error
if client.thread:
client.thread = 'base'
self.thread['base'].add(client)
return self.threads[client.thread].remove(client)
return False