~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/rpc/drivers/_helpers/ObjectLibrarian.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it
 
5
# and/or modify it under the terms of the GNU General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public
 
15
# License along with program; see the file COPYING. If not,
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2001-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# ObjectLibrarian.py
 
23
#
 
24
# DESCRIPTION:
 
25
# Helper module that keeps track of object references for use
 
26
# with non-Object interfaces
 
27
#
 
28
# NOTES:
 
29
#
 
30
 
 
31
import sha, time, thread
 
32
 
 
33
class InvalidObjectReference(StandardError):
 
34
  pass
 
35
 
 
36
_objects = {}
 
37
_index = 0.0
 
38
 
 
39
 
 
40
def getObjectReference(object):
 
41
  try:
 
42
    return object._ObjectLibrarian__refid
 
43
  except AttributeError:
 
44
 
 
45
    # This is to introduce some obscurity to the id handles...
 
46
    global _index
 
47
    _index += time.clock()
 
48
    object._ObjectLibrarian__refid = \
 
49
        sha.new("%s%s%s"%(id(object), object, _index)).hexdigest()
 
50
    
 
51
    # add creation time
 
52
    now = time.time()
 
53
    object._ObjectLibrarian__creationtime = now
 
54
    object._ObjectLibrarian__lastusage = now
 
55
    
 
56
    return object._ObjectLibrarian__refid
 
57
 
 
58
 
 
59
def archiveObject(object):
 
60
  _objects[getObjectReference(object)] = object
 
61
 
 
62
 
 
63
def retrieveObject(handle):
 
64
  try:
 
65
    object = _objects[handle]
 
66
 
 
67
    if hasattr(object,"_ObjectLibrarian__myGC"):
 
68
      # call private Garbage Collector (f.e. for sessions)
 
69
      object._ObjectLibrarian__myGC.beingRetrieved(object)
 
70
    else:
 
71
      # just store last used time
 
72
      object._ObjectLibrarian__lastusage = time.time()
 
73
    
 
74
    return object
 
75
  except KeyError:
 
76
    raise InvalidObjectReference
 
77
 
 
78
 
 
79
def deferenceObject(object):
 
80
  try:
 
81
    del _objects[object._ObjectLibrarian__refid]
 
82
  except KeyError:
 
83
    pass
 
84
  except AttributeError:
 
85
    pass
 
86
 
 
87
def getObjectLibrarianStatus():
 
88
  status={}
 
89
  status["objcount"]=len(_objects)
 
90
  return status
 
91
 
 
92
class GarbageCollector:
 
93
  def __init__(self):
 
94
    self.defaultTimeout=3600  # in seconds
 
95
    self.loopLength=20        # in seconds
 
96
    self.running=0
 
97
    
 
98
  def setDefaultTimeout(self,timespan):
 
99
    self.defaultTimeout=timespan
 
100
 
 
101
  def getDefaultTimeout(self):
 
102
    return self.defaultTimeout
 
103
 
 
104
  def setLoopLength(self,length):
 
105
    self.loopLength=length
 
106
    
 
107
  def getLoopLength(self):
 
108
    return self.loopLength
 
109
  
 
110
  def cleanUp(self):
 
111
    # is this threadsafe ?
 
112
    try:
 
113
      keys=_objects.keys()
 
114
      now=time.time()
 
115
      for handle in keys:
 
116
        try:
 
117
          object=_objects[handle]
 
118
          if not hasattr(object,"_ObjectLibrarian__myGC"):
 
119
            if object._ObjectLibrarian__lastusage + self.defaultTimeout < now:
 
120
              deferenceObject(object)
 
121
              GDebug.printMesg(1,"Timeout reached for object %s" & object)
 
122
              
 
123
        except:
 
124
          pass
 
125
 
 
126
    except:
 
127
      pass
 
128
      
 
129
  def loop(self):
 
130
    while 1==1:
 
131
      try:
 
132
        self.cleanUp()
 
133
        time.sleep(self.loopLength)
 
134
      except:
 
135
        pass
 
136
      
 
137
  def start_in_new_thread(self):
 
138
    if self.running:
 
139
      return
 
140
    self.running=1
 
141
    thread.start_new_thread(self.loop,())   
 
142
 
 
143
 
 
144
_global_GarbageCollector = GarbageCollector()
 
145
 
 
146