1. 程式人生 > >dubbo-php-framework的TcpServer解析(五)

dubbo-php-framework的TcpServer解析(五)

這篇開始分析BaseServer的run方法,這裡完成了server的真實啟動。

//啟動server,我們假定cmd為start,也就是分析initServer和start方法,其中extstart和extrestart是不註冊zk的p2p模式,這裡可以看到只是設定了屬性,其他的流程和start一樣的。
public function run($cmd = 'help') 
    {        
        switch ($cmd) 
        {
            //start
			case 'extstart':
			case 'extrestart':
				//開啟p2p模式
				$this->start_without_registry = true;
            case 'start':
            case 'restart':
                $this->initServer();
                $this->start();
                break;
            default:
                echo 'Usage: app_admin.php start | stop | reload | restart | extstart | extrestart' . PHP_EOL;
                break;
        }
    }

private function initServer() 
    {
        // Creating a swoole server resource object
        $swooleServerName = '\swoole_server';
        $this->sw = new $swooleServerName($this->host, $this->port, $this->mode, $this->sockType);
        
        //create monitor
    	$monitor = new AppMonitor($this->processName, $this->config);
        //設定appMonitor
        $monitor->setServer($this->sw);
        $this->sw->appMonitor = $monitor;

		//建立overload monitor
		$this->overloadMonitor = new OverloadMonitor($this->processName);

        //對整數進行容錯處理
		$this->setting['worker_num'] = intval($this->setting['worker_num']);
        $this->setting['dispatch_mode'] = intval($this->setting['dispatch_mode']);
        $this->setting['daemonize'] = intval($this->setting['daemonize']);

        // Setting the runtime parameters
        $this->sw->set($this->setting);

        // Set Event Server callback function
        $this->sw->on('Start', array($this, 'onMasterStart'));
        $this->sw->on('ManagerStart', array($this, 'onManagerStart'));
        $this->sw->on('ManagerStop', array($this, 'onManagerStop'));
        $this->sw->on('WorkerStart', array($this, 'onWorkerStart'));
        $this->sw->on('Connect', array($this, 'onConnect'));
        $this->sw->on('Receive', array($this, 'onReceive'));
        $this->sw->on('Close', array($this, 'onClose'));
        $this->sw->on('WorkerStop', array($this, 'onWorkerStop'));
        //$this->sw->on('timer', array($this, 'onTimer'));
        if (isset($this->setting['task_worker_num'])) 
        {
            $this->sw->on('Task', array($this, 'onTask'));
            $this->sw->on('Finish', array($this, 'onFinish'));
        }
		$this->sw->on('WorkerError', array($this, 'onWorkerError'));

        // add listener
        if (is_array($this->listen))
        {
            foreach($this->listen as $v)
            {
                if (empty($v['host']) || empty($v['port']))
                {
                    continue;
                }
                $this->sw->addlistener($v['host'], $v['port'], $this->sockType);
            }
        }
        $this->logger->info("server host:".$this->host.'|'.$this->port);
    }
protected function start()
    {
       	if ($this->checkServerIsRunning()) 
       	{
            $this->logger->warn($this->processName . ": master process file " . $this->masterPidFile . " has already exists!");
            $this->logger->warn($this->processName . ": start [OK]");
          	return false;
       	}

        $this->logger->info($this->processName . ": start [OK]");
        $this->sw->start();
    }