nanoserv
[ class tree: nanoserv ] [ index: nanoserv ] [ all elements ]

Source for file NS_JSON_RPC_Service_Handler.php

Documentation is available at NS_JSON_RPC_Service_Handler.php

  1. <?php
  2.  
  3. /**
  4.  *
  5.  * nanoserv handlers - JSON-RPC server
  6.  * 
  7.  * Copyright (C) 2004-2010 Vincent Negrier aka. sIX <six@aegis-corp.org>
  8.  * 
  9.  * This library is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Lesser General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2.1 of the License, or (at your option) any later version.
  13.  * 
  14.  * This library is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.  * Lesser General Public License for more details.
  18.  * 
  19.  * You should have received a copy of the GNU Lesser General Public
  20.  * License along with this library; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
  22.  *
  23.  * @package nanoserv
  24.  * @subpackage Handlers
  25.  */
  26.  
  27. /**
  28.  * Require the HTTP server
  29.  */
  30. require_once "nanoserv-compat/handlers/NS_HTTP_Service_Handler.php";
  31.  
  32.  
  33. /**
  34.  * JSON-RPC Service handler class
  35.  *
  36.  * @package nanoserv
  37.  * @subpackage Handlers
  38.  */
  39.  
  40.     /**
  41.      * Request URL
  42.      * @var string 
  43.      */
  44.     protected $request_url = "";
  45.     
  46.     final public function on_Request($url{
  47.  
  48.         $this->request_url = $url;
  49.         
  50.         $req json_decode($this->request_content);
  51.  
  52.         $ret array("id" => $req->id);
  53.         
  54.         if ($req === NULL{
  55.  
  56.             $this->Set_Response_Status(400);
  57.  
  58.             switch (json_last_error()) {
  59.  
  60.                 case JSON_ERROR_DEPTH:        $ret["error""The maximum stack depth has been exceeded";                break;
  61.                 case JSON_ERROR_CTRL_CHAR:    $ret["error""Control character error, possibly incorrectly encoded";    break;
  62.                 case JSON_ERROR_SYNTAX:        $ret["error""Syntax error";                                                break;
  63.                 default:                    $ret["error""Unknown error";                                            break;
  64.             
  65.             }
  66.             
  67.             return json_encode($ret);
  68.         
  69.         }
  70.  
  71.         try {
  72.         
  73.             $ret["result"$this->on_Call($req->method$req->params);
  74.             
  75.         catch (Exception $e{
  76.  
  77.             $ret["error"$e->getMessage();
  78.         
  79.         }
  80.     
  81.         if (isset($req->id)) {
  82.  
  83.             return json_encode($ret);
  84.  
  85.         else {
  86.  
  87.             return "";
  88.  
  89.         }
  90.     
  91.     }
  92.  
  93.     /**
  94.      * Event called on JSON-RPC method call
  95.      *
  96.      * The value returned by on_Call() will be sent back as the JSON-RPC method call response
  97.      *
  98.      * @param string $method 
  99.      * @param array $args 
  100.      * @return mixed 
  101.      */
  102.     abstract public function on_Call($method$args);
  103.  
  104. }
  105.  
  106. ?>

Documentation generated on Wed, 30 Nov 2011 22:03:24 +0100 by phpDocumentor 1.4.3