-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheditor.py
More file actions
493 lines (361 loc) · 15 KB
/
editor.py
File metadata and controls
493 lines (361 loc) · 15 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# -*- coding: utf-8 -*-
"""
Simple sockjs-tornado coedit application.
By default will listen on port 8080.
"""
import tornado.ioloop
import tornado.web
import json
import sockjs.tornado
from collections import namedtuple
import urllib
Cursor = namedtuple('Cursor', ('row', 'column'))
users = []
def insert(original, new, pos):
"""
function to insert a string into another string in certain position
:param original: the string to be modified
:param new: the string to insert
:param pos: the index of the string character to insert the new string
"""
return original[:pos] + new + original[pos:]
def delta_change_apply(client_text, delta):
"""
function to apply the change to the client text
:param client_text: the client text to be modified in json string
:param delta: object{"action":"","range":"","text" or "lines":""}
"""
#JSON.parse
change = json.loads(delta)
action = change['action']
input_range = change['range']
start = input_range['start']
start_row = int(start['row'])
start_column = int(start['column'])
end = input_range['end']
end_row = int(end['row'])
end_column = int(end['column'])
# break the client text into lines
lines = json.JSONDecoder(strict=False).decode(client_text).split('\n')
if action == 'insertText':
#load the input text
input_text = change['text']
#deal with a new line insertion
if input_text == '\n':
#get the row where the insertion newline happened
change_string = lines[start_row]
#replace the old string in the list with string
# before the new line insertion
lines[start_row] = change_string[:start_column]
#insert the rest of the string as a new string
# right after the first part
lines.insert(start_row+1, change_string[start_column:])
#deal with a string insertion
else:
#get the string where the insertion happened
change_string = lines[start_row]
#insert the new string into certain position of the old string
#according to the column, replace the old one in the list
lines[start_row] = insert(change_string,input_text,start_column)
elif action == 'removeText':
#load the remove text
input_text = change['text']
#deal with one line remove
if input_text == '\n':
#combine two lines into one line,
#replace it with the first old line in the list
lines[start_row] = lines[start_row]+lines[end_row]
#remove the second old line in the list
lines.pop(end_row)
#deal with a string remove
else:
#get the string where the deletion happened
change_string = lines[start_row]
#remove the substring from the string
# according to the column position
lines[start_row] \
= change_string[:start_column] + change_string[end_column:]
elif action == 'insertLines':
# load the list of the insertion lines
input_lines = change['lines']
#insert the list of lines after the start tow
lines = lines[:start_row] + input_lines + lines[start_row:]
elif action == 'removeLines':
#remove the lines between the range of start row and end row
lines = lines[:start_row] + lines[end_row:]
#return the updated client text
return json.dumps('\n'.join(lines))
# user class definition
class User(object):
_cursor = None
# user id initial number
next_user = 0
def __init__(self, cursor=None, name=None):
"""
the constructor of the user class
:param cursor: user's cursor position, default set to be (0,0)
:param name: username, default set to be "user"+user id
"""
self.id = User.next_user
User.next_user += 1
self.username = name or "user" + str(User.next_user)
self.cursor = cursor or Cursor(0, 0)
@property
def cursor(self):
"""cursor getter function"""
return self._cursor
@cursor.setter
def cursor(self, value):
"""cursor setter function"""
self._cursor = value
[x.broadcast_to(source=self) for x in users if x is not self]
class IndexHandler(tornado.web.RequestHandler):
"""Regular HTTP handler to serve the chatroom page"""
def get(self):
self.render('index.html')
# ChatConnection class
class ChatConnection(sockjs.tornado.SockJSConnection):
"""Chat connection implementation"""
# Class level variable
#participants list
participants = []
#users list
users = []
#the change from the users based on the current client_text
client_text_delta = []
#the set to check if there are more than one user typing during the period
typing_user_num_check = set()
#the default start screen text of the user
client_text = json.dumps("\n")
#remember all the cursors latest action
cursor_position = []
#a tuple list recording the user's id and the user typing_in message's row
user_typing_row = []
#a list of the change because of the time lag
# between user and server communication after the client text
# update command is sent to user
collison_fix_delta = []
def on_open(self, info):
"""the new user connection setting"""
#get the username from the user's cookie
cookie_name\
= urllib.unquote(str(info.cookies)
.replace("Set-Cookie: username=", "", 1)).decode('utf8')
# Add client to the clients list
self.participants.append(self)
# Create user and add it to the user list
self.users.append(User(name=cookie_name))
#index of the user in the user list
index = len(users)-1
# create new user's cursor on other user's screen
constructor = json.dumps({
'act': 'create_cursor',
'name': self.users[index].username,
'user_id': self.users[index].id,
'row': self.users[index].cursor.row,
'column': self.users[index].cursor.column
})
# broadcast the new user's cursor to other users
self.broadcast(
(x for x in self.participants if x != self),
constructor,
)
# broadcast client text to the new user
self.broadcast(
(x for x in self.participants if x == self),
json.dumps({
'act': 'startup',
'info': ChatConnection.client_text
})
)
# setup the modification of the client text to the new user
for a in self.client_text_delta:
self.broadcast((x for x in self.participants if x == self), a)
# update all users' latest cursor position to the new user
for a in self.cursor_position:
self.broadcast((x for x in self.participants if x == self), a[1])
# add the new user's cursor to the cursor list
self.cursor_position.append((self.users[index].id, constructor))
def on_message(self, message):
"""
server action when received information from the user
:param message: the json string received from the user
"""
#deal with the new input from the user
if "action" in message:
#the user to the set of the typing user num list
self.typing_user_num_check.add(self)
#broadcast the typing info to the other user
self.broadcast((x for x in self.participants if x != self), message)
#record the change after the client test is updated
self.client_text_delta.append(message)
#record the change because of the time lag
# between user and server communication after the client text
# update command is sent to user
self.collison_fix_delta.append(message)
#update the client text
ChatConnection.client_text\
= delta_change_apply(ChatConnection.client_text, message)
#cleat the list recording the change after client text updated
self.client_text_delta.pop()
#JSON.parse
delta = json.loads(message)
input_range = delta['range']
start = input_range['start']
start_row = start['row']
end = input_range['end']
end_row = end['row']
#record the user id and the row they are editing
if start_row == end_row:
self.user_typing_row.append(
(self.users[self.participants.index(self)].id,
start_row)
)
else:
self.user_typing_row.append(
(self.users[self.participants.index(self)].id,
start_row)
)
self.user_typing_row.append(
(self.users[self.participants.index(self)].id,
end_row)
)
#deal with the new mouse selection action of the user
elif "start" in message:
#JSON.parse
cursor = json.loads(message)
start = cursor['start']
end = cursor['end']
#the change selection command formed
constructor = json.dumps({
'act': 'change_selection',
'name': self.users[self.participants.index(self)].username,
'user_id': self.users[self.participants.index(self)].id,
'start_row': start['row'],
'start_column': start['column'],
'end_row': end['row'],
'end_column': end['column'],
})
#broadcast the selection change to all the other users
self.broadcast(
(x for x in self.participants if x != self),
constructor,
)
#update this change to the cursor position
for x in self.cursor_position:
#check if user's cursor position is already in the list
if x[0] == self.users[self.participants.index(self)].id:
self.cursor_position.remove(x)
#append the new one to the cursor position list
self.cursor_position.append(
(self.users[self.participants.index(self)].id,
constructor)
)
#deal with the mouse move action of the user
else:
#JSON.parse
cursor = json.loads(message)
#form the command of cursor moving
constructor = json.dumps({
'act': 'move_cursor',
'name': self.users[self.participants.index(self)].username,
'user_id': self.users[self.participants.index(self)].id,
'row': cursor['row'],
'column': cursor['column'],
})
#broadcast the cursor move action to all the other users
self.broadcast(
(x for x in self.participants if x != self),
constructor,
)
#update this change to the cursor position
for x in self.cursor_position:
#check if user's cursor position is already in the list
if x[0] == self.users[self.participants.index(self)].id:
self.cursor_position.remove(x)
#append the new one to the cursor position list
self.cursor_position.append(
(self.users[self.participants.index(self)].id,
constructor)
)
def on_close(self):
"""disconnect user and remove all his information"""
#form the command to remove user's cursor from other user's screen
constructor = json.dumps({
'act': 'remove_cursor',
'user_id': self.users[self.participants.index(self)].id,
})
#broadcast the remove cursor action to all the other users
self.broadcast(
(x for x in self.participants if x != self),
constructor,
)
#delete the user's cursor from the cursor position list
for x in self.cursor_position:
if x[0] == self.users[self.participants.index(self)].id:
self.cursor_position.remove(x)
#remove the user from the users' list and the client list
self.users.pop(self.participants.index(self))
self.participants.remove(self)
def poll(c):
"""period call function to check if there is a collison"""
#the flag if the file need to be reset
file_reset = False
#check if there are more than one user connected
#check if during the period there are more than one user editing
# and whether there are more than one user editing the same line
if len(c._connection.participants) > 1\
and len(c._connection.typing_user_num_check) > 1:
for index, first in enumerate(c._connection.user_typing_row):
for second in c._connection.user_typing_row[(index+1):]:
if first[0] != second[0] and first[1] == second[1]:
file_reset = True
break
# when the file reset flag is true break the outside loop
if file_reset:
break
#clear the period typing user check list
c._connection.typing_user_num_check.clear()
#clear the list which remember the change before the update client text
#fired but the change hasn't been applied yet
del c._connection.collison_fix_delta[:]
# reset user's text when there is a collison
if file_reset:
#broadcast the latest client text to all users
c.broadcast(c._connection.participants,
json.dumps({
'act': 'correction',
'info': c._connection.client_text
})
)
for y in c._connection.collison_fix_delta:
c.broadcast(c._connection.participants, y)
file_reset = False
#clear the list which remember the change before the update client text
#fired but the change hasn't been applied yet
del c._connection.collison_fix_delta[:]
if __name__ == "__main__":
import logging
logging.getLogger().setLevel(logging.DEBUG)
# 1. Create chat router
ChatRouter = sockjs.tornado.SockJSRouter(ChatConnection, '/chat')
# 2. Create Tornado application and set the static path
# you need to modify the path to your own for the project to work
app = tornado.web.Application(
[(r"/", IndexHandler)] + ChatRouter.urls,
static_path="static",
)
# 3. Make Tornado app listen on port 8080
app.listen(8080)
# set up the main loop
main_loop = tornado.ioloop.IOLoop.instance()
#period call setup
ping = tornado.ioloop.PeriodicCallback(
lambda: poll(ChatRouter),
1000,
io_loop=main_loop,
)
#start the period call
ping.start()
# 4. Start IOLoop
main_loop.start()