Skip to content

Commit

Permalink
Merge pull request #703 from imobachgs/async-std-channels
Browse files Browse the repository at this point in the history
Use `async_std` channels and tasks
  • Loading branch information
imobachgs authored Aug 16, 2023
2 parents f86969b + 5aaac0e commit ba63172
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
16 changes: 10 additions & 6 deletions rust/agama-dbus-server/src/network/dbus/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ use crate::network::{
use log;

use agama_lib::network::types::SSID;
use async_std::{channel::Sender, sync::Arc};
use futures::lock::{MappedMutexGuard, Mutex, MutexGuard};
use std::{
net::{AddrParseError, Ipv4Addr},
sync::{mpsc::Sender, Arc},
};
use std::net::{AddrParseError, Ipv4Addr};
use zbus::{
dbus_interface,
zvariant::{ObjectPath, OwnedObjectPath},
Expand Down Expand Up @@ -129,6 +127,7 @@ impl Connections {
let actions = self.actions.lock().await;
actions
.send(Action::AddConnection(id, ty.try_into()?))
.await
.unwrap();
Ok(())
}
Expand All @@ -151,6 +150,7 @@ impl Connections {
let actions = self.actions.lock().await;
actions
.send(Action::RemoveConnection(id.to_string()))
.await
.unwrap();
Ok(())
}
Expand All @@ -160,7 +160,7 @@ impl Connections {
/// It includes adding, updating and removing connections as needed.
pub async fn apply(&self) -> zbus::fdo::Result<()> {
let actions = self.actions.lock().await;
actions.send(Action::Apply).unwrap();
actions.send(Action::Apply).await.unwrap();
Ok(())
}
}
Expand Down Expand Up @@ -232,6 +232,7 @@ impl Ipv4 {
let actions = self.actions.lock().await;
actions
.send(Action::UpdateConnection(connection.clone()))
.await
.unwrap();
Ok(())
}
Expand Down Expand Up @@ -376,7 +377,10 @@ impl Wireless {
) -> zbus::fdo::Result<()> {
let actions = self.actions.lock().await;
let connection = NetworkConnection::Wireless(connection.clone());
actions.send(Action::UpdateConnection(connection)).unwrap();
actions
.send(Action::UpdateConnection(connection))
.await
.unwrap();
Ok(())
}
}
Expand Down
24 changes: 11 additions & 13 deletions rust/agama-dbus-server/src/network/dbus/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This module defines a D-Bus service which exposes Agama's network configuration.
use crate::network::NetworkSystem;
use agama_lib::connection_to;
use std::{error::Error, thread};
use std::error::Error;

/// Represents the Agama networking D-Bus service.
///
Expand All @@ -20,19 +20,17 @@ impl NetworkService {
.await
.expect("Could not read network state");

thread::spawn(move || {
async_std::task::block_on(async {
network
.setup()
.await
.expect("Could not set up the D-Bus tree");
connection
.request_name(SERVICE_NAME)
.await
.unwrap_or_else(|_| panic!("Could not request name {SERVICE_NAME}"));
async_std::task::spawn(async move {
network
.setup()
.await
.expect("Could not set up the D-Bus tree");
connection
.request_name(SERVICE_NAME)
.await
.unwrap_or_else(|_| panic!("Could not request name {SERVICE_NAME}"));

network.listen().await;
})
network.listen().await;
});
Ok(())
}
Expand Down
3 changes: 1 addition & 2 deletions rust/agama-dbus-server/src/network/dbus/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use futures::lock::Mutex;
use zbus::zvariant::{ObjectPath, OwnedObjectPath};

use crate::network::{action::Action, dbus::interfaces, model::*};
use async_std::{channel::Sender, sync::Arc};
use log;
use std::collections::HashMap;
use std::sync::mpsc::Sender;
use std::sync::Arc;

const CONNECTIONS_PATH: &str = "/org/opensuse/Agama/Network1/connections";
const DEVICES_PATH: &str = "/org/opensuse/Agama/Network1/devices";
Expand Down
6 changes: 3 additions & 3 deletions rust/agama-dbus-server/src/network/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::network::{
dbus::Tree, model::Connection, nm::NetworkManagerAdapter, Action, Adapter, NetworkState,
};
use agama_lib::error::ServiceError;
use async_std::channel::{unbounded, Receiver, Sender};
use std::error::Error;
use std::sync::mpsc::{channel, Receiver, Sender};

/// Represents the network system, wrapping a [NetworkState] and setting up the D-Bus tree.
pub struct NetworkSystem {
Expand All @@ -17,7 +17,7 @@ pub struct NetworkSystem {

impl NetworkSystem {
pub fn new(state: NetworkState, conn: zbus::Connection) -> Self {
let (actions_tx, actions_rx) = channel();
let (actions_tx, actions_rx) = unbounded();
let tree = Tree::new(conn, actions_tx.clone());
Self {
state,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl NetworkSystem {
///
/// This function is expected to be executed on a separate thread.
pub async fn listen(&mut self) {
while let Ok(action) = self.actions_rx.recv() {
while let Ok(action) = self.actions_rx.recv().await {
if let Err(error) = self.dispatch_action(action).await {
eprintln!("Could not process the action: {}", error);
}
Expand Down
3 changes: 1 addition & 2 deletions setup-service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ test -f /etc/zypp/repos.d/d_l_python.repo || \
$SUDO zypper --non-interactive \
addrepo https://download.opensuse.org/repositories/devel:/languages:/python/openSUSE_Tumbleweed/ d_l_python
$SUDO zypper --non-interactive --gpg-auto-import-keys install gcc gcc-c++ make openssl-devel ruby-devel \
python-langtable-data \
git augeas-devel jemalloc-devel || exit 1
python-langtable-data git augeas-devel jemalloc-devel awk || exit 1

# only install cargo if it is not available (avoid conflicts with rustup)
which cargo || $SUDO zypper --non-interactive install cargo
Expand Down

0 comments on commit ba63172

Please sign in to comment.