public function start() { $this->parts = parse_url($this->url); $transport = $this->_transport(); $ip = $this->_ip(); $port = $this->_port(); $socket_path = $transport . '://' . $ip . ':' . $port; if ($this->client->connect($socket_path)) { _debug("connection to {$this->url} established"); // send request data $this->send_request(); // start loop and wait for response JAXLLoop::run(); } else { _debug("unable to open {$this->url}"); } }
public function __construct($name, $perm = 0600) { $this->name = $name; $this->perm = $perm; $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'))); } else { _error("pipe with name {$name} already exists"); } }
public function on_write_ready($fd) { //_debug("on write ready called"); $total = strlen($this->obuffer); $bytes = @fwrite($fd, $this->obuffer); $this->send_bytes += $bytes; _debug("sent " . $bytes . "/" . $this->send_bytes . " of data"); _debug(substr($this->obuffer, 0, $bytes)); $this->obuffer = substr($this->obuffer, $bytes, $total - $bytes); // unwatch for write if obuffer is empty if (strlen($this->obuffer) === 0) { JAXLLoop::unwatch($fd, array('write' => true)); $this->writing = false; } //_debug("current obuffer size: ".strlen($this->obuffer).""); }
public function stop() { JAXLLoop::unwatch($this->in, array('read' => true)); }
public function start($opts = array()) { // is bosh bot? if (@$this->cfg['bosh_url']) { $this->trans->session_start(); for (;;) { // while any of the curl request is pending // keep receiving response while (sizeof($this->trans->chs) != 0) { $this->trans->recv(); } // if no request in queue, ping bosh end point // and repeat recv $this->trans->ping(); } $this->trans->session_end(); return; } // is xmpp client or component? // if on_connect event have no callbacks // set default on_connect callback to $this->start_stream() // i.e. xmpp client mode if (!$this->ev->exists('on_connect')) { $this->add_cb('on_connect', array($this, 'start_stream')); } // connect to the destination host/port if ($this->connect($this->get_socket_path())) { // reset in case we connected back after retries $this->retry_attempt = 0; // emit $this->ev->emit('on_connect'); // parse opts if (@$opts['--with-debug-shell']) { $this->enable_debug_shell(); } if (@$opts['--with-unix-sock']) { $this->enable_unix_sock(); } // run main loop JAXLLoop::run(); // emit $this->ev->emit('on_disconnect'); } else { if ($this->trans->errno == 61 || $this->trans->errno == 110 || $this->trans->errno == 111) { _debug("unable to connect with errno " . $this->trans->errno . " (" . $this->trans->errstr . ")"); $this->retry(); } else { $this->ev->emit('on_connect_error', array($this->trans->errno, $this->trans->errstr)); } } }
public static function run($min_read = 0, $min_write = 0) { if (!self::$is_running) { self::$is_running = true; self::$clock = new JAXLClock(); while (self::$active_read_fds >= $min_read && self::$active_write_fds >= $min_write) { self::select(); } _debug("no more active fd's to select"); self::$is_running = false; } }
protected function del_write_cb($client_id) { if (!$this->clients[$client_id]['writing']) { return; } JAXLLoop::unwatch($this->clients[$client_id]['fd'], array('write' => true)); $this->clients[$client_id]['writing'] = false; }
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ require dirname(__FILE__) . '/_bootstrap.php'; if ($argc < 2) { echo "Usage: {$argv['0']} /path/to/server.sock" . PHP_EOL; exit; } JAXLLogger::$level = JAXLLogger::INFO; $server = null; function on_request($client, $raw) { global $server; $server->send($client, $raw); JAXLLogger::info("got client callback " . $raw); } if (file_exists($argv[1])) { unlink($argv[1]); } $server = new JAXLSocketServer('unix://' . $argv[1], null, 'on_request'); JAXLLoop::run(); echo "done" . PHP_EOL;
public function start($cb = null) { $this->cb = $cb; JAXLLoop::run(); }
public function run() { JAXLCli::prompt(); JAXLLoop::run(); }
public function start($opts = array(), $pami_client) { // injection for pami_client // is bosh bot? if (@$this->cfg['bosh_url']) { $this->trans->session_start(); for (;;) { // while any of the curl request is pending // keep receiving response while (sizeof($this->trans->chs) != 0) { $this->trans->recv(); } // echo "eee"; // if no request in queue, ping bosh end point // and repeat recv $this->trans->ping(); } $this->trans->session_end(); return; } // is xmpp client or component? // if on_connect event have no callbacks // set default on_connect callback to $this->start_stream() // i.e. xmpp client mode if (!$this->ev->exists('on_connect')) { $this->add_cb('on_connect', array($this, 'start_stream')); } // connect to the destination host/port if ($this->connect(($this->cfg['port'] == 5223 ? "ssl" : "tcp") . "://" . $this->cfg['host'] . ":" . $this->cfg['port'])) { // emit $this->ev->emit('on_connect'); // parse opts if (@$opts['--with-debug-shell']) { $this->enable_debug_shell(); } if (@$opts['--with-unix-sock']) { $this->enable_unix_sock(); } // run main loop JAXLLoop::run($pami_client); // emit $this->ev->emit('on_disconnect'); } else { if ($this->trans->errno == 61 || $this->trans->errno == 110 || $this->trans->errno == 111) { $retry_after = pow(2, $this->retry_attempt) * $this->retry_interval; $this->retry_attempt++; _debug("unable to connect with errno " . $this->trans->errno . " (" . $this->trans->errstr . "), will try again in " . $retry_after . " seconds"); // TODO: use sigalrm instead // they usually doesn't gel well inside select loop sleep($retry_after); $this->start(); } else { $this->ev->emit('on_connect_error', array($this->trans->errno, $this->trans->errstr)); } } }
function manager_read() { global $astman; $response = $astman->wait_response(true); $reconnects = $astman->reconnects; $oldsocket = $astman->socket; while ($response === false && $reconnects > 0) { $astman->disconnect(); if ($astman->connect($astman->server . ':' . $astman->port, $astman->username, $astman->secret, $astman->events) !== false) { JAXLLoop::watch($astman->socket, array('read' => 'manager_read')); JAXLLoop::unwatch($oldsocket, array('read' => true)); $response = true; } else { if ($reconnects > 1) { $astman->log("reconnect command failed, sleeping before next attempt"); sleep(1); } else { $astman->log("FATAL: no reconnect attempts left, command permanently failed"); exit; } } $reconnects--; } }
public static function run($pami_client) { if (!self::$is_running) { self::$is_running = true; self::$clock = new JAXLClock(); while (self::$active_read_fds + self::$active_write_fds > 0) { self::select(); if ($pami_client != null) { $pami_client->process(); } } _debug("no more active fd's to select"); } }
public static function run() { if (!self::$is_running) { self::$is_running = true; self::$clock = new JAXLClock(); while (self::$active_read_fds + self::$active_write_fds > 0) { self::select(); } JAXLLogger::debug("no more active fd's to select"); self::$is_running = false; } }