Implement MethodCallHandler 11/136911/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 3 Jul 2017 11:33:34 +0000 (20:33 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 4 Jul 2017 04:54:51 +0000 (13:54 +0900)
Change-Id: I0d8780047a66d415ed72cbce4558529497651e63
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/private/JobSchedulerService.h
src/server/JobManager.cpp [new file with mode: 0644]
src/server/JobManager.h [new file with mode: 0644]
src/server/JobSchedulerService.cpp
src/server/MethodCallHandler.cpp
src/server/MethodCallHandler.h

index 57688340573d64ab88ef221dbd5c5b299dbe1970..14eb710b4679621f1f34722474a86734b632bd76 100644 (file)
@@ -24,6 +24,8 @@
 
 namespace ctx {
 
+       class JobManager;
+
        class EXPORT_API JobSchedulerService : public ISystemService {
        public:
                JobSchedulerService();
@@ -41,8 +43,12 @@ namespace ctx {
                void onUserActivated(uid_t uid);
                void onUserDeactivated(uid_t uid);
 
+               // Own members
+               JobManager* getJobManager(uid_t uid);
+
        private:
                IServiceRunner* __serviceRunner;
+               JobManager* __jobManager;
        };
 
 }
diff --git a/src/server/JobManager.cpp b/src/server/JobManager.cpp
new file mode 100644 (file)
index 0000000..262b4df
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JobManager.h"
+
+using namespace ctx;
+
+JobManager::JobManager(uid_t uid)
+{
+}
+
+JobManager::~JobManager()
+{
+}
+
+bool JobManager::isSupported(JobContext::Type type, const std::string& uri)
+{
+       //TODO
+       return false;
+}
+
+int JobManager::addJob(JobInfo* jobInfo)
+{
+       //TODO
+       delete jobInfo;
+       return E_SUPPORT;
+}
+
+int JobManager::startJob(int jobId)
+{
+       //TODO
+       return E_SUPPORT;
+}
+
+int JobManager::stopJob(int jobId)
+{
+       //TODO
+       return E_SUPPORT;
+}
+
+int JobManager::removeJob(int jobId)
+{
+       //TODO
+       return E_SUPPORT;
+}
+
+JobInfo* JobManager::getJob(int jobId)
+{
+       //TODO
+       return NULL;
+}
+
+std::vector<JobInfo*> JobManager::getAllJob()
+{
+       std::vector<JobInfo*> jobInfos;
+       //TODO
+       return jobInfos;
+}
diff --git a/src/server/JobManager.h b/src/server/JobManager.h
new file mode 100644 (file)
index 0000000..f4de4aa
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_JOB_SCHEDULER_JOB_MANAGER_H__
+#define __CONTEXT_JOB_SCHEDULER_JOB_MANAGER_H__
+
+#include <vector>
+#include <JobInfo.h>
+#include <JobContext.h>
+
+namespace ctx {
+
+       class JobManager {
+       public:
+               JobManager(uid_t uid);
+               ~JobManager();
+
+               bool isSupported(JobContext::Type type, const std::string& uri);
+
+               int addJob(JobInfo* jobInfo);
+               int startJob(int jobId);
+               int stopJob(int jobId);
+               int removeJob(int jobId);
+
+               JobInfo* getJob(int jobId);
+               std::vector<JobInfo*> getAllJob();
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_JOB_MANAGER_H__ */
index 9adb409715bf3e101dbc3917fbf7c89a6a23d8fa..a623dee0ec789059977e8b8ed03959a7f359090a 100644 (file)
 
 #include <JobSchedulerTypesPrivate.h>
 #include <JobSchedulerService.h>
+#include "JobManager.h"
 #include "MethodCallHandler.h"
 
 using namespace ctx;
 
 JobSchedulerService::JobSchedulerService() :
-       __serviceRunner(NULL)
+       __serviceRunner(NULL),
+       __jobManager(NULL)
 {
 }
 
@@ -64,3 +66,9 @@ void JobSchedulerService::onUserActivated(uid_t uid)
 void JobSchedulerService::onUserDeactivated(uid_t uid)
 {
 }
+
+JobManager* JobSchedulerService::getJobManager(uid_t uid)
+{
+       // TODO
+       return NULL;
+}
index eee8dcdd1a5d9d946cfad875745f3337bf1197a0..a4d95a68b1a13783b1f09fa044fc100a50e7311e 100644 (file)
  * limitations under the License.
  */
 
+#include <algorithm>
+#include <JobSchedulerTypesPrivate.h>
+#include <JobSchedulerService.h>
+#include "JobManager.h"
 #include "MethodCallHandler.h"
 
 using namespace ctx;
@@ -29,9 +33,152 @@ MethodCallHandler::~MethodCallHandler()
 
 void MethodCallHandler::onMethodCalled(IMethodCall* methodCall)
 {
+       try {
+               if (methodCall->getMethodName() == METHOD_IS_SUPPORTED) {
+                       __isSupported(*methodCall);
+
+               } else if (methodCall->getMethodName() == METHOD_ADD_JOB) {
+                       __addJob(*methodCall);
+
+               } else if (methodCall->getMethodName() == METHOD_START_JOB) {
+                       __startJob(*methodCall);
+
+               } else if (methodCall->getMethodName() == METHOD_STOP_JOB) {
+                       __stopJob(*methodCall);
+
+               } else if (methodCall->getMethodName() == METHOD_REMOVE_JOB) {
+                       __removeJob(*methodCall);
+
+               } else if (methodCall->getMethodName() == METHOD_GET_JOB) {
+                       __getJob(*methodCall);
+
+               } else if (methodCall->getMethodName() == METHOD_GET_ALL_JOB) {
+                       __getAllJob(*methodCall);
+               }
+       } catch (const int error) {
+               _W("Catch: %s", CTX_ERROR_STR(error));
+               methodCall->reply(error);
+       }
+
        delete methodCall;
 }
 
 void MethodCallHandler::onDisconnected()
 {
 }
+
+JobManager& MethodCallHandler::__getJobManager()
+{
+       JobSchedulerService* svc = static_cast<JobSchedulerService*>(__caller->getHostService());
+       JobManager* mgr = svc->getJobManager(__caller->getUid());
+       IF_FAIL_THROW(mgr, static_cast<int>(E_ACCESS));
+
+       return *mgr;
+}
+
+void MethodCallHandler::__isSupported(IMethodCall& methodCall)
+{
+       int type = 0;
+       const char* uri = NULL;
+       g_variant_get(methodCall.getParam(), "(i&s)", &type, &uri);
+
+       _D("Checking %s(%d)", uri, type);
+
+       bool supported = __getJobManager().isSupported(static_cast<JobContext::Type>(type), uri);
+       IF_FAIL_THROW(supported, static_cast<int>(E_SUPPORT));
+
+       methodCall.reply(E_NONE);
+}
+
+void MethodCallHandler::__addJob(IMethodCall& methodCall)
+{
+       const char* jobInfoStr = NULL;
+       g_variant_get(methodCall.getParam(), "(&s)", &jobInfoStr);
+
+       _SI("Adding %s", jobInfoStr);
+
+       JobManager& manager = __getJobManager();
+       JobInfo* jobInfo = JobInfo::deserialize(jobInfoStr);
+       IF_FAIL_THROW_TAG(jobInfo, static_cast<int>(E_PARAM), _E, "JobInfo deserialization failed");
+
+       int jobId = manager.addJob(jobInfo);
+       IF_FAIL_THROW(jobId > 0, jobId);
+
+       _I("Added Job-%d", jobId);
+
+       methodCall.reply(g_variant_new("(i)", jobId));
+}
+
+void MethodCallHandler::__startJob(IMethodCall& methodCall)
+{
+       int jobId = 0;
+       g_variant_get(methodCall.getParam(), "(i)", &jobId);
+
+       _I("Starting Job-%d", jobId);
+
+       int err = __getJobManager().startJob(jobId);
+       IF_FAIL_THROW(IS_SUCCESS(err), err);
+
+       methodCall.reply(E_NONE);
+}
+
+void MethodCallHandler::__stopJob(IMethodCall& methodCall)
+{
+       int jobId = 0;
+       g_variant_get(methodCall.getParam(), "(i)", &jobId);
+
+       _I("Stopping Job-%d", jobId);
+
+       int err = __getJobManager().stopJob(jobId);
+       IF_FAIL_THROW(IS_SUCCESS(err), err);
+
+       methodCall.reply(E_NONE);
+}
+
+void MethodCallHandler::__removeJob(IMethodCall& methodCall)
+{
+       int jobId = 0;
+       g_variant_get(methodCall.getParam(), "(i)", &jobId);
+
+       _I("Removing Job-%d", jobId);
+
+       int err = __getJobManager().removeJob(jobId);
+       IF_FAIL_THROW(IS_SUCCESS(err), err);
+
+       methodCall.reply(E_NONE);
+}
+
+void MethodCallHandler::__getJob(IMethodCall& methodCall)
+{
+       int jobId = 0;
+       g_variant_get(methodCall.getParam(), "(i)", &jobId);
+
+       _I("Retrieving Job-%d", jobId);
+
+       JobInfo* jobInfo = __getJobManager().getJob(jobId);
+       IF_FAIL_THROW(jobInfo, static_cast<int>(E_PARAM));
+
+       methodCall.reply(g_variant_new("(s)", jobInfo->serialize().c_str()));
+
+       delete jobInfo;
+}
+
+void MethodCallHandler::__getAllJob(IMethodCall& methodCall)
+{
+       _I("Retrieving all jobs");
+
+       std::vector<JobInfo*> jobInfos = __getJobManager().getAllJob();
+       IF_FAIL_THROW(!jobInfos.empty(), static_cast<int>(E_NO_DATA));
+
+       GVariantBuilder builder;
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
+
+       for (auto& info : jobInfos) {
+               g_variant_builder_add(&builder, "s", info->serialize().c_str());
+               delete info;
+       }
+
+       GVariant* jobInfoArr = g_variant_builder_end(&builder);
+
+       methodCall.reply(g_variant_new_tuple(&jobInfoArr, 1));
+}
index ba2b5e875729be9a35f503f2e1237427c9748325..6f759bf349e0afb9ec1cdaa570839a273f4ae325 100644 (file)
@@ -23,7 +23,7 @@
 
 namespace ctx {
 
-       class SensorProvider;
+       class JobManager;
 
        class MethodCallHandler : public IMethodCallHandler {
        public:
@@ -34,6 +34,16 @@ namespace ctx {
                void onDisconnected();
 
        private:
+               JobManager& __getJobManager();
+
+               void __isSupported(IMethodCall& methodCall);
+               void __addJob(IMethodCall& methodCall);
+               void __startJob(IMethodCall& methodCall);
+               void __stopJob(IMethodCall& methodCall);
+               void __removeJob(IMethodCall& methodCall);
+               void __getJob(IMethodCall& methodCall);
+               void __getAllJob(IMethodCall& methodCall);
+
                IClient* __caller;
        };