/** * Constructor. * @return void */ public function init() { if ($this->isEnabled()) { $this->asteriskclient = Pool::getInstance($this->config->asteriskclientname->value); } }
/** * Called when new data received * @return void */ public function onRead() { if ($this->state === self::CONN_STATE_START) { if (($ver = $this->readline()) === null) { return; } $this->pool->setAmiVersion($this->addr, $ver); $this->state = self::CONN_STATE_GOT_INITIAL_PACKET; $this->auth(); } while (($line = $this->readline()) !== null) { //Daemon::log('>>> '.$line); if ($line === '') { $this->instate = self::INPUT_STATE_END_OF_PACKET; $packet =& $this->packets[$this->cnt]; ++$this->cnt; } else { $this->instate = self::INPUT_STATE_PROCESSING; list($header, $value) = Pool::extract($line); $this->packets[$this->cnt][$header] = $value; } if ((int) $this->state === self::CONN_STATE_AUTH) { if ($this->instate === self::INPUT_STATE_END_OF_PACKET) { if ($packet['response'] === 'success') { if ($this->state === self::CONN_STATE_CHALLENGE_PACKET_SENT) { if (is_callable($this->onChallenge)) { call_user_func($this->onChallenge, $this, $packet['challenge']); } } else { if ($packet['message'] === 'authentication accepted') { $this->state = self::CONN_STATE_HANDSHAKED_OK; Daemon::$process->log(__METHOD__ . ': Authentication ok. Connected to ' . parse_url($this->addr, PHP_URL_HOST)); if ($this->onConnected) { $this->connected = true; $this->onConnected->executeAll($this); $this->onConnected = null; } $this->event('connected'); } } } else { $this->state = self::CONN_STATE_HANDSHAKED_ERROR; Daemon::$process->log(__METHOD__ . ': Authentication failed. Connection to ' . parse_url($this->addr, PHP_URL_HOST) . ' failed.'); if ($this->onConnected) { $this->connected = false; $this->onConnected->executeAll($this); $this->onConnected = null; } $this->finish(); } $this->packets = []; } } elseif ($this->state === self::CONN_STATE_HANDSHAKED_OK) { if ($this->instate === self::INPUT_STATE_END_OF_PACKET) { // Event if (isset($packet['event']) && !isset($packet['actionid'])) { $this->event('event_' . $packet['event'], $packet); $this->event('event', $packet); } elseif (isset($packet['actionid'])) { $action_id =& $packet['actionid']; if (isset($this->callbacks[$action_id])) { if (isset($this->assertions[$action_id])) { $this->packets[$action_id][] = $packet; if (count(array_uintersect_uassoc($this->assertions[$action_id], $packet, 'strcasecmp', 'strcasecmp')) === count($this->assertions[$action_id])) { if (is_callable($this->callbacks[$action_id])) { call_user_func($this->callbacks[$action_id], $this, $this->packets[$action_id]); unset($this->callbacks[$action_id]); } unset($this->assertions[$action_id]); unset($this->packets[$action_id]); } } else { if (is_callable($this->callbacks[$action_id])) { call_user_func($this->callbacks[$action_id], $this, $packet); unset($this->callbacks[$action_id]); } } } } unset($packet); unset($this->packets[$this->cnt - 1]); } } } }