A job's user data accepts a byte string with its length. 93/139193/3
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 18 Jul 2017 01:58:42 +0000 (10:58 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 18 Jul 2017 02:35:56 +0000 (11:35 +0900)
It does not need to be a NULL-terminate string anymore.

Change-Id: I52f99a2fbe025fd7af2dbc3c2fba7985c6984a82
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/job_scheduler_internal.h
src/client-dummy/job_scheduler.cpp
src/client/job_scheduler.cpp
src/shared/JobInfo.cpp
src/shared/JobInfo.h

index 32b6c051935110d498a3c369e321736c2e18b004..714a136ef5aa0c0aa466873f4639dc4d3deffb06 100644 (file)
@@ -52,10 +52,10 @@ typedef struct _ctx_sched_job_context_s* ctx_sched_job_context_h;
  * @remarks    @c job should be destroyed using ctx_sched_job_destroy().
  * @param[in]  scheduler       TBD
  * @param[in]  job                     TBD
- * @param[in]  user_data       TBD
+ * @param[in]  cb_data         TBD
  * @return     @c true to continue to iterate, otherwise @c false.
  */
-typedef bool (*ctx_sched_foreach_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, void* user_data);
+typedef bool (*ctx_sched_foreach_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, void* cb_data);
 
 /**
  * @brief      Called when a job has been executed.
@@ -65,9 +65,9 @@ typedef bool (*ctx_sched_foreach_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h
  * @param[in]  scheduler       TBD
  * @param[in]  job                     TBD
  * @param[in]  timeout         @c true if only the mandatory requirements are satisfied; @c false otherwise.
- * @param[in]  user_data       TBD
+ * @param[in]  cb_data         TBD
  */
-typedef void (*ctx_sched_start_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, bool timeout, void* user_data);
+typedef void (*ctx_sched_start_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, bool timeout, void* cb_data);
 
 /**
  * @brief      Called when a job needs to be stopped.
@@ -76,9 +76,9 @@ typedef void (*ctx_sched_start_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h jo
  * @remarks    @c job should be destroyed using ctx_sched_job_destroy().
  * @param[in]  scheduler       TBD
  * @param[in]  job                     TBD
- * @param[in]  user_data       TBD
+ * @param[in]  cb_data         TBD
  */
-typedef void (*ctx_sched_stop_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, void* user_data);
+typedef void (*ctx_sched_stop_job_cb)(ctx_sched_h scheduler, ctx_sched_job_h job, void* cb_data);
 
 
 /**
@@ -181,10 +181,10 @@ int ctx_sched_get_job(ctx_sched_h scheduler, int job_id, ctx_sched_job_h* job);
  * @brief      Gets all job information handles. Synchronous.
  * @param[in]  scheduler       TBD
  * @param[in]  callback        TBD
- * @param[in]  user_data       TBD
+ * @param[in]  cb_data         TBD
  * @return     @c 0 on success, otherwise a negative error value
  */
-int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* user_data);
+int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* cb_data);
 
 
 /**
@@ -192,10 +192,10 @@ int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callba
  * @param[in]  scheduler       TBD
  * @param[in]  start_cb        TBD
  * @param[in]  stop_cb         TBD
- * @param[in]  user_data       TBD
+ * @param[in]  cb_data         TBD
  * @return     @c 0 on success, otherwise a negative error value
  */
-int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* user_data);
+int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* cb_data);
 
 
 /**
@@ -289,15 +289,12 @@ int ctx_sched_job_set_dbus(ctx_sched_job_h job, const char* bus_name,
 
 /**
  * @brief      Attaches user data that can be retrieved using ctx_sched_job_get_user_data().
- * @details    A Null-terminate string can be attached to a job. The attached string can be accessed
- *                     using ctx_sched_job_get_user_data(), after acquiring a job handle using ctx_sched_get_job().
- *                     Note that, the string should be a valid Json data string, i.e., containing no double-quotes
- *                     and other Json special characters.
  * @param[in]  job                     Job handle
  * @param[in]  user_data       User data
+ * @param[in]  length          Length of the data
  * @return     @c 0 on success, otherwise a negative error value
  */
-int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data);
+int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data, size_t length);
 
 
 /**
@@ -318,13 +315,14 @@ int ctx_sched_job_get_id(ctx_sched_job_h job, int* job_id);
 
 /**
  * @brief      Gets the user data string attached to a job
- * @details    This function returns a pointer to the string contained in the @c job.
- *                     The @c job should be not freed unless you do not reference the @c user_data anymore.
+ * @details    If the job contains no user data, the @c user_data and the @c length are set to @c NULL and 0 respectively.
+ * @remarks    If the @c user_data is not @c NULL, it should be freed using @c free().
  * @param[in]  job                     Job handle
  * @param[out] user_data       User data string
+ * @param[out] length          Length of the data
  * @return     @c 0 on success, otherwise a negative error value
  */
-int ctx_sched_job_get_user_data(ctx_sched_job_h job, const char** user_data);
+int ctx_sched_job_get_user_data(ctx_sched_job_h job, char** user_data, size_t* length);
 
 
 /**
index e2a583b8647ebf4f1f42a9352311c0c0500800bd..1aa7a3bb01e081b36a84b78736e8d97a56fc55de 100644 (file)
@@ -84,12 +84,12 @@ EXPORT_API int ctx_sched_get_job(ctx_sched_h scheduler, int job_id, ctx_sched_jo
        return E_SUPPORT;
 }
 
-EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* user_data)
+EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* cb_data)
 {
        return E_SUPPORT;
 }
 
-EXPORT_API int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* user_data)
+EXPORT_API int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* cb_data)
 {
        return E_SUPPORT;
 }
@@ -146,7 +146,7 @@ EXPORT_API int ctx_sched_job_set_dbus(ctx_sched_job_h job,
        return E_SUPPORT;
 }
 
-EXPORT_API int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data)
+EXPORT_API int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data, size_t length)
 {
        return E_SUPPORT;
 }
@@ -161,7 +161,7 @@ EXPORT_API int ctx_sched_job_get_id(ctx_sched_job_h job, int* job_id)
        return E_SUPPORT;
 }
 
-EXPORT_API int ctx_sched_job_get_user_data(ctx_sched_job_h job, const char** user_data)
+EXPORT_API int ctx_sched_job_get_user_data(ctx_sched_job_h job, char** user_data, size_t* length)
 {
        return E_SUPPORT;
 }
index 967323ade6956d3c57701e8eb273d87fa438fac0..2dcab3a6e75f325529acad31d727922ac02c84dc 100644 (file)
@@ -32,10 +32,10 @@ typedef struct _ctx_sched_s {
        JobManagerProxy jobManager;
        ctx_sched_start_job_cb startJobCb;
        ctx_sched_stop_job_cb stopJobCb;
-       void* userData;
+       void* cbData;
 
        _ctx_sched_s() :
-               startJobCb(NULL), stopJobCb(NULL), userData(NULL) {}
+               startJobCb(NULL), stopJobCb(NULL), cbData(NULL) {}
 } ctx_sched_s;
 
 
@@ -107,7 +107,7 @@ EXPORT_API int ctx_sched_cancel(ctx_sched_h scheduler, int job_id)
        return ctx_sched_remove_job(scheduler, job_id);
 }
 
-static bool __cancelJob(ctx_sched_h scheduler, ctx_sched_job_h job, void* user_data)
+static bool __cancelJob(ctx_sched_h scheduler, ctx_sched_job_h job, void* cb_data)
 {
        int jid = -1;
 
@@ -183,7 +183,7 @@ EXPORT_API int ctx_sched_get_job(ctx_sched_h scheduler, int job_id, ctx_sched_jo
        return E_NONE;
 }
 
-EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* user_data)
+EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_job_cb callback, void* cb_data)
 {
        IF_FAIL_RETURN(scheduler && callback, E_PARAM);
 
@@ -206,7 +206,7 @@ EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_jo
                jobHandle->jobInfo = jobInfo;
                jobInfo = NULL;
 
-               if (!callback(scheduler, jobHandle, user_data)) {
+               if (!callback(scheduler, jobHandle, cb_data)) {
                        std::for_each(jobInfos.begin(), jobInfos.end(), [](JobInfo*& j){ delete j; });
                        break;
                }
@@ -215,9 +215,9 @@ EXPORT_API int ctx_sched_foreach_job(ctx_sched_h scheduler, ctx_sched_foreach_jo
        return E_NONE;
 }
 
-static void __startJob(JobInfo* jobInfo, bool timeout, void* userData)
+static void __startJob(JobInfo* jobInfo, bool timeout, void* cbData)
 {
-       ctx_sched_s* scheduler = static_cast<ctx_sched_s*>(userData);
+       ctx_sched_s* scheduler = static_cast<ctx_sched_s*>(cbData);
        ctx_sched_job_s* job = new(std::nothrow) ctx_sched_job_s();
 
        if (!job) {
@@ -227,12 +227,12 @@ static void __startJob(JobInfo* jobInfo, bool timeout, void* userData)
        }
 
        job->jobInfo = jobInfo;
-       scheduler->startJobCb(scheduler, job, timeout, scheduler->userData);
+       scheduler->startJobCb(scheduler, job, timeout, scheduler->cbData);
 }
 
-static void __stopJob(JobInfo* jobInfo, void* userData)
+static void __stopJob(JobInfo* jobInfo, void* cbData)
 {
-       ctx_sched_s* scheduler = static_cast<ctx_sched_s*>(userData);
+       ctx_sched_s* scheduler = static_cast<ctx_sched_s*>(cbData);
        ctx_sched_job_s* job = new(std::nothrow) ctx_sched_job_s();
 
        if (!job) {
@@ -242,16 +242,16 @@ static void __stopJob(JobInfo* jobInfo, void* userData)
        }
 
        job->jobInfo = jobInfo;
-       scheduler->stopJobCb(scheduler, job, scheduler->userData);
+       scheduler->stopJobCb(scheduler, job, scheduler->cbData);
 }
 
-EXPORT_API int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* user_data)
+EXPORT_API int ctx_sched_set_job_cb(ctx_sched_h scheduler, ctx_sched_start_job_cb start_cb, ctx_sched_stop_job_cb stop_cb, void* cb_data)
 {
        IF_FAIL_RETURN(scheduler && start_cb && stop_cb, E_PARAM);
 
        scheduler->startJobCb = start_cb;
        scheduler->stopJobCb = stop_cb;
-       scheduler->userData = user_data;
+       scheduler->cbData = cb_data;
        scheduler->jobManager.setJobCb(__startJob, __stopJob, scheduler);
 
        return E_NONE;
@@ -384,11 +384,11 @@ EXPORT_API int ctx_sched_job_set_dbus(ctx_sched_job_h job,
        return E_NONE;
 }
 
-EXPORT_API int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data)
+EXPORT_API int ctx_sched_job_set_user_data(ctx_sched_job_h job, const char* user_data, size_t length)
 {
-       IF_FAIL_RETURN(job && user_data, E_PARAM);
+       IF_FAIL_RETURN(job && user_data && length > 0, E_PARAM);
 
-       job->jobInfo->setUserData(user_data);
+       job->jobInfo->setUserData(user_data, length);
 
        return E_NONE;
 }
@@ -411,11 +411,11 @@ EXPORT_API int ctx_sched_job_get_id(ctx_sched_job_h job, int* job_id)
        return E_NONE;
 }
 
-EXPORT_API int ctx_sched_job_get_user_data(ctx_sched_job_h job, const char** user_data)
+EXPORT_API int ctx_sched_job_get_user_data(ctx_sched_job_h job, char** user_data, size_t* length)
 {
-       IF_FAIL_RETURN(job && user_data, E_PARAM);
+       IF_FAIL_RETURN(job && user_data && length, E_PARAM);
 
-       *user_data = job->jobInfo->getUserData().c_str();
+       *length = job->jobInfo->getUserData(user_data);
 
        return E_NONE;
 }
index 5eaf8f33ff0ca766d5e6495f2210ee09655b41b9..e7b6a0b095799430357152fed6b9101a00356ac5 100644 (file)
@@ -117,9 +117,17 @@ JobAction* JobInfo::getAction()
        return __action;
 }
 
-const std::string& JobInfo::getUserData() const
+size_t JobInfo::getUserData(char** data) const
 {
-       return __userData;
+       if (__userData.empty()) {
+               *data = NULL;
+               return 0;
+       }
+
+       gsize length = 0;
+       *data  = reinterpret_cast<char*>(g_base64_decode(__userData.c_str(), &length));
+
+       return length;
 }
 
 JobInfo& JobInfo::setId(int jobId)
@@ -170,9 +178,23 @@ JobInfo& JobInfo::setAction(JobAction* action)
        return *this;
 }
 
-JobInfo& JobInfo::setUserData(const std::string& userData)
+JobInfo& JobInfo::setUserData(const char* data, size_t length)
 {
-       __userData = userData;
+       if (data == NULL || length == 0) {
+               __userData.clear();
+               return *this;
+       }
+
+       char* encoded = g_base64_encode(reinterpret_cast<const guchar*>(data), length);
+
+       if (encoded == NULL) {
+               _W("User data encoding failed");
+               return *this;
+       }
+
+       __userData.assign(encoded).shrink_to_fit();
+       g_free(encoded);
+
        return *this;
 }
 
index 812e8ea4c27bf65290da9a9bc80d372a0137f961..ab70e9473172bdd2f138f83973346e3527fa0f8e 100644 (file)
@@ -54,7 +54,7 @@ namespace ctx {
 
                JobAction* getAction();
 
-               const std::string& getUserData() const;
+               size_t getUserData(char** data) const;
 
                JobInfo& setId(int jobId);
 
@@ -67,8 +67,7 @@ namespace ctx {
 
                JobInfo& setAction(JobAction* action);
 
-               // UserData should be a string that can be a string element of Json
-               JobInfo& setUserData(const std::string& userData);
+               JobInfo& setUserData(const char* data, size_t length);
 
                std::string serialize() const;;
                void toJson(Json::Value& jsonRoot) const;