/** * Retrieve the sole instance of this class * * @return Streamwide_Engine_Logger */ private static function _getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; }
/** * Run the controller. Dequeues signals from the SW Engine's internal queue and * notifies the listeners that a signal from SW Engine has been received * * @return void */ public function run() { $timeout = $this->_options[self::OPT_TIMEOUT]; Streamwide_Engine_Logger::info('Going to main loop'); for (;;) { // Fetch a new signal from the SW Engine's queue $signal = Streamwide_Engine_Signal::dequeue(array('timeout' => $timeout)); if (false === $signal) { continue; } // Update the loggers event items Streamwide_Engine_Logger::updateLogEventItems(array($signal->getPhpId() => $signal->getParams())); // Log the received signal Streamwide_Engine_Logger::dump($signal->toArray(), 'Received event from SW Engine:'); if ($signal->getName() === Streamwide_Engine_Signal::CREATE) { // We have received a CREATE (new call), we need to create a new application to handle // the call if (false === ($application = $this->_createNewApplication($signal))) { continue; } // Add the new application to the controller's running apps storage (will call // the application's start method) $this->addApp($signal->getPhpId(), $application); } else { // We have received a signal from SW Engine, we need to notify the listeners $event = new Streamwide_Engine_Events_Event($signal->getName()); $event->setParam('signal', $signal); $this->dispatchEvent($event); } } }
/** * Sends a signal to SW Engine * * @param integer $retries If as_send_event returns false, we can retry to send the signal * @return boolean */ public function send($retries = 0) { if (!is_int($retries)) { $retries = (int) $retries; } $array = $this->toArray(); do { Streamwide_Engine_Logger::dump($array, 'Sending to SW Engine:'); $sent = $this->_engineProxy->sendEvent($array); Streamwide_Engine_Logger::info(sprintf('Signal "%s" %s sent to SW Engine', $array[self::SIG_NAME], $sent === true ? 'successfully' : 'unsuccessfully')); $retries--; } while (false === $sent && $retries > 0); return $sent; }
/** * Flush the listeners belonging to $groupName in the current php id * * @param string $groupName * @return void */ protected function _flushByGroup($groupName) { $phpId = $this->_registry->get(SW_ENGINE_CURRENT_PHPID); if (null === $phpId) { return; } if (!isset($this->_list[$phpId])) { return; } foreach ($this->_list[$phpId] as $eventType => $listeners) { foreach ($listeners as $offset => $listener) { if ($listener->getGroup() === $groupName) { if (isset($currentEventType) && $currentEventType !== $eventType) { if (count($this->_list[$phpId][$currentEventType]) === 0) { unset($this->_list[$phpId][$currentEventType]); } } unset($this->_list[$phpId][$eventType][$offset]); $this->_count--; $currentEventType = $eventType; } } } if (isset($currentEventType)) { $this->_performListMaintenance($phpId, $currentEventType); } $logMessage = 'Flushed ALL event listeners belonging to group "%s" in the current phpId ("%d") in class "%s"'; Streamwide_Engine_Logger::debug(sprintf($logMessage, $groupName, $phpId, $this->_class)); $logMessage = 'The event listeners count for class "%s" is %d'; Streamwide_Engine_Logger::debug(sprintf($logMessage, $this->_class, $this->_count)); }
/** * Execute the action that was attached to a certain event * * @param Streamwide_Event_Interface $event * @return mixed */ public function execute(Streamwide_Event_Interface $event = null) { Streamwide_Engine_Logger::info(sprintf('Executing callback "%s"', $this->_loggingName)); $args = $this->_args; $args[] = null === $event ? $this->_event : $event; return call_user_func_array($this->_callback, $args); }