diff options
author | Christian Klinger | 2016-06-08 15:47:33 +0200 |
---|---|---|
committer | Christian Klinger | 2016-06-08 15:47:33 +0200 |
commit | 506fe169abcd603d6e0804e90b16c5c30b1ee20e (patch) | |
tree | 7ff60abf3504949bf88bff88f32df23604487946 /modules-available/dozmod | |
parent | first version of the dozmod proxy (without caching). (diff) | |
download | slx-admin-506fe169abcd603d6e0804e90b16c5c30b1ee20e.tar.gz slx-admin-506fe169abcd603d6e0804e90b16c5c30b1ee20e.tar.xz slx-admin-506fe169abcd603d6e0804e90b16c5c30b1ee20e.zip |
added a caching api proxy (that also validates the client IPs) for dozmod.
Diffstat (limited to 'modules-available/dozmod')
-rw-r--r-- | modules-available/dozmod/api.inc.php | 84 |
1 files changed, 79 insertions, 5 deletions
diff --git a/modules-available/dozmod/api.inc.php b/modules-available/dozmod/api.inc.php index f5525ef8..569b60a8 100644 --- a/modules-available/dozmod/api.inc.php +++ b/modules-available/dozmod/api.inc.php @@ -1,11 +1,64 @@ <?php -/* Hier kommt der Dozmod proxy hin */ +/* small API server that acts as a proxy to the dozmod server. + * To reduce the number of requests and connections to dozmod-server, results + * gets cached into a file cache. + * + * Required Configuration: + * CONFIG_DOZMOD_EXPIRE: Expiration time in seconds for the cache + * CONFIG_DOZMOD: URL to the dozmod server + * + **/ + require 'modules/locations/inc/location.inc.php'; + define('LIST_URL', CONFIG_DOZMOD . '/vmchooser/list'); define('VMX_URL', CONFIG_DOZMOD . '/vmchooser/lecture'); $availableRessources = ['vmx', 'test', 'netrules']; +/* BEGIN: A simple caching mechanism ---------------------------- */ + +function cache_hash($obj) { + return md5(serialize($obj)); +} + +function cache_key_to_filename($key) { + return "/tmp/bwlp-slxadmin-cache-$key"; // TODO: hash +} + +function cache_put($key, $value) { + $filename = cache_key_to_filename($key); + file_put_contents($filename, $value); +} + +function cache_has($key) { + $filename = cache_key_to_filename($key); + $mtime = filemtime($filename); + + if (!$mtime) { + return false; // cache miss + } + if ( time() - $mtime > CONFIG_DOZMOD_EXPIRE) { + return false; + } else { + return true; + } +} + + +function cache_get($key) { + $filename = cache_key_to_filename($key); + return file_get_contents($filename); +} + +/* good for large binary files */ +function cache_get_passthru($key) { + $filename = cache_key_to_filename($key); + $fp = fopen($filename, "r"); + fpassthru($fp); +} +/* END: Cache ---------------------------------------------------- */ + /* this script requires 2 (3 with implicit client ip) parameters * @@ -18,9 +71,8 @@ function println($str) { echo "$str\n"; } /* return an array of lecutre uuids. * Parameter: an array with location Ids - * Cacheable * */ -function getLecturesForLocations($locationIds) { +function _getLecturesForLocations($locationIds) { $ids = implode('%20', $locationIds); $url = LIST_URL . "?locations=$ids"; $responseXML = Download::asString($url, 60, $code); @@ -32,13 +84,36 @@ function getLecturesForLocations($locationIds) { } return $uuids; } +/** Caching wrapper around _getLecturesForLocations() *// +function getLecturesForLocations($locationIds) { + $key = 'lectures_' . cache_hash($locationIds); + if (cache_has($key)) { + return unserialize(cache_get($key)); + } else { + $value = _getLecturesForLocations($locationIds); + cache_put($key, serialize($value)); + return $value; + } +} -function getVMX($lecture_uuid) { +function _getVMX($lecture_uuid) { $url = VMX_URL . '/' . $lecture_uuid; $response = Download::asString($url, 60, $code); return $response; } +/** Caching wrapper around _getVMX() **/ +function getVMX($lecture_uuid) { + $key = 'vmx_' . $lecture_uuid; + if (cache_has($key)) { + cache_get_passthru($key); + } else { + $value = _getVMX($lecture_uuid); + cache_put($key, $value); + return $value; + } +} + // -----------------------------------------------------------------------------// $ip = $_SERVER['REMOTE_ADDR']; @@ -58,7 +133,6 @@ $location_ids = Location::getFromIP($request['ip']); /* lookup lecture uuids */ $lectures = getLecturesForLocations(array($location_ids)); - /* validate request -------------------------------------------- */ /* check ressources */ if (!in_array($request['ressource'], $availableRessources)) { |