#!/usr/bin/python
# coding: UTF-8
# Rec10 TS Recording Tools
# Copyright (C) 2009 Yukikaze
from SocketServer import *

import configreader
import recdb
RECV_BUF = 8192

class Handler(BaseRequestHandler):
    """
    This is a server to encode ts files in another pc.

    """
    def handle(self):
        print "connect from:", self.client_address
        cons = getTask()
        print cons + " is sent"
        self.request.send(cons)
        """while True:
            #ret = self.request.recv(RECV_BUF)
            #if len(ret) == 0:
            #    break
            self.request.send()

            """
        self.request.close()

def getTask():
    tasks = recdb.getnow("1")
    rett = ""
    for task in tasks:
        if task["type"] == recdb.REC_ENCODE_QUE:
            title = task['title']
            opt = task['opt']
            btime = task['btime']
            etime = task['etime']
            chtxt = task['chtxt']
            recdb.del_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime)
            recdb.rec_reckey(recdb.REC_ENCODE_GRID, title, chtxt, btime, etime, opt)
            rett = "title=" + title + "," + "opt=" + opt + ";"
            break
    return rett
def main():
    port = int(configreader.getpath("gridserver_port"))
    sv = ThreadingTCPServer(('', port), Handler)
    print "This is a Rec10 GridServer."
    print "Waiting for connections at port" + str(port)
    print 'listen to:', sv.socket.getsockname()
    sv.serve_forever()

if __name__ == '__main__': main()
