forked from onlinecity/php-smpp-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuemodel.class.php
More file actions
289 lines (264 loc) · 7.06 KB
/
queuemodel.class.php
File metadata and controls
289 lines (264 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* Redis backend abstraction layer.
* Using the phpredis extension this class will handle all operations with the redis backend.
*
* Copyright (C) 2011 OnlineCity
* Licensed under the MIT license, which can be read at: http://www.opensource.org/licenses/mit-license.php
* @author hd@onlinecity.dk
*/
class QueueModel
{
protected $redis;
protected $key;
protected $useIgBinary;
protected $options;
/**
* Construct a new SmsSender worker
* @param array $options
* @throws RedisException
*/
public function __construct($options)
{
$this->redis = new Redis();
$this->redis->connect($options['queue']['host'],$options['queue']['port'],$options['queue']['connect_timeout']);
$this->redis->select($options['queue']['index']);
$this->key = $options['queue']['queuekey'];
$this->useIgBinary = ($options['queue']['use_igbinary'] && function_exists('igbinary_serialize'));
$this->options = $options;
}
/**
* Close connection to queue backend
*/
public function close()
{
$this->redis->close();
}
/**
* Produce one or more SMS'es (push to queue).
* Returns the length of the queue on success and false on failure.
* @param array $messages
* @return integer
*/
public function produce($messages)
{
$pipeline = $this->redis->multi(Redis::PIPELINE);
foreach ($messages as $m) {
$pipeline->lpush($this->key.':inactive',$this->serialize($m));
}
$replies = $pipeline->exec();
return end($replies);
}
/**
* Consume a single SMS. Blocking with timeout.
* Timeout is specified in seconds.
* Returns false on timeout
*
* @param integer $pid
* @param integer $timeout
* @return SmsMessage
*/
public function consume($pid,$timeout=5)
{
$m = $this->redis->brpoplpush($this->key.':inactive',$this->key.':active:'.$pid,$timeout);
if (is_null($m) || $m == '*-1' || $m == '*') return null;
return $this->unserialize($m);
}
/**
* Store SMSC ids with their SMS IDs
*
* @param integer $smsId
* @param array $smscIds
*/
public function storeIds($smsId,array $smscIds,array $msisdns)
{
$retention = (int) $this->options['queue']['retention'];
$pipeline = $this->redis->multi(Redis::PIPELINE);
foreach ($smscIds as $i => $id) {
$pipeline->sAdd($this->key.':ids:'.$smsId,$id);
$pipeline->sAdd($this->key.':msisdns:'.$smsId,$msisdns[$i]);
$pipeline->setex($this->key.':id:'.$id,3600*$retention,$smsId);
}
$pipeline->expire($this->key.':ids:'.$smsId,3600*$retention);
$pipeline->expire($this->key.':msisdns:'.$smsId,3600*$retention);
$replies = $pipeline->exec();
return end($replies);
}
/**
* Get the matching sms ids for a bunch of SMSC ids
* @param array $smscIds
* @return array
*/
public function getSmsIds($smscIds)
{
$pipeline = $this->redis->multi(Redis::PIPELINE);
foreach ($smscIds as $i => $id) {
$pipeline->get($this->key.':id:'.$id);
}
$replies = $pipeline->exec();
if (!$replies) return false;
$smsids = array();
foreach ($replies as $i => $reply) {
if ($reply) $smsids[$smscIds[$i]] = $reply;
}
if (empty($smsids)) return false;
return $smsids;
}
/**
* Store a bunch of DeliveryReports
* @param DeliveryReport $dlr
*/
public function storeDlr(array $dlrs)
{
$pipeline = $this->redis->multi(Redis::PIPELINE);
foreach ($dlrs as $dlr) {
$d = call_user_func((($this->options['queue']['use_igbinary_for_dlr']) ? 'igbinary_serialize' : 'serialize'),$dlr);
$pipeline->lPush($this->options['queue']['dlr_queue'],$d);
}
$replies = $pipeline->exec();
return end($replies);
}
/**
* Defer delivery of a SMS.
*
* @param integer $pid
* @param SmsMessage $message
*/
public function defer($pid, SmsMessage $message)
{
$m = $this->serialize($message);
$this->redis->lRem($this->key.':active:'.$pid,$m);
$this->redis->lPush($this->key.':deferred',$m);
}
/**
* Get MSISDNs for a sms.
*
* @param integer $smsId
* @return array
*/
public function getMsisdnsForMessage($smsId)
{
return $this->redis->sMembers($this->key.':msisdns:'.$smsId);
}
/**
* Get the latest deferred message
* @return SmsMessage
*/
public function lastDeferred()
{
$m = $this->redis->lIndex($this->key.':deferred',-1);
if (is_null($m) || $m == '*-1' || $m == '*') return null;
return $this->unserialize($m);
}
/**
* Remove (pop) the lastest deferred message
* @return SmsMessage
*/
public function popLastDeferred()
{
$m = $this->redis->rPop($this->key.':deferred');
$m = $this->unserialize($m);
return $m;
}
/**
* Ping the backend to keep our connection alive
*/
public function ping()
{
$this->redis->ping();
}
/**
* Shorthand for unserialize
* @param string $d
* @return mixed
*/
private function unserialize($d)
{
return call_user_func(($this->useIgBinary ? 'igbinary_unserialize' : 'unserialize'),$d);
}
/**
* Shorthand for serialize
* @param mixed $d
* @return string
*/
private function serialize($d)
{
return call_user_func(($this->useIgBinary ? 'igbinary_serialize' : 'serialize'),$d);
}
}
class SmsMessage
{
public $id;
public $sender;
public $message;
public $recipients;
public $retries;
public $lastRetry;
public $isFlashSms;
/**
* Create a new SMS Message to send
*
* @param integer $id
* @param string $sender
* @param string $message
* @param array $recipients array of msisdns
* @param boolean $isFlashSms
*/
public function __construct($id, $sender, $message, $recipients, $isFlashSms=false)
{
$this->id = $id;
$this->sender = $sender;
$this->message = $message;
$this->recipients = $recipients;
$this->retries = 0;
$this->lastRetry = null;
$this->isFlashSms = $isFlashSms;
}
}
class DeliveryReport implements Serializable
{
public $providerId;
public $messageId;
public $msisdn;
public $statusReceived;
public $statusCode;
public $errorCode;
// Normal status codes
const STATUS_DELIVERED = 1;
const STATUS_BUFFERED = 2;
const STATUS_ERROR = 3;
const STATUS_EXPIRED = 4;
// Extra status codes, not used often
const STATUS_QUEUED = 5;
const STATUS_INSUFFICIENT_CREDIT = 6;
const STATUS_BLACKLISTED = 7;
const STATUS_UNKNOWN_RECIPIENT = 8;
const STATUS_PROVIDER_ERROR = 9;
const STATUS_INVALID_SMS_ENCODING = 10;
const STATUS_DELETED = 11;
// Error codes
const ERROR_UNKNOWN = 1;
const ERROR_EXPIRED = 2;
const ERROR_INSUFFICIENT_CREDIT = 3;
const ERROR_BLACKLISTED = 4;
const ERROR_UNKNOWN_RECIPIENT = 5;
const ERROR_INVALID_SMS_ENCODING = 6;
const ERROR_DELETED = 7;
public function __construct($messageId, $msisdn, $statusReceived, $statusCode, $errorCode=null, $providerId =null)
{
$this->messageId = $messageId;
$this->msisdn = $msisdn;
$this->statusReceived = $statusReceived;
$this->statusCode = $statusCode;
$this->errorCode = $errorCode;
$this->providerId = $providerId;
}
public function serialize()
{
return serialize(array($this->providerId, $this->messageId, $this->msisdn, $this->statusReceived, $this->statusCode, $this->errorCode));
}
public function unserialize($data)
{
list($this->providerId, $this->messageId, $this->msisdn, $this->statusReceived, $this->statusCode, $this->errorCode) = unserialize($data);
}
}