Add build & packaging script of context-agent 59/119659/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Sun, 19 Mar 2017 15:30:16 +0000 (00:30 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 27 Mar 2017 04:47:14 +0000 (13:47 +0900)
context-agent will be a user-level assistant service of the main
context-service daemon.

Change-Id: I74db377718ecd9883c3cb75bef556f32d02cc4d3
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
28 files changed:
CMakeLists.txt
packaging/context-service.service [deleted file]
packaging/context-service.spec
packaging/contextd-agent.service [new file with mode: 0644]
packaging/contextd.service [new file with mode: 0644]
packaging/org.tizen.contextagent.conf [new file with mode: 0644]
packaging/org.tizen.contextagent.service [new file with mode: 0644]
src/ActiveUserMonitor.cpp [deleted file]
src/ActiveUserMonitor.h [deleted file]
src/AlarmInitializer.cpp [deleted file]
src/AlarmInitializer.h [deleted file]
src/DBusConnector.cpp [deleted file]
src/DBusConnector.h [deleted file]
src/Main.cpp [deleted file]
src/ServiceLoader.cpp [deleted file]
src/ServiceLoader.h [deleted file]
src/agent/AgentMain.cpp [new file with mode: 0644]
src/agent/CMakeLists.txt [new file with mode: 0644]
src/server/ActiveUserMonitor.cpp [new file with mode: 0644]
src/server/ActiveUserMonitor.h [new file with mode: 0644]
src/server/AlarmInitializer.cpp [new file with mode: 0644]
src/server/AlarmInitializer.h [new file with mode: 0644]
src/server/CMakeLists.txt [new file with mode: 0644]
src/server/DBusConnector.cpp [new file with mode: 0644]
src/server/DBusConnector.h [new file with mode: 0644]
src/server/ServerMain.cpp [new file with mode: 0644]
src/server/ServiceLoader.cpp [new file with mode: 0644]
src/server/ServiceLoader.h [new file with mode: 0644]

index 04c4f5a39de4bcb4b960ecb8a9c993577f983bd0..ff2c8a04a6e37da4982efbc01ffed44093c6638e 100644 (file)
@@ -2,36 +2,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 PROJECT(context-service)
 INCLUDE(GNUInstallDirs)
 
-SET(target "contextd")
-
-SET(DEPS "libsystemd-login glib-2.0 gio-2.0 dlog capi-base-common alarm-service")
-SET(DEPS "${DEPS} context-common-server")
-SET(DEPS "${DEPS} context-app-history-server")
-SET(DEPS "${DEPS} context-sensor-recorder-server")
-SET(DEPS "${DEPS} context-store-server")
-SET(DEPS "${DEPS} context-job-scheduler-server")
-
-SET(INCDIR "${CMAKE_INSTALL_INCLUDEDIR}/context-service")
-
-INCLUDE_DIRECTORIES(
-       ${CMAKE_INSTALL_PREFIX}/${INCDIR}/private
-)
-
-FILE(GLOB_RECURSE SRCS src/*.cpp)
-MESSAGE("Sources: ${SRCS}")
-
 ADD_DEFINITIONS(-O2 -Wall -fPIC -fPIE -fdata-sections -ffunction-sections -fvisibility=hidden)
-ADD_DEFINITIONS(-DLOG_TAG="CONTEXT")
 SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -pie -Wl,--as-needed -Wl,--gc-section -Wl,--print-gc-section")
 
-INCLUDE(FindPkgConfig)
-pkg_check_modules(DEPS_PKG REQUIRED ${DEPS})
-
-FOREACH(flag ${DEPS_PKG_CFLAGS})
-       SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
-ENDFOREACH(flag)
-
-ADD_EXECUTABLE(${target} ${SRCS})
-TARGET_LINK_LIBRARIES(${target} ${DEPS_PKG_LDFLAGS})
-
-INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_BINDIR})
+ADD_SUBDIRECTORY(src/server)
+ADD_SUBDIRECTORY(src/agent)
diff --git a/packaging/context-service.service b/packaging/context-service.service
deleted file mode 100644 (file)
index e5f2f31..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-[Unit]
-Description=Contextual Service Framework Daemon
-
-[Service]
-User=service_fw
-Group=service_fw
-Type=dbus
-BusName=org.tizen.context
-ExecStart=/usr/bin/contextd
-Restart=on-failure
-RestartSec=1
-
-[Install]
-WantedBy=default.target
index 2b90a72b6ccfe2457ea6ab93fec71502a6947545..5df292d5fc7599d7681922ce981c499162e36525 100644 (file)
@@ -5,8 +5,11 @@ Release:    1
 Group:      Service/Context
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
-Source1:       context-service.service
+Source1:       contextd.service
 Source2:       org.tizen.context.conf
+Source3:       contextd-agent.service
+Source4:       org.tizen.contextagent.conf
+Source5:       org.tizen.contextagent.service
 
 BuildRequires: cmake
 BuildRequires: pkgconfig(libsystemd-login)
@@ -25,6 +28,14 @@ BuildRequires: pkgconfig(context-job-scheduler-server)
 %description
 This is a systemd service that contains all features provided by the Tizen Contextual Service Framework.
 
+
+%package -n context-agent
+Summary:       User-level agent service of the Tizen Contextual Service Framework
+
+%description -n context-agent
+This is a systemd service that runs in the user session to assist the main service daemon of the Contextual Service Framework.
+
+
 %prep
 %setup -q
 
@@ -42,15 +53,32 @@ make %{?_smp_mflags}
 
 mkdir -p %{buildroot}%{_unitdir}/default.target.wants
 install -m 0644 %{SOURCE1} %{buildroot}%{_unitdir}
-ln -s ../context-service.service %{buildroot}%{_unitdir}/default.target.wants/context-service.service
+ln -s ../contextd.service %{buildroot}%{_unitdir}/default.target.wants/contextd.service
 
 mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d
 install -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/dbus-1/system.d/
 
+mkdir -p %{buildroot}%{_unitdir_user}
+install -m 0644 %{SOURCE3} %{buildroot}%{_unitdir_user}
+
+mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/session.d
+install -m 0644 %{SOURCE4} %{buildroot}%{_sysconfdir}/dbus-1/session.d/
+
+mkdir -p %{buildroot}/usr/share/dbus-1/services
+install -m 0644 %{SOURCE5} %{buildroot}/usr/share/dbus-1/services/
+
 %files
 %manifest packaging/%{name}.manifest
 %config %{_sysconfdir}/dbus-1/system.d/*
-%{_bindir}/*
-%{_unitdir}/context-service.service
-%{_unitdir}/*/context-service.service
+%{_bindir}/contextd
+%{_unitdir}/contextd.service
+%{_unitdir}/*/contextd.service
+%license LICENSE
+
+%files -n context-agent
+%manifest packaging/%{name}.manifest
+%config %{_sysconfdir}/dbus-1/session.d/*
+%{_bindir}/contextd-agent
+%{_unitdir_user}/contextd-agent.service
+/usr/share/dbus-1/services/*
 %license LICENSE
diff --git a/packaging/contextd-agent.service b/packaging/contextd-agent.service
new file mode 100644 (file)
index 0000000..e6e36b3
--- /dev/null
@@ -0,0 +1,9 @@
+[Unit]
+Description=Contextual Service Framework User-Level Agent Daemon
+
+[Service]
+Type=dbus
+BusName=org.tizen.contextagent
+ExecStart=/usr/bin/contextd-agent
+Restart=on-failure
+RestartSec=1
diff --git a/packaging/contextd.service b/packaging/contextd.service
new file mode 100644 (file)
index 0000000..0fb5d90
--- /dev/null
@@ -0,0 +1,14 @@
+[Unit]
+Description=Contextual Service Framework Server Daemon
+
+[Service]
+User=service_fw
+Group=service_fw
+Type=dbus
+BusName=org.tizen.context
+ExecStart=/usr/bin/contextd
+Restart=on-failure
+RestartSec=1
+
+[Install]
+WantedBy=default.target
diff --git a/packaging/org.tizen.contextagent.conf b/packaging/org.tizen.contextagent.conf
new file mode 100644 (file)
index 0000000..3683fc7
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+<busconfig>
+       <policy smack="System">
+               <deny own="org.tizen.contextagent"/>
+               <allow send_destination="org.tizen.contextagent"/>
+       </policy>
+       <policy smack="User">
+               <allow own="org.tizen.contextagent"/>
+               <allow send_destination="org.tizen.contextagent"/>
+       </policy>
+       <policy context="default">
+               <allow send_destination="org.tizen.contextagent"/>
+       </policy>
+</busconfig>
diff --git a/packaging/org.tizen.contextagent.service b/packaging/org.tizen.contextagent.service
new file mode 100644 (file)
index 0000000..198b7d1
--- /dev/null
@@ -0,0 +1,7 @@
+[Unit]
+Description=Contextual Service Framework User-Level Agent D-Bus
+
+[D-BUS Service]
+Name=org.tizen.contextagent
+Exec=/bin/false
+SystemdService=contextd-agent.service
diff --git a/src/ActiveUserMonitor.cpp b/src/ActiveUserMonitor.cpp
deleted file mode 100644 (file)
index 7ed9f8e..0000000
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * 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 <systemd/sd-login.h>
-#include "ActiveUserMonitor.h"
-
-#define ROOT_UID 0
-
-using namespace ctx;
-
-ActiveUserMonitor::ActiveUserMonitor() :
-       __connection(NULL),
-       __activateUser(NULL),
-       __deactivateUser(NULL),
-       __activeUid(ROOT_UID),
-       __userNewSignalId(0),
-       __userRemovedSignalId(0)
-{
-}
-
-ActiveUserMonitor::~ActiveUserMonitor()
-{
-}
-
-void ActiveUserMonitor::start(GDBusConnection* conn, uid_cb_t activateUser, uid_cb_t deactivateUser)
-{
-       __connection = conn;
-       __activateUser = activateUser;
-       __deactivateUser = deactivateUser;
-
-       __userNewSignalId = g_dbus_connection_signal_subscribe(__connection,
-                       NULL, "org.freedesktop.login1.Manager", "UserNew", NULL,
-                       NULL, G_DBUS_SIGNAL_FLAGS_NONE, __onUserNew, this, NULL);
-
-       __userRemovedSignalId = g_dbus_connection_signal_subscribe(__connection,
-                       NULL, "org.freedesktop.login1.Manager", "UserRemoved", NULL,
-                       NULL, G_DBUS_SIGNAL_FLAGS_NONE, __onUserRemoved, this, NULL);
-
-       g_timeout_add(200, __checkCurrentUser, this);
-}
-
-void ActiveUserMonitor::stop()
-{
-       g_dbus_connection_signal_unsubscribe(__connection, __userNewSignalId);
-       g_dbus_connection_signal_unsubscribe(__connection, __userRemovedSignalId);
-}
-
-void ActiveUserMonitor::__onUserNew(GDBusConnection* conn, const gchar* sender,
-               const gchar* path, const gchar* iface, const gchar* name,
-               GVariant* param, gpointer userData)
-{
-       uint32_t uid = 0;
-       g_variant_get_child(param, 0, "u", &uid);
-       IF_FAIL_VOID_TAG(uid > 0, _W, "UID == 0");
-
-       _D("UID: %u", uid);
-
-       ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
-
-       if (monitor->__activeUid == uid)
-               return;
-
-       if (monitor->__activeUid > ROOT_UID) {
-               _W("Over-activation of the user %u", uid);
-               monitor->__deactivateUser(monitor->__activeUid);
-       }
-
-       monitor->__activateUser(uid);
-       monitor->__activeUid = uid;
-}
-
-void ActiveUserMonitor::__onUserRemoved(GDBusConnection* conn, const gchar* sender,
-               const gchar* path, const gchar* iface, const gchar* name,
-               GVariant* param, gpointer userData)
-{
-       uint32_t uid = 0;
-       g_variant_get_child(param, 0, "u", &uid);
-       IF_FAIL_VOID_TAG(uid > 0, _W, "UID == 0");
-
-       _D("UID: %u", uid);
-
-       ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
-
-       if (monitor->__activeUid == ROOT_UID) {
-               _W("No active user");
-               return;
-       }
-
-       if (monitor->__activeUid != uid) {
-               _W("Mismatched uid");
-       }
-
-       monitor->__deactivateUser(uid);
-       monitor->__activeUid = ROOT_UID;
-}
-
-gboolean ActiveUserMonitor::__checkCurrentUser(gpointer userData)
-{
-       ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
-       IF_FAIL_RETURN(monitor->__activeUid == ROOT_UID, G_SOURCE_REMOVE);
-
-       uid_t* users = NULL;
-       int numUsers = sd_get_active_uids(&users);
-
-       if (numUsers > 0)
-               monitor->__activeUid = users[0];
-
-       g_free(users);
-
-       if (monitor->__activeUid != ROOT_UID) {
-               _D("UID: %u", monitor->__activeUid);
-               monitor->__activateUser(monitor->__activeUid);
-       }
-
-       return G_SOURCE_REMOVE;
-}
diff --git a/src/ActiveUserMonitor.h b/src/ActiveUserMonitor.h
deleted file mode 100644 (file)
index f349c85..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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_ACTIVE_USER_MONITOR_H__
-#define __CONTEXT_ACTIVE_USER_MONITOR_H__
-
-#include <ContextTypes.h>
-
-namespace ctx {
-
-       class ActiveUserMonitor {
-
-               typedef void (*uid_cb_t)(uid_t);
-
-       public:
-               ActiveUserMonitor();
-               ~ActiveUserMonitor();
-
-               void start(GDBusConnection* conn, uid_cb_t activateUser, uid_cb_t deactivateUser);
-               void stop();
-
-       private:
-               static void __onUserNew(GDBusConnection* conn, const gchar* sender,
-                               const gchar* path, const gchar* iface, const gchar* name,
-                               GVariant* param, gpointer userData);
-
-               static void __onUserRemoved(GDBusConnection* conn, const gchar* sender,
-                               const gchar* path, const gchar* iface, const gchar* name,
-                               GVariant* param, gpointer userData);
-
-               static gboolean __checkCurrentUser(gpointer userData);
-
-               GDBusConnection* __connection;
-               uid_cb_t __activateUser;
-               uid_cb_t __deactivateUser;
-               uid_t __activeUid;
-               guint __userNewSignalId;
-               guint __userRemovedSignalId;
-       };
-
-}
-
-#endif
diff --git a/src/AlarmInitializer.cpp b/src/AlarmInitializer.cpp
deleted file mode 100644 (file)
index 04aa0c6..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * 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 <alarm.h>
-#include <ContextTypes.h>
-#include "AlarmInitializer.h"
-
-#define IDENTIFIER             "contextd"
-
-using namespace ctx;
-
-static bool __initialized = false;
-
-AlarmInitializer::AlarmInitializer()
-{
-       _I("Init Alarm");
-       int result = alarmmgr_init(IDENTIFIER);
-       if (result != ALARMMGR_RESULT_SUCCESS) {
-               _E("Alarm manager initialization failed");
-               return;
-       }
-       alarmmgr_remove_all();
-       __initialized = true;
-}
-
-AlarmInitializer::~AlarmInitializer()
-{
-       if (!__initialized)
-               return;
-
-       _I("Release Alarm");
-       alarmmgr_remove_all();
-       alarmmgr_fini();
-       __initialized = false;
-}
diff --git a/src/AlarmInitializer.h b/src/AlarmInitializer.h
deleted file mode 100644 (file)
index 40ae599..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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_ALARM_INITIALIZER_H__
-#define __CONTEXT_ALARM_INITIALIZER_H__
-
-namespace ctx {
-
-       class AlarmInitializer {
-       public:
-               AlarmInitializer();
-               ~AlarmInitializer();
-       };
-
-}
-
-#endif
diff --git a/src/DBusConnector.cpp b/src/DBusConnector.cpp
deleted file mode 100644 (file)
index 257311e..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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 "DBusConnector.h"
-
-using namespace ctx;
-
-DBusConnector::DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb) :
-       __owner(-1),
-       __connection(NULL),
-       __busAcquiredCb(acqCb),
-       __busLostCb(lostCb)
-{
-       __init();
-}
-
-DBusConnector::~DBusConnector()
-{
-       __release();
-}
-
-void DBusConnector::__onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
-{
-       DBusConnector* connector = static_cast<DBusConnector*>(userData);
-       connector->__connection = conn;
-}
-
-void DBusConnector::__onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
-{
-       _I(GREEN("Acquired"));
-       DBusConnector* connector = static_cast<DBusConnector*>(userData);
-       connector->__busAcquiredCb(conn);
-}
-
-void DBusConnector::__onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData)
-{
-       _E(RED("Lost"));
-       DBusConnector* connector = static_cast<DBusConnector*>(userData);
-       connector->__busLostCb(conn);
-}
-
-void DBusConnector::__init()
-{
-       __owner = g_bus_own_name(G_BUS_TYPE_SYSTEM, CTX_DBUS_DEST, G_BUS_NAME_OWNER_FLAGS_NONE,
-                       __onBusAcquired, __onNameAcquired, __onNameLost, this, NULL);
-}
-
-void DBusConnector::__release()
-{
-       if (!__connection)
-               return;
-
-       g_dbus_connection_close_sync(__connection, NULL, NULL);
-       g_object_unref(__connection);
-}
diff --git a/src/DBusConnector.h b/src/DBusConnector.h
deleted file mode 100644 (file)
index 2311219..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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_DBUS_CONNECTOR_H__
-#define __CONTEXT_DBUS_CONNECTOR_H__
-
-#include <ContextTypes.h>
-
-namespace ctx {
-
-       typedef void (*bus_acquired_cb_t)(GDBusConnection *conn);
-       typedef void (*bus_lost_cb_t)(GDBusConnection *conn);
-
-       class DBusConnector {
-       public:
-               DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb);
-               ~DBusConnector();
-
-       private:
-               static void __onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
-               static void __onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
-               static void __onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData);
-
-               void __init();
-               void __release();
-
-               guint __owner;
-               GDBusConnection *__connection;
-               bus_acquired_cb_t __busAcquiredCb;
-               bus_lost_cb_t __busLostCb;
-
-       };
-
-}
-
-#endif
diff --git a/src/Main.cpp b/src/Main.cpp
deleted file mode 100644 (file)
index 38c976d..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- * 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 <cstdlib>
-#include <cstring>
-#include <ContextTypes.h>
-#include <Timer.h>
-#include "DBusConnector.h"
-#include "ServiceLoader.h"
-#include "ActiveUserMonitor.h"
-#include "AlarmInitializer.h"
-
-using namespace ctx;
-
-namespace {
-       class MainLoop {
-       public:
-               static bool start();
-               static void stop();
-       private:
-               MainLoop() {}
-               static GMainLoop* __mainLoop;
-       };
-}
-
-GMainLoop* MainLoop::__mainLoop = NULL;
-
-bool MainLoop::start()
-{
-       __mainLoop = g_main_loop_new(NULL, FALSE);
-       IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "Memory allocation failed");
-
-       _I(CYAN("Starting..."));
-       g_main_loop_run(__mainLoop);
-
-       g_main_loop_unref(__mainLoop);
-
-       return true;
-}
-
-void MainLoop::stop()
-{
-       _I(PURPLE("Terminating..."));
-       g_main_loop_quit(__mainLoop);
-}
-
-static ServiceLoader __serviceLoader;
-static ActiveUserMonitor __activeUserMonitor;
-static AlarmInitializer __alarmInit;
-
-static void __activateUser(uid_t uid)
-{
-       __serviceLoader.startUser(uid);
-}
-
-static void __deactivateUser(uid_t uid)
-{
-       __serviceLoader.stopUser();
-}
-
-static void __startService(GDBusConnection* conn)
-{
-       Timer::setDBusConnection(conn);
-       __activeUserMonitor.start(conn, __activateUser, __deactivateUser);
-
-       _I("Loading services");
-       if (!__serviceLoader.load(conn)) {
-               _E(RED("No service loaded"));
-               return;
-       }
-
-       _I("Service loading successful");
-       __serviceLoader.startSystem();
-}
-
-static gboolean __stopService(gpointer data)
-{
-       __activeUserMonitor.stop();
-
-       _I("Unloading services");
-       __serviceLoader.stopUser();
-       __serviceLoader.stopSystem();
-
-       MainLoop::stop();
-       return G_SOURCE_REMOVE;
-}
-
-static void __busAcquired(GDBusConnection* conn)
-{
-       __startService(conn);
-}
-
-static void __busLost(GDBusConnection* conn)
-{
-       __stopService(NULL);
-}
-
-static void __signalHandler(int signum)
-{
-       _I(YELLOW("SIGNAL-%d: '%s'"), signum, strsignal(signum));
-       static bool terminated = false;
-       if (!terminated) {
-               g_idle_add(__stopService, NULL);
-               terminated = true;
-       }
-}
-
-int main(int argc, char* argv[])
-{
-       static struct sigaction signalAction;
-       signalAction.sa_handler = __signalHandler;
-       sigemptyset(&signalAction.sa_mask);
-
-       sigaction(SIGINT, &signalAction, NULL);
-       sigaction(SIGHUP, &signalAction, NULL);
-       sigaction(SIGTERM, &signalAction, NULL);
-       sigaction(SIGQUIT, &signalAction, NULL);
-       sigaction(SIGABRT, &signalAction, NULL);
-
-       DBusConnector dbusConnector(__busAcquired, __busLost);
-
-       MainLoop::start();
-
-       return EXIT_SUCCESS;
-}
diff --git a/src/ServiceLoader.cpp b/src/ServiceLoader.cpp
deleted file mode 100644 (file)
index 03acfbc..0000000
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * 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 <AppHistoryService.h>
-#include <SensorRecorderService.h>
-#include <ContextStoreService.h>
-#include <JobSchedulerService.h>
-
-#include "ServiceLoader.h"
-
-#define ROOT_UID 0
-
-using namespace ctx;
-
-ServiceLoader::ServiceLoader() :
-       __activeUser(ROOT_UID)
-{
-}
-
-ServiceLoader::~ServiceLoader()
-{
-       for (auto& svc : __userServices) {
-               delete svc;
-       }
-       for (auto& svc : __systemServices) {
-               delete svc;
-       }
-}
-
-bool ServiceLoader::load(GDBusConnection* conn)
-{
-       __create<AppHistoryService>(conn);
-       __create<SensorRecorderService>(conn);
-       __create<ContextStoreService>(conn);
-       __create<JobSchedulerService>(conn);
-
-       return (!__systemServices.empty() || !__userServices.empty());
-}
-
-void ServiceLoader::startSystem()
-{
-       for (auto& svc : __systemServices) {
-               svc->start();
-       }
-}
-
-void ServiceLoader::stopSystem()
-{
-       for (auto& svc : __systemServices) {
-               svc->stop();
-       }
-}
-
-void ServiceLoader::startUser(uid_t uid)
-{
-       IF_FAIL_VOID(__activeUser != uid);
-       _I("Starting services for %u", static_cast<unsigned int>(uid));
-
-       ServiceBase::setActiveUser(uid);
-
-       for (auto& svc : __userServices) {
-               svc->start();
-       }
-
-       for (auto& svc : __systemServices) {
-               svc->notifyUserNew();
-       }
-
-       __activeUser = uid;
-}
-
-void ServiceLoader::stopUser()
-{
-       IF_FAIL_VOID(__activeUser != ROOT_UID);
-       _I("Stopping services for %u", static_cast<unsigned int>(__activeUser));
-
-       for (auto& svc : __userServices) {
-               svc->stop();
-       }
-
-       ServiceBase::setActiveUser(ROOT_UID);
-
-       for (auto& svc : __systemServices) {
-               svc->notifyUserRemoved();
-       }
-
-       __activeUser = ROOT_UID;
-}
diff --git a/src/ServiceLoader.h b/src/ServiceLoader.h
deleted file mode 100644 (file)
index 5bedbb2..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * 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_SERVICE_LOADER_H__
-#define __CONTEXT_SERVICE_LOADER_H__
-
-#include <vector>
-#include <ContextTypes.h>
-#include <ServiceBase.h>
-
-namespace ctx {
-
-       class ServiceLoader {
-       public:
-               ServiceLoader();
-               ~ServiceLoader();
-
-               bool load(GDBusConnection* conn);
-
-               void startUser(uid_t uid);
-               void stopUser();
-               void startSystem();
-               void stopSystem();
-
-       private:
-               uid_t __activeUser;
-               std::vector<ServiceBase*> __userServices;
-               std::vector<ServiceBase*> __systemServices;
-
-               template<typename ServiceType> void __create(GDBusConnection* conn)
-               {
-                       ServiceBase *svc = NULL;
-                       try {
-                               svc = new ServiceType(conn);
-                       } catch (std::exception& e) {
-                               _W("Service creation failed (%s)", e.what());
-                               return;
-                       }
-                       if (svc->isUserService()) {
-                               __userServices.push_back(svc);
-                       } else {
-                               __systemServices.push_back(svc);
-                       }
-               }
-       };
-
-}
-
-#endif
diff --git a/src/agent/AgentMain.cpp b/src/agent/AgentMain.cpp
new file mode 100644 (file)
index 0000000..fc1dc8d
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * 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 <cstdlib>
+
+int main(int argc, char* argv[])
+{
+       return EXIT_SUCCESS;
+}
diff --git a/src/agent/CMakeLists.txt b/src/agent/CMakeLists.txt
new file mode 100644 (file)
index 0000000..114362f
--- /dev/null
@@ -0,0 +1,21 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+SET(target "contextd-agent")
+
+SET(DEPS glib-2.0 gio-2.0 dlog capi-base-common)
+
+FILE(GLOB_RECURSE SRCS *.cpp)
+MESSAGE("Sources: ${SRCS}")
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(DEPS_AGENT_PKG REQUIRED ${DEPS})
+
+FOREACH(flag ${DEPS_AGENT_PKG_CFLAGS})
+       SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
+ENDFOREACH(flag)
+
+ADD_EXECUTABLE(${target} ${SRCS})
+SET_TARGET_PROPERTIES(${target} PROPERTIES COMPILE_DEFINITIONS "LOG_TAG=\"CONTEXT-AGENT\"")
+TARGET_LINK_LIBRARIES(${target} ${DEPS_AGENT_PKG_LDFLAGS})
+
+INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/src/server/ActiveUserMonitor.cpp b/src/server/ActiveUserMonitor.cpp
new file mode 100644 (file)
index 0000000..7ed9f8e
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+ * 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 <systemd/sd-login.h>
+#include "ActiveUserMonitor.h"
+
+#define ROOT_UID 0
+
+using namespace ctx;
+
+ActiveUserMonitor::ActiveUserMonitor() :
+       __connection(NULL),
+       __activateUser(NULL),
+       __deactivateUser(NULL),
+       __activeUid(ROOT_UID),
+       __userNewSignalId(0),
+       __userRemovedSignalId(0)
+{
+}
+
+ActiveUserMonitor::~ActiveUserMonitor()
+{
+}
+
+void ActiveUserMonitor::start(GDBusConnection* conn, uid_cb_t activateUser, uid_cb_t deactivateUser)
+{
+       __connection = conn;
+       __activateUser = activateUser;
+       __deactivateUser = deactivateUser;
+
+       __userNewSignalId = g_dbus_connection_signal_subscribe(__connection,
+                       NULL, "org.freedesktop.login1.Manager", "UserNew", NULL,
+                       NULL, G_DBUS_SIGNAL_FLAGS_NONE, __onUserNew, this, NULL);
+
+       __userRemovedSignalId = g_dbus_connection_signal_subscribe(__connection,
+                       NULL, "org.freedesktop.login1.Manager", "UserRemoved", NULL,
+                       NULL, G_DBUS_SIGNAL_FLAGS_NONE, __onUserRemoved, this, NULL);
+
+       g_timeout_add(200, __checkCurrentUser, this);
+}
+
+void ActiveUserMonitor::stop()
+{
+       g_dbus_connection_signal_unsubscribe(__connection, __userNewSignalId);
+       g_dbus_connection_signal_unsubscribe(__connection, __userRemovedSignalId);
+}
+
+void ActiveUserMonitor::__onUserNew(GDBusConnection* conn, const gchar* sender,
+               const gchar* path, const gchar* iface, const gchar* name,
+               GVariant* param, gpointer userData)
+{
+       uint32_t uid = 0;
+       g_variant_get_child(param, 0, "u", &uid);
+       IF_FAIL_VOID_TAG(uid > 0, _W, "UID == 0");
+
+       _D("UID: %u", uid);
+
+       ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
+
+       if (monitor->__activeUid == uid)
+               return;
+
+       if (monitor->__activeUid > ROOT_UID) {
+               _W("Over-activation of the user %u", uid);
+               monitor->__deactivateUser(monitor->__activeUid);
+       }
+
+       monitor->__activateUser(uid);
+       monitor->__activeUid = uid;
+}
+
+void ActiveUserMonitor::__onUserRemoved(GDBusConnection* conn, const gchar* sender,
+               const gchar* path, const gchar* iface, const gchar* name,
+               GVariant* param, gpointer userData)
+{
+       uint32_t uid = 0;
+       g_variant_get_child(param, 0, "u", &uid);
+       IF_FAIL_VOID_TAG(uid > 0, _W, "UID == 0");
+
+       _D("UID: %u", uid);
+
+       ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
+
+       if (monitor->__activeUid == ROOT_UID) {
+               _W("No active user");
+               return;
+       }
+
+       if (monitor->__activeUid != uid) {
+               _W("Mismatched uid");
+       }
+
+       monitor->__deactivateUser(uid);
+       monitor->__activeUid = ROOT_UID;
+}
+
+gboolean ActiveUserMonitor::__checkCurrentUser(gpointer userData)
+{
+       ActiveUserMonitor* monitor = static_cast<ActiveUserMonitor*>(userData);
+       IF_FAIL_RETURN(monitor->__activeUid == ROOT_UID, G_SOURCE_REMOVE);
+
+       uid_t* users = NULL;
+       int numUsers = sd_get_active_uids(&users);
+
+       if (numUsers > 0)
+               monitor->__activeUid = users[0];
+
+       g_free(users);
+
+       if (monitor->__activeUid != ROOT_UID) {
+               _D("UID: %u", monitor->__activeUid);
+               monitor->__activateUser(monitor->__activeUid);
+       }
+
+       return G_SOURCE_REMOVE;
+}
diff --git a/src/server/ActiveUserMonitor.h b/src/server/ActiveUserMonitor.h
new file mode 100644 (file)
index 0000000..f349c85
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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_ACTIVE_USER_MONITOR_H__
+#define __CONTEXT_ACTIVE_USER_MONITOR_H__
+
+#include <ContextTypes.h>
+
+namespace ctx {
+
+       class ActiveUserMonitor {
+
+               typedef void (*uid_cb_t)(uid_t);
+
+       public:
+               ActiveUserMonitor();
+               ~ActiveUserMonitor();
+
+               void start(GDBusConnection* conn, uid_cb_t activateUser, uid_cb_t deactivateUser);
+               void stop();
+
+       private:
+               static void __onUserNew(GDBusConnection* conn, const gchar* sender,
+                               const gchar* path, const gchar* iface, const gchar* name,
+                               GVariant* param, gpointer userData);
+
+               static void __onUserRemoved(GDBusConnection* conn, const gchar* sender,
+                               const gchar* path, const gchar* iface, const gchar* name,
+                               GVariant* param, gpointer userData);
+
+               static gboolean __checkCurrentUser(gpointer userData);
+
+               GDBusConnection* __connection;
+               uid_cb_t __activateUser;
+               uid_cb_t __deactivateUser;
+               uid_t __activeUid;
+               guint __userNewSignalId;
+               guint __userRemovedSignalId;
+       };
+
+}
+
+#endif
diff --git a/src/server/AlarmInitializer.cpp b/src/server/AlarmInitializer.cpp
new file mode 100644 (file)
index 0000000..04aa0c6
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * 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 <alarm.h>
+#include <ContextTypes.h>
+#include "AlarmInitializer.h"
+
+#define IDENTIFIER             "contextd"
+
+using namespace ctx;
+
+static bool __initialized = false;
+
+AlarmInitializer::AlarmInitializer()
+{
+       _I("Init Alarm");
+       int result = alarmmgr_init(IDENTIFIER);
+       if (result != ALARMMGR_RESULT_SUCCESS) {
+               _E("Alarm manager initialization failed");
+               return;
+       }
+       alarmmgr_remove_all();
+       __initialized = true;
+}
+
+AlarmInitializer::~AlarmInitializer()
+{
+       if (!__initialized)
+               return;
+
+       _I("Release Alarm");
+       alarmmgr_remove_all();
+       alarmmgr_fini();
+       __initialized = false;
+}
diff --git a/src/server/AlarmInitializer.h b/src/server/AlarmInitializer.h
new file mode 100644 (file)
index 0000000..40ae599
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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_ALARM_INITIALIZER_H__
+#define __CONTEXT_ALARM_INITIALIZER_H__
+
+namespace ctx {
+
+       class AlarmInitializer {
+       public:
+               AlarmInitializer();
+               ~AlarmInitializer();
+       };
+
+}
+
+#endif
diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt
new file mode 100644 (file)
index 0000000..170e43e
--- /dev/null
@@ -0,0 +1,32 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+SET(target "contextd")
+
+SET(DEPS libsystemd-login glib-2.0 gio-2.0 dlog capi-base-common alarm-service)
+SET(DEPS ${DEPS} context-common-server)
+SET(DEPS ${DEPS} context-app-history-server)
+SET(DEPS ${DEPS} context-sensor-recorder-server)
+SET(DEPS ${DEPS} context-store-server)
+SET(DEPS ${DEPS} context-job-scheduler-server)
+
+SET(INCDIR "${CMAKE_INSTALL_INCLUDEDIR}/context-service")
+
+INCLUDE_DIRECTORIES(
+       ${CMAKE_INSTALL_PREFIX}/${INCDIR}/private
+)
+
+FILE(GLOB_RECURSE SRCS *.cpp)
+MESSAGE("Sources: ${SRCS}")
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(DEPS_SERVER_PKG REQUIRED ${DEPS})
+
+FOREACH(flag ${DEPS_SERVER_PKG_CFLAGS})
+       SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
+ENDFOREACH(flag)
+
+ADD_EXECUTABLE(${target} ${SRCS})
+SET_TARGET_PROPERTIES(${target} PROPERTIES COMPILE_DEFINITIONS "LOG_TAG=\"CONTEXT\"")
+TARGET_LINK_LIBRARIES(${target} ${DEPS_SERVER_PKG_LDFLAGS})
+
+INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/src/server/DBusConnector.cpp b/src/server/DBusConnector.cpp
new file mode 100644 (file)
index 0000000..257311e
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * 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 "DBusConnector.h"
+
+using namespace ctx;
+
+DBusConnector::DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb) :
+       __owner(-1),
+       __connection(NULL),
+       __busAcquiredCb(acqCb),
+       __busLostCb(lostCb)
+{
+       __init();
+}
+
+DBusConnector::~DBusConnector()
+{
+       __release();
+}
+
+void DBusConnector::__onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+       DBusConnector* connector = static_cast<DBusConnector*>(userData);
+       connector->__connection = conn;
+}
+
+void DBusConnector::__onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+       _I(GREEN("Acquired"));
+       DBusConnector* connector = static_cast<DBusConnector*>(userData);
+       connector->__busAcquiredCb(conn);
+}
+
+void DBusConnector::__onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+       _E(RED("Lost"));
+       DBusConnector* connector = static_cast<DBusConnector*>(userData);
+       connector->__busLostCb(conn);
+}
+
+void DBusConnector::__init()
+{
+       __owner = g_bus_own_name(G_BUS_TYPE_SYSTEM, CTX_DBUS_DEST, G_BUS_NAME_OWNER_FLAGS_NONE,
+                       __onBusAcquired, __onNameAcquired, __onNameLost, this, NULL);
+}
+
+void DBusConnector::__release()
+{
+       if (!__connection)
+               return;
+
+       g_dbus_connection_close_sync(__connection, NULL, NULL);
+       g_object_unref(__connection);
+}
diff --git a/src/server/DBusConnector.h b/src/server/DBusConnector.h
new file mode 100644 (file)
index 0000000..2311219
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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_DBUS_CONNECTOR_H__
+#define __CONTEXT_DBUS_CONNECTOR_H__
+
+#include <ContextTypes.h>
+
+namespace ctx {
+
+       typedef void (*bus_acquired_cb_t)(GDBusConnection *conn);
+       typedef void (*bus_lost_cb_t)(GDBusConnection *conn);
+
+       class DBusConnector {
+       public:
+               DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb);
+               ~DBusConnector();
+
+       private:
+               static void __onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
+               static void __onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
+               static void __onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData);
+
+               void __init();
+               void __release();
+
+               guint __owner;
+               GDBusConnection *__connection;
+               bus_acquired_cb_t __busAcquiredCb;
+               bus_lost_cb_t __busLostCb;
+
+       };
+
+}
+
+#endif
diff --git a/src/server/ServerMain.cpp b/src/server/ServerMain.cpp
new file mode 100644 (file)
index 0000000..38c976d
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * 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 <cstdlib>
+#include <cstring>
+#include <ContextTypes.h>
+#include <Timer.h>
+#include "DBusConnector.h"
+#include "ServiceLoader.h"
+#include "ActiveUserMonitor.h"
+#include "AlarmInitializer.h"
+
+using namespace ctx;
+
+namespace {
+       class MainLoop {
+       public:
+               static bool start();
+               static void stop();
+       private:
+               MainLoop() {}
+               static GMainLoop* __mainLoop;
+       };
+}
+
+GMainLoop* MainLoop::__mainLoop = NULL;
+
+bool MainLoop::start()
+{
+       __mainLoop = g_main_loop_new(NULL, FALSE);
+       IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "Memory allocation failed");
+
+       _I(CYAN("Starting..."));
+       g_main_loop_run(__mainLoop);
+
+       g_main_loop_unref(__mainLoop);
+
+       return true;
+}
+
+void MainLoop::stop()
+{
+       _I(PURPLE("Terminating..."));
+       g_main_loop_quit(__mainLoop);
+}
+
+static ServiceLoader __serviceLoader;
+static ActiveUserMonitor __activeUserMonitor;
+static AlarmInitializer __alarmInit;
+
+static void __activateUser(uid_t uid)
+{
+       __serviceLoader.startUser(uid);
+}
+
+static void __deactivateUser(uid_t uid)
+{
+       __serviceLoader.stopUser();
+}
+
+static void __startService(GDBusConnection* conn)
+{
+       Timer::setDBusConnection(conn);
+       __activeUserMonitor.start(conn, __activateUser, __deactivateUser);
+
+       _I("Loading services");
+       if (!__serviceLoader.load(conn)) {
+               _E(RED("No service loaded"));
+               return;
+       }
+
+       _I("Service loading successful");
+       __serviceLoader.startSystem();
+}
+
+static gboolean __stopService(gpointer data)
+{
+       __activeUserMonitor.stop();
+
+       _I("Unloading services");
+       __serviceLoader.stopUser();
+       __serviceLoader.stopSystem();
+
+       MainLoop::stop();
+       return G_SOURCE_REMOVE;
+}
+
+static void __busAcquired(GDBusConnection* conn)
+{
+       __startService(conn);
+}
+
+static void __busLost(GDBusConnection* conn)
+{
+       __stopService(NULL);
+}
+
+static void __signalHandler(int signum)
+{
+       _I(YELLOW("SIGNAL-%d: '%s'"), signum, strsignal(signum));
+       static bool terminated = false;
+       if (!terminated) {
+               g_idle_add(__stopService, NULL);
+               terminated = true;
+       }
+}
+
+int main(int argc, char* argv[])
+{
+       static struct sigaction signalAction;
+       signalAction.sa_handler = __signalHandler;
+       sigemptyset(&signalAction.sa_mask);
+
+       sigaction(SIGINT, &signalAction, NULL);
+       sigaction(SIGHUP, &signalAction, NULL);
+       sigaction(SIGTERM, &signalAction, NULL);
+       sigaction(SIGQUIT, &signalAction, NULL);
+       sigaction(SIGABRT, &signalAction, NULL);
+
+       DBusConnector dbusConnector(__busAcquired, __busLost);
+
+       MainLoop::start();
+
+       return EXIT_SUCCESS;
+}
diff --git a/src/server/ServiceLoader.cpp b/src/server/ServiceLoader.cpp
new file mode 100644 (file)
index 0000000..03acfbc
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * 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 <AppHistoryService.h>
+#include <SensorRecorderService.h>
+#include <ContextStoreService.h>
+#include <JobSchedulerService.h>
+
+#include "ServiceLoader.h"
+
+#define ROOT_UID 0
+
+using namespace ctx;
+
+ServiceLoader::ServiceLoader() :
+       __activeUser(ROOT_UID)
+{
+}
+
+ServiceLoader::~ServiceLoader()
+{
+       for (auto& svc : __userServices) {
+               delete svc;
+       }
+       for (auto& svc : __systemServices) {
+               delete svc;
+       }
+}
+
+bool ServiceLoader::load(GDBusConnection* conn)
+{
+       __create<AppHistoryService>(conn);
+       __create<SensorRecorderService>(conn);
+       __create<ContextStoreService>(conn);
+       __create<JobSchedulerService>(conn);
+
+       return (!__systemServices.empty() || !__userServices.empty());
+}
+
+void ServiceLoader::startSystem()
+{
+       for (auto& svc : __systemServices) {
+               svc->start();
+       }
+}
+
+void ServiceLoader::stopSystem()
+{
+       for (auto& svc : __systemServices) {
+               svc->stop();
+       }
+}
+
+void ServiceLoader::startUser(uid_t uid)
+{
+       IF_FAIL_VOID(__activeUser != uid);
+       _I("Starting services for %u", static_cast<unsigned int>(uid));
+
+       ServiceBase::setActiveUser(uid);
+
+       for (auto& svc : __userServices) {
+               svc->start();
+       }
+
+       for (auto& svc : __systemServices) {
+               svc->notifyUserNew();
+       }
+
+       __activeUser = uid;
+}
+
+void ServiceLoader::stopUser()
+{
+       IF_FAIL_VOID(__activeUser != ROOT_UID);
+       _I("Stopping services for %u", static_cast<unsigned int>(__activeUser));
+
+       for (auto& svc : __userServices) {
+               svc->stop();
+       }
+
+       ServiceBase::setActiveUser(ROOT_UID);
+
+       for (auto& svc : __systemServices) {
+               svc->notifyUserRemoved();
+       }
+
+       __activeUser = ROOT_UID;
+}
diff --git a/src/server/ServiceLoader.h b/src/server/ServiceLoader.h
new file mode 100644 (file)
index 0000000..5bedbb2
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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_SERVICE_LOADER_H__
+#define __CONTEXT_SERVICE_LOADER_H__
+
+#include <vector>
+#include <ContextTypes.h>
+#include <ServiceBase.h>
+
+namespace ctx {
+
+       class ServiceLoader {
+       public:
+               ServiceLoader();
+               ~ServiceLoader();
+
+               bool load(GDBusConnection* conn);
+
+               void startUser(uid_t uid);
+               void stopUser();
+               void startSystem();
+               void stopSystem();
+
+       private:
+               uid_t __activeUser;
+               std::vector<ServiceBase*> __userServices;
+               std::vector<ServiceBase*> __systemServices;
+
+               template<typename ServiceType> void __create(GDBusConnection* conn)
+               {
+                       ServiceBase *svc = NULL;
+                       try {
+                               svc = new ServiceType(conn);
+                       } catch (std::exception& e) {
+                               _W("Service creation failed (%s)", e.what());
+                               return;
+                       }
+                       if (svc->isUserService()) {
+                               __userServices.push_back(svc);
+                       } else {
+                               __systemServices.push_back(svc);
+                       }
+               }
+       };
+
+}
+
+#endif