Move the io_handler code out of shill_event
Create io_handler.h and glib_io_handler.{cc,h} to contain
all the io hander stuff. The code is ostensibly the same.
BUG=chromium-os:15803
TEST=Rerun unit tests
Change-Id: I3e37ac643c5c4449ae0db8b9a5f02225e8cd2aec
Reviewed-on: http://gerrit.chromium.org/gerrit/1630
Tested-by: Paul Stewart <pstew@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
diff --git a/glib_io_handler.cc b/glib_io_handler.cc
new file mode 100644
index 0000000..db01172
--- /dev/null
+++ b/glib_io_handler.cc
@@ -0,0 +1,58 @@
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdio.h>
+#include <glib.h>
+
+#include "shill/glib_io_handler.h"
+
+namespace shill {
+
+static gboolean DispatchIOHandler(GIOChannel *chan,
+ GIOCondition cond,
+ gpointer data);
+
+GlibIOInputHandler::GlibIOInputHandler(int fd,
+ Callback1<InputData*>::Type *callback)
+ : callback_(callback) {
+ channel_ = g_io_channel_unix_new(fd);
+ g_io_channel_set_close_on_unref(channel_, TRUE);
+ source_id_ = g_io_add_watch(channel_,
+ (GIOCondition)(G_IO_IN | G_IO_NVAL |
+ G_IO_HUP | G_IO_ERR),
+ DispatchIOHandler, this);
+}
+
+GlibIOInputHandler::~GlibIOInputHandler() {
+ g_source_remove(source_id_);
+ g_io_channel_shutdown(channel_, TRUE, NULL);
+ g_io_channel_unref(channel_);
+}
+
+static gboolean DispatchIOHandler(GIOChannel *chan,
+ GIOCondition cond,
+ gpointer data) {
+ GlibIOInputHandler *handler = static_cast<GlibIOInputHandler *>(data);
+ unsigned char buf[4096];
+ gsize len;
+ GIOError err;
+
+ if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
+ return FALSE;
+
+ err = g_io_channel_read(chan, reinterpret_cast<gchar *>(buf), sizeof(buf),
+ &len);
+ if (err) {
+ if (err == G_IO_ERROR_AGAIN)
+ return TRUE;
+ return FALSE;
+ }
+
+ InputData input_data = { buf, len };
+ handler->callback_->Run(&input_data);
+
+ return TRUE;
+}
+
+} // namespace shill
diff --git a/glib_io_handler.h b/glib_io_handler.h
new file mode 100644
index 0000000..32c0aae
--- /dev/null
+++ b/glib_io_handler.h
@@ -0,0 +1,30 @@
+// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SHILL_GLIB_IO_HANDLER_
+#define SHILL_GLIB_IO_HANDLER_
+
+#include <stdio.h>
+#include <glib.h>
+
+#include <base/callback_old.h>
+
+#include "shill/io_handler.h"
+
+namespace shill {
+
+class GlibIOInputHandler : public IOInputHandler {
+ public:
+ GIOChannel *channel_;
+ Callback1<InputData *>::Type *callback_;
+ guint source_id_;
+
+ GlibIOInputHandler(int fd, Callback1<InputData*>::Type *callback);
+ ~GlibIOInputHandler();
+};
+
+
+} // namespace shill
+
+#endif // SHILL_GLIB_IO_HANDLER_
diff --git a/io_handler.h b/io_handler.h
new file mode 100644
index 0000000..88f812b
--- /dev/null
+++ b/io_handler.h
@@ -0,0 +1,23 @@
+// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SHILL_IO_HANDLER_
+#define SHILL_IO_HANDLER_
+
+namespace shill {
+
+struct InputData {
+ unsigned char *buf;
+ size_t len;
+};
+
+class IOInputHandler {
+ public:
+ IOInputHandler() {}
+ virtual ~IOInputHandler() {}
+};
+
+} // namespace shill
+
+#endif // SHILL_IO_HANDLER_
diff --git a/shill_event.cc b/shill_event.cc
index a0832a6..80ed0c8 100644
--- a/shill_event.cc
+++ b/shill_event.cc
@@ -8,60 +8,11 @@
#include <base/callback_old.h>
#include <base/message_loop_proxy.h>
+#include "shill/glib_io_handler.h"
#include "shill/shill_event.h"
namespace shill {
-static gboolean DispatchIOHandler(GIOChannel *chan,
- GIOCondition cond,
- gpointer data);
-
-class GlibIOInputHandler : public IOInputHandler {
-public:
- GIOChannel *channel_;
- Callback1<InputData *>::Type *callback_;
- guint source_id_;
-
- GlibIOInputHandler(int fd, Callback1<InputData*>::Type *callback)
- : callback_(callback) {
- channel_ = g_io_channel_unix_new(fd);
- g_io_channel_set_close_on_unref(channel_, TRUE);
- source_id_ = g_io_add_watch(channel_,
- (GIOCondition)(G_IO_IN | G_IO_NVAL |
- G_IO_HUP | G_IO_ERR),
- DispatchIOHandler, this);
- }
-
- ~GlibIOInputHandler() {
- g_source_remove(source_id_);
- g_io_channel_shutdown(channel_, TRUE, NULL);
- g_io_channel_unref(channel_);
- }
-};
-
-static gboolean DispatchIOHandler(GIOChannel *chan,
- GIOCondition cond,
- gpointer data) {
- GlibIOInputHandler *handler = static_cast<GlibIOInputHandler *>(data);
- unsigned char buf[4096];
- gsize len;
- GIOError err;
-
- if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
- return FALSE;
-
- err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
- if (err) {
- if (err == G_IO_ERROR_AGAIN)
- return TRUE;
- return FALSE;
- }
-
- InputData input_data = { buf, len };
- handler->callback_->Run(&input_data);
-
- return TRUE;
-}
EventDispatcher::EventDispatcher()
: dont_use_directly_(new MessageLoopForUI),
message_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()) {
diff --git a/shill_event.h b/shill_event.h
index cc2bdb1..bed38c7 100644
--- a/shill_event.h
+++ b/shill_event.h
@@ -13,6 +13,8 @@
#include <base/memory/scoped_ptr.h>
#include <base/message_loop.h>
+#include "shill/io_handler.h"
+
namespace base {
class MessageLoopProxy;
} // namespace base
@@ -20,17 +22,6 @@
namespace shill {
-struct InputData {
- unsigned char *buf;
- size_t len;
-};
-
-class IOInputHandler {
- public:
- IOInputHandler() {}
- virtual ~IOInputHandler() {}
-};
-
// This is the main event dispatcher. It contains a central instance, and
// is the entity responsible for dispatching events out of all queues to
// their listeners during the idle loop.