diff -r 000000000000 -r c397a899490f gmyth-stream/server/0.3/lib/request_handler.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gmyth-stream/server/0.3/lib/request_handler.py Wed May 30 19:53:13 2007 +0100 @@ -0,0 +1,332 @@ +#!/usr/bin/env python + +__author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza" +__author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br" +__license__ = "GPL" +__version__ = "0.4" + +import os +import threading +import SocketServer +import BaseHTTPServer +import socket +import urlparse +import cgi +import lib.utils as utils +import logging + +from log import Log +import lib.transcoder as transcoder + +__all__ = ("RequestHandler") + +class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): + """Class that implements an HTTP request handler for our server.""" + log = logging.getLogger("gms.request") + def_transcoder = None + transcoders = utils.PluginSet(transcoder.Transcoder) + transcoders_log = Log() + tid_queue = [] + + menu = { + "Log": "/get_log.do", + "Stop": "/stop-transcoder.do", + "Status": "/status.do", + "All Log": "/get_all_log.do", + "Version": "/version.do", + "Shutdown": "/shutdown.do" + } + + @classmethod + def load_plugins_transcoders(cls, directory): + cls.transcoders.load_from_directory(directory) + + if cls.def_transcoder is None and cls.transcoders: + cls.def_transcoder = cls.transcoders[0].name + # load_plugins_transcoders() + + + def do_dispatch(self, body): + self.url = self.path + + pieces = urlparse.urlparse(self.path) + self.path = pieces[2] + self.query = cgi.parse_qs(pieces[4]) + + if self.path == "/": + self.serve_main(body) + elif self.path == "/shutdown.do": + self.serve_shutdown(body) + elif self.path == "/stop-transcoder.do": + self.serve_stop_transcoder(body) + elif self.path == "/status.do": + self.serve_status(body) + elif self.path == "/version.do": + self.serve_version(body) + elif self.path == "/new_id.do": + self.serve_new_id(body) + elif self.path == "/get_log.do": + self.serve_get_log(body) + elif self.path == "/get_all_log.do": + self.serve_get_all_log(body) + elif self.path == "/stream.do": + self.serve_stream(body) + else: + action = self.query.get("action", None) + if action and "stream.do" in action: + self.serve_stream(body) + else: + self.send_error(404, "File not found") + # do_dispatch() + + + def do_GET(self): + self.do_dispatch(True) + # do_GET() + + + def do_HEAD(self): + self.do_dispatch(False) + # do_HEAD() + + + def _nav_items(self): + ret = "" + for name, url in self.menu.items(): + ret += utils.getHTML("menu", {"name": name, "url": url}) + + return ret + # _nav_items() + + def serve_main(self, body): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + if body: + self.wfile.write(utils.getHTML("index", {"menu": self._nav_items()})) + # serve_main() + + def serve_version(self, body): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + if body: + self.wfile.write("Version: %s" % __version__) + + + def serve_shutdown(self, body): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + if body: + self.wfile.write(utils.getHTML("shutdown")) + self.server.server_close() + # serve_shutdown() + + + def serve_stop_all_transcoders(self, body): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + if body: + self.server.stop_transcoders() + self.wfile.write(utils.getHTML("stop_all", {"menu": self._nav_items()})) + # serve_stop_all_transcoders() + + + def serve_stop_selected_transcoders(self, body, tids=[]): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + opts = "" + if body: + transcoders = self.server.get_transcoders() + + for tid in tids: + for t, r in transcoders: + if t.tid == int(tid): + try: + t.stop() + except Exception, e: + self.log.info("Plugin already stopped") + + opts += utils._create_html_item("%s" % t) + + break + + self.wfile.write(utils.getHTML("stop_selected", + {"menu": self._nav_items(), + "opts": opts})) + # serve_stop_selected_transcoders() + + + def serve_stop_transcoder(self, body): + req = self.query.get("request", None) + tid = self.query.get("tid", None) + if req and "all" in req: + self.serve_stop_all_transcoders(body) + elif tid: + self.serve_stop_selected_transcoders(body, tid[0].split(";")) + else: + self.serve_status(body) + # serve_stop_transcoder() + + + def serve_status(self, body): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + stopone = "" + + if body: + tl = self.server.get_transcoders() + if not tl: + running = "

No running transcoder.

\n" + stopall = "" + stopone = "" + + elif self.query.get("tid", None): + req_tid = int(self.query.get("tid")[0]) + for transcoder, request in tl: + if transcoder.tid == req_tid: + self.wfile.write("Status: %s %%" % transcoder.status) + return True + + return False + + else: + running = "

Running transcoders:

\n" + stopall = utils._create_html_item("" + "[STOP ALL]" % + self.menu["Stop"]) + + for transcoder, request in tl: + stopone += utils._create_html_item("%s;" + "" + " [STOP] ") % ( + transcoder, self.menu["Stop"], transcoder.tid) + + self.wfile.write(utils.getHTML("status", + {"menu": self._nav_items(), + "running": running, + "stopall": stopall, + "stopone": stopone})) + # serve_status() + + + def _get_transcoder(self): + # get transcoder option: mencoder is the default + request_transcoders = self.query.get("transcoder", ["mencoder"]) + + for t in request_transcoders: + transcoder = self.transcoders.get(t) + if transcoder: + return transcoder + + if not transcoder: + return self.transcoders[self.def_transcoder] + # _get_transcoder() + + + def _get_new_id(self, tid): + self.server.last_tid = utils.create_tid(tid) + self.tid_queue.append(self.server.last_tid) + return self.server.last_tid + # _get_new_id() + + + def serve_new_id(self, body): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + + if body: + self.wfile.write("%s" % self._get_new_id(self.server.last_tid)) + # serve_new_id() + + def serve_get_log(self, body): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + + if body: + if self.query.get("tid", None): + tid = int(self.query.get("tid")[0]) + stat = self.transcoders_log.get_status(tid) + self.wfile.write("%s" % stat) + else: + stat = self.transcoders_log.get_status() + for rtid, status in stat.iteritems(): + self.wfile.write("%s: %s

" % (rtid, status)) + # serve_get_log() + + def serve_get_all_log(self, body): + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.send_header('Connection', 'close') + self.end_headers() + + if body: + if self.query.get("tid", None): + tid = int(self.query.get("tid")[0]) + stat = self.transcoders_log.get_status(tid, True) + for status in stat: + self.wfile.write("%s

" % status) + else: + stat = self.transcoders_log.get_status(None, True) + for rtid, history in stat.iteritems(): + for status in history: + self.wfile.write("%s: %s
" % (rtid, status)) + self.wfile.write("

") + # serve_get_all_log() + + def serve_stream(self, body): + transcoder = self._get_transcoder() + try: + obj = transcoder(self.query) + except Exception, e: + self.send_error(500, str(e)) + return + + self.send_response(200) + self.send_header("Content-Type", obj.get_mimetype()) + self.send_header('Connection', 'close') + self.end_headers() + + if body: + test_tid = int(self.query.get("tid", "0")[0]) + if test_tid == 0 or test_tid not in self.tid_queue: + test_tid = self._get_new_id(self.server.last_tid) + + self.transcoders_log.insert(test_tid, "gms.%s" % obj.name) + obj.tid = test_tid + obj.log = self.transcoders_log + + self.server.add_transcoders(self, obj) + obj.start(self.wfile) + self.server.del_transcoders(self, obj) + # serve_stream() + + + def log_request(self, code='-', size='-'): + self.log.info('"%s" %s %s', self.requestline, str(code), str(size)) + # log_request() + + + def log_error(self, format, *args): + self.log.error("%s: %s" % (self.address_string(), format % args)) + # log_error() + + + def log_message(self, format, *args): + self.log.info("%s: %s" % (self.address_string(), format % args)) + # log_message() +# RequestHandler