]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Msn/msnmanager.php
Requeue waiting messages on start/connection loss
[quix0rs-gnu-social.git] / plugins / Msn / msnmanager.php
1 <?php\r
2 /*\r
3  * StatusNet - the distributed open-source microblogging tool\r
4  * Copyright (C) 2008, 2009, StatusNet, Inc.\r
5  *\r
6  * This program is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU Affero General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * This program is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU Affero General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU Affero General Public License\r
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 \r
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }\r
21 \r
22 /**\r
23  * MSN background connection manager for MSN-using queue handlers,\r
24  * allowing them to send outgoing messages on the right connection.\r
25  *\r
26  * Input is handled during socket select loop, keepalive pings during idle.\r
27  * Any incoming messages will be handled.\r
28  *\r
29  * In a multi-site queuedaemon.php run, one connection will be instantiated\r
30  * for each site being handled by the current process that has MSN enabled.\r
31  */\r
32 \r
33 class MsnManager extends ImManager {\r
34     public $conn = null;\r
35     private $lastping = null;\r
36     private $pingInterval;\r
37 \r
38     /**\r
39      * Initialise connection to server.\r
40      *\r
41      * @return boolean true on success\r
42      */\r
43     public function start($master) {\r
44         if (parent::start($master)) {\r
45             $this->requeue_waiting_messages();\r
46             $this->connect();\r
47             return true;\r
48         } else {\r
49             return false;\r
50         }\r
51     }\r
52 \r
53     /**\r
54     * Return any open sockets that the run loop should listen\r
55     * for input on.\r
56     *\r
57     * @return array Array of socket resources\r
58     */\r
59     public function getSockets() {\r
60         $this->connect();\r
61         if ($this->conn) {\r
62             return $this->conn->getSockets();\r
63         } else {\r
64             return array();\r
65         }\r
66     }\r
67 \r
68     /**\r
69      * Idle processing for io manager's execution loop.\r
70      * Send keepalive pings to server.\r
71      *\r
72      * @return void\r
73      */\r
74     public function idle($timeout = 0) {\r
75         if (empty($this->lastping) || time() - $this->lastping > $this->pingInterval) {\r
76             $this->send_ping();\r
77         }\r
78     }\r
79 \r
80     /**\r
81      * Message pump is triggered on socket input, so we only need an idle()\r
82      * call often enough to trigger our outgoing pings.\r
83      */\r
84     function timeout() {\r
85         return $this->pingInterval;\r
86     }\r
87 \r
88     /**\r
89      * Process MSN events that have come in over the wire.\r
90      *\r
91      * @param resource $socket Socket ready\r
92      * @return void\r
93      */\r
94     public function handleInput($socket) {\r
95         common_log(LOG_DEBUG, 'Servicing the MSN queue.');\r
96         $this->stats('msn_process');\r
97         $this->conn->receive();\r
98     }\r
99 \r
100     /**\r
101     * Initiate connection\r
102     *\r
103     * @return void\r
104     */\r
105     public function connect() {\r
106         if (!$this->conn) {\r
107             $this->conn = new MSN(\r
108                 array(\r
109                     'user' => $this->plugin->user,\r
110                     'password' => $this->plugin->password,\r
111                     'alias' => $this->plugin->nickname,\r
112                     'psm' => 'Send me a message to post a notice',\r
113                     'debug' => true\r
114                 )\r
115             );\r
116             $this->conn->registerHandler('IMin', array($this, 'handle_msn_message'));\r
117             $this->conn->registerHandler('SessionReady', array($this, 'handle_session_ready'));\r
118             $this->conn->registerHandler('Pong', array($this, 'update_ping_time'));\r
119             $this->conn->registerHandler('ConnectFailed', array($this, 'handle_connect_failed'));\r
120             $this->conn->registerHandler('Reconnect', array($this, 'handle_reconnect'));\r
121             $this->conn->signon();\r
122             $this->lastping = time();\r
123         }\r
124         return $this->conn;\r
125     }\r
126 \r
127     /**\r
128     * Called by the idle process to send a ping\r
129     * when necessary\r
130     *\r
131     * @return void\r
132     */\r
133     private function send_ping() {\r
134         $this->connect();\r
135         if (!$this->conn) {\r
136             return false;\r
137         }\r
138 \r
139         $this->conn->sendPing();\r
140         $this->lastping = time();\r
141         $this->pingInterval = 50;\r
142         return true;\r
143     }\r
144 \r
145     /**\r
146      * Update the time till the next ping\r
147      *\r
148      * @param $data Time till next ping\r
149      * @return void\r
150      */\r
151     public function update_ping_time($data) {\r
152         $this->pingInterval = $data;\r
153     }\r
154 \r
155     /**\r
156     * Called via a callback when a message is received\r
157     *\r
158     * Passes it back to the queuing system\r
159     *\r
160     * @param array $data Data\r
161     * @return boolean\r
162     */\r
163     public function handle_msn_message($data) {\r
164         $this->plugin->enqueue_incoming_raw($data);\r
165         return true;\r
166     }\r
167 \r
168     /**\r
169     * Called via a callback when a session becomes ready\r
170     *\r
171     * @param array $data Data\r
172     */\r
173     public function handle_session_ready($data) {\r
174         $sessionFailed = false;\r
175         $wm = Msn_waiting_message::top($data['to']);\r
176         while ($wm != NULL) {\r
177             if ($sessionFailed) {\r
178                 $this->plugin->send_message($wm->screenname, $wm->message);\r
179                 $sessionFailed = true;\r
180             } elseif (!$this->conn->sendMessage($wm->screenname, $wm->message, $ignore)) {\r
181                 $this->plugin->send_message($wm->screenname, $wm->message);\r
182             }\r
183 \r
184             $wm->delete();\r
185             $wm = Msn_waiting_message::top($data['to']);\r
186         }\r
187     }\r
188 \r
189     /**\r
190     * Requeue messages from the waiting table so we try\r
191     * to send them again\r
192     *\r
193     * @return void\r
194     */\r
195     protected function requeue_waiting_messages() {\r
196         $wm = Msn_waiting_message::top($data['to']);\r
197         while ($wm != NULL) {\r
198             $this->plugin->send_message($wm->screenname, $wm->message);\r
199             $wm->delete();\r
200             $wm = Msn_waiting_message::top($data['to']);\r
201         }\r
202     }\r
203 \r
204     /**\r
205     * Called by callback to log failure during connect\r
206     *\r
207     * @param void $data Not used (there to keep callback happy)\r
208     * @return void\r
209     */\r
210     public function handle_connect_failed($data) {\r
211         common_log(LOG_NOTICE, 'MSN connect failed, retrying');\r
212     }\r
213 \r
214     /**\r
215     * Called by callback to log reconnection\r
216     *\r
217     * @param void $data Not used (there to keep callback happy)\r
218     * @return void\r
219     */\r
220     public function handle_reconnect($data) {\r
221         common_log(LOG_NOTICE, 'MSN reconnecting');\r
222         // Requeue messages waiting in the DB\r
223         $this->requeue_waiting_messages();\r
224     }\r
225 \r
226     /**\r
227     * Enters a message into the database for sending via a callback\r
228     * when the session is established\r
229     *\r
230     * @param string $to Intended recipient\r
231     * @param string $message Message\r
232     */\r
233     private function enqueue_waiting_message($to, $message) {\r
234         $wm = new Msn_waiting_message();\r
235 \r
236         $wm->screenname = $to;\r
237         $wm->message    = $message;\r
238         $wm->created    = common_sql_now();\r
239         $result         = $wm->insert();\r
240 \r
241         if (!$result) {\r
242             common_log_db_error($wm, 'INSERT', __FILE__);\r
243             throw new ServerException('DB error inserting queue item');\r
244         }\r
245 \r
246         return true;\r
247     }\r
248 \r
249     /**\r
250      * Send a message using the daemon\r
251      *\r
252      * @param $data Message data\r
253      * @return boolean true on success\r
254      */\r
255     public function send_raw_message($data) {\r
256         $this->connect();\r
257         if (!$this->conn) {\r
258             return false;\r
259         }\r
260 \r
261         $waitForSession = false;\r
262         if (!$this->conn->sendMessage($data['to'], $data['message'], $waitForSession)) {\r
263             if ($waitForSession) {\r
264                 $this->enqueue_waiting_message($data['to'], $data['message']);\r
265             } else {\r
266                 return false;\r
267             }\r
268         }\r
269 \r
270         // Sending a command updates the time till next ping\r
271         $this->lastping = time();\r
272         $this->pingInterval = 50;\r
273         return true;\r
274     }\r
275 }\r