/**
  * 
  * Get Manager instance
  * @return X_Threads_Manager
  */
 public static function instance()
 {
     if (self::$instance === null) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 function stopAction()
 {
     $thread = X_Threads_Manager::instance()->getMonitor()->getThread('upnp-announcer');
     if ($thread->getState() != X_Threads_Thread_Info::STOPPED) {
         X_Threads_Manager::instance()->halt($thread);
     }
     $this->_helper->redirector('index');
 }
 public final function startAction()
 {
     //X_Debug::e("Inside action");
     /* @var $request Zend_Controller_Request_Http */
     $request = $this->getRequest();
     // complete-hash
     $hash = $request->getPost('hash', false);
     // hash-salt
     $salt = $request->getPost('salt', false);
     // key-id
     $key = $request->getPost('key', false);
     $threadId = $request->getParam('thread', false);
     /*
     $runnableClass	= $request->getPost('runnable', false);
     $params			= $request->getPost('params', array());
     */
     $manager = X_Threads_Manager::instance();
     // check if hash_funct(value-of(key).$salt) == $hash
     if (!$manager->getStarter()->isValid($key, $hash, $salt)) {
         return $this->triggerError('403: Forbidden', X_Threads_Manager::ERROR_FORBIDDEN);
     }
     /*
     try {
     	
     	$thread = $manager->appendJob($runnableClass, $params, $threadId);
     	
     	$this->triggerOk("Thread started", $thread->getId());
     	
     	if ( !$thread->isRunning() ) {
     		$manager->resume($thread);
     	}
     	
     } catch (Exception $e) {
     	return $this->triggerError($e->getMessage(), $e->getCode());
     }
     */
     $thread = $manager->newThread($threadId);
     $this->triggerOk("Thread started", $thread->getId());
     $thread->loop();
 }
 public function stop()
 {
     if (!$this->isStreaming()) {
         return;
     }
     $thread = X_Threads_Manager::instance()->getThreadInfo(self::THREAD_ID);
     $infos = $thread->getInfo();
     if (isset($infos['message_params']) && isset($infos['message_params']['streamerId'])) {
         $engineId = $infos['message_params']['streamerId'];
         $engine = X_VlcShares_Plugins::helpers()->streamer()->get($engineId);
         //$engine = new $engineClass();
         if ($engine instanceof X_Streamer_StopperEngine) {
             $engine->doStop($infos);
         }
     } else {
         X_Debug::e("Invalid streamer");
     }
     X_Threads_Manager::instance()->halt($thread);
 }
 protected function _initThreads()
 {
     $this->bootstrap('db');
     $this->bootstrap('configs');
     $dbAdapter = $this->getResource('db');
     $configs = $this->getResource('configs');
     $url = null;
     $logger = false;
     try {
         if ($configs instanceof Zend_Config) {
             $url = @$configs->general->threads->forker;
             if ($configs->general->debug->enabled) {
                 $logger = @$configs->general->threads->logger;
             }
         }
     } catch (Exception $e) {
     }
     if ($url == null) {
         $url = 'http://localhost/vlc-shares/threads/start';
     }
     if ($logger == null) {
         $logger = false;
     }
     X_Threads_Manager::instance()->setMonitor(new X_Threads_Monitor_Db(new X_Threads_Monitor_Db_Mapper(new Application_Model_DbTable_Threads())))->setMessenger(new X_Threads_Messenger_ZendQueue($dbAdapter))->setStarter(new X_Threads_Starter_HttpPost($url))->setLogger($logger);
 }
        $threads = X_Threads_Manager::instance()->getMonitor()->getThreads();
        echo "---- THREADS LIST ----" . PHP_EOL;
        foreach ($threads as $thread) {
            /* @var $thread X_Threads_Thread_Info */
            echo sprintf("Thread ID: %s\n\tState: %s\n\tInfo: %s", $thread->getId(), $thread->getState(), print_r($thread->getInfo(), true)) . PHP_EOL;
        }
        echo "----------------------" . PHP_EOL;
    }
    if ($clear) {
        $threads = X_Threads_Manager::instance()->getMonitor()->getThreads();
        foreach ($threads as $thread) {
            /* @var $thread X_Threads_Thread_Info */
            if ($thread->getState() == X_Threads_Thread_Info::STOPPED) {
                X_Threads_Manager::instance()->getMonitor()->removeThread($thread);
                X_Threads_Manager::instance()->getMessenger()->clearQueue($thread);
            }
        }
    }
    if ($reset) {
        $threads = X_Threads_Manager::instance()->getMonitor()->getThreads();
        foreach ($threads as $thread) {
            /* @var $thread X_Threads_Thread_Info */
            X_Threads_Manager::instance()->getMessenger()->clearQueue($thread);
            X_Threads_Manager::instance()->getMonitor()->removeThread($thread, true);
        }
    }
    return 0;
} catch (Exception $e) {
    echo "Error: {$e->getMessage()}" . PHP_EOL;
    return 1;
}
 public function renewNext()
 {
     $this->manager->getMessenger()->enQueue($this, new X_Threads_Message_Renew());
     $this->log("Appending renew to the queue");
 }
 function clearAction()
 {
     $id = $this->getRequest()->getParam('id', false);
     $csrf = $this->getRequest()->getParam('csrf', false);
     if (!$id) {
         throw new Exception("Thread id missing");
     }
     $hash = new Zend_Form_Element_Hash('csrf', array('salt' => __CLASS__));
     if (!$hash->isValid($csrf)) {
         throw new Exception("Invalid token");
     }
     $hash->initCsrfToken();
     $thread = X_Threads_Manager::instance()->getMonitor()->getThread($id);
     X_Threads_Manager::instance()->getMessenger()->clearQueue($thread);
     $this->_helper->flashMessenger(array('type' => 'success', 'text' => X_Env::_('threads_done')));
     $this->_helper->redirector('index', 'tmanager');
 }