]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/iomanager.php
Merge branch 'testing' into 0.9.x
[quix0rs-gnu-social.git] / lib / iomanager.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Abstract class for i/o managers
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  QueueManager
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @author    Brion Vibber <brion@status.net>
27  * @copyright 2009-2010 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 abstract class IoManager
33 {
34     const SINGLE_ONLY = 0;
35     const INSTANCE_PER_SITE = 1;
36     const INSTANCE_PER_PROCESS = 2;
37
38     /**
39      * Factory function to get an appropriate subclass.
40      */
41     public abstract static function get();
42
43     /**
44      * Tell the i/o queue master if and how we can handle multi-site
45      * processes.
46      *
47      * Return one of:
48      *   IoManager::SINGLE_ONLY
49      *   IoManager::INSTANCE_PER_SITE
50      *   IoManager::INSTANCE_PER_PROCESS
51      */
52     public static function multiSite()
53     {
54         return IoManager::SINGLE_ONLY;
55     }
56     
57     /**
58      * If in a multisite configuration, the i/o master will tell
59      * your manager about each site you'll have to handle so you
60      * can do any necessary per-site setup.
61      *
62      * The new site will be the currently live configuration during
63      * this call.
64      */
65     public function addSite()
66     {
67         /* no-op */
68     }
69
70     /**
71      * This method is called when data is available on one of your
72      * i/o manager's sockets. The socket with data is passed in,
73      * in case you have multiple sockets.
74      *
75      * If your i/o manager is based on polling during idle processing,
76      * you don't need to implement this.
77      *
78      * @param resource $socket
79      * @return boolean true on success, false on failure
80      */
81     public function handleInput($socket)
82     {
83         return true;
84     }
85
86     /**
87      * Return any open sockets that the run loop should listen
88      * for input on. If input comes in on a listed socket,
89      * the matching manager's handleInput method will be called.
90      *
91      * @return array of resources
92      */
93     function getSockets()
94     {
95         return array();
96     }
97
98     /**
99      * Maximum planned time between poll() calls when input isn't waiting.
100      * Actual time may vary!
101      *
102      * When we get a polling hit, the timeout will be cut down to 0 while
103      * input is coming in, then will back off to this amount if no further
104      * input shows up.
105      *
106      * By default polling is disabled; you must override this to enable
107      * polling for this manager.
108      *
109      * @return int max poll interval in seconds, or 0 to disable polling
110      */
111     function pollInterval()
112     {
113         return 0;
114     }
115
116     /**
117      * Request a maximum timeout for listeners before the next idle period.
118      * Actual wait may be shorter, so don't go crazy in your idle()!
119      * Wait could be longer if other handlers performed some slow activity.
120      *
121      * Return 0 to request that listeners return immediately if there's no
122      * i/o and speed up the idle as much as possible; but don't do that all
123      * the time as this will burn CPU.
124      *
125      * @return int seconds
126      */
127     function timeout()
128     {
129         return 60;
130     }
131
132     /**
133      * Called by IoManager after each handled item or empty polling cycle.
134      * This is a good time to e.g. service your XMPP connection.
135      *
136      * Doesn't need to be overridden if there's no maintenance to do.
137      */
138     function idle()
139     {
140         return true;
141     }
142
143     /**
144      * The meat of a polling manager... check for something to do
145      * and do it! Note that you should not take too long, as other
146      * i/o managers may need to do some work too!
147      *
148      * On a successful hit, the next poll() call will come as soon
149      * as possible followed by exponential backoff up to pollInterval()
150      * if no more data is available.
151      *
152      * @return boolean true if events were hit
153      */
154     public function poll()
155     {
156         return false;
157     }
158
159     /**
160      * Initialization, run when the queue manager starts.
161      * If this function indicates failure, the handler run will be aborted.
162      *
163      * @param IoMaster $master process/event controller
164      * @return boolean true on success, false on failure
165      */
166     public function start($master)
167     {
168         $this->master = $master;
169         return true;
170     }
171
172     /**
173      * Cleanup, run when the queue manager ends.
174      * If this function indicates failure, a warning will be logged.
175      *
176      * @return boolean true on success, false on failure
177      */
178     public function finish()
179     {
180         return true;
181     }
182
183     /**
184      * Ping iomaster's queue status monitor with a stats update.
185      * Only valid during input loop!
186      *
187      * @param string $counter keyword for counter to increment
188      */
189     public function stats($counter, $owners=array())
190     {
191         $this->master->stats($counter, $owners);
192     }
193 }
194