/** * Disconnect from the Asterisk Manager Interface. * * @return $this */ public function disconnect() { $results = $this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this); if ($results->stopped()) { return $this; } $this->connection->close(); $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this); return $this; }
$phone = $argv[6]; $sms->setContent($msg); $sms->setTo($phone); // SMS multipart MSG - This is used to send 1 big message splitted in several parts, up to 255 messages if ($argv[7] == 1) { $sms->setConcatRefId('58'); $sms->setConcatTotalMsg('2'); $sms->setConcatSeqNum('1'); $a->send($sms); $sms->setContent('---Testing Multipart message '); $sms->setTo($phone); $sms->setConcatRefId('58'); $sms->setConcatTotalMsg('2'); $sms->setConcatSeqNum('2'); } $a->send($sms); $time = time(); while (true) { usleep(1000); // 1ms delay // Since we declare(ticks=1) at the top, the following line is not necessary $a->process(); } $a->close(); // send logoff and close the connection. } catch (Exception $e) { echo $e->getMessage() . "\n"; } //////////////////////////////////////////////////////////////////////////////// // Code ENDS. ////////////////////////////////////////////////////////////////////////////////
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $pamiClientOptions = array('log4php.properties' => $input->getOption('log4php-configuration'), 'host' => $input->getOption('ami-host'), 'scheme' => 'tcp://', 'port' => $input->getOption('ami-port'), 'username' => $input->getOption('ami-username'), 'secret' => $input->getOption('ami-password'), 'connect_timeout' => $input->getOption('ami-connect-timeout'), 'read_timeout' => $input->getOption('ami-read-timeout')); $pamiClient = new ClientImpl($pamiClientOptions); $output->write("<comment>Opening asterisk connection... </comment>"); $pamiClient->open(); $output->writeln('<info>done</info>'); $output->write("<comment>Opening rabbitmq connection... </comment>"); $amqpConn = new AMQPConnection($input->getOption('rabbit-host'), $input->getOption('rabbit-port'), $input->getOption('rabbit-username'), $input->getOption('rabbit-password'), $input->getOption('rabbit-vhost')); $ch = $amqpConn->channel(); $exchange = $input->getOption('rabbit-exchange-name'); $ch->exchange_declare($exchange, 'fanout', false, true, false); $output->writeln('<info>done</info>'); $i = 0; $counter = 0; $pamiClient->registerEventListener(function (EventMessage $event) use($output, $ch, $exchange, &$counter, &$i) { // Send to RabbitMQ $msg = new AMQPMessage(json_encode($event->getKeys()), array('content_type' => 'application/json', 'timestamp' => time(), 'delivery_mode' => 2)); $ch->basic_publish($msg, $exchange); gc_collect_cycles(); $output->writeln(sprintf(" >> <comment>[%s] [%s bytes]</comment> <info>%s</info>", date('Y-m-d G:i:s') . substr((string) microtime(), 1, 8), memory_get_usage(), $event->getName())); $counter = 0; $i = 0; }); $closer = function ($autoExit = true) use($pamiClient, $amqpConn, $ch, $output) { $output->writeln(''); $output->write('<comment>Closing ami connection... </comment>'); $pamiClient->close(); // send logoff and close the connection. $output->writeln("<info>done</info>"); $output->write('<comment>Closing rabbitmq connection... </comment>'); $ch->close(); $amqpConn->close(); $output->writeln("<info>done</info>"); if ($autoExit) { exit(0); } }; declare (ticks=1) { pcntl_signal(\SIGINT, $closer); pcntl_signal(\SIGTERM, $closer); while (true) { try { usleep(1000); // 1ms delay $i++; if ($i == 10000) { // show some feedback every 10000 iterations or so, roughly every 10 seconds if nothing is processed $counter++; $output->writeln(sprintf(" >> <comment>Waiting for events... [%d seconds]</comment> <info>Ping...</info>", $counter * 10)); $i = 0; // for every 10 seconds that go by, send a ping/pong event to the asterisk server // if send times out, it'll throw an exception, which will end this script... supervisor should restart /** @var ResponseMessage $pong */ $pong = $pamiClient->send(new \PAMI\Message\Action\PingAction()); if ('Success' == $pong->getKey('response')) { // Send the pong event onto RabbitMQ. This has a faux keep-alive effect on the RabbitMQ connection $msg = new AMQPMessage(json_encode($pong->getKeys()), array('content_type' => 'application/json', 'timestamp' => time(), 'delivery_mode' => 2)); $ch->basic_publish($msg, $exchange); $output->writeln(sprintf(" >> <comment>[%s] [%s bytes]</comment> <info>%s</info>", date('Y-m-d G:i:s') . substr((string) microtime(), 1, 8), memory_get_usage(), 'Pong')); } } $pamiClient->process(); } catch (\Exception $e) { // try to close any connections $closer(false); // rethrow the exception throw $e; } } } $closer(false); return 0; }
/** * Closes the connection to ami. * * return void */ public function close() { $this->_ami->close(); }