Skip to content

Commit 95b44bf

Browse files
committed
Last command left.
1 parent d92ccee commit 95b44bf

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

korobool/chat/Threaded.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def notify(self, message):
4848

4949
def close(self):
5050
# Send closing message to client and then close the connection
51-
print('Closing notification to client is not implemented yet...')
5251
self.closing = True
52+
self.send({'cmd': 'CMD_CLOSE'})
5353
self.thread.join()
5454

5555
# These methods ere executed in separate thread. Be aware of synchronization issues.
@@ -64,6 +64,11 @@ def __serve(stw):
6464
command = ServingThreadWrapper.__receive_and_parse_client_command(stw)
6565
ServingThreadWrapper.__process_command(stw, command)
6666

67+
print('trying to close client connection...')
68+
ServingThreadWrapper.__process_server_messages_queue(stw)
69+
stw.conn.close()
70+
stw.notify({'cmd': 'CMD_CLIENT_LEFT', 'id': stw})
71+
6772
@staticmethod
6873
def __process_server_messages_queue(stw):
6974

@@ -97,13 +102,13 @@ def __receive_and_parse_client_command(stw):
97102
print('Protocol error. Closing client connection.')
98103
stw.closing = True
99104

100-
print('!DATA!\n', body)
105+
# print('!DATA!\n', body)
101106

102107
command = json.loads(body)
103108

104109
if not command:
105110
print('Protocol error. Closing client connection.')
106-
stw.closing = True
111+
stw.close()
107112

108113
return command
109114

@@ -113,10 +118,6 @@ def __process_command(stw, command):
113118
print('new command from client received', command['cmd'])
114119
if command['cmd'] == 'CMD_PING':
115120
stw.send({'cmd': 'CMD_PONG', 'msg': 'pong from server'})
116-
117-
if command['cmd'] == 'CMD_BROADCAST':
118-
stw.notify(command)
119-
120-
if command['cmd'] == 'CMD_MESSAGE':
121+
else:
121122
stw.notify(command)
122123

korobool/chat/chat_client.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from contextlib import closing
2+
import os
13
import threading
4+
import sys
25

36
__author__ = 'Oleksandr Korobov'
47

@@ -12,10 +15,21 @@
1215
s.connect((HOST, PORT))
1316

1417

18+
def close_client():
19+
pid = os.getpid()
20+
os.kill(pid, 9)
21+
22+
1523
def receive_and_parse_command():
24+
1625
while True:
17-
header = s.recv(4)
18-
if not header: return
26+
try:
27+
header = s.recv(4)
28+
if not header: break
29+
except:
30+
print('connection error. exiting...')
31+
close_client()
32+
1933

2034
size = int(struct.unpack('i', header)[0])
2135
print('receiving bytes:', size)
@@ -25,6 +39,10 @@ def receive_and_parse_command():
2539

2640
print('data from server', command)
2741

42+
if command['cmd'] == 'CMD_CLOSE':
43+
close_client()
44+
45+
2846
read_thread = threading.Thread(target=receive_and_parse_command)
2947
read_thread.start()
3048

@@ -48,6 +66,9 @@ def receive_and_parse_command():
4866
message = input("Enter message: ")
4967
package = {'cmd': 'CMD_MESSAGE', 'msg': message}
5068

69+
if command_text.upper() == 'SCLOSESERVER':
70+
package = {'cmd': 'CMD_SCLOSESERVER'}
71+
5172
data = json.dumps(package)
5273

5374
b = bytes(data, 'utf-8')

korobool/chat/sever.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import time
23

34
__author__ = 'Oleksandr Korobov'
45

@@ -19,34 +20,50 @@ def __start_serve_connection(self, conn, addr):
1920
self.__clients_pool.append(stw)
2021

2122
def __close_all_clients(self):
22-
for client in self.__clients_pool:
23-
client.close()
24-
self.__clients_pool.clear()
23+
print('closing ', len(self.__clients_pool), 'clients')
24+
25+
while len(self.__clients_pool) > 0:
26+
self.__clients_pool[0].close()
27+
#self.socket.shutdown(socket.SHUT_RDWR)
28+
self.socket.close()
2529

2630
def serve(self):
2731
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2832
self.socket.bind((self.host, self.port))
2933

34+
self.socket.settimeout(2)
3035
while not self.closing:
36+
3137
self.socket.listen(5)
32-
conn, addr = self.socket.accept()
33-
self.__start_serve_connection(conn, addr)
38+
try:
39+
conn, addr = self.socket.accept()
40+
self.__start_serve_connection(conn, addr)
41+
except:
42+
pass # Remove this awful exception swallowing
3443
self.__close_all_clients()
3544

3645
def stop_serve(self):
3746
self.closing = True
38-
print('Proper server closing is not implemented yet...')
47+
self.__close_all_clients()
3948

4049
def notify(self, sender, message):
4150
print('Notification received:', message)
51+
4252
if message['cmd'] == 'CMD_BROADCAST':
4353
for client in self.__clients_pool:
4454
if not client is None and not client.closing:
4555
#print('Posting to '. client.name)
4656
client.post(message)
57+
4758
if message['cmd'] == 'CMD_MESSAGE':
4859
client.post({'cmd': 'CMD_SEVER_WARNING', 'msg': 'message sending is not implemented yet'})
4960

61+
if message['cmd'] == 'CMD_CLIENT_LEFT':
62+
print('Client left', message['id'])
63+
self.__clients_pool.remove(message['id'])
64+
65+
if message['cmd'] == 'CMD_SCLOSESERVER':
66+
self.closing = True
5067

5168
chat_sever = ChatServer(PORT = 50007)
5269

@@ -57,4 +74,4 @@ def signal_handler(signal, frame):
5774

5875
signal.signal(signal.SIGINT, signal_handler)
5976

60-
chat_sever.serve()
77+
chat_sever.serve()

0 commit comments

Comments
 (0)