-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.py
369 lines (340 loc) · 21 KB
/
test_server.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import socket
import os
from _thread import *
from threading import *
import time
# files present in the server storage
files = {'file1':'','file2':'','file3':''}
clients = {}
page_tables = []
currently_reading = [0,0,0]
m1 = Lock()
m2 = Lock()
m3 = Lock()
lock_for_currently_writing =[m1,m2,m3]
waiting = []
flag = 0
ServerSocket = socket.socket()
host = '127.0.0.1'
port = 1233
ThreadCount = 0
try:
ServerSocket.bind((host, port))
except socket.error as e:
print(str(e))
print('Waiting for a Connection..')
ServerSocket.listen(5)
def threaded_client(connection,client_id):
connection.send(str.encode('1Functionalities : read "filename" | write "filename" | stopread "filename"'))
while True:
reply = ""
data = connection.recv(2048)
from_client = data.decode('utf-8')
# client closes connection
# exit the loop
if from_client == 'exit':
for i in page_tables:
if i[0] == client_id:
print('Connection with client ',client_id,' closed')
filename = i[1]
if i[2] == "read":
currently_reading[int(filename[len(filename)-1])-1]-=1
page_tables[client_id-1][1] = "closed client"
page_tables[client_id-1][2] = ""
break
connection.close()
break
# message from client
from_client = from_client.split(" ")
print("Request from client ",client_id," :",from_client)
if len(from_client) != 2:
print("Client ",client_id,' did not provide enough arguments')
reply = "1Error : Invalid command. Please enter proper command"
connection.send(str.encode(reply))
continue
# client wishes to read
if from_client[0] == 'read':
# reading filename from client
filename = from_client[1]
global new_read
new_read = "Yes"
global timeout
timeout = "No"
# checking if filename present in files or not
if filename not in files:
new_read = "No"
print("Client ",client_id,' provided invalid filename to Read')
reply = "1File not found! Enter valid filename"
connection.send(str.encode(reply))
else:
# checking if client has read access
if (page_tables[client_id-1])[2] == "read":
new_read = "No"
# checking if read access is for current file
if (page_tables[client_id-1])[1] == filename:
print("Client ",client_id,' already has a read access')
reply = "1You already have the read access\nRead access only!!\nContents of "+filename+" are : \n***************************************************************\n"+files[filename]
connection.send(str.encode(reply))
else:
print("Client ",client_id," already has a read access for : "+(page_tables[client_id-1])[1])
cur_filename = (page_tables[client_id-1])[1]
reply = "1You have the read access only for "+cur_filename+"\nRead access only!!\nContents of "+cur_filename+" are : \n***************************************************************\n"+files[cur_filename]
connection.send(str.encode(reply))
# checking if the client is writing to another file
elif (page_tables[client_id-1])[1] != filename and (page_tables[client_id-1])[2] == "write":
new_read = "No"
print("Client ",client_id,' has a write access only for file ',filename)
reply = "1Access denied. You have write access for file "+filename
connection.send(str.encode(reply))
# checking if required page is being written
for i in page_tables:
if i[1] == filename and i[2] == "write":
new_read = "No"
# checking if current client is writing
if i[0] == client_id:
print("Client ",client_id,' has a write access for the file',filename)
reply = "1Access denied. You only have write access"
connection.send(str.encode(reply))
else:
print("Client ",i[0],' is writing to the file')
reply = "1Access denied. Client "+str(i[0])+" is currently writing.\nWait until read access is granted? [Y/N]"
connection.send(str.encode(reply))
dataReadWait = connection.recv(2048)
from_client_read_wait = dataReadWait.decode('utf-8')
if from_client_read_wait != 'Y':
reply = "1...Aborting..."
connection.send(str.encode(reply))
else:
reply = "...Waiting..."
connection.send(str.encode(reply))
waiting.append(client_id)
start = time.time()
while((lock_for_currently_writing[int(filename[len(filename)-1])-1]).locked()):
elapsed = time.time()-start
if (elapsed > 30):
timeout = "Yes"
print("Client ",client_id,' request time-out')
reply = "1Request time-out. Try again"
# Add file to reading list.
connection.send(str.encode(reply))
break
if (timeout != "Yes"):
waiting.pop(0)
page_tables[client_id-1] = [client_id,filename,"read"]
currently_reading[int(filename[len(filename)-1])-1]+=1
print("Client ",client_id,' has been granted a read access')
reply = "1Read access granted\nContents of "+filename+" are : \n***************************************************************\n"+files[filename]
# Add file to reading list.
connection.send(str.encode(reply))
# file name is not currently being written and client does not have read access
if (new_read == "Yes"):
page_tables[client_id-1] = [client_id,filename,"read"]
currently_reading[int(filename[len(filename)-1])-1]+=1
print("Client ",client_id,' has been granted a Read access')
reply = "1Contents of "+filename+" are : \n***************************************************************\n"+files[filename]
# Add file to reading list.
connection.send(str.encode(reply))
elif from_client[0] == 'stopread':
# check if client is not currently reading
if (page_tables[client_id-1])[2] == "write" or (page_tables[client_id-1])[2] == "":
print("Client ",client_id,' has not been granted a read access')
reply = "1Denied. You do not have read access and hence cannot stop"
connection.send(str.encode(reply))
else:
filename = from_client[1]
# checking if filename is valid
if filename not in files:
print("Client ",client_id,' provided invalid filename to Read')
reply = "1File not found! Enter valid filename"
connection.send(str.encode(reply))
# client has read access to some other file
elif (page_tables[client_id-1])[1] != filename:
print("Client ",client_id,' does not have read access to ',filename)
reply = "1Denied. You do not have read access to filename "+filename+". You have read access to "+(page_tables[client_id-1])[1]
connection.send(str.encode(reply))
# client has read access to current file
else:
print("Client ",client_id,' read access revoked')
page_tables[client_id-1] = [client_id,filename,""]
currently_reading[int(filename[len(filename)-1])-1]-=1
reply = "1Reading stopped."
# Add file to reading list.
connection.send(str.encode(reply))
elif from_client[0] == 'write':
filename = from_client[1]
global new_write
new_write = "Yes"
timeout = "No"
# checking if filename present in files or not
if filename not in files:
print("Client ",client_id,' provided invalid filename to write')
reply = "1File not found! Enter valid filename"
connection.send(str.encode(reply))
else:
# checking if client has a read access for any file
if (page_tables[client_id-1])[2] == "read":
new_write = "No"
print("Client ",client_id," access denied. Client already has a read access for : "+(page_tables[client_id-1])[1])
cur_filename = (page_tables[client_id-1])[1]
reply = "1Access denied. You are reading "+cur_filename
connection.send(str.encode(reply))
# checking if client has write access for a file
elif (page_tables[client_id-1])[2] == "write":
new_write = "No"
# checking if write access is to current file
if (page_tables[client_id-1])[1] == filename:
print("Client ",client_id,' already has a write access')
reply = "1You already have the write access\nEnter what has to be written : "
connection.send(str.encode(reply))
from_client_to_write = (connection.recv(2048)).decode('utf-8')
updated_file = files[filename] +from_client_to_write+"."
files[filename] = updated_file
reply = "1" + filename + " updated successfully\nDo you wish to continue writing? [Y/N] : "
connection.send(str.encode(reply))
dataWriteContinue = connection.recv(2048)
from_client_write_continue = dataWriteContinue.decode('utf-8')
if from_client_write_continue != 'Y':
reply = "1...Stopping write process..."
page_tables[client_id-1] = [client_id,filename,""]
(lock_for_currently_writing[int(filename[len(filename)-1])-1]).release()
print("Client " + str(client_id) + " has revoked writing access")
connection.send(str.encode(reply))
else:
reply = "...Continuing..."
connection.send(str.encode(reply))
else:
print("Client ",client_id," access denied. Client has write access for : "+(page_tables[client_id-1])[1])
cur_filename = (page_tables[client_id-1])[1]
reply = "1Access denied. You are currently writing to "+cur_filename
connection.send(str.encode(reply))
# checking if page is currently being read by another client
elif currently_reading[int(filename[len(filename)-1])-1] != 0:
new_write = "No"
print("Client/s are reading the file")
reply = "1Access denied. Client/s are currently reading.\nWait until write access is granted? [Y/N]"
connection.send(str.encode(reply))
dataWriteWait = connection.recv(2048)
from_client_write_wait = dataWriteWait.decode('utf-8')
if from_client_write_wait != 'Y':
reply = "1...Aborting..."
connection.send(str.encode(reply))
else:
reply = "...Waiting..."
connection.send(str.encode(reply))
start = time.time()
while(currently_reading[int(filename[len(filename)-1])-1] != 0):
elapsed = time.time()-start
if (elapsed > 30):
timeout = "Yes"
print("Client ",client_id,' request time-out')
reply = "1Request time-out. Try again"
connection.send(str.encode(reply))
break
if (timeout != "Yes") :
page_tables[client_id-1] = [client_id,filename,"write"]
(lock_for_currently_writing[int(filename[len(filename)-1])-1]).acquire()
print("Client ",client_id,' has been given write access')
reply = "1Access granted\nEnter what has to be written : "
connection.send(str.encode(reply))
from_client_to_write = (connection.recv(2048)).decode('utf-8')
updated_file = files[filename] +from_client_to_write+"."
files[filename] = updated_file
reply = "1" + filename + " updated successfully\nDo you wish to continue writing? [Y/N] : "
connection.send(str.encode(reply))
dataWriteContinue = connection.recv(2048)
from_client_write_continue = dataWriteContinue.decode('utf-8')
if from_client_write_continue != 'Y':
reply = "1...Stopping write process..."
(lock_for_currently_writing[int(filename[len(filename)-1])-1]).release()
page_tables[client_id-1] = [client_id,filename,""]
print("Client " + str(client_id) + " has revoked writing access")
connection.send(str.encode(reply))
else:
reply = "1...Continuing..."
connection.send(str.encode(reply))
# checking if page is being written by another client
# Not including read here as multiple clients can be reading the same file
for i in page_tables:
if i[1] == filename and i[2] == "write" and i[0] != client_id:
new_write = "No"
print("Client ",i[0],' is writing to the file')
print(client_id)
reply = "1Access denied. Client "+str(i[0])+" is currently writing.\nWait until write access is granted? [Y/N]"
connection.send(str.encode(reply))
dataReadWait = connection.recv(2048)
from_client_read_wait = dataReadWait.decode('utf-8')
if from_client_read_wait != 'Y':
reply = "1...Aborting..."
connection.send(str.encode(reply))
else:
reply = "...Waiting..."
connection.send(str.encode(reply))
waiting.append(client_id)
start = time.time()
while((lock_for_currently_writing[int(filename[len(filename)-1])-1]).locked()):
elapsed = time.time()-start
if (elapsed > 30):
timeout = "Yes"
print("Client ",client_id,' request time-out')
reply = "1Request time-out. Try again"
connection.send(str.encode(reply))
break
if (timeout != "Yes"):
waiting.pop(0)
page_tables[client_id-1] = [client_id,filename,"write"]
(lock_for_currently_writing[int(filename[len(filename)-1])-1]).acquire()
print("Client ",client_id,' has been given write access')
reply = "1Access granted\nEnter what has to be written : "
connection.send(str.encode(reply))
from_client_to_write = (connection.recv(2048)).decode('utf-8')
updated_file = files[filename] +from_client_to_write+"."
files[filename] = updated_file
reply = "1" + filename + " updated successfully\nDo you wish to continue writing? [Y/N] : "
connection.send(str.encode(reply))
dataWriteContinue = connection.recv(2048)
from_client_write_continue = dataWriteContinue.decode('utf-8')
if from_client_write_continue != 'Y':
reply = "1...Stopping write process..."
print("Client " + str(client_id) + " has revoked writing access")
(lock_for_currently_writing[int(filename[len(filename)-1])-1]).release()
page_tables[client_id-1] = [client_id,filename,""]
connection.send(str.encode(reply))
else:
reply = "1...Continuing..."
connection.send(str.encode(reply))
# Client does have read access nor write access to current or any other file
if (new_write == "Yes"):
page_tables[client_id-1] = [client_id,filename,"write"]
(lock_for_currently_writing[int(filename[len(filename)-1])-1]).acquire()
print("Client ",client_id,' has been given write access')
reply = "1Access granted\nEnter what has to be written : "
connection.send(str.encode(reply))
from_client_to_write = (connection.recv(2048)).decode('utf-8')
updated_file = files[filename] +from_client_to_write+"."
files[filename] = updated_file
reply = "1" + filename + " updated successfully\nDo you wish to continue writing? [Y/N] : "
connection.send(str.encode(reply))
dataWriteContinue = connection.recv(2048)
from_client_write_continue = dataWriteContinue.decode('utf-8')
if from_client_write_continue != 'Y':
reply = "1...Stopping write process..."
page_tables[client_id-1] = [client_id,filename,""]
(lock_for_currently_writing[int(filename[len(filename)-1])-1]).release()
print("Client " + str(client_id) + " has revoked writing access")
connection.send(str.encode(reply))
else:
reply = "1...Continuing..."
connection.send(str.encode(reply))
# Command not indentified by the
else :
print("Invalid command from Client ",client_id)
reply = "1Enter valid command"
connection.send(str.encode(reply))
while True:
Client, address = ServerSocket.accept()
print('Connected to: ' + address[0] + ':' + str(address[1]))
page_tables.append([ThreadCount+1,"",""])
start_new_thread(threaded_client, (Client, ThreadCount+1))
ThreadCount += 1
print('Client Number: ' + str(ThreadCount))
ServerSocket.close()