imdaemon.php 3.49 KB
Newer Older
1
#!/usr/bin/env php
2 3
<?php
/*
4
 * StatusNet - the distributed open-source microblogging tool
5
 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

21
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
22

23 24
$shortoptions = 'fi::a';
$longoptions = array('id::', 'foreground', 'all');
25

26 27
$helptext = <<<END_OF_IM_HELP
Daemon script for receiving new notices from IM users.
28

29
    -i --id           Identity (default none)
30 31
    -a --all          Handle XMPP for all local sites
                      (requires Stomp queue handler, status_network setup)
32
    -f --foreground   Stay in the foreground (default background)
33

34
END_OF_IM_HELP;
35

36 37
require_once INSTALLDIR.'/scripts/commandline.inc';

38
class ImDaemon extends SpawningDaemon
39
{
40 41 42
    protected $allsites = false;

    function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
43
    {
44 45
        if ($threads != 1) {
            // This should never happen. :)
46
            throw new Exception("IMDaemon can must run single-threaded");
47
        }
48
        parent::__construct($id, $daemonize, $threads);
49
        $this->allsites = $allsites;
50 51
    }

52
    function runThread()
53
    {
54
        common_log(LOG_INFO, 'Waiting to listen to IM connections and queues');
55

56
        $master = new ImMaster($this->get_id(), $this->processManager());
57
        $master->init($this->allsites);
58
        $master->service();
59

60
        common_log(LOG_INFO, 'terminating normally');
61

62
        return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
63 64
    }

65
}
66

67
class ImMaster extends IoMaster
68
{
69 70 71 72 73 74 75 76
    protected $processManager;

    function __construct($id, $processManager)
    {
        parent::__construct($id);
        $this->processManager = $processManager;
    }

77 78 79 80 81 82
    /**
     * Initialize IoManagers for the currently configured site
     * which are appropriate to this instance.
     */
    function initManagers()
    {
83 84
        $classes = array();
        if (Event::handle('StartImDaemonIoManagers', array(&$classes))) {
85
            $qm = QueueManager::get();
86 87
            $qm->setActiveGroup('im');
            $classes[] = $qm;
88
            $classes[] = $this->processManager;
89 90 91 92 93
        }
        Event::handle('EndImDaemonIoManagers', array(&$classes));
        foreach ($classes as $class) {
            $this->instantiate($class);
        }
94
    }
95 96
}

97 98 99 100 101 102
if (version_compare(PHP_VERSION, '5.2.6', '<')) {
    $arch = php_uname('m');
    if ($arch == 'x86_64' || $arch == 'amd64') {
        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";
        exit(1);
    }
103 104
}

105 106
if (have_option('i', 'id')) {
    $id = get_option_value('i', 'id');
107
} else if (count($args) > 0) {
108
    $id = $args[0];
109
} else {
110
    $id = null;
111
}
112

113
$foreground = have_option('f', 'foreground');
114
$all = have_option('a') || have_option('--all');
115

116
$daemon = new ImDaemon($id, !$foreground, 1, $all);
117

118
$daemon->runOnce();