-
Notifications
You must be signed in to change notification settings - Fork 987
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dev Created new Json server that uses big data library. Dev Created new get_response_ids function for retrieving response ids for a token.
- Loading branch information
Showing
5 changed files
with
227 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
/** | ||
* Class containing helper functions for dealing with "big data". | ||
* @author Sam Mousa <sam@befound.nl> | ||
*/ | ||
class BigData { | ||
|
||
/** | ||
* This function combines json_encode and echo. | ||
* If a stream is passed (or is part of the array) it's content will be | ||
* directly streamed instead of reading it into memory first. | ||
* Supported flags: | ||
* JSON_FORCE_OBJECT | ||
* @param array $json | ||
* @param int $options Same flags used in JSON_ENCODE. | ||
*/ | ||
public static function json_echo($json, $options) | ||
{ | ||
// Scan array for any streams. | ||
$hasStream = array_reduce($json, array('BigData', 'hasStream'), false); | ||
|
||
// If there is no stream we are done. | ||
if (!$hasStream) | ||
{ | ||
echo json_encode($json, $options); | ||
} | ||
else | ||
{ | ||
self::json_echo_data($json, ($options & JSON_FORCE_OBJECT) == JSON_FORCE_OBJECT); | ||
} | ||
|
||
} | ||
|
||
protected static function hasStream(&$result, $item) | ||
{ | ||
if ($result === true) | ||
{ | ||
return true; | ||
} | ||
elseif(is_array($item)) | ||
{ | ||
return array_reduce($item, array('BigData', 'hasStream'), false); | ||
} | ||
// Should use get_resource_type to do stricter check. | ||
elseif (self::isStream($item)) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
|
||
protected static function isStream($item) | ||
{ | ||
return is_resource($item); | ||
} | ||
|
||
protected static function isAssociative($array) | ||
{ | ||
foreach ($array as $key => $value) | ||
{ | ||
if (is_string($key)) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
protected static function json_echo_data($json) | ||
{ | ||
if ((is_array($json) && self::isAssociative($json)) || is_object($json)) | ||
{ | ||
self::json_echo_object($json); | ||
} | ||
elseif (is_array($json)) | ||
{ | ||
self::json_echo_array($json); | ||
} | ||
elseif (is_numeric($json)) | ||
{ | ||
self::json_echo_number($json); | ||
} | ||
elseif (is_string($json)) | ||
{ | ||
self::json_echo_string($json); | ||
} | ||
elseif (self::isStream($json)) | ||
{ | ||
self::json_echo_stream($json); | ||
} | ||
} | ||
|
||
private static function json_echo_array($json) | ||
{ | ||
echo '['; | ||
foreach ($json as $key => $entry) | ||
{ | ||
echo json_encode($key) . ':'; | ||
self::json_echo_data($entry); | ||
echo ', '; // The extra comma is allowed: { 1: 'test', 2: 'test',} is valid. | ||
} | ||
echo ']'; | ||
} | ||
|
||
private static function json_echo_number($json) | ||
{ | ||
echo $json; | ||
} | ||
|
||
private static function json_echo_object($json) | ||
{ | ||
echo '{'; | ||
foreach ($json as $key => $entry) | ||
{ | ||
echo json_encode($key) . ':'; | ||
self::json_echo_data($entry); | ||
echo ', '; // The extra comma is allowed: { 1: 'test', 2: 'test',} is valid. | ||
} | ||
echo '}'; | ||
} | ||
|
||
private static function json_echo_string($json) | ||
{ | ||
echo json_encode($json); | ||
} | ||
|
||
private static function json_echo_stream($json) | ||
{ | ||
// Encode stream to base64. | ||
echo "'"; | ||
stream_filter_append($json, 'convert.base64-encode', STREAM_FILTER_READ, array('line-length' => 50, 'line-break-chars' => "\n")); | ||
fpassthru($json); | ||
echo "'"; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
Yii::import('application.libraries.BigData', true); | ||
class LSjsonRPCServer extends jsonRPCServer | ||
{ | ||
/** | ||
* This function handle a request binding it to a given object | ||
* | ||
* @param object $object | ||
* @return boolean | ||
*/ | ||
public static function handle($object) { | ||
|
||
// checks if a JSON-RCP request has been received | ||
if ( | ||
$_SERVER['REQUEST_METHOD'] != 'POST' || | ||
empty($_SERVER['CONTENT_TYPE']) || | ||
$_SERVER['CONTENT_TYPE'] != 'application/json' | ||
) { | ||
// This is not a JSON-RPC request | ||
return false; | ||
} | ||
|
||
// reads the input data | ||
$request = json_decode(file_get_contents('php://input'),true); | ||
|
||
// executes the task on local object | ||
try { | ||
if ($result = @call_user_func_array(array($object,$request['method']),$request['params'])) { | ||
$response = array ( | ||
'id' => $request['id'], | ||
'result' => $result, | ||
'error' => NULL | ||
); | ||
} else { | ||
$response = array ( | ||
'id' => $request['id'], | ||
'result' => NULL, | ||
'error' => 'unknown method or incorrect parameters' | ||
); | ||
} | ||
} catch (Exception $e) { | ||
$response = array ( | ||
'id' => $request['id'], | ||
'result' => NULL, | ||
'error' => $e->getMessage() | ||
); | ||
} | ||
|
||
// output the response | ||
if (!empty($request['id'])) { // notifications don't want response | ||
header('content-type: text/javascript'); | ||
BigData::json_echo($response); | ||
} | ||
|
||
// finish | ||
return true; | ||
} | ||
} | ||
?> |