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  from zope.interface import Interface 
 30   
 31  from flumotion.common import log 
 32   
 33   
34 -class _ServerContextFactory(log.Loggable):
35 36 logCategory = "SSLServer" 37
38 - def __init__(self, pemFile):
39 self._pemFile = pemFile
40
41 - def getContext(self):
42 """ 43 Create an SSL context. 44 """ 45 from OpenSSL import SSL 46 ctx = SSL.Context(SSL.SSLv23_METHOD) 47 try: 48 ctx.use_certificate_file(self._pemFile) 49 ctx.use_privatekey_file(self._pemFile) 50 except SSL.Error, e: 51 self.warning('SSL error: %r' % e.args) 52 self.error('Could not open certificate %s' % self._pemFile) 53 return ctx
54
55 -class IServable(Interface):
56 """ 57 I am an interface for objects that want to be servable through a 58 L{Server}. 59 """
60 - def getFactory(self):
61 """ 62 @rtype: L{twisted.spread.pb.PBServerFactory} 63 """
64
65 - def setConnectionInfo(self, host, port, useSSL):
66 """ 67 @param host: the host to listen as 68 @type host: str 69 @param port: the port to listen on 70 @type port: int 71 @param useSSL: whether this connection uses SSL 72 @type useSSL: bool 73 """
74
75 -class Server(log.Loggable):
76 logCategory = "server" 77
78 - def __init__(self, servable):
79 """ 80 I expose a servable to the network using TCP or SSL. 81 82 @type servable: an implemtor of L{IServable} 83 """ 84 self._servable = servable
85
86 - def startSSL(self, host, port, pemFile, configDir):
87 """ 88 Listen as the given host and on the given port using SSL. 89 Use the given .pem file, or look for it in the config directory. 90 91 @returns: {twisted.internet.interfaces.IListeningPort} on which 92 we are listening; call .stopListening() to stop. 93 """ 94 from flumotion.common import common 95 common.assertSSLAvailable() 96 97 # if no path in pemFile, then look for it in the config directory 98 if not os.path.split(pemFile)[0]: 99 pemFile = os.path.join(configDir, pemFile) 100 if not os.path.exists(pemFile): 101 self.error(".pem file %s does not exist.\n" \ 102 "For more information, see \n" \ 103 "http://www.flumotion.net/doc/flumotion/manual/html/" \ 104 "chapter-security.html" % pemFile) 105 log.debug('manager', 'Using PEM certificate file %s' % pemFile) 106 ctxFactory = _ServerContextFactory(pemFile) 107 108 self.info('Starting on port %d using SSL' % port) 109 if not host == "": 110 self.info('Listening as host %s' % host) 111 self._servable.setConnectionInfo(host, port, True) 112 return reactor.listenSSL(port, self._servable.getFactory(), 113 ctxFactory, interface=host)
114
115 - def startTCP(self, host, port):
116 """ 117 Listen as the given host and on the given port using normal TCP. 118 119 @returns: {twisted.internet.interfaces.IListeningPort} on which 120 we are listening; call .stopListening() to stop. 121 """ 122 self.info('Starting on port %d using TCP' % port) 123 if not host == "": 124 self.info('Listening as host %s' % host) 125 self._servable.setConnectionInfo(host, port, False) 126 return reactor.listenTCP(port, self._servable.getFactory(), 127 interface=host)
128