Skip to content

Commit

Permalink
Updating dependency for CSP
Browse files Browse the repository at this point in the history
  • Loading branch information
ucirello committed Jan 17, 2015
1 parent e964a40 commit d2ccde9
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require-dev": {
"phpunit/php-code-coverage": "2.0.*",
"ext-xdebug": "*",
"dericofilho/csp": "1.0.*",
"dericofilho/csp": "1.1.*",
"satooshi/php-coveralls": "dev-master"
},
"suggest": {
Expand Down
Binary file modified fmt.phar
Binary file not shown.
2 changes: 1 addition & 1 deletion fmt.phar.sha1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c53823f3557d344aa98aa56d37c0a915fac10c06
3d9116e65a6f63271672c179c9525470357f0670
103 changes: 100 additions & 3 deletions fmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
// SOFTWARE.

define('PHP_INT_LENGTH', strlen(sprintf("%u", PHP_INT_MAX)));
function cofunc(callable$fn) {
function cofunc(callable $fn) {
$pid = pcntl_fork();
if (-1 == $pid) {
trigger_error('could not fork', E_ERROR);
Expand All @@ -54,6 +54,7 @@ function cofunc(callable$fn) {
}

class CSP_Channel {
const CLOSED = '-1';
private $ipc;
private $ipc_fn;
private $key;
Expand All @@ -68,6 +69,9 @@ public function __construct() {
]);

}
public function msg_count() {
return $this->msg_count;
}
public function close() {
$this->closed = true;
do {
Expand All @@ -81,10 +85,35 @@ public function in($msg) {
if ($this->closed || !msg_queue_exists($this->key)) {
return;
}
$shm = new Message();
$shm->store($msg);
$error = 0;
@msg_send($this->ipc, 1, $shm->key(), false, true, $error);
++$this->msg_count;
}
public function non_blocking_in($msg) {
if ($this->closed || !msg_queue_exists($this->key)) {
return self::CLOSED;
}
$shm = new Message();
$shm->store($msg);
@msg_send($this->ipc, 1, $shm->key(), false);
$error = 0;
@msg_send($this->ipc, 1, $shm->key(), false, false, $error);
if (MSG_EAGAIN === $error) {
$shmAbortedMessage = new Message($shm->key());
$shmAbortedMessage->destroy();
return false;
}
++$this->msg_count;
$first_loop = true;
do {
$data = msg_stat_queue($this->ipc);
if (!$first_loop && 0 == $data['msg_qnum']) {
break;
}
$first_loop = false;
} while (true);
return true;
}
public function out() {
if ($this->closed || !msg_queue_exists($this->key)) {
Expand All @@ -93,12 +122,28 @@ public function out() {
$msgtype = null;
$ipcmsg = null;
$error = null;
msg_receive($this->ipc, 1, $msgtype, (1 * PHP_INT_LENGTH) + 1, $ipcmsg, false, $error);
msg_receive($this->ipc, 1, $msgtype, (1 * PHP_INT_LENGTH) + 1, $ipcmsg, false, 0, $error);
--$this->msg_count;
$shm = new Message($ipcmsg);
$ret = $shm->fetch();
return $ret;
}
public function non_blocking_out() {
if ($this->closed || !msg_queue_exists($this->key)) {
return [self::CLOSED, null];
}
$msgtype = null;
$ipcmsg = null;
$error = null;
msg_receive($this->ipc, 1, $msgtype, (1 * PHP_INT_LENGTH) + 1, $ipcmsg, false, MSG_IPC_NOWAIT, $error);
if (MSG_ENOMSG === $error) {
return [false, null];
}
--$this->msg_count;
$shm = new Message($ipcmsg);
$ret = $shm->fetch();
return [true, $ret];
}
}
class Message {
private $key;
Expand Down Expand Up @@ -137,6 +182,58 @@ public function destroy() {
function make_channel() {
return new CSP_Channel();
}

/*
$chn = &$chn;
$var = &$var;
$var2 = &$var2;
select_channel([
[$chn, $var, function () {
echo "Message Sent";
}],
[$var, $chn, function ($msg) {
echo "Message Received";
}],
['default', function () {
echo "Default";
}, $var2],
]);
*/
function select_channel(array $actions) {
while (true) {
foreach ($actions as $action) {
if ('default' == $action[0]) {
call_user_func_array($action[1]);
break 2;
} elseif (is_callable($action[1])) {
$chn = &$action[0];
$callback = &$action[1];

list($ok, $result) = $chn->non_blocking_out();
if (true === $ok) {
call_user_func_array($callback, [$result]);
break 2;
}
} elseif ($action[0] instanceof CSP_Channel) {
$chn = &$action[0];
$msg = &$action[1];
$callback = &$action[2];
$params = array_slice($action, 3);

$ok = $chn->non_blocking_in($msg);
if (CSP_Channel::CLOSED === $ok) {
throw new Exception('Cannot send to closed channel');
} elseif (true === $ok) {
call_user_func($callback);
break 2;
}
} else {
throw new Exception('Invalid action for CSP select_channel');
}
}
}
}
;
}
$enable_cache = false;
Expand Down
103 changes: 100 additions & 3 deletions fmt.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
// SOFTWARE.

define('PHP_INT_LENGTH', strlen(sprintf("%u", PHP_INT_MAX)));
function cofunc(callable$fn) {
function cofunc(callable $fn) {
$pid = pcntl_fork();
if (-1 == $pid) {
trigger_error('could not fork', E_ERROR);
Expand All @@ -60,6 +60,7 @@ function cofunc(callable$fn) {
}

class CSP_Channel {
const CLOSED = '-1';
private $ipc;
private $ipc_fn;
private $key;
Expand All @@ -74,6 +75,9 @@ public function __construct() {
]);

}
public function msg_count() {
return $this->msg_count;
}
public function close() {
$this->closed = true;
do {
Expand All @@ -87,10 +91,35 @@ public function in($msg) {
if ($this->closed || !msg_queue_exists($this->key)) {
return;
}
$shm = new Message();
$shm->store($msg);
$error = 0;
@msg_send($this->ipc, 1, $shm->key(), false, true, $error);
++$this->msg_count;
}
public function non_blocking_in($msg) {
if ($this->closed || !msg_queue_exists($this->key)) {
return self::CLOSED;
}
$shm = new Message();
$shm->store($msg);
@msg_send($this->ipc, 1, $shm->key(), false);
$error = 0;
@msg_send($this->ipc, 1, $shm->key(), false, false, $error);
if (MSG_EAGAIN === $error) {
$shmAbortedMessage = new Message($shm->key());
$shmAbortedMessage->destroy();
return false;
}
++$this->msg_count;
$first_loop = true;
do {
$data = msg_stat_queue($this->ipc);
if (!$first_loop && 0 == $data['msg_qnum']) {
break;
}
$first_loop = false;
} while (true);
return true;
}
public function out() {
if ($this->closed || !msg_queue_exists($this->key)) {
Expand All @@ -99,12 +128,28 @@ public function out() {
$msgtype = null;
$ipcmsg = null;
$error = null;
msg_receive($this->ipc, 1, $msgtype, (1 * PHP_INT_LENGTH) + 1, $ipcmsg, false, $error);
msg_receive($this->ipc, 1, $msgtype, (1 * PHP_INT_LENGTH) + 1, $ipcmsg, false, 0, $error);
--$this->msg_count;
$shm = new Message($ipcmsg);
$ret = $shm->fetch();
return $ret;
}
public function non_blocking_out() {
if ($this->closed || !msg_queue_exists($this->key)) {
return [self::CLOSED, null];
}
$msgtype = null;
$ipcmsg = null;
$error = null;
msg_receive($this->ipc, 1, $msgtype, (1 * PHP_INT_LENGTH) + 1, $ipcmsg, false, MSG_IPC_NOWAIT, $error);
if (MSG_ENOMSG === $error) {
return [false, null];
}
--$this->msg_count;
$shm = new Message($ipcmsg);
$ret = $shm->fetch();
return [true, $ret];
}
}
class Message {
private $key;
Expand Down Expand Up @@ -143,6 +188,58 @@ public function destroy() {
function make_channel() {
return new CSP_Channel();
}

/*
$chn = &$chn;
$var = &$var;
$var2 = &$var2;
select_channel([
[$chn, $var, function () {
echo "Message Sent";
}],
[$var, $chn, function ($msg) {
echo "Message Received";
}],
['default', function () {
echo "Default";
}, $var2],
]);
*/
function select_channel(array $actions) {
while (true) {
foreach ($actions as $action) {
if ('default' == $action[0]) {
call_user_func_array($action[1]);
break 2;
} elseif (is_callable($action[1])) {
$chn = &$action[0];
$callback = &$action[1];

list($ok, $result) = $chn->non_blocking_out();
if (true === $ok) {
call_user_func_array($callback, [$result]);
break 2;
}
} elseif ($action[0] instanceof CSP_Channel) {
$chn = &$action[0];
$msg = &$action[1];
$callback = &$action[2];
$params = array_slice($action, 3);

$ok = $chn->non_blocking_in($msg);
if (CSP_Channel::CLOSED === $ok) {
throw new Exception('Cannot send to closed channel');
} elseif (true === $ok) {
call_user_func($callback);
break 2;
}
} else {
throw new Exception('Invalid action for CSP select_channel');
}
}
}
}
;
}
$enable_cache = false;
Expand Down
Binary file modified refactor.phar
Binary file not shown.
2 changes: 1 addition & 1 deletion refactor.phar.sha1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
662b59cc369017b02a66c4556f5ca6a2f261ab94
9993de710b7307dea378ab8bb686f8694323be42
Loading

0 comments on commit d2ccde9

Please sign in to comment.