3 * StatusNet, the distributed open-source microblogging tool
5 * Abstract class for i/o managers
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.
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.
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/>.
22 * @category QueueManager
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/
32 abstract class IoManager
34 const SINGLE_ONLY = 0;
35 const INSTANCE_PER_SITE = 1;
36 const INSTANCE_PER_PROCESS = 2;
39 * Factory function to get an appropriate subclass.
41 public abstract static function get();
44 * Tell the i/o queue master if and how we can handle multi-site
48 * IoManager::SINGLE_ONLY
49 * IoManager::INSTANCE_PER_SITE
50 * IoManager::INSTANCE_PER_PROCESS
52 public static function multiSite()
54 return IoManager::SINGLE_ONLY;
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.
62 * The new site will be the currently live configuration during
65 public function addSite()
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.
75 * If your i/o manager is based on polling during idle processing,
76 * you don't need to implement this.
78 * @param resource $socket
79 * @return boolean true on success, false on failure
81 public function handleInput($socket)
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.
91 * @return array of resources
99 * Maximum planned time between poll() calls when input isn't waiting.
100 * Actual time may vary!
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
106 * By default polling is disabled; you must override this to enable
107 * polling for this manager.
109 * @return int max poll interval in seconds, or 0 to disable polling
111 function pollInterval()
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.
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.
125 * @return int seconds
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.
136 * Doesn't need to be overridden if there's no maintenance to do.
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!
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.
152 * @return boolean true if events were hit
154 public function poll()
160 * Initialization, run when the queue manager starts.
161 * If this function indicates failure, the handler run will be aborted.
163 * @param IoMaster $master process/event controller
164 * @return boolean true on success, false on failure
166 public function start($master)
168 $this->master = $master;
173 * Cleanup, run when the queue manager ends.
174 * If this function indicates failure, a warning will be logged.
176 * @return boolean true on success, false on failure
178 public function finish()
184 * Ping iomaster's queue status monitor with a stats update.
185 * Only valid during input loop!
187 * @param string $counter keyword for counter to increment
189 public function stats($counter, $owners=array())
191 $this->master->stats($counter, $owners);