-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit
executable file
·348 lines (287 loc) · 9.51 KB
/
init
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
#!/usr/bin/python3
import sys, glob
sys.path.insert(0, glob.glob('/home/cs557-inst/thrift-0.13.0/lib/py/build/lib*')[0])
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.Thrift import TType, TMessageType, TException, TApplicationException
from thrift.Thrift import TProcessor
from thrift.protocol import TProtocol
try:
from thrift.protocol import fastbinary
except:
fastbinary = None
import re
import hashlib
class NodeID:
"""
Attributes:
- id
- ip
- port
"""
thrift_spec = (
None, # 0
(1, TType.STRING, 'id', None, None, ), # 1
(2, TType.STRING, 'ip', None, None, ), # 2
(3, TType.I32, 'port', None, None, ), # 3
)
def __init__(self, id=None, ip=None, port=None,):
self.id = id
self.ip = ip
self.port = port
def read(self, iprot):
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
if ftype == TType.STOP:
break
if fid == 1:
if ftype == TType.STRING:
self.id = iprot.readString();
else:
iprot.skip(ftype)
elif fid == 2:
if ftype == TType.STRING:
self.ip = iprot.readString();
else:
iprot.skip(ftype)
elif fid == 3:
if ftype == TType.I32:
self.port = iprot.readI32();
else:
iprot.skip(ftype)
else:
iprot.skip(ftype)
iprot.readFieldEnd()
iprot.readStructEnd()
def write(self, oprot):
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
return
oprot.writeStructBegin('NodeID')
if self.id is not None:
oprot.writeFieldBegin('id', TType.STRING, 1)
oprot.writeString(self.id)
oprot.writeFieldEnd()
if self.ip is not None:
oprot.writeFieldBegin('ip', TType.STRING, 2)
oprot.writeString(self.ip)
oprot.writeFieldEnd()
if self.port is not None:
oprot.writeFieldBegin('port', TType.I32, 3)
oprot.writeI32(self.port)
oprot.writeFieldEnd()
oprot.writeFieldStop()
oprot.writeStructEnd()
def validate(self):
return
def __repr__(self):
L = ['%s=%r' % (key, value)
for key, value in self.__dict__.iteritems()]
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
def __eq__(self, other):
return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not (self == other)
class Iface:
def setFingertable(self, node_list):
"""
Parameters:
- node_list
"""
pass
class Client(Iface):
def __init__(self, iprot, oprot=None):
self._iprot = self._oprot = iprot
if oprot is not None:
self._oprot = oprot
self._seqid = 0
def setFingertable(self, node_list):
"""
Parameters:
- node_list
"""
self.send_setFingertable(node_list)
self.recv_setFingertable()
def send_setFingertable(self, node_list):
self._oprot.writeMessageBegin('setFingertable', TMessageType.CALL, self._seqid)
args = setFingertable_args()
args.node_list = node_list
args.write(self._oprot)
self._oprot.writeMessageEnd()
self._oprot.trans.flush()
def recv_setFingertable(self):
(fname, mtype, rseqid) = self._iprot.readMessageBegin()
if mtype == TMessageType.EXCEPTION:
x = TApplicationException()
x.read(self._iprot)
self._iprot.readMessageEnd()
raise x
result = setFingertable_result()
result.read(self._iprot)
self._iprot.readMessageEnd()
return
class setFingertable_args:
"""
Attributes:
- node_list
"""
thrift_spec = (
None, # 0
(1, TType.LIST, 'node_list', (TType.STRUCT,(NodeID, NodeID.thrift_spec)), None, ), # 1
)
def __init__(self, node_list=None,):
self.node_list = node_list
def read(self, iprot):
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
if ftype == TType.STOP:
break
if fid == 1:
if ftype == TType.LIST:
self.node_list = []
(_etype3, _size0) = iprot.readListBegin()
for _i4 in range(_size0):
_elem5 = NodeID()
_elem5.read(iprot)
self.node_list.append(_elem5)
iprot.readListEnd()
else:
iprot.skip(ftype)
else:
iprot.skip(ftype)
iprot.readFieldEnd()
iprot.readStructEnd()
def write(self, oprot):
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
return
oprot.writeStructBegin('setFingertable_args')
if self.node_list is not None:
oprot.writeFieldBegin('node_list', TType.LIST, 1)
oprot.writeListBegin(TType.STRUCT, len(self.node_list))
for iter6 in self.node_list:
iter6.write(oprot)
oprot.writeListEnd()
oprot.writeFieldEnd()
oprot.writeFieldStop()
oprot.writeStructEnd()
def validate(self):
return
def __repr__(self):
L = ['%s=%r' % (key, value)
for key, value in self.__dict__.iteritems()]
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
def __eq__(self, other):
return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not (self == other)
class setFingertable_result:
thrift_spec = (
)
def read(self, iprot):
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
if ftype == TType.STOP:
break
else:
iprot.skip(ftype)
iprot.readFieldEnd()
iprot.readStructEnd()
def write(self, oprot):
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
return
oprot.writeStructBegin('setFingertable_result')
oprot.writeFieldStop()
oprot.writeStructEnd()
def validate(self):
return
def __repr__(self):
L = ['%s=%r' % (key, value)
for key, value in self.__dict__.iteritems()]
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
def __eq__(self, other):
return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not (self == other)
hs=[chr(i+ord('0')) for i in range(10)]
hs.extend([chr(i+ord('a')) for i in range(6)])
hsd=dict([(hs[i],i) for i in range(len(hs))])
def hs_add(h1,h2):
if len(h1)>len(h2):
swp=h1;h1=h2;h2=swp
df=len(h2)-len(h1)
res=[]
c=0
for i in range(len(h1)-1,-1,-1):
r=hsd[h1[i]]+hsd[h2[i+df]]+c
res.append(hs[r%16])
c=r//16
for i in range(df-1,-1,-1):
r=hsd[h2[i]]+c
res.append(hs[r%16])
c=r//16
if c>0:
res.append(hs[c])
res.reverse()
return ''.join(res)
def create_node(s):
sha256=hashlib.sha256()
sha256.update(s.encode('utf-8'))
k=sha256.hexdigest()
m=re.match('([^:]+):([^:]+)',s)
return NodeID(k,m.group(1),int(m.group(2)))
def bs(k,lst,b,e):
if b+1==e:
return e
m=(b+e)//2
if k<=lst[m].id: return bs(k,lst,b,m)
else: return bs(k,lst,m,e)
def create_fing(id,lst):
fing=[]
dd=['1','2','4','8']
add=['0']*64
for i in range(64):
for j in range(4):
add[63-i]=dd[j]
fid=hs_add(id,add)[-64:]
idx=bs(fid,lst,0,len(lst))
if idx>=len(lst):
fing.append(lst[0])
else:
fing.append(lst[idx])
add[63-i]='0'
return fing
def init_server(nid,lst,cdct=None):
fing=create_fing(nid.id,lst)
if cdct==None:
transport = TSocket.TSocket(nid.ip, nid.port)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Client(protocol)
transport.open()
try: client.setFingertable(fing)
finally: transport.close()
else:
#buffered clients
client=cdct['%s:%d'%(nid.ip, nid.port)]
client.setFingertable(fing)
if __name__=='__main__':
fn=sys.argv[1]
lst=sorted([create_node(l[:-1]) for l in open(fn)],key=lambda x:x.id)
for l in lst:
init_server(l,lst)