-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunicator.py
More file actions
100 lines (87 loc) · 2.51 KB
/
Copy pathcommunicator.py
File metadata and controls
100 lines (87 loc) · 2.51 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
import core
import os
def receiveCommand(client):
try:
command = client.recv(9)[2:].decode('utf-8').split(core.DELIMITER)
command = [int(i) for i in command]
print(f"Received command: Type = {command[0]} | Color = {command[1]} | Flip = {command[2]} | Crop = {command[3]}")
return command
except Exception as e:
print(e)
return None
def receiveImage(client):
try:
# metadata = client.recv(8)[2:].decode('utf-8')
# metadata = [int(i) for i in metadata]
# print(f"Received image info: Length = {metadata[0]}")
image = bytearray()
received = client.recv(4)
length = int.from_bytes(received,"big")
counter = 0
while counter < length:
received = client.recv(core.BUFFER_SIZE)
image.extend(received)
counter += len(received)
print(f"Received image: Length = {len(image)}")
return image
except Exception as e:
print(e)
return None
def sendImage(client):
counter = 0
metadata = str(os.path.getsize(core.GENERATED_IMAGE)) + '\n'
try:
print(f"Sending {metadata.encode()}")
client.send(metadata.encode())
except:
print("Problem sending image metadata")
try:
file = open(core.GENERATED_IMAGE,'rb')
img_data = file.read(core.BUFFER_SIZE)
while img_data:
client.send(img_data)
counter += 1
#print(f'Sent package {counter}')
img_data = file.read(core.BUFFER_SIZE)
print(f"Done sending {counter} packages of the image")
file.close()
except Exception as e:
print(e)
return None
def receiveVideo(client):
try:
video = bytearray()
received = client.recv(4)
length = int.from_bytes(received,"big")
counter = 0
while counter < length:
received = client.recv(core.BUFFER_SIZE)
video.extend(received)
counter += len(received)
print(f"Received video: Length = {counter}")
return video
except Exception as e:
print(e)
return None
def sendVideo(client):
counter = 0
metadata = str(os.path.getsize(core.GENERATED_VIDEO)) + '\n'
print(f"Sending {metadata.encode()}")
try:
client.send(metadata.encode())
except:
print("Problem sending video metadata")
try:
file = open(core.GENERATED_VIDEO,'rb')
vid_data = file.read(core.BUFFER_SIZE)
while vid_data:
client.send(vid_data)
counter += 1
if counter%50 == 0:
print(f'Sent package {counter}')
vid_data = file.read(core.BUFFER_SIZE)
print(f"Done sending {counter} packages of the video")
file.close()
except:
print(f"Problem opening file {core.GENERATED_VIDEO}")
return None