#!/usr/bin/env python
"""
filename: debugServer.py

Description:
    This module just launches a dummy image processing server and sends back
    the contents of "xmlout.xml" (hopefully generated by previously processing some
    image data).
"""


import pdb
import time
import threading
import Queue
import StringIO
import Image
import sys

from xml.etree import ElementTree as ET

from SketchFramework.SketchGUI import _SketchGUI
from SketchFramework.Point import Point
from SketchFramework.Stroke import Stroke
from SketchFramework.NetworkReceiver import ServerThread, Message

from Observers import CircleObserver
from Observers import ArrowObserver
from Observers import DebugObserver
from Observers import TextObserver
from Observers import DiGraphObserver
from Observers import TuringMachineObserver


from sketchvision.ImageStrokeConverter import imageBufferToStrokes, GETNORMWIDTH
from Utils.StrokeStorage import StrokeStorage
from Utils import Logger

from Observers.ObserverBase import Animator

# Constants
WIDTH = 1024
HEIGHT = 3 * WIDTH / 4

MID_W = WIDTH/2
MID_H = HEIGHT/2

   
logger = Logger.getLogger("DebugSketchGUI", Logger.DEBUG)



class FileResponseThread(threading.Thread):
    def __init__(self, inQ, outQ, filename = "xmlout.xml"):
        threading.Thread.__init__(self)
        self.daemon = True
        self.fname = filename
        self.inQ = inQ
        self.outQ = outQ


    def run(self):
       while True:
            image = self.inQ.get()
            logger.debug("Received data")
            fp = open(self.fname, "r")
            try:
                output = fp.read()
                msg = Message(Message.TYPE_XML, output)
                #time.sleep(3)
                self.outQ.put(msg)
            except Exception as e:
                print e
            finally:
                fp.close()


 

class DummyGUI(_SketchGUI):

    Singleton = None
    def __init__(self, filename = "xmlout.xml"):

       # Private data members
       self._serverThread = None
       self._dummyProcThread = None
       self._fname = filename
       self._setupImageServer()


       self.run()
        


    def _setupImageServer(self):
        "Set up the server thread to start listening for image data, which it puts into its response queue. Then the imgprocthread converts image data to strokes, which are enqueued in self._strokeQueue"
        self._serverThread = ServerThread(port = 30000)
        img_recv_queue = self._serverThread.getRequestQueue()
        self._xmlResponseQueue = self._serverThread.getResponseQueue()

        self._fileResponseThread = FileResponseThread(img_recv_queue, self._xmlResponseQueue, filename = self._fname)
        self._fileResponseThread.start()

        self._serverThread.start()


    def run(self):
        while True:
            time.sleep(100)

            

if __name__ == "__main__":
    if len(sys.argv) > 1:
        xmlFilename = sys.argv[1]
        logger.debug("Using %s" % (xmlFilename))
        DummyGUI(filename = xmlFilename)
    else:
        DummyGUI()

