<?php

define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));

require_once (BASE_ROOT_PATH . '/fooder.php');
require_once (BASE_ROOT_PATH . '/helper/search/server.php');
require_once (BASE_ROOT_PATH . '/helper/search/processor.php');
require_once (BASE_ROOT_PATH . '/helper/search/event_handler.php');
require_once (BASE_ROOT_PATH . '/helper/search/util.php');
require_once (BASE_ROOT_PATH . '/helper/category_helper.php');
require_once (BASE_ROOT_PATH . '/helper/brand_helper.php');
require_once (BASE_ROOT_PATH . '/helper/goods/commonid_helper.php');
require_once (BASE_ROOT_PATH . '/helper/search/searcher.php');
require_once (BASE_ROOT_PATH . '/helper/special_helper.php');
require_once (BASE_ROOT_PATH . '/helper/search/area_library.php');
require_once (BASE_ROOT_PATH . '/helper/brand_helper.php');
require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
require_once (BASE_ROOT_PATH . '/helper/message/subscriber.php');

function all_channels()
{
    return array('searcher');
}

$gMessageStates = null;
$gMessageLock = null;

function search_work($sockfd)
{
    global $gMessageStates;
    global $gMessageLock;

    $gMessageStates = new MsgStates();
    $gMessageLock = Mutex::create();

    Base::run_util();

    StatesHelper::init();
    $listener = new message\subscriber($gMessageStates,$gMessageLock);
    $listener->start();

    if(StatesHelper::fetch_state('init')) {
        Log::record("StatesHelper::fetch_state first.",Log::DEBUG);
        search\searcher::instance()->init();
        search\area_library::instance();
    }

    search\CentraHelper::instance()->init(new search\processor());
    search\CentraHelper::instance()->run_loop($sockfd);
}

function fork_subprocess($count,$listen_fd)
{
    if (($pid = pcntl_fork()) === 0)
    {
        //ob_end_clean(); // Discard the output buffer and close
        fclose(STDIN);  // Close all of the standard
        fclose(STDOUT); // file descriptors as we
        fclose(STDERR); // are running as a daemon.

        Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
        search_work($listen_fd);
        exit();
    }
    elseif($pid === -1)
    {
        Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
        die('could not fork');
    }
    else
    {
        Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
        $ret = pcntl_waitpid($pid,$status,WNOHANG);
        if($ret == 0) {
            Log::record("spawn-fcgi: successful ret == 0 PID: {$pid}",Log::DEBUG);
        }
        elseif($ret == -1) {
            Log::record("spawn-fcgi: ret == 0  PID: {$pid}",Log::DEBUG);
        }
        else {
            Log::record("spawn-fcgi: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
        }
    }
}

function remote_addr()
{
    global $config;

    $host = $config['searcher']['host'];
    $port = $config['searcher']['port'];

    return "{$host}:{$port}";
}

$listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
if($listen_fd == false) {
    echo "无法创建socket,请退出之前进程.\n";
}

$count = 1;
while ($count-- > 0) {
    fork_subprocess($count,$listen_fd);
}

//search_work($listen_fd);




