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 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  import time 
 23   
 24  from flumotion.component.plugs import base 
 25   
 26  __all__ = ['BouncerPlug'] 
 27  __version__ = "$Rev$" 
 28   
 29   
30 -class BouncerPlug(base.ComponentPlug):
31 """ 32 I am the base class for all bouncer plugs. 33 """ 34 logCategory = 'bouncer-plug' 35
36 - def start(self, component):
37 self._idCounter = 0 38 self._idFormat = time.strftime('%Y%m%d%H%M%S-%%d') 39 self._keycards = {} # keycard id -> Keycard 40 return base.ComponentPlug.start(self, component)
41
42 - def authenticate(self, keycard):
43 raise NotImplementedError("Subclass does not override authenticate")
44
45 - def set_expire_function(self, expire):
46 self.expire = expire
47
48 - def generateKeycardId(self):
49 # FIXME: what if it already had one ? 50 # FIXME: deal with wraparound ? 51 keycardId = self._idFormat % self._idCounter 52 self._idCounter += 1 53 return keycardId
54
55 - def addKeycard(self, keycard):
56 """ 57 Adds a keycard to the keycards store. 58 Can be called with the same keycard more than one time. 59 If the keycard has already been added successfully, 60 adding it again will succeed and return True. 61 62 @param keycard: the keycard to add. 63 @return: if the plug accepts the keycard. 64 """ 65 # give keycard an id and store it in our hash 66 if keycard.id in self._keycards: 67 self.debug("%r in %r", keycard.id, self._keycards) 68 # already in there 69 return True 70 71 keycardId = self.generateKeycardId() 72 keycard.id = keycardId 73 74 if hasattr(keycard, 'ttl') and keycard.ttl <= 0: 75 self.debug("no ttlz") 76 self.log('immediately expiring keycard %r', keycard) 77 return False 78 79 self._addKeycard(keycard) 80 return True
81
82 - def _addKeycard(self, keycard):
83 """ 84 Adds a keycard without checking. 85 Used by sub-class knowing what they do. 86 """ 87 self._keycards[keycard.id] = keycard 88 self.on_keycardAdded(keycard) 89 90 self.debug("added keycard with id %s, ttl %r", keycard.id, 91 getattr(keycard, 'ttl', None))
92
93 - def removeKeycard(self, keycard):
94 del self._keycards[keycard.id] 95 self.on_keycardRemoved(keycard) 96 self.info("removed keycard with id %s" % keycard.id)
97
98 - def removeKeycardId(self, keycardId):
99 self.debug("removing keycard with id %s" % keycardId) 100 keycard = self._keycards[keycardId] 101 self.removeKeycard(keycard)
102
103 - def expireKeycardId(self, keycardId):
104 self.log("expiring keycard with id %r", keycardId) 105 self.expire((keycardId, ))
106
107 - def expireKeycardIds(self, keycardIds):
108 self.log("expiring keycards with ids %r", keycardIds) 109 self.expire(keycardIds)
110
111 - def on_keycardAdded(self, keycard):
112 """ 113 Override to update sub-class specific data related to keycards. 114 Called when the base bouncer accepts and references a new keycard. 115 """
116
117 - def on_keycardRemoved(self, keycard):
118 """ 119 Override to cleanup sub-class specific data related to keycards. 120 Called when the base bouncer has cleanup his references to a keycard. 121 """
122 123
124 -class TrivialBouncerPlug(BouncerPlug):
125
126 - def authenticate(self, keycard):
129