Package flumotion :: Package common :: Module server
[hide private]

Source Code for Module flumotion.common.server

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """ 
 23  Server functionality. 
 24  """ 
 25   
 26  import os 
 27   
 28  from twisted.internet import reactor 
 29   
 30  from flumotion.common import log 
 31  from flumotion.twisted import compat 
 32   
33 -class _ServerContextFactory(log.Loggable):
34 35 logCategory = "SSLServer" 36
37 - def __init__(self, pemFile):
38 self._pemFile = pemFile
39
40 - def getContext(self):
41 """ 42 Create an SSL context. 43 """ 44 from OpenSSL import SSL 45 ctx = SSL.Context(SSL.SSLv23_METHOD) 46 try: 47 ctx.use_certificate_file(self._pemFile) 48 ctx.use_privatekey_file(self._pemFile) 49 except SSL.Error, e: 50 self.warning('SSL error: %r' % e.args) 51 self.error('Could not open certificate %s' % self._pemFile) 52 return ctx
53
54 -class IServable(compat.Interface):
55 """ 56 I am an interface for objects that want to be servable through a 57 L{Server}. 58 """
59 - def getFactory(self):
60 """ 61 @rtype: L{twisted.spread.pb.PBServerFactory} 62 """
63
64 - def setConnectionInfo(self, host, port, useSSL):
65 """ 66 @param host: the host to listen as 67 @type host: str 68 @param port: the port to listen on 69 @type port: int 70 @param useSSL: whether this connection uses SSL 71 @type useSSL: bool 72 """
73
74 -class Server(log.Loggable):
75 logCategory = "server" 76
77 - def __init__(self, servable):
78 """ 79 I expose a servable to the network using TCP or SSL. 80 81 @type servable: an implemtor of L{IServable} 82 """ 83 self._servable = servable
84
85 - def startSSL(self, host, port, pemFile, configDir):
86 """ 87 Listen as the given host and on the given port using SSL. 88 Use the given .pem file, or look for it in the config directory. 89 90 @returns: {twisted.internet.interfaces.IListeningPort} on which 91 we are listening; call .stopListening() to stop. 92 """ 93 # if no path in pemFile, then look for it in the config directory 94 if not os.path.split(pemFile)[0]: 95 pemFile = os.path.join(configDir, pemFile) 96 if not os.path.exists(pemFile): 97 self.error(".pem file %s does not exist.\n" \ 98 "For more information, see \n" \ 99 "http://www.flumotion.net/doc/flumotion/manual/html/" \ 100 "chapter-security.html" % pemFile) 101 log.debug('manager', 'Using PEM certificate file %s' % pemFile) 102 ctxFactory = _ServerContextFactory(pemFile) 103 104 self.info('Starting on port %d using SSL' % port) 105 if not host == "": 106 self.info('Listening as host %s' % host) 107 self._servable.setConnectionInfo(host, port, True) 108 return reactor.listenSSL(port, self._servable.getFactory(), 109 ctxFactory, interface=host)
110
111 - def startTCP(self, host, port):
112 """ 113 Listen as the given host and on the given port using normal TCP. 114 115 @returns: {twisted.internet.interfaces.IListeningPort} on which 116 we are listening; call .stopListening() to stop. 117 """ 118 self.info('Starting on port %d using TCP' % port) 119 if not host == "": 120 self.info('Listening as host %s' % host) 121 self._servable.setConnectionInfo(host, port, False) 122 return reactor.listenTCP(port, self._servable.getFactory(), 123 interface=host)
124