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