gmyth-stream/server/0.3/lib/server.py
author morphbr
Tue May 29 21:24:48 2007 +0100 (2007-05-29)
branchtrunk
changeset 718 3fbcd3d9b2d1
permissions -rw-r--r--
[svn r724] * GMyth-Streamer version 0.3 released
- Improved Log architecture;
- Creation of a history for the transcoder's actions
- Creation of an id for each transcoder instanciated
- Also wrapps default actions for python's default logger

- Created new functions to make use of this new Log architecture;
- serve_new_id
- serve_get_log
- serve_get_all_log

- _Lot_ of small bug fixes;

- Inserted header for all files;

- Splited files with too many lines (more than 1 class per file)
in more files;
     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()