Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
204 lines (168 loc) · 5.32 KB
/
Copy pathserver.py
File metadata and controls
204 lines (168 loc) · 5.32 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
import SocketServer
import shelve
import math
#Get graph: client sends every graph as a number of blocks of 1024 bytes, have to update object and then shelve it
#first of all I should save the clients address and then update only that object
#client should send size to begin with to determine if it's the largest yet, and if it is update the object
#if it is not it should rather send the biggest graph available so that the client may update itself
#
class ComputeNode:
def __init__(self, address):
self.address = address
self.graph = ""
self.receiving = True
self.size = 0
self.nmbr_blocks = 0
self.block_size = 1024.0
self.taboo_list = ""
def resetGraph(self):
self.graph = ""
def setGraph(self, graph):
self.graph += graph
def setSize(self, size):
self.size = size
def setNmbrBlocks(self):
if(self.size == 0):
print "SIZE HAS NOT BEEN SET"
self.nmbr_blocks = math.ceil(self.size**2/self.block_size)
def decreaseNmbrBlocks(self):
self.nmbr_blocks -= 1
def getNmbrBlocks(self):
return self.nmbr_blocks
def getSize(self):
return self.size
def setReceive(self, rec):
self.receiving = rec
def getReceive(self):
return self.receiving
def updateTaboo(self, taboo):
self.taboo_list += taboo
global d
global bestGraph #represents the best ComputeNode object with key 'best'
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
d = shelve.open("ComputeNodes.db")
self.data = self.request.recv(1024).strip()
key = self.client_address[0]
def GraphRequest(request):
if d.has_key('best'):
best = d['best']
request.sendall(str(best.getSize()))
request.sendall(best.graph)
else:
request.sendall('0')
def ReceiveSize(cNode, size):
print "Receiving Size"
cNode.setSize(size)
cNode.setNmbrBlocks()
cNode.resetGraph()
cNode.setReceive(True)
d[key] = cNode
if not d.has_key('best'):
d['best'] = cNode
def ReceiveBlock(cNode):
print "Receiving Block"
if cNode.getNmbrBlocks() == 1:
cNode.setReceive(False)
cNode.setGraph(self.data.strip('\0'))
cNode.decreaseNmbrBlocks()
d[key] = cNode
if cNode.getSize() > d['best'].getSize():
if hasattr(d['best'], 'taboo_list') and hasattr(cNode, 'taboo_list'):
taboo_list = d['best'].taboo_list
cNode.taboo_list = ""
d['best'] = cNode
print "New best with size: ", cNode.getSize()
print cNode.graph
print
print
else:
cNode.setGraph(self.data.strip('\0'))
cNode.decreaseNmbrBlocks()
d[key] = cNode
def TabooRequest(request):
print "TabooRequest"
if d.has_key('best'):
request.sendall(str(len(d['best'].taboo_list)))
print str(len(d['best'].taboo_list))
request.sendall(d['best'].taboo_list)
else:
request.sendall("-1")
def ReceiveTaboo(request):
print "ReceiveTaboo"
taboo = self.data.strip('\0')[3:]
print "taboo received: ", taboo
if d.has_key('best'):
best = d['best']
if len(taboo) < best.getSize():
print "SENDING -1"
self.request.sendall("-1")
else:
self.request.sendall("0")
columns = [best.taboo_list[i:i+best.getSize()] for i in range(0, len(best.taboo_list), best.getSize())]
if taboo not in columns:
print "Updated best with taboo"
best.updateTaboo(taboo)
d['best'] = best
else:
if d.has_key(key):
d[key].updateTaboo(taboo)
cNode = d[key]
d['best'] = cNode
print "New best with taboo list"
self.request(sendall("0"))
else:
cNode = ComputeNode(key)
cNode.updateTaboo(taboo)
print "New best with taboo list"
d[key] = cNode
d['best'] = cNode
self.request(sendall("0"))
print "{} wrote:".format(self.client_address[0])
print len(self.data)
if(int(self.data.strip('\0')[:3]) == 299):
ReceiveTaboo(self.request)
if(len(self.data) == 4):
if(int(self.data.strip('\0')) == 309):
GraphRequest(self.request)
elif(int(self.data.strip('\0')[:3]) == 303):
TabooRequest(self.request)
else:
if d.has_key(key):
cNode = d[key]
size = int(self.data.strip('\0'))
ReceiveSize(cNode, size)
else:
cNode = ComputeNode(key)
size = int(self.data.strip('\0'))
ReceiveSize(cNode, size)
else:
cNode = d[key]
if cNode.getReceive():
ReceiveBlock(cNode)
# if d.has_key(key):
# cNode = d[key]
# if cNode.getReceive():
# ReceiveBlock(cNode)
#
# else:
# size = int(self.data.strip('\0'))
# if size < 300:
# ReceiveSize(cNode, size)
# else:
# cNode = ComputeNode(key)
#
# size = int(self.data.strip('\0'))
# if(size < 300):
# ReceiveSize(cNode, size)
d.close()
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
d = shelve.open("ComputeNodes.db")
if d.has_key('best'):
print "Best from db: ", d['best'].getSize()
print "With graph: ", d['best'].graph
print "with taboolist", d['best'].taboo_list
d.close()
server.serve_forever()