-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_manager.py
More file actions
65 lines (58 loc) · 1.69 KB
/
client_manager.py
File metadata and controls
65 lines (58 loc) · 1.69 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
from client_request import Client_Request
import _thread
import threading
import select
class Client_Manager:
def __init__(self, socket):
self.socket = socket
self.connected = False
self.exit = False
def start(self):
self.listen_for_user_authentication()
def listen_for_user_authentication(self):
while True:
if self.connected:
self.handle_connected()
break
else:
data = self.socket.recv(1024)
request = Client_Request(data.decode('ascii'), self)
response = request.handle()
if response == 'CLIENT_CONNECTED':
continue
self.socket.send(response.encode('ascii'))
def handle_connected(self):
#_thread.start_new_thread(self.listen_for_server_messages())
threading.Thread(target=self.listen_for_server_messages, name='Message_stream').start()
#_thread.start_new_thread(self.listen_for_user_input())
threading.Thread(target=self.listen_for_user_input, name='User_stream').start()
def listen_for_server_messages(self):
print('Listening')
while True:
if exit:
print('Disconnecting from server')
return
data = self.socket.recv(1024)
if not data:
print('Disconnected')
return
print('New Data ', data)
request = Client_Request(data.decode('ascii'), self)
response = request.handle()
if response == '':
continue
elif response == None:
continue
if response == 'CLIENT_CLOSE':
continue
self.socket.send(response.encode('ascii'))
self.socket.close()
def listen_for_user_input(self):
print('Waiting input')
while True:
request = Client_Request('NEW_ACTION;', self)
response = request.handle()
if response == 'DISCONNECT;':
exit = True
break
self.socket.send(response.encode('ascii'))