OSDN Git Service

Event loop wrapper classes for wificond
authorNingyuan Wang <nywang@google.com>
Tue, 24 May 2016 20:35:04 +0000 (13:35 -0700)
committerNingyuan Wang <nywang@google.com>
Thu, 26 May 2016 21:31:57 +0000 (14:31 -0700)
This CL includes the following change:
  1. An abstract class EventLoop for the event loop.
  2. An impementation LooperBackedEventLoop using
  looper in libutils.

BUG=28867514
TEST=compile
TEST=manually tested

Change-Id: Ieeded846761d627487144d122d3a733abda46d1e

Android.mk
event_loop.h [new file with mode: 0644]
looper_backed_event_loop.cpp [new file with mode: 0644]
looper_backed_event_loop.h [new file with mode: 0644]
main.cpp

index d92ad31..9229728 100644 (file)
@@ -23,9 +23,12 @@ LOCAL_CLANG := true
 LOCAL_CPPFLAGS := -std=c++11 -Wall -Werror -Wno-unused-parameter
 LOCAL_INIT_RC := wificond.rc
 LOCAL_SRC_FILES := \
-    main.cpp
+    main.cpp \
+    looper_backed_event_loop.cpp
 LOCAL_SHARED_LIBRARIES := \
-    libbase
+    libbase \
+    libutils
+LOCAL_MODULE_HOST_OS := linux
 include $(BUILD_EXECUTABLE)
 
 ###
diff --git a/event_loop.h b/event_loop.h
new file mode 100644 (file)
index 0000000..38db142
--- /dev/null
@@ -0,0 +1,45 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// 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 WIFICOND_EVENT_LOOP_H_
+#define WIFICOND_EVENT_LOOP_H_
+
+#include <functional>
+
+namespace android {
+namespace wificond {
+
+// Abstract class for dispatching tasks.
+class EventLoop {
+ public:
+  virtual ~EventLoop() {}
+
+  // Enqueues a callback.
+  // This function can be called on any thread.
+  virtual void PostTask(const std::function<void()>& callback) = 0;
+
+  // Enqueues a callback to be processed after a specified period of time.
+  // |delay_ms| is delay time in milliseconds. It should not be negative.
+  // This function can be called on any thread.
+  virtual void PostDelayedTask(const std::function<void()>& callback,
+                               int64_t delay_ms) = 0;
+  //TODO(nywang): monitoring file descriptor for data
+};
+
+}  // namespace wificond
+}  // namespace android
+
+#endif  // WIFICOND_EVENT_LOOP_H_
diff --git a/looper_backed_event_loop.cpp b/looper_backed_event_loop.cpp
new file mode 100644 (file)
index 0000000..ef2389a
--- /dev/null
@@ -0,0 +1,80 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// 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 <looper_backed_event_loop.h>
+
+#include <utils/Looper.h>
+#include <utils/Timers.h>
+
+namespace {
+
+class EventLoopCallback : public android::MessageHandler {
+ public:
+  explicit EventLoopCallback(const std::function<void()>& callback)
+      : callback_(callback) {
+  }
+
+  ~EventLoopCallback() override = default;
+
+  virtual void handleMessage(const android::Message& message) {
+    callback_();
+  }
+
+ private:
+  const std::function<void()> callback_;
+
+  DISALLOW_COPY_AND_ASSIGN(EventLoopCallback);
+};
+
+}  // namespace
+
+namespace android {
+namespace wificond {
+
+
+LooperBackedEventLoop::LooperBackedEventLoop()
+    : should_continue_(true) {
+  looper_ = android::Looper::prepare(Looper::PREPARE_ALLOW_NON_CALLBACKS);
+}
+
+LooperBackedEventLoop::~LooperBackedEventLoop() {
+}
+
+void LooperBackedEventLoop::PostTask(const std::function<void()>& callback) {
+  sp<android::MessageHandler> event_loop_callback =
+      new EventLoopCallback(callback);
+  looper_->sendMessage(event_loop_callback, NULL);
+}
+
+void LooperBackedEventLoop::PostDelayedTask(
+    const std::function<void()>& callback,
+    int64_t delay_ms) {
+  sp<android::MessageHandler> looper_callback = new EventLoopCallback(callback);
+  looper_->sendMessageDelayed(ms2ns(delay_ms), looper_callback, NULL);
+}
+
+void LooperBackedEventLoop::Poll() {
+  while (should_continue_) {
+    looper_->pollOnce(-1);
+  }
+}
+
+void LooperBackedEventLoop::TriggerExit() {
+  PostTask([this](){ should_continue_ = false; });
+}
+
+}  // namespace wificond
+}  // namespace android
diff --git a/looper_backed_event_loop.h b/looper_backed_event_loop.h
new file mode 100644 (file)
index 0000000..0cb6d45
--- /dev/null
@@ -0,0 +1,59 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// 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 WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
+#define WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
+
+#include <event_loop.h>
+
+#include <android-base/macros.h>
+#include <utils/Looper.h>
+
+namespace android {
+namespace wificond {
+
+class LooperBackedEventLoop: public EventLoop {
+ public:
+  LooperBackedEventLoop();
+  ~LooperBackedEventLoop() override;
+
+  // See event_loop.h
+  void PostTask(const std::function<void()>& callback) override;
+
+  // See event_loop.h
+  void PostDelayedTask(const std::function<void()>& callback,
+                       int64_t delay_ms) override;
+
+  // Performs all pending callbacks and waiting for new events until
+  // TriggerExit() is called.
+  // This method can be called from any thread context.
+  void Poll();
+
+  // Posts a task to stop event loop polling.
+  // This method can be called from any thread context.
+  void TriggerExit();
+
+ private:
+  sp<android::Looper> looper_;
+  bool should_continue_;
+
+  DISALLOW_COPY_AND_ASSIGN(LooperBackedEventLoop);
+};
+
+}  // namespace wificond
+}  // namespace android
+
+#endif  // WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
index 42ec251..028b787 100644 (file)
--- a/main.cpp
+++ b/main.cpp
 #include <unistd.h>
 
 #include <csignal>
+#include <memory>
 
 #include <android-base/logging.h>
+#include <android-base/macros.h>
+
+#include <looper_backed_event_loop.h>
 
 namespace {
 
-volatile bool ShouldContinue = true;
+class ScopedSignalHandler final {
+ public:
+  ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
+    if (s_event_loop_ != nullptr) {
+      LOG(FATAL) << "Only instantiate one signal handler per process!";
+    }
+    s_event_loop_ = event_loop;
+    std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
+    std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
+  }
+
+  ~ScopedSignalHandler() {
+    std::signal(SIGINT, SIG_DFL);
+    std::signal(SIGTERM, SIG_DFL);
+    s_event_loop_ = nullptr;
+  }
+
+ private:
+  static android::wificond::LooperBackedEventLoop* s_event_loop_;
+  static void LeaveLoop(int signal) {
+    if (s_event_loop_ != nullptr) {
+      s_event_loop_->TriggerExit();
+    }
+  }
+
+  DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
+};
+
+android::wificond::LooperBackedEventLoop*
+    ScopedSignalHandler::s_event_loop_ = nullptr;
 
 }  // namespace
 
-void leave_loop(int signal) {
-  ShouldContinue = false;
+void callback_for_test() {
+  LOG(INFO) << "callback is executed...";
 }
 
 int main(int argc, char** argv) {
   android::base::InitLogging(argv);
   LOG(INFO) << "wificond is starting up...";
-  std::signal(SIGINT, &leave_loop);
-  std::signal(SIGTERM, &leave_loop);
-  while (ShouldContinue) {
-    sleep(1);
-  }
+  std::unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher_(
+      new android::wificond::LooperBackedEventLoop());
+  ScopedSignalHandler scoped_signal_handler(event_dispatcher_.get());
+  //TODO(nywang): b//28982981, Remove this when we have unittest for event loop checked in.
+  event_dispatcher_->PostTask(&callback_for_test);
+  event_dispatcher_->PostDelayedTask(&callback_for_test, 5000);
+  event_dispatcher_->Poll();
   LOG(INFO) << "Leaving the loop...";
   return 0;
 }