forked from klaxa/Distributed-encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
208 lines (187 loc) · 4.63 KB
/
client.py
File metadata and controls
208 lines (187 loc) · 4.63 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
204
205
206
207
208
#!/usr/bin/python2
###############################
# Distributed encoding client #
###############################
import os
import os.path
import socket
import struct
import sys
import threading
import time
if sys.platform.startswith('linux'):
X264 = "ffmpeg"
elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
X264 = "x264.exe"
else:
print "Your platform %s is not supported." % sys.platform
PORT = 13337
UPLOAD = False
NETWORK_CHUNK = 4096
DEBUG = 5
VERSION = "0.2"
print "Distributed encoding client version %s running on %s." % (VERSION, sys.platform)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# misc. functions
def getCRC(string):
CRCs = re.findall("\[[0-F]{8}\]", string)
if len(CRCs) == 0:
return 0
return CRCs[0].strip("][")
def info(string):
if DEBUG > 4:
print "(II)", string
def warn(string):
if DEBUG > 2:
print "(WW)", string
def error(string):
if DEBUG > 0:
print "(EE)", string
def usage():
print "Stub print usage here, lol"
# socket functions
def get(sock, size):
info("Retrieving " + str(size) + " bytes.")
ret = ""
chunk = NETWORK_CHUNK
while size != 0:
if size < chunk:
chunk = size
temp = sock.recv(chunk)
size -= len(temp)
ret += temp
return ret
def get_into(sock, fd, size):
info("Retrieving " + str(size) + " bytes and writing to file.")
chunk = NETWORK_CHUNK
bytes = size
start = time.time()
while size != 0:
if size < chunk:
chunk = size
temp = sock.recv(chunk)
size -= len(temp)
fd.write(temp)
end = time.time()
if end - start > 0:
bitrate = (bytes / 1024) / (end - start)
else:
bitrate = 9001
info("Retrieved %d in %ds. (%d kB/s)" % (bytes, int(end - start), bitrate))
def get_line(sock):
info("Retrieving line from socket.")
line = ""
char = sock.recv(1)
while char != '\n':
line += char
char = sock.recv(1)
return line
def send_file(sock, filename):
size = os.path.getsize(filename)
info("Sending file %s of size %d" % (filename, size))
fd = open(filename, "r")
start = time.time()
sock.send(struct.pack("!i", size))
sock.sendall(fd.read())
end = time.time()
if end - start > 0:
bitrate = (size / 1024) / (end - start)
else:
bitrate = 9001
info("Sent %d in %ds. (%d kB/s)" % (size, int(end - start), bitrate))
os.remove(filename)
class FileSender(threading.Thread):
def __init__(self, sock, filename):
threading.Thread.__init__(self)
self.sock = sock
self.filename = filename
def run(self):
self.sock.send("RDY")
self.sock.send("[8bit] " + self.filename + '\n')
send_file(self.sock, "[8bit] " + self.filename)
self.sock.close()
# client specific functions
def add(sock, filename, encode):
server.send("ADD")
server.send(filename + '\n')
ret = get(sock, 1)
if ret == "D":
info("Duplicate not added.")
exit(0)
elif ret == "Y":
info("Adding new file...")
elif ret == "N":
info("File has not been found.")
if UPLOAD:
sock.send("Y")
info("Sending file...")
send_file(sock, filename)
else:
sock.send("N")
info("Not sending file, terminating")
exit(0)
info("Sending encode settings.")
sock.send(encode + '\n')
if get(sock, 1) == "S":
info("Success!")
else:
error("Unkown error!")
sock.close()
def encode(sock):
address = sock.getpeername()
sock.send("LGN")
ret = get(sock, 1)
if ret == "N":
sock.close()
info("Nothing to encode.")
exit(0)
elif ret == "Y":
filename = get_line(sock)
(size,) = struct.unpack("!i", get(sock, 4))
download = open(filename, "w")
get_into(sock, download, size)
encode = get_line(sock)
sock.close()
#x264_execute = X264 + " %s -o \"[8bit] %s\" \"%s\"" % (encode, filename, filename)
x264_execute = X264 + " -i %s %s \"[8bit] %s\"" % (filename, encode, filename)
info("Executing %s" % x264_execute)
#placebo = "mv %s [8bit]\ %s" % (filename, filename)
os.system(x264_execute)
print address
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(address)
sender = FileSender(sock, filename)
sender.start()
os.remove(filename)
def log_on(sock, host, n):
if n == 0:
while True:
sock.connect((host, PORT))
encode(sock)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
for i in range(n):
sock.connect((host, PORT))
encode(sock)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def main():
print sys.argv
if len(sys.argv) < 3:
usage()
exit(1)
if sys.argv[2] == "ADD":
if len(sys.argv) < 5:
usage()
exit(1)
server.connect((sys.argv[1], PORT))
add(server, sys.argv[3], sys.argv[4])
exit(0)
if sys.argv[2] == "LGN":
if len(sys.argv) < 4:
log_on(server, sys.argv[1], 0)
else:
log_on(server, sys.argv[1], int(sys.argv[3]))
exit(0)
usage()
exit(1)
main()