Exemple #1
0
 public function execute()
 {
     //1. check if user is login as needed
     $arrUserinfo = Saf_SmartMain::getUserInfo();
     if (empty($arrUserinfo)) {
         //ouput error
     }
     //2. get and validate input params
     $arrRequest = Saf_SmartMain::getCgi();
     $arrInput = $arrRequest['get'];
     if (!isset($arrInput['id'])) {
         //output error
     }
     Bd_Log::debug('request input', 0, $arrInput);
     //3. call PageService
     $objServicePageSample = new Service_Page_Sample();
     $arrPageInfo = $objServicePageSample->execute($arrInput);
     //4. chage data to out format
     $arrOutput = $arrPageInfo;
     //5. build page
     // smarty模板,以下渲染模板的代码依赖于提供一个tpl模板
     //$tpl = Bd_TplFactory::getInstance();
     //$tpl->assign($arrOutput);
     //$tpl->display('en/newapp/index.tpl');
     //这里直接输出,作为示例
     $strOut = $arrOutput['data'];
     echo $strOut;
     //notice日志信息打印,只需要添加日志信息,saf会自动打一条log
     Bd_Log::addNotice('out', $arrOutput);
 }
Exemple #2
0
 public function execute($arrInput)
 {
     Bd_Log::debug('sample page service called');
     $arrResult = array();
     $arrResult['errno'] = 0;
     try {
         $intId = intval($arrInput['id']);
         if ($intId <= 0) {
             //参数错误的时候,从配置文件取消息
             $strData = Bd_Conf::getAppConf('sample/msg');
             $arrResult['data'] = $strData;
         } else {
             if ($this->objServiceDataSample->isExist($intId)) {
                 //以下获取数据的方式提供3种示例,3选1
                 //1. 调用本地DS
                 $strData = $this->objServiceDataSample->getSample($intId);
                 //2. 子系统交互, 注意:请确保conf/saf.conf中的api_lib配置成Navi, 否则会出错
                 //$strData = $this->objServiceDataSample->callOtherApp($intId);
                 //3. 调用本地库
                 //$objUtil = new App2_Util();
                 //$strData = $objUtil->getUtilMsg();
                 $arrResult['data'] = $strData;
             } else {
                 $arrResult['errno'] = 222;
                 //示例错误码
             }
         }
     } catch (Exception $e) {
         Bd_Log::warning($e->getMessage(), $e->getCode());
         $arrResult['errno'] = $e->getCode();
     }
     return $arrResult;
 }
Exemple #3
0
 public function execute($arrInput)
 {
     Bd_Log::debug('sample api page service  called');
     if ($arrInput == null) {
         $arrCgi = Saf_SmartMain::getCgi();
         $arrInput = $arrCgi['get'];
     }
     $intId = intval($arrInput['id']);
     $arrResult = array();
     $arrResult['errno'] = 0;
     try {
         if ($intId <= 0) {
             $arrResult['errno'] = 222;
             //示例错误码
         } else {
             $strData = $this->objServiceDataSample->getSample($intId);
             $arrResult['data'] = $strData;
         }
     } catch (Exception $e) {
         Bd_Log::warning($e->getMessage(), $e->getCode());
         $arrResult['errno'] = $e->getCode();
     }
     return $arrResult;
 }
Exemple #4
0
 protected static function parameterFilter($type, $parameter)
 {
     $result = false;
     switch ($type) {
         case 'int':
             $result = is_integer($parameter);
             break;
         case 'string':
             $result = is_string($parameter);
             break;
         case 'object':
             $result = is_object($parameter);
             break;
         case 'array':
             $result = is_array($parameter);
             break;
         case 'null':
             $result = is_null($parameter);
             break;
     }
     if ($result === false) {
         Bd_Log::debug("The input parameter's type is wrrong");
     }
     return $result;
 }
Exemple #5
0
 /**
  * Try to figure out the server URL with possible Proxys / Ports etc.
  *
  * @return string Server URL with domain:port
  */
 private function _getServerUrl()
 {
     Bd_Log::debug('_getServerUrl', 0, $_SERVER);
     /*
     $server_url = '';
     $server_url = $_SERVER['HTTP_HOST'];
     return $server_url;
     */
     if (defined('CAS_CALLBACK_SERVER')) {
         return CAS_CALLBACK_SERVER;
     }
     $server_url = '';
     if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
         // explode the host list separated by comma and use the first host
         $hosts = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
         $server_url = $hosts[0];
     } else {
         if (!empty($_SERVER['HTTP_X_FORWARDED_SERVER'])) {
             $server_url = $_SERVER['HTTP_X_FORWARDED_SERVER'];
         } else {
             if (empty($_SERVER['SERVER_NAME'])) {
                 $server_url = $_SERVER['HTTP_HOST'];
             } else {
                 $server_url = $_SERVER['HTTP_HOST'];
             }
         }
     }
     if (!strpos($server_url, ':')) {
         if (empty($_SERVER['HTTP_X_FORWARDED_PORT'])) {
             $server_port = $_SERVER['SERVER_PORT'];
         } else {
             $server_port = $_SERVER['HTTP_X_FORWARDED_PORT'];
         }
         if ($this->_isHttps() && $server_port != 443 || !$this->_isHttps() && $server_port != 80) {
             $server_url .= ':';
             $server_url .= $server_port;
         }
     }
     return $server_url;
 }
Exemple #6
0
 /**
  * @brief 获取db对象
  *
  * @param $clusterName 集群名称
  * @param $key 负载均衡key
  * @param $getNew 是否重新连接
  *
  * @return 
  */
 public static function getConn($clusterName, $key = NULL, $getNew = false)
 {
     $hookBeforeInit = Bd_Conf::getConf('/db/hook_before_init');
     if ($hookBeforeInit === false) {
         //cannot find hookBeforeInit in conf file
         self::$_error['errno'] = LOAD_CONF_ERROR;
         self::$_error['error'] = 'Can not read hookBeforeInit, please check db/global.conf';
         Bd_Log::warning(self::$_error['error'], self::$_error['errno']);
         return false;
     }
     if ($hookBeforeInit != NULL) {
         //user sets hookBeforeInit
         if (is_callable($hookBeforeInit)) {
             $clusterName = call_user_func($hookBeforeInit, $clusterName);
         } else {
             //warnning
             self::$_error['errno'] = SET_HOOK_ERROR;
             self::$_error['error'] = 'Hook(beforinit):' . $before . 'is not callable';
             Bd_Log::warning(self::$_error['error'], self::$_error['errno']);
         }
     }
     $conf =& self::$_conf;
     $hosts =& self::$_hosts;
     $dbData =& self::$_dbData;
     $lastDb =& self::$_lastDb;
     //(1) alreay save a connection (2)user do not need to recreate
     if (!empty($lastDb[$clusterName]) && !$getNew) {
         Bd_Log::trace('Return an existing connection', 0, array('db_cluster' => $clusterName));
         return $lastDb[$clusterName];
     }
     if (self::_init($clusterName) === false) {
         return false;
     }
     //create a new db object
     $db = new Bd_DB(Bd_Db_ConnMgr::$ENABLE_PROFILING);
     //add hook
     if ('' !== ($before = $conf['hook_before_query'])) {
         if (!$db->addHook(Bd_Db::HK_BEFORE_QUERY, $clusterName . '-before', $before)) {
             self::$_error['errno'] = SET_HOOK_ERROR;
             self::$_error['error'] = 'Hook(befor query):' . $before . ' is not callable';
             Bd_Log::warning(self::$_error['error'], self::$_error['errno']);
         }
     }
     if ('' !== ($after = $conf['hook_after_query'])) {
         if (!$db->addHook(Bd_Db::HK_AFTER_QUERY, $clusterName . '-after', $after)) {
             self::$_error['errno'] = SET_HOOK_ERROR;
             self::$_error['error'] = 'Hook(after query):' . $after . ' is not callable';
             Bd_Log::warning(self::$_error['error'], self::$_error['errno']);
         }
     }
     if ('' !== ($onFail = $conf['hook_on_fail'])) {
         if (!$db->onFail($onFail)) {
             self::$_error['errno'] = SET_HOOK_ERROR;
             self::$_error['error'] = 'Hook(on fail):' . $onFail . ' is not callable';
             Bd_Log::warning(self::$_error['error'], self::$_error['errno']);
         }
     }
     //try to connect host until there is not host or connecting successfully
     while (true) {
         //balancer could not select a valid host to connect
         if (count($hosts['valid_hosts']) === 0 || ($index = $dbData['host_selector']->select($hosts, $key)) === false) {
             self::$_error['errno'] = ALL_CONNECT_ERROR;
             self::$_error['error'] = 'No host could be connected in the cluster';
             Bd_Log::warning(self::$_error['error'], self::$_error['errno'], array('db_cluster' => $clusterName));
             $hookOnConnFail = $conf['hook_on_connect_fail'];
             if ($hookOnConnFail != NULL) {
                 if (is_callable($hookOnConnFail)) {
                     call_user_func($hookOnConnFail);
                 } else {
                     //warnning
                     self::$_error['errno'] = SET_HOOK_ERROR;
                     self::$_error['error'] = 'Hook(on connect fail):' . $hookOnConnFail . 'is not callable';
                     Bd_Log::warning(self::$_error['error'], self::$_error['errno']);
                 }
             }
             return false;
         }
         //log parameters
         $logPara = array('db_cluster' => $clusterName, 'db_host' => $hosts['valid_hosts'][$index]['ip'], 'db_port' => $hosts['valid_hosts'][$index]['port'], 'default_db' => $conf['default_db']);
         for ($i = 1; $i <= $conf['retry_times']; $i++) {
             $timeout = $conf['connect_timeout_s'];
             if ($timeout > 0) {
                 $db->setConnectTimeOut($timeout);
             }
             $r_timeout = $conf['read_timeout_s'];
             if ($r_timeout > 0) {
                 $db->setOption(MYSQL_OPT_READ_TIMEOUT, $r_timeout);
             }
             $w_timeout = $conf['write_timeout_s'];
             if ($w_timeout > 0) {
                 $db->setOption(MYSQL_OPT_WRITE_TIMEOUT, $w_timeout);
             }
             Bd_Log::debug("retry times: {$i}");
             $start = microtime(true) * 1000;
             //connect
             $ret = $db->connect($hosts['valid_hosts'][$index]['ip'], $conf['username'], $conf['password'], $conf['default_db'], $hosts['valid_hosts'][$index]['port'], $conf['connect_flag']);
             $end = microtime(true) * 1000;
             if ($ret) {
                 if (empty($conf['charset']) || $db->charset($conf['charset'])) {
                     $logPara['time_ms'] = $end - $start;
                     Bd_Log::trace('Connect to Mysql successfully', 0, $logPara);
                     $lastDb[$clusterName] = $db;
                     return $lastDb[$clusterName];
                 } else {
                     Bd_Log::debug('Set charset failed');
                 }
             }
         }
         //connect failed
         self::$_error['errno'] = CONNECT_ERROR;
         self::$_error['error'] = 'Connect to Mysql failed';
         Bd_Log::warning(self::$_error['error'], self::$_error['errno'], $logPara);
         self::_recordFailedHost($index);
     }
     return false;
 }
Exemple #7
0
 public function addSample($strData)
 {
     Bd_Log::debug("sample data service submitSample called");
     $arrFields = array('data' => $strData);
     return $this->objDaoSample->addSample($arrFields);
 }
Exemple #8
0
 /**
  * @brief 此接口封装了Ak_McClient初始化的过程
  *
  * @return  初始化成功返回Ak_McClient对象实例,失败则返回false
  * @retval  object/boolean
  * @author chenyijie
  * @date 2012/09/27 20:53:48
  **/
 public function create()
 {
     $zkHost = array();
     $zkhostFromConf = Bd_Conf::getConf('cacheproxy/AkCache/zkhost');
     if ($zkhostFromConf === false) {
         Bd_Log::warning('Get conf form cache.conf failed when create AkCacheClient');
         return false;
     }
     //装配ip和port
     foreach ($zkhostFromConf as $value) {
         $content = $value['ip'] . ':' . $value['port'];
         $zkHost[] = $content;
     }
     Ak_Zookeeper::setHost($zkHost);
     Ak_AClient::SetGlobalConf(array('ZookeeperHost' => $zkHost));
     //创建cache客户端实例
     $keyArray = array('pid', 'zk_path', 'default_expire', 'curr_idc', 'delete_directly', 'delete_delay', 'connect_timeout', 'poll_timeout', 'send_timeout', 'recv_timeout', 'acm_connect_timeout', 'acm_read_timeout', 'acm_write_timeout');
     $mcConf = array();
     $confArray = Bd_Conf::getConf('cacheproxy/AkCache/mc_conf/');
     //去除confArray中读取到的空项
     foreach ($keyArray as $key) {
         if (isset($confArray[$key]) && $confArray[$key] !== '') {
             $mcConf[$key] = $confArray[$key];
         }
     }
     if (empty($mcConf)) {
         //错误号参见CacheProxyAbstract.php中的定义
         Bd_Log::warning('Get conf form cache.conf failed when create AkCacheClient', 6, null);
         return false;
     }
     //去除不需要进行string -> int类型转换的项
     array_splice($keyArray, 0, 2);
     array_splice($keyArray, 1, 1);
     //记录需要进行string -> bool转换的项
     $boolArray = array_splice($keyArray, 1, 2);
     foreach ($keyArray as $key) {
         if (array_key_exists($key, $mcConf)) {
             $mcConf[$key] = intval($mcConf[$key]);
         }
     }
     foreach ($boolArray as $key) {
         if ($mcConf[$key] === 'true') {
             $mcConf[$key] = true;
         } else {
             $mcConf[$key] = false;
         }
     }
     /* 装配后的mcConf的具体示例
        $mcConf = array(
            'pid'     => $pid,
            'zk_path' => $zk_path,
            'default_expire' => 86400,
            'curr_idc' => 'tc',
            'delete_directly' => true,
            'delete_delay' => true,
            'connect_timeout' => 10,
            'poll_timeout' => 20,
            'send_timeout' => 100,
            'recv_timeout' => 500,
            'acm_connect_timeout' => 30,
            'acm_read_timeout' => 500,
            'acm_write_timeout' => 100,
        );
        */
     $objMcClient = Ak_McClient::create($mcConf);
     if (is_null($objMcClient)) {
         Bd_Log::debug("create AkCacheClient failed", 2, $mcConf);
         return false;
     }
     return $objMcClient;
 }