This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWorkerPipe.php
127 lines (115 loc) · 2.95 KB
/
WorkerPipe.php
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
<?php
/**
* DocBlox
*
* PHP Version 5
*
* @category DocBlox
* @package Parallel
* @author Mike van Riel <mike.vanriel@naenius.com>
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://docblox-project.org
*/
/**
* Class that represents a named pipe for a Worker.
*
* This class manages the named pipe for a worker and is able to push and pull
* specific data to facilitate IPC (interprocess communication).
*
* @category DocBlox
* @package Parallel
* @author Mike van Riel <mike.vanriel@naenius.com>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://docblox-project.org
*/
class DocBlox_Parallel_WorkerPipe
{
/** @var DocBlox_Parallel_Worker worker class that is associated */
protected $worker;
/** @var string Path to the pipe */
protected $path;
/**
* Initializes the named pipe.
*
* @param DocBlox_Parallel_Worker $worker Associated worker.
*/
public function __construct(DocBlox_Parallel_Worker $worker)
{
$this->worker = $worker;
$this->path = tempnam(sys_get_temp_dir(), 'dpm_');
posix_mkfifo($this->path, 0750);
}
/**
* If the named pipe was not cleaned up, do so now.
*/
public function __destruct()
{
if (file_exists($this->path)) {
$this->release();
}
}
/**
* Pull the worker data into the named pipe.
*
* @return void
*/
public function pull()
{
$this->writePipeContents();
}
/**
* Push the worker data back onto the worker and release the pipe.
*
* @return void
*/
public function push()
{
list($result, $error, $return_code) = $this->readPipeContents();
$this->release();
$this->worker->setResult($result);
$this->worker->setError($error);
$this->worker->setReturnCode($return_code);
}
/**
* Convenience method to show relation to readPipeContents.
*
* @return void
*/
protected function writePipeContents()
{
// push the gathered data onto a name pipe
$pipe = fopen($this->path, 'w');
fwrite(
$pipe, serialize(
array(
$this->worker->getResult(),
$this->worker->getError(),
$this->worker->getReturnCode()
)
)
);
fclose($pipe);
}
/**
* Returns the unserialized contents of the pipe.
*
* @return array
*/
protected function readPipeContents()
{
$pipe = fopen($this->path, 'r+');
$result = unserialize(fread($pipe, filesize($this->path)));
fclose($pipe);
return $result;
}
/**
* Releases the pipe.
*
* @return void
*/
protected function release()
{
unlink($this->path);
}
}