iomanager.php 5.6 KB

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