/** * Appends a new event to rabbitmq message queue * @throws LoggerException If the publish message fails. */ public function append(LoggerLoggingEvent $event) { if ($this->connection && $this->layout !== null && $this->append == true) { try { $this->channel->access_request($this->virtual_host, false, false, true, true); $message = $this->getLayout()->format($event); $msgObject = new AMQPMessage(json_encode($message), array('content_type' => 'text/plain')); $this->channel->basic_publish($msgObject, $this->exchange, $this->routing_key); } catch (LoggerException $e) { } } }
/** * start service */ public function run() { echo "X10.Event.Publisher is starting...\n================\n"; // Create Rabbit MQ channel $this->amqConn = new AMQPConnection($this->config['amqp']['host'], $this->config['amqp']['port'], $this->config['amqp']['user'], $this->config['amqp']['pass']); $this->amqChannel = $this->amqConn->channel(); $this->amqChannel->access_request($this->config['amqp']['vhost'], false, false, true, true); /* Create a TCP/IP socket. */ $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($this->socket === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else { echo "OK.\n"; } //get status echo "Reading devices status ...\n\n"; $this->status = $this->commander->getStatus(); foreach($this->status['selected'] as $house => $device){ $this->selectedDevices[$house] = $device; } echo "Attempting to connect to '" . $this->config['mochad']['host'] . "' on port '" . $this->config['mochad']['port'] . "'...\n"; $result = socket_connect($this->socket, $this->config['mochad']['host'], $this->config['mochad']['port']); if ($result === false) { echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($this->socket)) . "\n"; } else { echo "OK.\n"; } echo "Reading socket:\n\n"; while (!$this->stop && ($out = socket_read($this->socket, 2048))) { echo "\nSocket Text:\n$out\n=====================\n"; $events = $this->parseEvents($out); foreach ($events as $event) { $this->processEvent($event); } } }