-
Notifications
You must be signed in to change notification settings - Fork 20
/
mirror-proxy.py
executable file
·121 lines (101 loc) · 3.92 KB
/
mirror-proxy.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
#!/usr/bin/env python
#
# Cloudlet Infrastructure for Mobile Computing
#
# Author: Kiryong Ha <[email protected]>
#
# Copyright (C) 2011-2013 Carnegie Mellon University
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import time
import Queue
import struct
import os
import sys
if os.path.isdir("../gabriel") is True:
sys.path.insert(0, "..")
from gabriel.proxy.common import AppProxyStreamingClient
from gabriel.proxy.common import AppProxyThread
from gabriel.proxy.common import ResultpublishClient
from gabriel.proxy.common import Protocol_measurement
from gabriel.proxy.common import get_service_list
from gabriel.common.config import ServiceMeta as SERVICE_META
from gabriel.common.config import Const
import pdb
import base64
from PIL import Image, ImageOps
import io, StringIO
# bad idea to transfer image back using json
class DummyVideoApp(AppProxyThread):
def handle(self, header, data):
# PERFORM Cognitive Assistant Processing
sys.stdout.write("processing: ")
sys.stdout.write("%s\n" % header)
image = Image.open(io.BytesIO(data))
mirror_image=ImageOps.mirror(image)
mirror_output = StringIO.StringIO()
mirror_image.save(mirror_output, 'JPEG')
jpeg_image = mirror_output.getvalue()
result = base64.b64encode(jpeg_image)
mirror_output.close()
return result
class DummyAccApp(AppProxyThread):
def chunks(self, l, n):
for i in xrange(0, len(l), n):
yield l[i:i + n]
def handle(self, header, acc_data):
ACC_SEGMENT_SIZE = 16# (int, float, float, float)
for chunk in self.chunks(acc_data, ACC_SEGMENT_SIZE):
(acc_time, acc_x, acc_y, acc_z) = struct.unpack("!ifff", chunk)
print "time: %d, acc_x: %f, acc_y: %f, acc_x: %f" % \
(acc_time, acc_x, acc_y, acc_z)
return None
if __name__ == "__main__":
result_queue = list()
# result_queue = [Queue.Queue(1000)]
sys.stdout.write("Discovery Control VM\n")
service_list = get_service_list(sys.argv)
video_ip = service_list.get(SERVICE_META.VIDEO_TCP_STREAMING_ADDRESS)
video_port = service_list.get(SERVICE_META.VIDEO_TCP_STREAMING_PORT)
return_addresses = service_list.get(SERVICE_META.RESULT_RETURN_SERVER_LIST)
# image receiving thread
video_frame_queue = Queue.Queue(Const.APP_LEVEL_TOKEN_SIZE)
print "TOKEN SIZE OF OFFLOADING ENGINE: %d" % Const.APP_LEVEL_TOKEN_SIZE
video_client = AppProxyStreamingClient((video_ip, video_port), video_frame_queue)
video_client.start()
video_client.isDaemon = True
dummy_video_app = DummyVideoApp(video_frame_queue, result_queue, \
app_id=Protocol_measurement.APP_DUMMY) # dummy app for image processing
dummy_video_app.start()
dummy_video_app.isDaemon = True
# result pub/sub
result_pub = ResultpublishClient(return_addresses, result_queue)
result_pub.start()
result_pub.isDaemon = True
try:
while True:
time.sleep(1)
except Exception as e:
pass
except KeyboardInterrupt as e:
sys.stdout.write("user exits\n")
finally:
if video_client is not None:
video_client.terminate()
if dummy_video_app is not None:
dummy_video_app.terminate()
if acc_client is not None:
acc_client.terminate()
if acc_app is not None:
acc_app.terminate()
result_pub.terminate()