1
|
|
|
<?php |
2
|
|
|
namespace Ackintosh\Snidel; |
3
|
|
|
|
4
|
|
|
use Ackintosh\Snidel\IpcKey; |
5
|
|
|
|
6
|
|
|
abstract class AbstractQueue |
7
|
|
|
{ |
8
|
|
|
/** @var int */ |
9
|
|
|
protected $ownerPid; |
10
|
|
|
|
11
|
|
|
/** @var \Ackintosh\Snidel\IpcKey */ |
12
|
|
|
protected $ipcKey; |
13
|
|
|
|
14
|
|
|
/** @var resource */ |
15
|
|
|
protected $id; |
16
|
|
|
|
17
|
|
|
/** @var array */ |
18
|
|
|
protected $stat; |
19
|
|
|
|
20
|
|
|
/** @var int */ |
21
|
|
|
protected $queuedCount = 0; |
22
|
|
|
|
23
|
|
|
/** @var int */ |
24
|
|
|
protected $dequeuedCount = 0; |
25
|
|
|
|
26
|
|
|
public function __construct($ownerPid) |
27
|
|
|
{ |
28
|
|
|
$this->ownerPid = $ownerPid; |
29
|
|
|
$this->ipcKey = new IpcKey($this->ownerPid, str_replace('\\', '_', get_class($this))); |
30
|
|
|
$this->id = msg_get_queue($this->ipcKey->generate()); |
31
|
|
|
$this->stat = msg_stat_queue($this->id); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $message |
36
|
|
|
*/ |
37
|
|
|
protected function sendMessage($message) |
38
|
|
|
{ |
39
|
|
|
return msg_send($this->id, 1, $message); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
* @throws \RuntimeException |
45
|
|
|
*/ |
46
|
|
|
protected function receiveMessage() |
47
|
|
|
{ |
48
|
|
|
$msgtype = $message = null; |
49
|
|
|
|
50
|
|
|
// argument #3: specify the maximum number of bytes allowsed in one message queue. |
51
|
|
|
$success = msg_receive($this->id, 1, $msgtype, $this->stat['msg_qbytes'], $message); |
52
|
|
|
if (!$success) { |
53
|
|
|
throw new \RuntimeException('failed to receive message.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $message; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
public function queuedCount() |
63
|
|
|
{ |
64
|
|
|
return $this->queuedCount; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
public function dequeuedCount() |
71
|
|
|
{ |
72
|
|
|
return $this->dequeuedCount; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
abstract public function enqueue($something); |
76
|
|
|
abstract public function dequeue(); |
77
|
|
|
|
78
|
|
|
public function __destruct() |
79
|
|
|
{ |
80
|
|
|
if ($this->ipcKey->isOwner(getmypid())) { |
81
|
|
|
$this->ipcKey->delete(); |
82
|
|
|
return msg_remove_queue($this->id); |
83
|
|
|
} |
84
|
|
|
}// @codeCoverageIgnore |
85
|
|
|
} |
86
|
|
|
|