From 5fbb33354eced6edc0cd494af434d9a5a7c0379f Mon Sep 17 00:00:00 2001 From: micio Date: Tue, 6 Aug 2013 19:19:36 +0000 Subject: [PATCH] Examples/SkylarkUpload : use session variables instead of static maps to store progress data git-svn-id: svn://ultimatepp.org/upp/trunk@6231 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- examples/SkylarkUpload/Handlers.icpp | 47 +++++++++++++--------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/examples/SkylarkUpload/Handlers.icpp b/examples/SkylarkUpload/Handlers.icpp index 40f6ef8bd..e10a969ac 100644 --- a/examples/SkylarkUpload/Handlers.icpp +++ b/examples/SkylarkUpload/Handlers.icpp @@ -1,17 +1,11 @@ #include "SkylarkUpload.h" -struct UploadStat : Moveable -{ - int totalSize; - int currentSize; -}; - int ProgressHandler(int reason, Http &http, int size) { - static VectorMap stats; - // get the upload unique identifier - Value id = http["uploadid"]; + String id = http["uploadid"]; + String currentId = "." + id + ".currentSize"; + String totalId = "." + id + ".totalSize"; // must be reentrant INTERLOCKED { @@ -20,16 +14,20 @@ int ProgressHandler(int reason, Http &http, int size) // got headers ? case PROGRESS_HEADER: { - UploadStat &stat = stats.Add(id); - stat.totalSize = size; - stat.currentSize = 0; + http + .SessionSet(currentId, 0) + .SessionSet(totalId, size) + ; break; } // reading contents ? case PROGRESS_CONTENT: { - stats.Get(id).currentSize = size; + // avoid unnnedded session storing + int oldCurrent = http[currentId]; + if(oldCurrent != size) + http.SessionSet(currentId, size); break; } @@ -37,9 +35,7 @@ int ProgressHandler(int reason, Http &http, int size) case PROGRESS_END: { // signals end by resetting total size - // we can't remove the key here, progress query - // can happen after - stats.Get(id).totalSize = 0; + http.SessionSet(totalId, 0); break; } @@ -47,22 +43,23 @@ int ProgressHandler(int reason, Http &http, int size) default: // PROGRESS_QUERY { // check if key is there --> upload started - int i = stats.Find(id); - if(i >= 0) + int total = http[totalId]; + if(!IsNull(total)) { - // upload has started - UploadStat &stat = stats[i]; - - if(stat.totalSize) + if(total) { // if upload not ended, return the progress % // take care to NOT return 100% up to upload ended really - return min(99, (int)(100.0 * stat.currentSize / stat.totalSize)); + int current = http[currentId]; + return min(99, (int)(100.0 * current / total)); } else { - // upload ended, remove id from map and return 100% - stats.RemoveKey(id); + // upload ended, nullify session variables and return 100% + http + .SessionSet(currentId, Null) + .SessionSet(totalId, Null) + ; return 100; } }