Define ContextPublisher and IContextObserver 10/140110/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Jul 2017 02:26:00 +0000 (11:26 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Jul 2017 04:33:06 +0000 (13:33 +0900)
Change-Id: I0a1aa5c59daab5c12c56c97f24099a834aa1fc5b
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/server/ContextPublisher.cpp [new file with mode: 0644]
src/server/ContextPublisher.h [new file with mode: 0644]
src/server/IContextObserver.h [new file with mode: 0644]

diff --git a/src/server/ContextPublisher.cpp b/src/server/ContextPublisher.cpp
new file mode 100644 (file)
index 0000000..b2f0037
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * 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 <vector>
+#include <ContextTypes.h>
+#include <SharedUtil.h>
+#include "IContextObserver.h"
+#include "ContextPublisher.h"
+
+using namespace ctx;
+
+static std::vector<std::pair<const char*, ContextPublisher* (*)(uid_t)>> __creators;
+
+void ContextPublisher::registerPublisher(const char* uri, ContextPublisher* (*creator)(uid_t))
+{
+       __creators.emplace_back(uri, creator);
+}
+
+ContextPublisher* ContextPublisher::build(const char* uri, uid_t uid)
+{
+       for (auto& it : __creators) {
+               if (STR_EQ(it.first, uri)) {
+                       return (it.second)(uid);
+               }
+       }
+
+       return NULL;
+}
+
+ContextPublisher::~ContextPublisher()
+{
+}
+
+void ContextPublisher::addObserver(IContextObserver* observer)
+{
+       if (__observers.empty()) {
+               read();
+               subscribe();
+       }
+
+       __observers.push_back(observer);
+}
+
+void ContextPublisher::removeObserver(IContextObserver* observer)
+{
+       __observers.remove(observer);
+
+       if (__observers.empty()) {
+               unsubscribe();
+               clearData();
+       }
+}
+
+bool ContextPublisher::isUserContext()
+{
+       return false;
+}
+
+const Json::Value& ContextPublisher::getData()
+{
+       return __data;
+}
+
+void ContextPublisher::notifyObservers()
+{
+       for (auto& observer : __observers) {
+               observer->ready();
+       }
+}
+
+bool ContextPublisher::isReady()
+{
+       return !__data.isNull();
+}
+
+void ContextPublisher::clearData()
+{
+       __data = Json::nullValue;
+}
diff --git a/src/server/ContextPublisher.h b/src/server/ContextPublisher.h
new file mode 100644 (file)
index 0000000..3e8f083
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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_CONTEXT_PUBLISHER_H__
+#define __CONTEXT_JOB_SCHEDULER_CONTEXT_PUBLISHER_H__
+
+#include <list>
+#include <json/json.h>
+
+#define REGISTER_PUBLISHER(URI, PUBS) \
+       static ctx::ContextPublisher* __create(uid_t uid) \
+       { \
+               return new PUBS(uid); \
+       } \
+       __attribute__((__constructor__)) static void __registerPublisher() \
+       { \
+               ctx::ContextPublisher::registerPublisher(URI, __create); \
+       }
+
+namespace ctx {
+
+       class IContextObserver;
+
+       class ContextPublisher {
+       public:
+               virtual ~ContextPublisher();
+
+               void addObserver(IContextObserver* observer);
+               void removeObserver(IContextObserver* observer);
+
+               virtual const char* getUri() = 0;
+               virtual bool isUserContext();
+               virtual const Json::Value& getData();
+
+               static void registerPublisher(const char* uri, ContextPublisher* (*creator)(uid_t));
+               static ContextPublisher* build(const char* uri, uid_t uid);
+
+       protected:
+               void notifyObservers();
+
+               virtual void read() = 0;
+               virtual void subscribe() = 0;
+               virtual void unsubscribe() = 0;
+
+               virtual bool isReady();
+               virtual void clearData();
+
+               Json::Value __data;
+
+       private:
+               std::list<IContextObserver*> __observers;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_CONTEXT_PUBLISHER_H__ */
diff --git a/src/server/IContextObserver.h b/src/server/IContextObserver.h
new file mode 100644 (file)
index 0000000..2508fad
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * 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_I_CONTEXT_OBSERVER_H__
+#define __CONTEXT_JOB_SCHEDULER_I_CONTEXT_OBSERVER_H__
+
+namespace ctx {
+
+       class IContextObserver {
+       public:
+               virtual ~IContextObserver() {}
+
+               virtual void ready() = 0;
+       };
+
+}
+
+#endif /* __CONTEXT_JOB_SCHEDULER_I_CONTEXT_OBSERVER_H__ */