]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/iomanager.php
Misses this file to merge. I like the comments.
[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 static function get() {
42         throw new MethodNotImplementedException(__METHOD__);
43     }
44
45     /**
46      * Tell the i/o queue master if and how we can handle multi-site
47      * processes.
48      *
49      * Return one of:
50      *   IoManager::SINGLE_ONLY
51      *   IoManager::INSTANCE_PER_SITE
52      *   IoManager::INSTANCE_PER_PROCESS
53      */
54     public static function multiSite()
55     {
56         return IoManager::SINGLE_ONLY;
57     }
58     
59     /**
60      * If in a multisite configuration, the i/o master will tell
61      * your manager about each site you'll have to handle so you
62      * can do any necessary per-site setup.
63      *
64      * The new site will be the currently live configuration during
65      * this call.
66      */
67     public function addSite()
68     {
69         /* no-op */
70     }
71
72     /**
73      * This method is called when data is available on one of your
74      * i/o manager's sockets. The socket with data is passed in,
75      * in case you have multiple sockets.
76      *
77      * If your i/o manager is based on polling during idle processing,
78      * you don't need to implement this.
79      *
80      * @param resource $socket
81      * @return boolean true on success, false on failure
82      */
83     public function handleInput($socket)
84     {
85         return true;
86     }
87
88     /**
89      * Return any open sockets that the run loop should listen
90      * for input on. If input comes in on a listed socket,
91      * the matching manager's handleInput method will be called.
92      *
93      * @return array of resources
94      */
95     function getSockets()
96     {
97         return array();
98     }
99
100     /**
101      * Maximum planned time between poll() calls when input isn't waiting.
102      * Actual time may vary!
103      *
104      * When we get a polling hit, the timeout will be cut down to 0 while
105      * input is coming in, then will back off to this amount if no further
106      * input shows up.
107      *
108      * By default polling is disabled; you must override this to enable
109      * polling for this manager.
110      *
111      * @return int max poll interval in seconds, or 0 to disable polling
112      */
113     function pollInterval()
114     {
115         return 0;
116     }
117
118     /**
119      * Request a maximum timeout for listeners before the next idle period.
120      * Actual wait may be shorter, so don't go crazy in your idle()!
121      * Wait could be longer if other handlers performed some slow activity.
122      *
123      * Return 0 to request that listeners return immediately if there's no
124      * i/o and speed up the idle as much as possible; but don't do that all
125      * the time as this will burn CPU.
126      *
127      * @return int seconds
128      */
129     function timeout()
130     {
131         return 60;
132     }
133
134     /**
135      * Called by IoManager after each handled item or empty polling cycle.
136      * This is a good time to e.g. service your XMPP connection.
137      *
138      * Doesn't need to be overridden if there's no maintenance to do.
139      */
140     function idle()
141     {
142         return true;
143     }
144
145     /**
146      * The meat of a polling manager... check for something to do
147      * and do it! Note that you should not take too long, as other
148      * i/o managers may need to do some work too!
149      *
150      * On a successful hit, the next poll() call will come as soon
151      * as possible followed by exponential backoff up to pollInterval()
152      * if no more data is available.
153      *
154      * @return boolean true if events were hit
155      */
156     public function poll()
157     {
158         return false;
159     }
160
161     /**
162      * Initialization, run when the queue manager starts.
163      * If this function indicates failure, the handler run will be aborted.
164      *
165      * @param IoMaster $master process/event controller
166      * @return boolean true on success, false on failure
167      */
168     public function start($master)
169     {
170         $this->master = $master;
171         return true;
172     }
173
174     /**
175      * Cleanup, run when the queue manager ends.
176      * If this function indicates failure, a warning will be logged.
177      *
178      * @return boolean true on success, false on failure
179      */
180     public function finish()
181     {
182         return true;
183     }
184
185     /**
186      * Ping iomaster's queue status monitor with a stats update.
187      * Only valid during input loop!
188      *
189      * @param string $counter keyword for counter to increment
190      */
191     public function stats($counter, $owners=array())
192     {
193         $this->master->stats($counter, $owners);
194     }
195 }
196