Package flumotion :: Package component :: Package misc :: Package porter :: Module porterclient
[hide private]

Source Code for Module flumotion.component.misc.porter.porterclient

  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  from twisted.internet.protocol import Protocol, Factory 
 23  from twisted.internet.tcp import Port, Connection 
 24  from twisted.internet import reactor, address 
 25  from twisted.cred import credentials 
 26   
 27  from flumotion.common import medium, log 
 28  from flumotion.twisted import defer, fdserver 
 29  from flumotion.twisted import pb as fpb 
 30   
 31  import socket 
 32   
 33  __version__ = "$Rev$" 
 34   
 35   
 36  # Very similar to tcp.Server, but we need to call things in a different order 
 37   
 38   
39 -class FDPorterServer(Connection):
40 """ 41 A connection class for use with passed FDs. 42 Similar to tcp.Server, but gets the initial FD from a different source, 43 obviously, and also passes along some data with the original connection. 44 """ 45
46 - def __init__(self, sock, protocol, addr, additionalData):
47 Connection.__init__(self, sock, protocol) 48 self.client = addr 49 50 # Inform the protocol we've made a connection. 51 protocol.makeConnection(self) 52 53 # Now, we want to feed in the extra data BEFORE the reactor reads 54 # anything additional from the socket. However, if we call this in 55 # the other order, and the socket gets closed (or passed to something 56 # non-twisted) after just the initial chunk, we'll be calling 57 # startReading() on something we've already stopped reading. That won't 58 # work too well... Fortunately, the reactor runs in this thread, so 59 # merely adding it (with startReading()) can't cause a read to happen 60 # immediately. 61 self.startReading() 62 self.connected = 1 63 64 protocol.dataReceived(additionalData)
65
66 - def getHost(self):
67 return address.IPv4Address('TCP', *( 68 self.socket.getsockname() + ('INET', )))
69
70 - def getPeer(self):
71 return address.IPv4Address('TCP', *(self.client + ('INET', )))
72 73
74 -class PorterMedium(medium.BaseMedium):
75 """ 76 A medium we use to talk to the porter. 77 Mostly, we use this to say what mountpoints (or perhaps, later, 78 (hostname, mountpoint) pairs?) we expect to receive requests for. 79 """ 80
81 - def registerPath(self, path):
82 return self.callRemote("registerPath", path)
83
84 - def deregisterPath(self, path):
85 return self.callRemote("deregisterPath", path)
86
87 - def registerPrefix(self, prefix):
88 return self.callRemote("registerPrefix", prefix)
89
90 - def deregisterPrefix(self, prefix):
91 return self.callRemote("deregisterPrefix", prefix)
92 93
94 -class PorterClientFactory(fpb.ReconnectingPBClientFactory):
95 """ 96 A PB client factory that knows how to log into a Porter. 97 Lives in streaming components, and accepts FDs passed over this connection. 98 """ 99
100 - def __init__(self, childFactory):
101 """ 102 Create a PorterClientFactory that will use childFactory to create 103 protocol instances for clients attached to the FDs received over this 104 connection. 105 """ 106 fpb.ReconnectingPBClientFactory.__init__(self) 107 108 self.medium = PorterMedium() 109 110 self.protocol = fdserver.FDPassingBroker 111 self._childFactory = childFactory
112
113 - def buildProtocol(self, addr):
114 p = self.protocol(self._childFactory, FDPorterServer) 115 p.factory = self 116 return p
117
118 - def registerPath(self, path):
119 return self.medium.registerPath(path)
120
121 - def deregisterPath(self, path):
122 return self.medium.deregisterPath(path)
123
124 - def registerPrefix(self, prefix):
125 return self.medium.registerPrefix(prefix)
126
127 - def deregisterPrefix(self, prefix):
128 return self.medium.deregisterPrefix(prefix)
129
130 - def registerDefault(self):
131 return self.medium.registerPrefix("/")
132
133 - def deregisterDefault(self):
134 return self.medium.deregisterPrefix("/")
135 136
137 -class HTTPPorterClientFactory(PorterClientFactory):
138
139 - def __init__(self, childFactory, mountPoints, do_start_deferred, 140 prefixes=None):
141 """ 142 @param mountPoints: a list of mountPoint strings that should be 143 registered to the porter 144 """ 145 PorterClientFactory.__init__(self, childFactory) 146 self._mountPoints = mountPoints 147 self._prefixes = prefixes or [] 148 self._do_start_deferred = do_start_deferred
149
150 - def _fireDeferred(self, r):
151 # If we still have the deferred, fire it (this happens after we've 152 # completed log in the _first_ time, not subsequent times) 153 if self._do_start_deferred: 154 self.debug("Firing initial deferred: should indicate " 155 "that login is complete") 156 self._do_start_deferred.callback(None) 157 self._do_start_deferred = None
158
159 - def gotDeferredLogin(self, deferred):
160 # This is called when we start logging in to give us the deferred for 161 # the login process. Once we're logged in, we want to set our 162 # remote ref, then register our path with the porter, then (possibly) 163 # fire a different deferred 164 self.debug("Got deferred login, adding callbacks") 165 deferred.addCallback(self.medium.setRemoteReference) 166 for mount in self._mountPoints: 167 self.debug("Registering mount point %s with porter", mount) 168 deferred.addCallback(lambda r, m: self.registerPath(m), 169 mount) 170 for mount in self._prefixes: 171 self.debug("Registering mount prefix %s with porter", mount) 172 deferred.addCallback(lambda r, m: self.registerPrefix(m), 173 mount) 174 deferred.addCallback(self._fireDeferred)
175