This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
server.py
executable file
·223 lines (194 loc) · 7.09 KB
/
server.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
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
import sys
import os
from socket import *
import logging
import pylantorrent
from pylantorrent.ltException import LTException
from pylantorrent.ltConnection import *
import simplejson as json
import traceback
import hashlib
# The first thing sent is a json header terminated by a single line
# of EOH
#
# {
# host
# port
# length
# requests = [ {filename, id, rename}, ]
# destinations =
# [ {
# host
# port
# requests = [ { filename, id, rename } ]
# block_size
# }, ]
# }
#
class LTServer(object):
def __init__(self, inf, outf):
self.json_header = {}
self.source_conn = LTSourceConnection(inf)
self.outf = outf
self.block_size = 128*1024
self.suffix = ".lantorrent"
self.created_files = []
self.v_con_array = []
self.files_a = []
self.md5str = None
def _close_files(self):
for f in self.files_a:
f.close()
self.files_a = []
def _close_connections(self):
for v_con in self.v_con_array:
v_con.close()
self.v_con_array = []
def clean_up(self, force=False):
self._close_connections()
self._close_files()
pylantorrent.log(logging.DEBUG, "cleaning up")
for f in self.created_files:
try:
pylantorrent.log(logging.DEBUG, "deleting file %s" % (f))
# dont delete /dev/null (or any other dev really)
ndx = f.strip().find("/dev")
if ndx != 0:
os.remove(f)
except:
pass
self.created_files = []
def _read_footer(self):
self.footer = self.source_conn.read_footer(self.md5str)
def _send_footer(self):
foot = {}
foot['md5sum'] = self.md5str
foot_str = json.dumps(foot)
pylantorrent.log(logging.DEBUG, "sending footer %s" % (foot_str))
for v_con in self.v_con_array:
v_con.send(foot_str)
def _read_header(self):
self.json_header = self.source_conn.read_header()
self.degree = int(self.json_header['degree'])
self.data_length = long(self.json_header['length'])
def print_results(self, s):
pylantorrent.log(logging.DEBUG, "printing\n--------- \n%s\n---------------" % (s))
self.outf.write(s)
self.outf.flush()
def _get_valid_vcons(self, destinations):
v_con_array = []
while len(destinations) > 0 and len(v_con_array) < self.degree:
ep = destinations.pop(0)
try:
v_con = LTDestConnection(ep, self)
v_con_array.append(v_con)
except LTException, vex:
# i think this is the only recoverable error
# keep track of them and return in output
s = vex.get_printable()
self.print_results(s)
each = len(destinations) / self.degree
rem = len(destinations) % self.degree
ndx = 0
for v_con in v_con_array:
end = ndx + each + rem
mine = destinations[ndx:end]
rem = 0
v_con.send_header(mine)
self.v_con_array = v_con_array
def _open_dest_files(self, requests_a):
files_a = []
for req in requests_a:
filename = req['filename']
try:
rn = req['rename']
if rn:
filename = filename + self.suffix
f = open(filename, "w")
files_a.append(f)
self.created_files.append(filename)
except Exception, ex:
pylantorrent.log(logging.ERROR, "Failed to open %s" % (filename), traceback)
raise LTException(503, str(ex), self.json_header['host'], int(self.json_header['port']), reqs=requests_a)
self.files_a = files_a
# perhaps this should even be an io event system or threads. For now
# it will throttle on the one blocking socket from the data source
# and push the rest to the vcon objects
def _process_io(self):
md5er = hashlib.md5()
read_count = 0
bs = self.block_size
while read_count < self.data_length:
if bs + read_count > self.data_length:
bs = self.data_length - read_count
data = self.source_conn.read_data(bs)
if data == None:
raise Exception("Data is None prior to receiving full file %d %d" % (read_count, self.data_length))
md5er.update(data)
for v_con in self.v_con_array:
v_con.send(data)
for f in self.files_a:
f.write(data)
read_count = read_count + len(data)
self.md5str = str(md5er.hexdigest()).strip()
pylantorrent.log(logging.DEBUG, "We have received sent %d bytes. The md5sum is %s" % (read_count, self.md5str))
def store_and_forward(self):
self._read_header()
header = self.json_header
requests_a = header['requests']
self._open_dest_files(requests_a)
destinations = header['destinations']
self._get_valid_vcons(destinations)
self._process_io()
# close all open files
self._close_files()
# read the footer from the sending machine
self._read_footer()
# send foot to all machines this is streaming to
self._send_footer()
# wait for eof and close
self._close_connections()
self._rename_files(requests_a)
pylantorrent.log(logging.DEBUG, "All data sent %s %d" % (self.md5str, len(requests_a)))
# if we got to here it was successfully written to a file
# and we can call it success. Print out a success message for
# everyfile written
vex = LTException(0, "Success", header['host'], int(header['port']), requests_a, md5sum=self.md5str)
s = vex.get_printable()
self.print_results(s)
self.clean_up()
def _rename_files(self, requests_a):
for req in requests_a:
realname = req['filename']
rn = req['rename']
if rn:
tmpname = realname + self.suffix
pylantorrent.log(logging.DEBUG, "renaming %s -> %s" % (tmpname, realname))
os.rename(tmpname, realname)
self.created_files.remove(tmpname)
def main(argv=sys.argv[1:]):
pylantorrent.log(logging.INFO, "server starting")
rc = 1
v = LTServer(sys.stdin, sys.stdout)
try:
v.store_and_forward()
rc = 0
except LTException, ve:
pylantorrent.log(logging.ERROR, "error %s" % (str(ve)), traceback)
s = ve.get_printable()
v.print_results(s)
v.clean_up()
except Exception, ex:
pylantorrent.log(logging.ERROR, "error %s" % (str(ex)), traceback)
vex = LTException(500, str(ex))
s = vex.get_printable()
v.print_results(s)
v.clean_up()
return rc
if __name__ == "__main__":
if 'LANTORRENT_HOME' not in os.environ:
msg = "The env LANTORRENT_HOME must be set"
print msg
raise Exception(msg)
rc = main()
sys.exit(rc)