Add main agent class 12/31012/3
authorAdam Malinowski <a.malinowsk2@partner.samsung.com>
Fri, 28 Nov 2014 13:00:01 +0000 (14:00 +0100)
committerAdam Malinowski <a.malinowsk2@partner.samsung.com>
Mon, 15 Dec 2014 06:57:47 +0000 (07:57 +0100)
Agent class has been added and its usage in main.cpp file.

Change-Id: Iaec77d164c966a389df97e415febdca6622dc082

src/agent/CMakeLists.txt
src/agent/main/Agent.cpp [new file with mode: 0644]
src/agent/main/Agent.h [new file with mode: 0644]
src/agent/main/main.cpp

index bdeac9e..cbb04e3 100644 (file)
@@ -19,6 +19,7 @@
 SET(ASKUSER_AGENT_PATH ${ASKUSER_PATH}/agent)
 
 SET(ASKUSER_SOURCES
+    ${ASKUSER_AGENT_PATH}/main/Agent.cpp
     ${ASKUSER_AGENT_PATH}/main/main.cpp
     )
 
diff --git a/src/agent/main/Agent.cpp b/src/agent/main/Agent.cpp
new file mode 100644 (file)
index 0000000..68883a7
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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.
+ */
+/**
+ * @file        src/agent/main/Agent.cpp
+ * @author      Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @brief       This file implements main class of ask user agent
+ */
+
+#include <unistd.h>
+
+#include <log/log.h>
+
+#include "Agent.h"
+
+namespace AskUser {
+
+namespace Agent {
+
+Agent::Agent() {
+    init();
+}
+
+Agent::~Agent() {
+    finish();
+}
+
+void Agent::init() {
+    // TODO: implement if needed
+
+    LOGD("Agent daemon initialized");
+}
+
+void Agent::run() {
+    // TODO: implement real task
+    while (true) {
+        sleep(1);
+    }
+
+    LOGD("Ask user agent task stopped");
+}
+
+void Agent::finish() {
+    // TODO: implement if needed
+
+    LOGD("Agent daemon has stopped commonly");
+}
+
+} // namespace Agent
+
+} // namespace AskUser
diff --git a/src/agent/main/Agent.h b/src/agent/main/Agent.h
new file mode 100644 (file)
index 0000000..97fe825
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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.
+ */
+/**
+ * @file        src/agent/main/Agent.h
+ * @author      Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @brief       This file defines main class of ask user agent
+ */
+
+#pragma once
+
+namespace AskUser {
+
+namespace Agent {
+
+class Agent {
+public:
+    Agent();
+    ~Agent();
+
+    void run();
+
+private:
+    void init();
+    void finish();
+};
+
+} // namespace Agent
+
+} // namespace AskUser
index e0f4a6a..d8857ba 100644 (file)
  */
 
 #include <cstdlib>
-#include <unistd.h>
-
+#include <exception>
 #include <systemd/sd-journal.h>
 #include <systemd/sd-daemon.h>
 
 #include <attributes/attributes.h>
 #include <log/log.h>
 
+#include "Agent.h"
+
 int main(int argc UNUSED, char **argv UNUSED) {
     init_log();
 
-    int ret = sd_notify(0, "READY=1");
-    if (ret == 0) {
-        LOGW("Ask user agent was not configured to notify its status");
-    } else if (ret < 0) {
-        LOGE("sd_notify failed: [" << ret << "]");
-    }
+    try {
+        AskUser::Agent::Agent agent;
+
+        int ret = sd_notify(0, "READY=1");
+        if (ret == 0) {
+            LOGW("Agent was not configured to notify its status");
+        } else if (ret < 0) {
+            LOGE("sd_notify failed: [" << ret << "]");
+        }
 
-    while (true) {
-        sleep(1);
+        agent.run();
+    } catch (const std::exception &e) {
+        LOGC("Agent stopped because of unhandled exception: <" << e.what() << ">");
+        return EXIT_FAILURE;
+    } catch (...) {
+        LOGC("Agent stopped because of unknown unhandled exception.");
+        return EXIT_FAILURE;
     }
 
     return EXIT_SUCCESS;