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