Concurrent.php 2.28 KB
<?php

/**
 * Created by PhpStorm.
 * User: liuziyang
 * Date: 14-4-25
 * Time: 0:49
 */
class Q_Webservice_Yar_Concurrent
{
    private $_callback;

    private $package = array();

    private $functionName;

    private $url;

    private $requestUrl;

    public function __construct($url = '')
    {
        $this->url = $url;
        $this->requestUrl = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
    }

    function callFunction($functionName, array $parameters = array(), $callback = null, array $opt = array())
    {
        $this->package['call_time'] = microtime(true);
        $this->package['function_name'] = $this->functionName = $functionName;
        $this->package['referer'] = $this->requestUrl;
        $this->package['service_url'] = $this->url;
        $this->package['parameter'] = $parameters;
        $_callback = null;
        if (!empty($callback)) {
            $this->_callback = $callback;
            $_callback = array($this, 'callback');
        }
        Yar_Concurrent_Client::call($this->url, $functionName, $parameters, $_callback, NULL, $opt);
    }

    function call($url, $functionName, array $parameters = array(), $callback = null, array $opt = array())
    {
        $this->package['call_time'] = microtime(true);
        $this->package['function_name'] = $this->functionName = $functionName;
        $this->package['referer'] = $this->requestUrl;
        $this->package['service_url'] = $this->url = $url;
        $this->package['parameter'] = $parameters;
        $_callback = null;
        if (!empty($callback)) {
            $this->_callback = $callback;
            $_callback = array($this, 'callback');
        }
        Yar_Concurrent_Client::call($url, $functionName, $parameters, $_callback, NULL, $opt);
    }

    public function callback($callBackData)
    {
        $this->package['result_time'] = microtime(true);
        $this->package['result'] = $callBackData;
        QAid_Logs_Record::yar($this->package);
        $callinfo = array(
            'uri' => $this->url,
            'method' => $this->functionName
        );
        call_user_func_array($this->_callback, array($callBackData, $callinfo));
    }

    function loop($callback = null, $error_callback = null)
    {
        Yar_Concurrent_Client::loop($callback, $error_callback);
    }
}