Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated example pipes.php with some log info, JAXLPipe now uses JAXLS…

…ocketClient, updated JAXLSocketClient to accept string or resource in connect method
  • Loading branch information...
commit 28864a259f142ef6676daafb5a57960f6f15e342 1 parent da18bab
@abhinavsingh abhinavsingh authored
Showing with 58 additions and 27 deletions.
  1. +30 −17 core/jaxl_pipe.php
  2. +17 −9 core/jaxl_socket_client.php
  3. +11 −1 examples/pipes.php
View
47 core/jaxl_pipe.php
@@ -36,6 +36,7 @@
*
*/
+require_once JAXL_CWD.'/core/jaxl_event.php';
require_once JAXL_CWD.'/core/jaxl_loop.php';
/**
@@ -53,45 +54,57 @@
*/
class JAXLPipe {
- protected $name = null;
protected $perm = 0600;
+ protected $recv_cb = null;
protected $fd = null;
+ protected $client = null;
- public function __construct($name, $perm=0600) {
+ public $name = null;
+
+ public function __construct($name, $read_cb=null) {
+ $this->ev = new JAXLEvent();
$this->name = $name;
- $this->perm = $perm;
+ $this->read_cb = $read_cb;
$pipe_path = $this->get_pipe_file_path();
if(!file_exists($pipe_path)) {
posix_mkfifo($pipe_path, $this->perm);
$this->fd = fopen($pipe_path, 'r+');
- if(!$this->fd) _error("unable to open pipe");
- stream_set_blocking($this->fd, false);
-
- // watch for incoming data on pipe
- JAXLLoop::watch($this->fd, array(
- 'read' => array(&$this, 'on_read_ready')
- ));
+ if(!$this->fd) {
+ _error("unable to open pipe");
+ }
+ else {
+ _debug("pipe opened using path $pipe_path");
+ _notice("Usage: $ echo 'Hello World!' > $pipe_path");
+
+ $this->client = new JAXLSocketClient();
+ $this->client->connect($this->fd);
+ $this->client->set_callback(array(&$this, 'on_data'));
+ }
}
else {
_error("pipe with name $name already exists");
}
}
- public function get_pipe_file_path() {
- return JAXL_CWD.'/.jaxl/pipes/jaxl_'.$this->name.'.pipe';
- }
-
public function __destruct() {
@fclose($this->fd);
@unlink($this->get_pipe_file_path());
_debug("unlinking pipe file");
}
- public function on_read_ready($fd) {
- $data = fread($fd, 1024);
- _debug($data);
+ public function get_pipe_file_path() {
+ return JAXL_CWD.'/.jaxl/pipes/jaxl_'.$this->name.'.pipe';
+ }
+
+ public function set_callback($recv_cb) {
+ $this->recv_cb = $recv_cb;
+ }
+
+ public function on_data($data) {
+ // callback
+ if($this->recv_cb) call_user_func($this->recv_cb, $data);
}
}
View
26 core/jaxl_socket_client.php
@@ -82,16 +82,24 @@ public function set_callback($recv_cb) {
$this->recv_cb = $recv_cb;
}
+ /**
+ * @param string | resource $socket_path
+ */
public function connect($socket_path) {
- $path_parts = explode(":", $socket_path);
- $this->transport = $path_parts[0];
- $this->host = substr($path_parts[1], 2, strlen($path_parts[1]));
- if(sizeof($path_parts) == 3) $this->port = $path_parts[2];
-
- _info("trying ".$socket_path);
- if($this->stream_context) $this->fd = @stream_socket_client($socket_path, $this->errno, $this->errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->stream_context);
- else $this->fd = @stream_socket_client($socket_path, $this->errno, $this->errstr, $this->timeout);
-
+ if(gettype($socket_path) == "string") {
+ $path_parts = explode(":", $socket_path);
+ $this->transport = $path_parts[0];
+ $this->host = substr($path_parts[1], 2, strlen($path_parts[1]));
+ if(sizeof($path_parts) == 3) $this->port = $path_parts[2];
+
+ _info("trying ".$socket_path);
+ if($this->stream_context) $this->fd = @stream_socket_client($socket_path, $this->errno, $this->errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->stream_context);
+ else $this->fd = @stream_socket_client($socket_path, $this->errno, $this->errstr, $this->timeout);
+ }
+ else {
+ $this->fd = &$socket_path;
+ }
+
if($this->fd) {
_debug("connected to ".$socket_path."");
stream_set_blocking($this->fd, $this->blocking);
View
12 examples/pipes.php
@@ -40,8 +40,18 @@
require_once 'jaxl.php';
JAXLLogger::$level = JAXL_INFO;
+// include jaxl pipes
require_once JAXL_CWD.'/core/jaxl_pipe.php';
-$pipe = new JAXLPipe(getmypid());
+
+// initialize
+$pipe_name = getmypid();
+$pipe = new JAXLPipe($pipe_name);
+
+// add read event callback
+$pipe->set_callback(function($data) {
+ global $pipe;
+ _info("read ".trim($data)." from pipe");
+});
JAXLLoop::run();
echo "done\n";
Please sign in to comment.
Something went wrong with that request. Please try again.