temp_objects._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * $Id: temp_objects.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
18  */
19 inherit "/kernel/module";
20 #include <macros.h>
21 #include <database.h>
22 class temp_objects : public module{
23 public:
24 
25 
26 
27 
28 
29  mapping mTempObjects;
30  Thread.Mutex mutex = Thread.Mutex();
31  Thread.Queue deleteQueue = Thread.Queue();
32 
33 /**
34  * Callback function to initialize module. Sets the save and restore function.
35  *
36  */
37 private:
38 void init_module()
39 {
40 private:
41  add_data_storage(STORE_TEMPOBJ, retrieve_temp_objects,
42  restore_temp_objects);
43  mTempObjects = ([ ]);
44 }
45 
46 void queued_destruct()
47 {
48 }
49 
50 public:
51 
52 /**
53  * Thread to check for temporary objects and deletes out of date objects.
54  * It also handles a deletion queue to prevent objects from being
55  * destructed while still cleaning up.
56  *
57  */
58 void check_temp_objects()
59 {
60  mixed err;
61  while ( 1 ) {
62  while ( deleteQueue->size() > 0 ) {
63  object obj = deleteQueue->read();
64  if ( objectp(obj) ) {
65  err = catch(obj->drop());
66  if ( err )
67  FATAL("Error while deleting:\n"+sprintf("%O\n", err));
68  }
69  }
70 
71  object l = mutex->lock();
72 
73  array idx = indices(mTempObjects);
74  foreach ( idx, object tmp ) {
75  LOG("Checking " + tmp->get_identifier() + " for deletion ("+
76  time()+":"+mTempObjects[tmp]+")!\n");
77  if ( time() > mTempObjects[tmp] ) {
78  if ( objectp(tmp) )
79  catch(tmp->delete());
80  m_delete(mTempObjects, tmp);
81  }
82  }
83  require_save(STORE_TEMPOBJ);
84  destruct(l);
85  sleep(300);
86  }
87 }
88 
89 /**
90  * Callback function when the module is loaded starts the
91  * thread to check for objects to be deleted.
92  *
93  * @see check_temp_objects
94  */
95 void load_module()
96 {
97  start_thread(check_temp_objects);
98 }
99 
100 /**
101  * Add a temporary object with a given timestamp when it should be deleted.
102  *
103  * @param object obj - the temporary object.
104  * @param int validT - the timestamp for that the object wont be deleted.
105  */
106 void add_temp_object(object obj, int validT)
107 {
108  if ( _SECURITY->access_delete(0, obj, CALLER) ) {
109  object l = mutex->lock();
110  mTempObjects[obj] = validT;
111  require_save(STORE_TEMPOBJ);
112  destruct(l);
113  }
114 }
115 
116 /**
117  * Callback function to retrieve all temporary objects.
118  *
119  * @return mapping of temporary objects.
120 private:
121  * @see restore_temp_objects
122  */
123 private:
124 final mapping retrieve_temp_objects()
125 {
126  if ( CALLER != _Database )
127  THROW("Invalid call to retrieve_temp_objects()", E_ACCESS);
128  return ([ "tempObjects": mTempObjects, ]);
129 }
130 
131 public:
132 
133 /**
134  * Restore the temporary objects from the database.
135  *
136  * @param mapping data - saved data of temp objects to be restored.
137 private:
138  * @see retrieve_temp_objects
139  */
140 private:
141 final void restore_temp_objects(mapping data)
142 {
143  if ( CALLER != _Database )
144  THROW("Invalid call to restore_temp_objects()", E_ACCESS);
145  mTempObjects = data["tempObjects"];
146 }
147 
148 public:
149 
150 /**
151  * Get the identifier of this module.
152  *
153  * @return the identifier "temp_objects"
154  */
155 string get_identifier() { return "temp_objects"; }
156 
157 
158 
159 
160 
161 
162 };