imdaemon.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - the distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', dirname(__DIR__));
  21. define('PUBLICDIR', INSTALLDIR);
  22. $shortoptions = 'fi::a';
  23. $longoptions = array('id::', 'foreground', 'all');
  24. $helptext = <<<END_OF_IM_HELP
  25. Daemon script for receiving new notices from IM users.
  26. -i --id Identity (default none)
  27. -a --all Handle XMPP for all local sites
  28. (requires Stomp queue handler, status_network setup)
  29. -f --foreground Stay in the foreground (default background)
  30. END_OF_IM_HELP;
  31. require_once INSTALLDIR.'/scripts/commandline.inc';
  32. class ImDaemon extends SpawningDaemon
  33. {
  34. protected $allsites = false;
  35. function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
  36. {
  37. if ($threads != 1) {
  38. // This should never happen. :)
  39. throw new Exception("IMDaemon can must run single-threaded");
  40. }
  41. parent::__construct($id, $daemonize, $threads);
  42. $this->allsites = $allsites;
  43. }
  44. function runThread()
  45. {
  46. common_log(LOG_INFO, 'Waiting to listen to IM connections and queues');
  47. $master = new ImMaster($this->get_id(), $this->processManager());
  48. $master->init($this->allsites);
  49. $master->service();
  50. common_log(LOG_INFO, 'terminating normally');
  51. return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
  52. }
  53. }
  54. class ImMaster extends IoMaster
  55. {
  56. protected $processManager;
  57. function __construct($id, $processManager)
  58. {
  59. parent::__construct($id);
  60. $this->processManager = $processManager;
  61. }
  62. /**
  63. * Initialize IoManagers for the currently configured site
  64. * which are appropriate to this instance.
  65. */
  66. function initManagers()
  67. {
  68. $classes = array();
  69. if (Event::handle('StartImDaemonIoManagers', array(&$classes))) {
  70. $qm = QueueManager::get();
  71. $qm->setActiveGroup('im');
  72. $classes[] = $qm;
  73. $classes[] = $this->processManager;
  74. }
  75. Event::handle('EndImDaemonIoManagers', array(&$classes));
  76. foreach ($classes as $class) {
  77. $this->instantiate($class);
  78. }
  79. }
  80. }
  81. if (have_option('i', 'id')) {
  82. $id = get_option_value('i', 'id');
  83. } else if (count($args) > 0) {
  84. $id = $args[0];
  85. } else {
  86. $id = null;
  87. }
  88. $foreground = have_option('f', 'foreground');
  89. $all = have_option('a') || have_option('--all');
  90. $daemon = new ImDaemon($id, !$foreground, 1, $all);
  91. $daemon->runOnce();