-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_handler.py
235 lines (155 loc) · 5.82 KB
/
request_handler.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
# SHOULD NOT BE ABLE TO CREATE TWO FOLDERS OF SAME NAME
import ram
import dbg
# from mount import start
from call import *
from _thread import *
from bcolors import *
from error import *
def processRequest(command, currentUser):
command = command.split()
arg = [000]
if command[0] == "e":
ram.user_space.pop(currentUser)
if command[0]=="t" and command[1]=="show":
res = " "
print("In t show")
for data in ram.user_space[currentUser][2]:
res = res + data+"\n"
ram.user_space[currentUser][2] = []
return res
# Prints
if command[0] == "ls":
return cls(ram.user_space[currentUser][0])
# t operation fname arguments
# Operations can be 0/1/2
# 0 = read, 1 = write, 2 = truncate
# arguments are the same as for read, write and truncate
if command[0] == "t":
sub_command = command[1]
temp = sub_command.split('-')
sub_command = int(temp[0])
delay = 0
if len(temp) == 2:
delay = int(temp[1])
if sub_command == 0:
start = int(command[3])
end = int(command[4])
# for truly multithreaded client-server communication this must not be returned here
start_new_thread(trwt,(command[2], sub_command, ram.user_space[currentUser][0], [start, end], currentUser, delay))
# trwt(command[2], sub_command, ram.user_space[currentUser][0], [start, end], currentUser)
return "0"
if sub_command == 1:
loc = int(command[3])
text = ' '.join(command[4:])
# for truly multithreaded client-server communication this must not be returned here
start_new_thread(trwt,(command[2], sub_command, ram.user_space[currentUser][0], [loc, text], currentUser, delay))
# trwt(command[2], sub_command, ram.user_space[currentUser][0], [loc, text], currentUser)
return "0"
if sub_command == 2:
size = int(command[3])
# for truly multithreaded client-server communication this must not be returned here
start_new_thread(trwt,(command[2], sub_command, ram.user_space[currentUser][0], [size], currentUser, delay))
# trwt(command[2], sub_command, ram.user_space[currentUser][0], [size], currentUser)
return "0"
# If file being created is already in directory table then through error
if command[0] == "make":
# if it is anythin other then 0 then it is for file not dir
dir_flag = False if command[2]!="0" else True
cmakeFile(ram.user_space[currentUser][0], command[1], dir_flag)
ram.changes = ram.changes + 1
return "0";
if command[0] == "delete":
status = cdelete(command[1], ram.user_space[currentUser][0]);
ram.changes = ram.changes + 1
return str(status)
# Prints
if command[0] == "data":
status, data = cshow_data(int(command[1]))
if status < 0:
return str(status)
return "100"+data
if command[0] == "open":
mode = 1 if command[2]!="0" else 0
status = cOpen(command[1], mode, currentUser)
if status == 0:
return "100"
return str(status)
if command[0] == "close":
status = cClose(command[1])
if status == 0:
return "100"
return str(status)
# Prints
if command[0] == "read":
start = int(command[2])
end = int(command[3])
status, text = cread(command[1], start, end)
if status == 0:
status = "100"
return str(status) + str(text)
# write filename loc text
if command[0] == "write":
fname = command[1]
loc = int(command[2])
text = ' '.join(command[3:])
status = cwrite(fname, loc, text)
if status == 0:
return "100"
ram.changes = ram.changes + 1
return str(status)
if command[0] == "truncate":
size = int(command[2])
status = ctruncate(command[1], size)
if status == 0:
return "100"
ram.changes = ram.changes + 1
return str(status)
if command[0] == "move":
status = cmove(command[1], command[2], ram.user_space[currentUser][0])
if status == 0:
return "100"
ram.changes = ram.changes + 1
return str(status)
# Prints
if command[0] == "memmap":
index = int(command[1])
status, map = cmemmap(index)
if status == -15:
return str(status)
return "100"+map
if command[0] == "chDir":
status = cchDir(command[1], ram.user_space[currentUser][0],currentUser)
if status == 0:
return "100"
return str(status)
# Prints
if command[0] == "currentInode":
cinode = ccurrentInode(currentUser);
# 0 is used as flag to not respond to client,so make it 00
if cinode == 0:
return "00"
return str(cinode);
# Prints
if command[0] == "partitionInfo":
return cpartitionInfo();
# Prints
if command[0] == "blockSize":
return str(cblockSize())
# Prints
if command[0] == "logicalSpace":
return str(clogicalSpace())
if command[0] == "freeSpace":
return str(ram.out_scope_block) +" "+ str(cfreeSpace())
if command[0] =="masterBlock":
return cmasterBlock()
if command[0] == "blocksConsumed":
status, blocks = cblocksConsumed(command[1],currentUser)
if status == 0:
status = "100"
elif status < 0:
return str(status)
return str(status) + ' '.join([str(x) for x in blocks])
if command[0] == "saveChanges":
hardWrite(filename)
return 0