4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
23 $shortoptions = 'fi::a';
24 $longoptions = array('id::', 'foreground', 'all');
26 $helptext = <<<END_OF_IM_HELP
27 Daemon script for receiving new notices from IM users.
29 -i --id Identity (default none)
30 -a --all Handle XMPP for all local sites
31 (requires Stomp queue handler, status_network setup)
32 -f --foreground Stay in the foreground (default background)
36 require_once INSTALLDIR.'/scripts/commandline.inc';
38 class ImDaemon extends SpawningDaemon
40 protected $allsites = false;
42 function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
45 // This should never happen. :)
46 throw new Exception("IMDaemon can must run single-threaded");
48 parent::__construct($id, $daemonize, $threads);
49 $this->allsites = $allsites;
54 common_log(LOG_INFO, 'Waiting to listen to IM connections and queues');
56 $master = new ImMaster($this->get_id(), $this->processManager());
57 $master->init($this->allsites);
60 common_log(LOG_INFO, 'terminating normally');
62 return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
67 class ImMaster extends IoMaster
69 protected $processManager;
71 function __construct($id, $processManager)
73 parent::__construct($id);
74 $this->processManager = $processManager;
78 * Initialize IoManagers for the currently configured site
79 * which are appropriate to this instance.
81 function initManagers()
84 if (Event::handle('StartImDaemonIoManagers', array(&$classes))) {
85 $qm = QueueManager::get();
86 $qm->setActiveGroup('im');
88 $classes[] = $this->processManager;
90 Event::handle('EndImDaemonIoManagers', array(&$classes));
91 foreach ($classes as $class) {
92 $this->instantiate($class);
97 if (version_compare(PHP_VERSION, '5.2.6', '<')) {
98 $arch = php_uname('m');
99 if ($arch == 'x86_64' || $arch == 'amd64') {
100 print "Aborting daemon - 64-bit PHP prior to 5.2.6 has known bugs in stream_select; you are running " . PHP_VERSION . " on $arch.\n";
105 if (have_option('i', 'id')) {
106 $id = get_option_value('i', 'id');
107 } else if (count($args) > 0) {
113 $foreground = have_option('f', 'foreground');
114 $all = have_option('a') || have_option('--all');
116 $daemon = new ImDaemon($id, !$foreground, 1, $all);