Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <glib.h> |
| 7 | |
| 8 | #include "shill/shill_event.h" |
| 9 | |
| 10 | namespace shill { |
| 11 | |
| 12 | EventQueueItem::EventQueueItem(EventDispatcher *dispatcher) |
| 13 | : dispatcher_(dispatcher) { |
| 14 | dispatcher->RegisterCallbackQueue(this); |
| 15 | } |
| 16 | |
| 17 | EventQueueItem::~EventQueueItem() { |
| 18 | dispatcher_->UnregisterCallbackQueue(this); |
| 19 | } |
| 20 | |
| 21 | void EventQueueItem::AlertDispatcher() { |
| 22 | dispatcher_->ExecuteOnIdle(); |
| 23 | } |
| 24 | |
| 25 | void EventDispatcher::DispatchEvents() { |
| 26 | for (size_t idx = 0; idx < queue_list_.size(); ++idx) |
| 27 | queue_list_[idx]->Dispatch(); |
| 28 | } |
| 29 | |
| 30 | static gboolean DispatchEventsHandler(gpointer data) { |
| 31 | EventDispatcher *dispatcher = static_cast<EventDispatcher *>(data); |
| 32 | dispatcher->DispatchEvents(); |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | void EventDispatcher::ExecuteOnIdle() { |
| 37 | g_idle_add(&DispatchEventsHandler, this); |
| 38 | } |
| 39 | |
| 40 | void EventDispatcher::RegisterCallbackQueue(EventQueueItem *queue) { |
| 41 | queue_list_.push_back(queue); |
| 42 | } |
| 43 | |
| 44 | void EventDispatcher::UnregisterCallbackQueue(EventQueueItem *queue) { |
| 45 | for (size_t idx = 0; idx < queue_list_.size(); ++idx) { |
| 46 | if (queue_list_[idx] == queue) { |
| 47 | queue_list_.erase(queue_list_.begin() + idx); |
| 48 | return; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame^] | 53 | static gboolean DispatchIOHandler(GIOChannel *chan, GIOCondition cond, |
| 54 | gpointer data); |
| 55 | |
| 56 | class GlibIOInputHandler : public IOInputHandler { |
| 57 | public: |
| 58 | GIOChannel *channel_; |
| 59 | Callback<InputData *> *callback_; |
| 60 | guint source_id_; |
| 61 | EventDispatcher *dispatcher_; |
| 62 | |
| 63 | GlibIOInputHandler(EventDispatcher *dispatcher, int fd, |
| 64 | Callback<InputData *> *callback) : |
| 65 | dispatcher_(dispatcher), callback_(callback) { |
| 66 | channel_ = g_io_channel_unix_new(fd); |
| 67 | g_io_channel_set_close_on_unref(channel_, TRUE); |
| 68 | source_id_ = g_io_add_watch(channel_, |
| 69 | (GIOCondition)(G_IO_IN | G_IO_NVAL | |
| 70 | G_IO_HUP | G_IO_ERR), |
| 71 | DispatchIOHandler, this); |
| 72 | } |
| 73 | |
| 74 | ~GlibIOInputHandler() { |
| 75 | g_source_remove(source_id_); |
| 76 | g_io_channel_shutdown(channel_, TRUE, NULL); |
| 77 | g_io_channel_unref(channel_); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | static gboolean DispatchIOHandler(GIOChannel *chan, GIOCondition cond, |
| 82 | gpointer data) { |
| 83 | GlibIOInputHandler *handler = static_cast<GlibIOInputHandler *>(data); |
| 84 | unsigned char buf[4096]; |
| 85 | gsize len; |
| 86 | GIOError err; |
| 87 | |
| 88 | if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) |
| 89 | return FALSE; |
| 90 | |
| 91 | err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len); |
| 92 | if (err) { |
| 93 | if (err == G_IO_ERROR_AGAIN) |
| 94 | return TRUE; |
| 95 | return FALSE; |
| 96 | } |
| 97 | |
| 98 | InputData input_data = { buf, len }; |
| 99 | handler->callback_->Run(&input_data); |
| 100 | |
| 101 | return TRUE; |
| 102 | } |
| 103 | |
| 104 | IOInputHandler *EventDispatcher::CreateInputHandler(int fd, |
| 105 | Callback<InputData *> *callback) { |
| 106 | return new GlibIOInputHandler(this, fd, callback); |
| 107 | } |
| 108 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 109 | } // namespace shill |