gmyth-stream/server/lib/server.py
author renatofilho
Tue Jan 29 20:12:30 2008 +0000 (2008-01-29)
branchtrunk
changeset 897 715f854a93cd
parent 718 3fbcd3d9b2d1
permissions -rw-r--r--
[svn r903] implemente audio-rate arg
morphbr@718
     1
#!/usr/bin/env python
morphbr@718
     2
morphbr@718
     3
__author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
morphbr@718
     4
__author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
morphbr@718
     5
__license__ = "GPL"
morphbr@718
     6
__version__ = "0.4"
morphbr@718
     7
morphbr@718
     8
import os
morphbr@718
     9
import threading
morphbr@718
    10
import SocketServer
morphbr@718
    11
import BaseHTTPServer
morphbr@718
    12
import socket
morphbr@718
    13
import urlparse
morphbr@718
    14
import cgi
morphbr@718
    15
import lib.utils as utils
morphbr@718
    16
import logging
morphbr@718
    17
morphbr@718
    18
from log import Log
morphbr@718
    19
from request_handler import RequestHandler
morphbr@718
    20
morphbr@718
    21
__all__ = ("Server", "serve_forever", "load_plugins_transcoders")
morphbr@718
    22
morphbr@718
    23
class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
morphbr@718
    24
    log = logging.getLogger("gms.server")
morphbr@718
    25
    last_tid = 0
morphbr@718
    26
    run = True
morphbr@718
    27
    _transcoders = {}
morphbr@718
    28
    _lock = threading.RLock()
morphbr@718
    29
morphbr@718
    30
    def serve_forever(self):
morphbr@718
    31
        self.log.info("GMyth-Streamer serving HTTP on %s:%s" %
morphbr@718
    32
                      self.socket.getsockname())
morphbr@718
    33
        try:
morphbr@718
    34
            while self.run:
morphbr@718
    35
                self.handle_request()
morphbr@718
    36
        except KeyboardInterrupt, e:
morphbr@718
    37
            pass
morphbr@718
    38
morphbr@718
    39
        self.log.debug("Stopping all remaining transcoders...")
morphbr@718
    40
        self.stop_transcoders()
morphbr@718
    41
        self.log.debug("Transcoders stopped!")
morphbr@718
    42
    # serve_forever()
morphbr@718
    43
morphbr@718
    44
morphbr@718
    45
    def get_request(self):
morphbr@718
    46
        skt = self.socket
morphbr@718
    47
        old = skt.gettimeout()
morphbr@718
    48
        skt.settimeout(0.5)
morphbr@718
    49
        while self.run:
morphbr@718
    50
            try:
morphbr@718
    51
                r = skt.accept()
morphbr@718
    52
                skt.settimeout(old)
morphbr@718
    53
                return r
morphbr@718
    54
            except socket.timeout, e:
morphbr@718
    55
                pass
morphbr@718
    56
        raise socket.error("Not running")
morphbr@718
    57
    # get_request()
morphbr@718
    58
morphbr@718
    59
morphbr@718
    60
    def server_close(self):
morphbr@718
    61
        self.run = False
morphbr@718
    62
        self.stop_transcoders()
morphbr@718
    63
morphbr@718
    64
        BaseHTTPServer.HTTPServer.server_close(self)
morphbr@718
    65
    # server_close()
morphbr@718
    66
morphbr@718
    67
morphbr@718
    68
    def stop_transcoders(self):
morphbr@718
    69
        self._lock.acquire()
morphbr@718
    70
        for transcoder, request in self._transcoders.iteritems():
morphbr@718
    71
            self.log.info("Stop transcoder: %s, client=%s" %
morphbr@718
    72
                          (transcoder, request.client_address))
morphbr@718
    73
            transcoder.stop()
morphbr@718
    74
        self._lock.release()
morphbr@718
    75
    # stop_transcoders()
morphbr@718
    76
morphbr@718
    77
morphbr@718
    78
    def get_transcoders(self):
morphbr@718
    79
        self._lock.acquire()
morphbr@718
    80
        try:
morphbr@718
    81
            return self._transcoders.items()
morphbr@718
    82
        finally:
morphbr@718
    83
            self._lock.release()
morphbr@718
    84
    # get_transcoders()
morphbr@718
    85
morphbr@718
    86
morphbr@718
    87
    def add_transcoders(self, request, transcoder):
morphbr@718
    88
        self._lock.acquire()
morphbr@718
    89
        try:
morphbr@718
    90
            self._transcoders[transcoder] = request
morphbr@718
    91
        finally:
morphbr@718
    92
            self._lock.release()
morphbr@718
    93
    # add_transcoders()
morphbr@718
    94
morphbr@718
    95
morphbr@718
    96
    def del_transcoders(self, request, transcoder):
morphbr@718
    97
        self._lock.acquire()
morphbr@718
    98
        try:
morphbr@718
    99
            del self._transcoders[transcoder]
morphbr@718
   100
        finally:
morphbr@718
   101
            self._lock.release()
morphbr@718
   102
    # del_transcoders()
morphbr@718
   103
# Server
morphbr@718
   104
morphbr@718
   105
morphbr@718
   106
morphbr@718
   107
def serve_forever(host="0.0.0.0", port=40000):
morphbr@718
   108
    addr = (host, port)
morphbr@718
   109
    RequestHandler.protocol_version = "HTTP/1.0"
morphbr@718
   110
    httpd = Server(addr, RequestHandler)
morphbr@718
   111
    httpd.serve_forever()
morphbr@718
   112
# serve_forever()
morphbr@718
   113
morphbr@718
   114
morphbr@718
   115
def load_plugins_transcoders(directory):
morphbr@718
   116
    RequestHandler.load_plugins_transcoders(directory)
morphbr@718
   117
# load_plugins_transcoders()