This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
ltConnection.py
executable file
·121 lines (104 loc) · 3.64 KB
/
ltConnection.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
import sys
import os
import json
import socket
import logging
import traceback
from pylantorrent.ltException import LTException
import pylantorrent
import threading
class LTConnection(object):
def __init__(self, json_ent, output_printer):
self.ex = None
self.output_printer = output_printer
if json_ent == None:
self.valid = False
return
try:
self.host = json_ent['host']
self.port = int(json_ent['port'])
self.file = json_ent['file']
self.rid = json_ent['id']
self.block_size = int(json_ent['block_size'])
self.degree = int(json_ent['degree'])
self.data_length = int(json_ent['length'])
except Exception, ex:
vex = LTException(504, str(json_ent) + " :: " + str(ex))
pylantorrent.log(logging.ERROR, str(vex), traceback)
raise vex
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.host, self.port))
self.socket = s
except Exception, ex:
vex = LTException(505, "%s:%d" % (self.host, self.port), self.host, self.port, self.file, self.rid)
pylantorrent.log(logging.ERROR, str(vex), traceback)
raise vex
self.valid = True
#self.read_thread = threading.Thread(target=self.read_output, args=())
#self.read_thread.start()
def get_block_size(self):
if not self.valid:
return 1024*128
return self.block_size
def send_header(self, destinations):
if not self.valid:
return
header = {}
header[files] = self.file
header['host'] = self.host
header['port'] = self.port
header['id'] = self.rid
header['block_size'] = self.block_size
header['degree'] = self.degree
header['length'] = self.data_length
header['destinations'] = destinations
send_str = json.dumps(header)
send_str = send_str + "\n"
pylantorrent.log(logging.DEBUG, "sending header %s" % (send_str))
signature = pylantorrent.get_auth_hash(send_str)
self.send(send_str)
self.send("EOH : %s\r\n" % (signature))
def send(self, data):
if not self.valid:
return
try:
self.socket.send(data)
except Exception, ex:
self.valid = False
self.ex = LTException(506, "%s:%d %s" % (self.host, self.port, str(ex)), self.host, self.port, self.file, self.rid)
pylantorrent.log(logging.WARNING, "send error " + str(self.ex), traceback)
j = vex.get_json()
s = json.dumps(j)
self.output_printer.print_results(s)
def read_output(self):
line = ""
while True:
try:
data = self.socket.recv(1024)
except:
data = ""
line = line + str(data)
la = line.split('\n')
while len(la) > 1:
z = la.pop(0)
pylantorrent.log(logging.DEBUG, "got resutls %s" % (z))
if z.strip() == "EOD":
break
self.output_printer.print_results(z)
line = la.pop(0)
if not data or data == "":
break
def close(self):
try:
self.valid = False
#self.read_thread.join()
self.socket.close()
except:
pass
def close_read(self):
if not self.valid:
return
self.socket.shutdown(socket.SHUT_WR)
def get_exception(self):
return self.ex