Back to index

unity  6.0.0
UBusWrapper.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00017  */
00018 
00019 #include "UBusWrapper.h"
00020 
00021 namespace unity
00022 {
00023 
00024 UBusManager::UBusManager()
00025   : server_(ubus_server_get_default())
00026 {}
00027 
00028 UBusManager::~UBusManager()
00029 {
00030   for (auto connection: connections_)
00031   {
00032     ubus_server_unregister_interest(server_, connection->id);
00033   }
00034 }
00035 
00036 void UBusManager::RegisterInterest(std::string const& interest_name,
00037                                    UBusManagerCallback slot)
00038 {
00039   UBusConnection::Ptr connection (new UBusConnection());
00040   connection->manager = this;
00041   connection->name = interest_name;
00042   connection->slot = slot;
00043   connection->id = ubus_server_register_interest(server_,
00044                                                  interest_name.c_str(),
00045                                                  UBusManager::OnCallback,
00046                                                  connection.get());
00047   connections_.push_back(connection);
00048 }
00049 
00050 void UBusManager::UnregisterInterest(std::string const& interest_name)
00051 {
00052   for (auto it = connections_.begin(); it != connections_.end(); ++it)
00053   {
00054     if ((*it)->name == interest_name)
00055     {
00056       ubus_server_unregister_interest(server_, (*it)->id);
00057       connections_.erase(it);
00058       break;
00059     }
00060   }
00061 }
00062 
00063 void UBusManager::OnCallback(GVariant* args, gpointer user_data)
00064 {
00065   UBusConnection* connection = static_cast<UBusConnection*>(user_data);
00066 
00067   connection->slot(args);
00068 }
00069 
00070 void UBusManager::SendMessage(std::string const& message_name, GVariant* args)
00071 {
00072   ubus_server_send_message(server_, message_name.c_str(), args);
00073 }
00074 
00075 }