Add custom context registration functions to support legacy context-trigger API 13/141213/4
authorMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 28 Jul 2017 14:14:10 +0000 (23:14 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 31 Jul 2017 02:47:42 +0000 (11:47 +0900)
Change-Id: Ie74076eec0f6a457125d5c6a385bf657303aa004
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/job_scheduler_internal.h
src/client-dummy/job_scheduler.cpp
src/client/JobManagerProxy.cpp
src/client/JobManagerProxy.h
src/client/job_scheduler.cpp
src/server/ContextPublisher.cpp
src/server/JobManager.cpp
src/server/JobManager.h
src/server/MethodCallHandler.cpp
src/server/MethodCallHandler.h
src/shared/JobSchedulerTypesPrivate.h

index 094f10b..b6b0d78 100644 (file)
@@ -506,6 +506,11 @@ int ctx_sched_publish_context_json(ctx_sched_h scheduler, const char* uri, const
 int ctx_sched_job_set_disjunction(ctx_sched_job_h job, bool disjunction);
 int ctx_sched_job_context_set_disjunction(ctx_sched_job_context_h job_context, bool disjunction);
 
+// Custom context registration functions to support legacy Context-Trigger API.
+int ctx_sched_custom_register(ctx_sched_h scheduler, const char* uri);
+int ctx_sched_custom_unregister(ctx_sched_h scheduler, const char* uri);
+int ctx_sched_custom_is_registered(ctx_sched_h scheduler, const char* uri, const char* pkg_id, bool* registered);
+
 
 /**
  * @}
index 53c373f..cf71d3f 100644 (file)
@@ -266,3 +266,18 @@ EXPORT_API int ctx_sched_job_context_set_disjunction(ctx_sched_job_context_h job
 {
        return E_SUPPORT;
 }
+
+EXPORT_API int ctx_sched_custom_register(ctx_sched_h scheduler, const char* uri)
+{
+       return E_SUPPORT;
+}
+
+EXPORT_API int ctx_sched_custom_unregister(ctx_sched_h scheduler, const char* uri)
+{
+       return E_SUPPORT;
+}
+
+EXPORT_API int ctx_sched_custom_is_registered(ctx_sched_h scheduler, const char* uri, const char* pkg_id, bool* registered)
+{
+       return E_SUPPORT;
+}
index 54200f0..49edf71 100644 (file)
@@ -180,6 +180,22 @@ int JobManagerProxy::publishContext(const std::string& uri, const std::string& p
        return __serviceProxy.call(METHOD_PUBLISH_CONTEXT, g_variant_new("(ss)", uri.c_str(), payload.c_str()));
 }
 
+int JobManagerProxy::registerCustom(const std::string& uri)
+{
+       return __serviceProxy.call(METHOD_REGISTER_CUSTOM, g_variant_new("(s)", uri.c_str()));
+}
+
+int JobManagerProxy::unregisterCustom(const std::string& uri)
+{
+       return __serviceProxy.call(METHOD_UNREGISTER_CUSTOM, g_variant_new("(s)", uri.c_str()));
+}
+
+bool JobManagerProxy::isRegisteredCustom(const std::string& uri, const std::string& pkgId)
+{
+       int error= __serviceProxy.call(METHOD_IS_REGISTERED_CUSTOM, g_variant_new("(ss)", uri.c_str(), pkgId.c_str()));
+       return IS_SUCCESS(error);
+}
+
 bool JobManagerProxy::__isAvailable(int type, const std::string& uri)
 {
        GVariant* param = g_variant_new("(is)", type, uri.c_str());
index d069a16..dee57f8 100644 (file)
@@ -50,6 +50,11 @@ namespace ctx {
 
                int publishContext(const std::string& uri, const std::string& payload);
 
+               // For supporting Context-Trigger APIs. These functions does not affect publishContext().
+               int registerCustom(const std::string& uri);
+               int unregisterCustom(const std::string& uri);
+               bool isRegisteredCustom(const std::string& uri, const std::string& pkgId);
+
        private:
                bool __isAvailable(int type, const std::string& uri);
                void __onStartJob(GVariant* param);
index ffe4cdf..fc58284 100644 (file)
@@ -666,3 +666,26 @@ EXPORT_API int ctx_sched_job_context_set_disjunction(ctx_sched_job_context_h job
 
        return E_NONE;
 }
+
+EXPORT_API int ctx_sched_custom_register(ctx_sched_h scheduler, const char* uri)
+{
+       IF_FAIL_RETURN(scheduler && uri, E_PARAM);
+
+       return scheduler->jobManager.registerCustom(uri);
+}
+
+EXPORT_API int ctx_sched_custom_unregister(ctx_sched_h scheduler, const char* uri)
+{
+       IF_FAIL_RETURN(scheduler && uri, E_PARAM);
+
+       return scheduler->jobManager.unregisterCustom(uri);
+}
+
+EXPORT_API int ctx_sched_custom_is_registered(ctx_sched_h scheduler, const char* uri, const char* pkg_id, bool* registered)
+{
+       IF_FAIL_RETURN(scheduler && uri && pkg_id && registered, E_PARAM);
+
+       *registered = scheduler->jobManager.isRegisteredCustom(uri, pkg_id);
+
+       return E_NONE;
+}
index 355dfb9..5adc1ef 100644 (file)
@@ -22,7 +22,7 @@
 #include "ContextPublisher.h"
 #include "publisher/CustomPublisher.h"
 
-#define CUSTOM_URI_REGEX       R"~(^http:\/\/[\w-\.]+\/context\/custom\/[\w-\._]+$)~"
+#define CUSTOM_URI_REGEX       R"~(^http:\/\/[\w-\.]+\/context\/custom\/[\w-\._\/]+$)~"
 
 using namespace ctx;
 
index f2974b3..c1e9a60 100644 (file)
@@ -283,6 +283,27 @@ std::vector<JobInfo*> JobManager::getAllJobInfo(IClient* owner)
        return jobInfos;
 }
 
+void JobManager::addCustom(const std::string& uri, const std::string& ownerId)
+{
+       std::string key = uri + "@" + ownerId;
+       _D("%s", key.c_str());
+       __registeredCustoms.insert(key);
+}
+
+void JobManager::removeCustom(const std::string& uri, const std::string& ownerId)
+{
+       std::string key = uri + "@" + ownerId;
+       _D("%s", key.c_str());
+       __registeredCustoms.erase(key);
+}
+
+bool JobManager::checkCustom(const std::string& uri, const std::string& ownerId)
+{
+       std::string key = uri + "@" + ownerId;
+       _D("%s", key.c_str());
+       return (__registeredCustoms.find(key) != __registeredCustoms.end());
+}
+
 JobInfo* JobManager::__getDuplicate(const std::string& ownerId, JobInfo* target)
 {
        for (auto* runner : __jobRunners) {
index 4926773..3ac2270 100644 (file)
@@ -18,6 +18,7 @@
 #define __CONTEXT_JOB_SCHEDULER_JOB_MANAGER_H__
 
 #include <list>
+#include <set>
 #include <JobInfo.h>
 #include <JobContext.h>
 #include <IClient.h>
@@ -49,6 +50,11 @@ namespace ctx {
                JobInfo* getJobInfo(int jobId, IClient* owner);
                std::vector<JobInfo*> getAllJobInfo(IClient* owner);
 
+               // For supporting Context-Trigger's custom context registration APIs
+               void addCustom(const std::string& uri, const std::string& ownerId);
+               void removeCustom(const std::string& uri, const std::string& ownerId);
+               bool checkCustom(const std::string& uri, const std::string& ownerId);
+
        private:
                int __addPeriodicJob(PeriodicJobInfo* jobInfo, IClient* owner);
                int __addOnDemandJob(OnDemandJobInfo* jobInfo, IClient* owner);
@@ -75,6 +81,9 @@ namespace ctx {
                std::list<JobRunner*> __jobRunners;
 
                JobInfoDatabase __jobInfoDatabase;
+
+               // For supporting Context-Trigger's custom context registration APIs
+               std::set<std::string> __registeredCustoms;
        };
 
 }
index eeebeea..7842a4e 100644 (file)
@@ -67,6 +67,15 @@ void MethodCallHandler::onMethodCalled(IMethodCall* methodCall)
 
                } else if (methodCall->getMethodName() == METHOD_PUBLISH_CONTEXT) {
                        __publishContext(*methodCall);
+
+               } else if (methodCall->getMethodName() == METHOD_REGISTER_CUSTOM) {
+                       __registerCustom(*methodCall);
+
+               } else if (methodCall->getMethodName() == METHOD_UNREGISTER_CUSTOM) {
+                       __unregisterCustom(*methodCall);
+
+               } else if (methodCall->getMethodName() == METHOD_IS_REGISTERED_CUSTOM) {
+                       __isRegisteredCustom(*methodCall);
                }
        } catch (const int error) {
                _W("Catch: %s", CTX_ERROR_STR(error));
@@ -259,3 +268,43 @@ void MethodCallHandler::__publishContext(IMethodCall& methodCall)
 
        pubs->putData(data);
 }
+
+void MethodCallHandler::__registerCustom(IMethodCall& methodCall)
+{
+       const char* uri = NULL;
+
+       g_variant_get(methodCall.getParam(), "(&s)", &uri);
+       IF_FAIL_THROW(uri, E_PARAM);
+       IF_FAIL_THROW(ContextManager::isCustomUri(uri), E_PARAM);
+
+       __getJobManager().addCustom(uri, __caller->getName());
+
+       methodCall.reply(E_NONE);
+}
+
+void MethodCallHandler::__unregisterCustom(IMethodCall& methodCall)
+{
+       const char* uri = NULL;
+
+       g_variant_get(methodCall.getParam(), "(&s)", &uri);
+       IF_FAIL_THROW(uri, E_PARAM);
+       IF_FAIL_THROW(ContextManager::isCustomUri(uri), E_PARAM);
+
+       __getJobManager().removeCustom(uri, __caller->getName());
+
+       methodCall.reply(E_NONE);
+}
+
+void MethodCallHandler::__isRegisteredCustom(IMethodCall& methodCall)
+{
+       const char* uri = NULL;
+       const char* pkgId = NULL;
+
+       g_variant_get(methodCall.getParam(), "(&s&s)", &uri, &pkgId);
+       IF_FAIL_THROW(uri && pkgId, E_PARAM);
+       IF_FAIL_THROW(ContextManager::isCustomUri(uri), E_PARAM);
+
+       bool exist = __getJobManager().checkCustom(uri, pkgId);
+
+       methodCall.reply(exist ? E_NONE : E_NO_DATA);
+}
index e243203..e439825 100644 (file)
@@ -37,16 +37,24 @@ namespace ctx {
                JobManager& __getJobManager();
 
                void __isSupported(IMethodCall& methodCall);
+
                void __addJob(IMethodCall& methodCall);
                void __startJob(IMethodCall& methodCall);
                void __stopJob(IMethodCall& methodCall);
                void __removeJob(IMethodCall& methodCall);
+
                void __jobFinished(IMethodCall& methodCall);
+
                void __getJob(IMethodCall& methodCall);
                void __getAllJob(IMethodCall& methodCall);
+
                void __enableLifecycleCallbacks(IMethodCall& methodCall);
                void __publishContext(IMethodCall& methodCall);
 
+               void __registerCustom(IMethodCall& methodCall);
+               void __unregisterCustom(IMethodCall& methodCall);
+               void __isRegisteredCustom(IMethodCall& methodCall);
+
                IClient* __caller;
                bool __callbackEnabled;
        };
index 34e6b69..70c3f24 100644 (file)
        "<method name='" METHOD_PUBLISH_CONTEXT "'>" \
        "       <arg type='s' name='uri' direction='in'/>" \
        "       <arg type='s' name='payload' direction='in'/>" \
+       "</method>" \
+       "<method name='" METHOD_REGISTER_CUSTOM "'>" \
+       "       <arg type='s' name='uri' direction='in'/>" \
+       "</method>" \
+       "<method name='" METHOD_UNREGISTER_CUSTOM "'>" \
+       "       <arg type='s' name='uri' direction='in'/>" \
+       "</method>" \
+       "<method name='" METHOD_IS_REGISTERED_CUSTOM "'>" \
+       "       <arg type='s' name='uri' direction='in'/>" \
+       "       <arg type='s' name='pkgId' direction='in'/>" \
        "</method>"
 
 #define METHOD_ADD_JOB                 "AddJob"
@@ -65,6 +75,9 @@
 #define METHOD_IS_SUPPORTED            "IsSupported"
 #define METHOD_ENABLE_CB               "EnableCb"
 #define METHOD_PUBLISH_CONTEXT "PublishContext"
+#define METHOD_REGISTER_CUSTOM         "RegisterCustom"
+#define METHOD_UNREGISTER_CUSTOM       "UnregisterCustom"
+#define METHOD_IS_REGISTERED_CUSTOM    "IsRegisteredCustom"
 
 #define SIGNAL_START_JOB               "JobStarted"
 #define SIGNAL_STOP_JOB                        "JobToBeStopped"