<commit_before>#!/usr/bin/env python 
#
# Cloudlet Infrastructure for Mobile Computing
#
#   Author: <NAME> <<EMAIL>>
#
#   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 sys
import json
import tempfile
import os

import SocketServer
import threading
import select
import traceback
import Queue
import struct
import socket
import multiprocessing

from config import Const as Const
from config import DEBUG
import log as logging
from protocol import Protocol_client
from protocol import Protocol_measurement


LOG = logging.getLogger(__name__)
image_queue_list = list()
acc_queue_list = list()
gps_queue_list = list()
result_queue = multiprocessing.Queue()


class MobileCommError(Exception):
    pass

class MobileSensorHandler(SocketServer.StreamRequestHandler, object):
    def setup(self):
        super(MobileSensorHandler, self).setup()
        self.average_FPS = 0.0
        self.current_FPS = 0.0
        self.init_connect_time = None
        self.previous_time = None
        self.current_time = 0
        self.frame_count = 0
        self.totoal_recv_size = 0
        self.ret_frame_ids = Queue.Queue()
        self.stop_queue = multiprocessing.Queue()

    def _recv_all(self, recv_size):
        data = ''
        while len(data) < recv_size:
            tmp_data = self.request.recv(recv_size - len(data))
            if tmp_data == None:
                raise MobileCommError("Cannot recv data at %s" % str(self))
            if len(tmp_data) == 0:
                raise MobileCommError("Recv 0 data at %s" % str(self))
            data += tmp_data
        return data

    def handle(self):
        global image_queue_list
        try:
            LOG.info("Google Glass is connected for (%s)" % str(self))
            self.init_connect_time = time.time()
            self.previous_time = time.time()

            socket_fd = self.request.fileno()
            stopfd = self.stop_queue._reader.fileno()
            input_list = [socket_fd, stopfd]
            if hasattr(self, 'output_queue') == True:
                result_fd = self.output_queue._reader.fileno()
                input_list += [result_fd]
            else:
                result_fd = -1
            except_list = [socket_fd, stopfd]

            is_running = True
            while is_running:
                inputready, outputready, exceptready = \
                        select.select(input_list, [], except_list)
                for s in inputready:
                    if s == socket_fd:
                        self._handle_input_data()
                    if s == stopfd:
                        is_running = False
                    # For output, check queue first. If we check output socket, 
                    # select will return immediately
                    if s == result_fd:
                        self._handle_output_result()
                for e in exceptready:
                    is_running = False
        except Exception as e:
            LOG.info(traceback.format_exc())
            LOG.debug("%s\n" % str(e))

        if self.connection is not None:
            self.connection.close()
            self.connection = None
        LOG.info("%s\tterminate thread" % str(self))

    def _handle_input_data(self):
        pass

    def _handle_output_result(self):
        pass

    def terminate(self):
        self.stop_queue.put("terminate\n")


class MobileVideoHandler(MobileSensorHandler):
    def setup(self):
        super(MobileVideoHandler, self).setup() 
        
    def __repr__(self):
        return "Mobile Video Server"

    def _handle_input_data(self):
        header_size = struct.unpack("!I", self._recv_all(4))[0]
        img_size = struct.unpack("!I", self._recv_all(4))[0]
        header_data = self._recv_all(header_size)
        image_data = self._recv_all(img_size)
        self.frame_count += 1

        # add header data for measurement
        if DEBUG.PACKET:
            header_json = json.loads(header_data)
            header_json[Protocol_measurement.JSON_KEY_CONTROL_SENT_TIME] = time.time()
            header_data = json.dumps(header_json)

        # measurement
        self.current_time = time.time()
        self.totoal_recv_size += (header_size + img_size + 8)
        self.current_FPS = 1 / (self.current_time - self.previous_time)
        self.average_FPS = self.frame_count / (self.current_time -
                self.init_connect_time)
        self.previous_time = self.current_time

        if (self.frame_count % 100 == 0):
            msg = "Video FPS : current(%f), avg(%f), BW(%f Mbps), offloading engine(%d)" % \
                    (self.current_FPS, self.average_FPS, \
                    8*self.totoal_recv_size/(self.current_time-self.init_connect_time)/1000/1000,
                    len(image_queue_list))
            LOG.info(msg)
        for image_queue in image_queue_list:
            if image_queue.full() is True:
                try:
                    image_queue.get_nowait()
                except Queue.Empty as e:
                    pass
            image_queue.put((header_data, image_data))

        if DEBUG.DIRECT_RETURN:
            # return frame id directly to flow control
            json_header = json.loads(header_data)
            frame_id = json_header.get(Protocol_client.FRAME_MESSAGE_KEY, None)
            if frame_id is not None:
                self.ret_frame_ids.put(frame_id)
            global result_queue
            result_queue.put(header_data)


class MobileAccHandler(MobileSensorHandler):
    def setup(self):
        super(MobileAccHandler, self).setup()
        
    def __str__(self):
        return "Mobile Acc Server"<commit_msg>direct case return ack without queue<commit_after>#!/usr/bin/env python 
#
# Cloudlet Infrastructure for Mobile Computing
#
#   Author: <NAME> <<EMAIL>>
#
#   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 sys
import json
import tempfile
import os

import SocketServer
import threading
import select
import traceback
import Queue
import struct
import socket
import multiprocessing

from config import Const as Const
from config import DEBUG
import log as logging
from protocol import Protocol_client
from protocol import Protocol_measurement


LOG = logging.getLogger(__name__)
image_queue_list = list()
acc_queue_list = list()
gps_queue_list = list()
result_queue = multiprocessing.Queue()


class MobileCommError(Exception):
    pass

class MobileSensorHandler(SocketServer.StreamRequestHandler, object):
    def setup(self):
        super(MobileSensorHandler, self).setup()
        self.average_FPS = 0.0
        self.current_FPS = 0.0
        self.init_connect_time = None
        self.previous_time = None
        self.current_time = 0
        self.frame_count = 0
        self.totoal_recv_size = 0
        self.ret_frame_ids = Queue.Queue()
        self.stop_queue = multiprocessing.Queue()

    def _recv_all(self, recv_size):
        data = ''
        while len(data) < recv_size:
            tmp_data = self.request.recv(recv_size - len(data))
            if tmp_data == None:
                raise MobileCommError("Cannot recv data at %s" % str(self))
            if len(tmp_data) == 0:
                raise MobileCommError("Recv 0 data at %s" % str(self))
            data += tmp_data
        return data

    def handle(self):
        global image_queue_list
        try:
            LOG.info("Google Glass is connected for (%s)" % str(self))
            self.init_connect_time = time.time()
            self.previous_time = time.time()

            socket_fd = self.request.fileno()
            stopfd = self.stop_queue._reader.fileno()
            input_list = [socket_fd, stopfd]
            if hasattr(self, 'output_queue') == True:
                result_fd = self.output_queue._reader.fileno()
                input_list += [result_fd]
            else:
                result_fd = -1
            except_list = [socket_fd, stopfd]

            is_running = True
            while is_running:
                inputready, outputready, exceptready = \
                        select.select(input_list, [], except_list)
                for s in inputready:
                    if s == socket_fd:
                        self._handle_input_data()
                    if s == stopfd:
                        is_running = False
                    # For output, check queue first. If we check output socket, 
                    # select will return immediately
                    if s == result_fd:
                        self._handle_output_result()
                for e in exceptready:
                    is_running = False
        except Exception as e:
            LOG.info(traceback.format_exc())
            LOG.debug("%s\n" % str(e))

        if self.connection is not None:
            self.connection.close()
            self.connection = None
        LOG.info("%s\tterminate thread" % str(self))

    def _handle_input_data(self):
        pass

    def _handle_output_result(self):
        pass

    def terminate(self):
        self.stop_queue.put("terminate\n")


class MobileVideoHandler(MobileSensorHandler):
    def setup(self):
        super(MobileVideoHandler, self).setup() 
        
    def __repr__(self):
        return "Mobile Video Server"

    def _handle_input_data(self):
        header_size = struct.unpack("!I", self._recv_all(4))[0]
        img_size = struct.unpack("!I", self._recv_all(4))[0]
        header_data = self._recv_all(header_size)
        image_data = self._recv_all(img_size)
        self.frame_count += 1

        # add header data for measurement
        if DEBUG.PACKET:
            header_json = json.loads(header_data)
            header_json[Protocol_measurement.JSON_KEY_CONTROL_SENT_TIME] = time.time()
            header_data = json.dumps(header_json)

        # measurement
        self.current_time = time.time()
        self.totoal_recv_size += (header_size + img_size + 8)
        self.current_FPS = 1 / (self.current_time - self.previous_time)
        self.average_FPS = self.frame_count / (self.current_time -
                self.init_connect_time)
        self.previous_time = self.current_time

        if (self.frame_count % 100 == 0):
            msg = "Video FPS : current(%f), avg(%f), BW(%f Mbps), offloading engine(%d)" % \
                    (self.current_FPS, self.average_FPS, \
                    8*self.totoal_recv_size/(self.current_time-self.init_connect_time)/1000/1000,
                    len(image_queue_list))
            LOG.info(msg)
        for image_queue in image_queue_list:
            if image_queue.full() is True:
                try:
                    image_queue.get_nowait()
                except Queue.Empty as e:
                    pass
            image_queue.put((header_data, image_data))

        if DEBUG.DIRECT_RETURN:
            packet = struct.pack("!I%ds" % len(header_data),
                    len(header_data), header_data)
            self.request.send(packet)
            self.wfile.flush()
            
            #frame_id = json_header.get(Protocol_client.FRAME_MESSAGE_KEY, None)
            #if frame_id is not None:
            #    self.ret_frame_ids.put(frame_id)
            #global result_queue
            #result_queue.put(header_data)


class MobileAccHandler(MobileSensorHandler):
    def setup(self):
        super(MobileAccHandler, self).setup()
        
    def __str__(self):
        return "Mobile Acc Server"
{'fraction_non_alphanumeric': 0.08640012717590016, 'fraction_numerical': 0.006676734758763215, 'mean_word_length': 2.0906411201179074, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3489565', 'n_tokens_mistral': 3650, 'n_tokens_neox': 3494, 'n_words': 902}