gmyth-stream/server/lib/server.py
branchtrunk
changeset 918 e565e84cefab
parent 718 3fbcd3d9b2d1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth-stream/server/lib/server.py	Thu Feb 21 17:52:35 2008 +0000
     1.3 @@ -0,0 +1,117 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +__author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
     1.7 +__author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
     1.8 +__license__ = "GPL"
     1.9 +__version__ = "0.4"
    1.10 +
    1.11 +import os
    1.12 +import threading
    1.13 +import SocketServer
    1.14 +import BaseHTTPServer
    1.15 +import socket
    1.16 +import urlparse
    1.17 +import cgi
    1.18 +import lib.utils as utils
    1.19 +import logging
    1.20 +
    1.21 +from log import Log
    1.22 +from request_handler import RequestHandler
    1.23 +
    1.24 +__all__ = ("Server", "serve_forever", "load_plugins_transcoders")
    1.25 +
    1.26 +class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
    1.27 +    log = logging.getLogger("gms.server")
    1.28 +    last_tid = 0
    1.29 +    run = True
    1.30 +    _transcoders = {}
    1.31 +    _lock = threading.RLock()
    1.32 +
    1.33 +    def serve_forever(self):
    1.34 +        self.log.info("GMyth-Streamer serving HTTP on %s:%s" %
    1.35 +                      self.socket.getsockname())
    1.36 +        try:
    1.37 +            while self.run:
    1.38 +                self.handle_request()
    1.39 +        except KeyboardInterrupt, e:
    1.40 +            pass
    1.41 +
    1.42 +        self.log.debug("Stopping all remaining transcoders...")
    1.43 +        self.stop_transcoders()
    1.44 +        self.log.debug("Transcoders stopped!")
    1.45 +    # serve_forever()
    1.46 +
    1.47 +
    1.48 +    def get_request(self):
    1.49 +        skt = self.socket
    1.50 +        old = skt.gettimeout()
    1.51 +        skt.settimeout(0.5)
    1.52 +        while self.run:
    1.53 +            try:
    1.54 +                r = skt.accept()
    1.55 +                skt.settimeout(old)
    1.56 +                return r
    1.57 +            except socket.timeout, e:
    1.58 +                pass
    1.59 +        raise socket.error("Not running")
    1.60 +    # get_request()
    1.61 +
    1.62 +
    1.63 +    def server_close(self):
    1.64 +        self.run = False
    1.65 +        self.stop_transcoders()
    1.66 +
    1.67 +        BaseHTTPServer.HTTPServer.server_close(self)
    1.68 +    # server_close()
    1.69 +
    1.70 +
    1.71 +    def stop_transcoders(self):
    1.72 +        self._lock.acquire()
    1.73 +        for transcoder, request in self._transcoders.iteritems():
    1.74 +            self.log.info("Stop transcoder: %s, client=%s" %
    1.75 +                          (transcoder, request.client_address))
    1.76 +            transcoder.stop()
    1.77 +        self._lock.release()
    1.78 +    # stop_transcoders()
    1.79 +
    1.80 +
    1.81 +    def get_transcoders(self):
    1.82 +        self._lock.acquire()
    1.83 +        try:
    1.84 +            return self._transcoders.items()
    1.85 +        finally:
    1.86 +            self._lock.release()
    1.87 +    # get_transcoders()
    1.88 +
    1.89 +
    1.90 +    def add_transcoders(self, request, transcoder):
    1.91 +        self._lock.acquire()
    1.92 +        try:
    1.93 +            self._transcoders[transcoder] = request
    1.94 +        finally:
    1.95 +            self._lock.release()
    1.96 +    # add_transcoders()
    1.97 +
    1.98 +
    1.99 +    def del_transcoders(self, request, transcoder):
   1.100 +        self._lock.acquire()
   1.101 +        try:
   1.102 +            del self._transcoders[transcoder]
   1.103 +        finally:
   1.104 +            self._lock.release()
   1.105 +    # del_transcoders()
   1.106 +# Server
   1.107 +
   1.108 +
   1.109 +
   1.110 +def serve_forever(host="0.0.0.0", port=40000):
   1.111 +    addr = (host, port)
   1.112 +    RequestHandler.protocol_version = "HTTP/1.0"
   1.113 +    httpd = Server(addr, RequestHandler)
   1.114 +    httpd.serve_forever()
   1.115 +# serve_forever()
   1.116 +
   1.117 +
   1.118 +def load_plugins_transcoders(directory):
   1.119 +    RequestHandler.load_plugins_transcoders(directory)
   1.120 +# load_plugins_transcoders()