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