Package flumotion :: Package component :: Package bouncers :: Module plug
[hide private]

Source Code for Module flumotion.component.bouncers.plug

  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,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  import time 
 19   
 20  from flumotion.component.plugs import base 
 21   
 22  __all__ = ['BouncerPlug'] 
 23  __version__ = "$Rev$" 
 24   
 25   
26 -class BouncerPlug(base.ComponentPlug):
27 """ 28 I am the base class for all bouncer plugs. 29 """ 30 logCategory = 'bouncer-plug' 31
32 - def start(self, component):
33 self._idCounter = 0 34 self._idFormat = time.strftime('%Y%m%d%H%M%S-%%d') 35 self._keycards = {} # keycard id -> Keycard 36 return base.ComponentPlug.start(self, component)
37
38 - def authenticate(self, keycard):
39 raise NotImplementedError("Subclass does not override authenticate")
40
41 - def set_expire_function(self, expire):
42 self.expire = expire
43
44 - def generateKeycardId(self):
45 # FIXME: what if it already had one ? 46 # FIXME: deal with wraparound ? 47 keycardId = self._idFormat % self._idCounter 48 self._idCounter += 1 49 return keycardId
50
51 - def addKeycard(self, keycard):
52 """ 53 Adds a keycard to the keycards store. 54 Can be called with the same keycard more than one time. 55 If the keycard has already been added successfully, 56 adding it again will succeed and return True. 57 58 @param keycard: the keycard to add. 59 @return: if the plug accepts the keycard. 60 """ 61 # give keycard an id and store it in our hash 62 if keycard.id in self._keycards: 63 self.debug("%r in %r", keycard.id, self._keycards) 64 # already in there 65 return True 66 67 keycardId = self.generateKeycardId() 68 keycard.id = keycardId 69 70 if hasattr(keycard, 'ttl') and keycard.ttl <= 0: 71 self.debug("no ttlz") 72 self.log('immediately expiring keycard %r', keycard) 73 return False 74 75 self._addKeycard(keycard) 76 return True
77
78 - def _addKeycard(self, keycard):
79 """ 80 Adds a keycard without checking. 81 Used by sub-class knowing what they do. 82 """ 83 self._keycards[keycard.id] = keycard 84 self.on_keycardAdded(keycard) 85 86 self.debug("added keycard with id %s, ttl %r", keycard.id, 87 getattr(keycard, 'ttl', None))
88
89 - def removeKeycard(self, keycard):
90 del self._keycards[keycard.id] 91 self.on_keycardRemoved(keycard) 92 self.info("removed keycard with id %s" % keycard.id)
93
94 - def removeKeycardId(self, keycardId):
95 self.debug("removing keycard with id %s" % keycardId) 96 keycard = self._keycards[keycardId] 97 self.removeKeycard(keycard)
98
99 - def expireKeycardId(self, keycardId):
100 self.log("expiring keycard with id %r", keycardId) 101 self.expire((keycardId, ))
102
103 - def expireKeycardIds(self, keycardIds):
104 self.log("expiring keycards with ids %r", keycardIds) 105 self.expire(keycardIds)
106
107 - def on_keycardAdded(self, keycard):
108 """ 109 Override to update sub-class specific data related to keycards. 110 Called when the base bouncer accepts and references a new keycard. 111 """
112
113 - def on_keycardRemoved(self, keycard):
114 """ 115 Override to cleanup sub-class specific data related to keycards. 116 Called when the base bouncer has cleanup his references to a keycard. 117 """
118 119
120 -class TrivialBouncerPlug(BouncerPlug):
121
122 - def authenticate(self, keycard):
125