-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
124 lines (108 loc) · 4.74 KB
/
client.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
"""
@author: Fotios Lygerakis
@UTA ID: 1001774373
"""
import os
import queue
import select
from config import HEADER_LENGTH
from utils.utils import send_msg, receive_file, save_file, set_up_username
"""
Code based on https://pythonprogramming.net/client-chatroom-sockets-tutorial-python-3/
"""
class Client:
def __init__(self):
self.send_file_to_server = False
# the username and connection socket
self.username, self.socket = [None, None]
# the name of the file to send to the server for checking
self.filename = None
# the text retrieved from the file in string format
self.text_string = None
# the queue to store the lexicon additions
self.q = queue.Queue()
def set_up_connection(self, username):
"""
Sets up connection and username
:param username: username for this client
:return: True if connection succeeded and username has been established, False otherwise
"""
try:
# set up the connection and establish a username to the server
response = set_up_username(username, HEADER_LENGTH)
if response is not None:
self.username, self.socket = response
return True
else:
return False
except Exception as e:
print(e)
return False
def main(self):
"""
Main functionality of the client
"""
while True:
# check for polling demand
read_sockets, _, exception_sockets = select.select([self.socket], [], [self.socket], 0.5)
# Iterate over notified sockets
for notified_socket in read_sockets:
# If notified socket is a server socket - new connection, accept it
if notified_socket == self.socket:
# Receive message
message = receive_file(notified_socket, header_length=HEADER_LENGTH)
# If False, client disconnected, cleanup
if message is False:
print("False")
message = message["data"].decode()
if message == "poll":
print("A poll is received from the server...")
while not self.q.empty():
word = self.q.get()
send_msg(self.socket, word, HEADER_LENGTH)
print("The word \'{}\' was retrieved by the server.".format(word))
send_msg(self.socket, "poll_end", HEADER_LENGTH)
if self.send_file_to_server:
response = self.send_file()
# in case file requested does not exist
if not response:
self.send_file_to_server = False
continue
print("Successfully uploaded file to the server.")
read_sockets, _, exception_sockets = select.select([self.socket], [], [self.socket])
# Iterate over notified sockets
for notified_socket in read_sockets:
# If notified socket is a server socket - new connection, accept it
if notified_socket == self.socket:
# Receive message
message = receive_file(notified_socket, header_length=HEADER_LENGTH)
# If False, client disconnected, cleanup
if message is False:
print("False")
message = message["data"].decode()
path = "client_files/"
filename = "annotated_{}_{}.txt".format(self.filename, self.username)
save_file(message, path, filename)
print("The spell check sequence has been completed.")
self.send_file_to_server = False
def send_file(self):
"""
Read the text from the 'self.filename' file
:return: True if file exists, False otherwise
"""
if os.path.isfile("client_files/" + self.filename):
with open("client_files/" + self.filename, "r") as file:
text_list = file.readlines()
self.text_string = ''.join(text_list)
send_msg(self.socket, self.text_string, HEADER_LENGTH)
return True
else:
print("\'{}\' does not exist.\nPlease provide a valid file name.".format(self.filename))
return False
def add_to_queue(self, word):
"""
Add a word into the clients queue
:param word: word to add in the queue
"""
self.q.put(word)
print("word \'{}\' added in clients queue".format(word))