-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoneprot.py
134 lines (116 loc) · 3.53 KB
/
noneprot.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
from socket import *
from queue import Queue
import pickle
import threading
import time
class Server:
def __init__(self, port, f_name):
self.port = port
self.buf = 1024**2
self.client_group = []
self.send_queue = Queue()
self.data = []
self.f_name = f_name
def Modification(self, recv_data):
if recv_data[0] == "I":
self.data = self.data[:recv_data[1]] + recv_data[2] + self.data[recv_data[1]:]
elif recv_data[0] == "D":
self.data = self.data[:recv_data[1]] + self.data[recv_data[1] + 1:]
def Send(self):
while True:
recv = self.send_queue.get()
for i in range(len(self.client_group)):
if recv[0] != self.client_group[i]:
self.client_group[i].send(recv[1])
else:
sender = i
self.client_group[sender].send(pickle.dumps("Success"))
def Recv(self, conn):
conn.sendall(pickle.dumps(self.data))
while True:
recv_data = conn.recv(self.buf)
load_data = pickle.loads(recv_data)
if load_data == "EXIT":
print("Client byebye")
self.client_group.remove(conn)
if len(self.client_group) == 0:
data_str = ''.join(str(s) for s in self.data)
with open(self.f_name, "w") as f:
f.write(data_str)
f.close
return
else:
self.send_queue.put([conn, recv_data])
self.Modification(load_data)
def main(self):
serverSock = socket(AF_INET, SOCK_STREAM)
serverSock.bind(('', self.port))
serverSock.listen(10)
#data_str = ''.join(str(s) for s in self.data)
#with open(self.f_name, 'w') as f:
# f.write(data_str)
# f.close()
send_thread = threading.Thread(target = self.Send)
send_thread.start()
while True:
connectionSock, addr = serverSock.accept()
if len(self.client_group) == 0:
with open(self.f_name, 'r') as f:
self.data = f.read()
self.client_group.append(connectionSock)
print("Connected to new client ", str(addr))
recv_thread = threading.Thread(target = self.Recv, args = (connectionSock, ))
recv_thread.start()
class Client:
def __init__(self, sock, input_queue):
self.sock = sock
self.buf = 1024**2
self.data = []
self.input_queue = input_queue
self.flag = 0
def Modification(self, recv_data):
if recv_data[0] == 'I':
self.data = self.data[:recv_data[1]] + recv_data[2] + self.data[recv_data[1]:]
elif recv_data[0] == "D":
self.data = self.data[:recv_data[1]] + self.data[recv_data[1] + 1:]
def Send(self, str_len):
start = 0
end = 0
time_i = 0
while True:
while self.flag == 1:
pass
end = time.time()
with open("nonetime_check_" + str(str_len) + ".txt", 'a') as f:
tmp_str = str(time_i) + " : " + str(end - start) + "\n"
f.write(tmp_str)
time_i += 1
modi_data = self.input_queue.get()
start = time.time()
self.sock.send(pickle.dumps(modi_data))
self.flag = 1
self.Modification(modi_data)
with open("test.txt", 'w') as f:
f.write(''.join(self.data))
def Recv(self):
while True:
recv_data = self.sock.recv(self.buf)
load_data = pickle.loads(recv_data)
if load_data == "Success":
self.flag = 0
else:
self.Modification(load_data)
with open("test.txt", 'w') as f:
f.write(''.join(self.data))
def main(self, ip_address, port):
self.sock.connect((ip_address, port))
recv_data = self.sock.recv(self.buf)
load_data = pickle.loads(recv_data)
self.data = load_data
str_len = len(self.data)
with open("test.txt", "w") as f:
f.write(''.join(self.data))
send_thread = threading.Thread(target = self.Send, args = (str_len, ))
send_thread.start()
recv_thread = threading.Thread(target = self.Recv)
recv_thread.start()