<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/7/19
 * Time: 下午2:53
 */

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

define('UGC_SUBSCRIBER_PROC',true);

require_once(BASE_ROOT_PATH . '/fooder.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');
require_once(BASE_ROOT_PATH . '/helper/ugc_helper.php');
require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');

function all_channels() {
    return array('special','follow','inviter');
}

$gMessageStates = null;
$gMessageLock = null;

class UgcHandler
{
    static public function onSpecial($msg)
    {
        if(empty($msg)) return false;
        $content = unserialize($msg);
        if($content == false || !is_array($content)) {
            return false;
        }

        $type   = $content['type'];
        $params = $content['params'];
        if($type == 'add') {
            search\relation_client::instance()->add_special(array('user_id' => $params['user_id'],'specials' => $params['specials']));
        }
        elseif($type == 'del') {
            search\relation_client::instance()->del_special(array('user_id' => $params['user_id'],'specials' => $params['specials']));
        }
        else {

        }
        return true;
    }

    static public function onFellow($msg)
    {
        if(empty($msg)) return false;

        $content = unserialize($msg);
        if($content == false || !is_array($content)) {
            return false;
        }
        $type   = $content['type'];
        $params = $content['params'];

        if($type == 'add') {
            search\relation_client::instance()->add_follow(['user_id' => $params['user_id'],'friends' => $params['friends']]);
        }
        elseif($type == 'del') {
            search\relation_client::instance()->del_follow(['user_id' => $params['user_id'],'friends' => $params['friends']]);
        }
        else {

        }
        return true;
    }
    static public function onInviter($msg)
    {
        if(empty($msg)) return false;

        $content = unserialize($msg);
        if($content == false || !is_array($content)) {
            return false;
        }
        $type   = $content['type'];
        $params = $content['params'];

        if($type == 'add') {
            search\relation_client::instance()->add_inviter(['user_id' => $params['user_id'],'inviter_id' => $params['inviter_id']]);
        }
        else {
        }
        return true;
    }
}

function work_proc()
{
    global $gMessageStates;
    global $gMessageLock;

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

    ugc_helper::init_server();
    StatesHelper::init();
    $listener = new message\subscriber($gMessageStates,$gMessageLock);
    $listener->run();
}

function fork_subprocess($count)
{
    if (($pid = pcntl_fork()) === 0)
    {
        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);
        work_proc();
        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);
        }
    }
}

fork_subprocess(1);
//work_proc();