--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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__ */
* limitations under the License.
*/
+#include <algorithm>
+#include <JobSchedulerTypesPrivate.h>
+#include <JobSchedulerService.h>
+#include "JobManager.h"
#include "MethodCallHandler.h"
using namespace ctx;
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));
+}