Skip to content

Commit

Permalink
Update dependencies + remove once_cell.
Browse files Browse the repository at this point in the history
  • Loading branch information
brocaar committed Jan 10, 2025
1 parent e38e605 commit 4eb94ca
Show file tree
Hide file tree
Showing 9 changed files with 345 additions and 226 deletions.
492 changes: 306 additions & 186 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@
simple_logger = "5.0"
syslog = "7.0"
toml = "0.8"
handlebars = "6.0"
handlebars = "6.3"
anyhow = "1.0"
humantime-serde = "1.1"
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.39", features = [
tokio = { version = "1.43", features = [
"macros",
"rt-multi-thread",
"time",
"fs",
"sync",
] }
once_cell = "1.19"
hex = "0.4.3"
rand = "0.8"
signal-hook = "0.3"
Expand All @@ -46,7 +45,7 @@

[dev-dependencies]
zeromq = "0.4"
bytes = "1.7"
bytes = "1.9"

[profile.release]
strip = true
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.79.0"
channel = "1.81.0"
components = ["rustfmt", "clippy"]
profile = "default"
2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-24.05.tar.gz") {} }:
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-24.11.tar.gz") {} }:

pkgs.mkShell {
buildInputs = [
Expand Down
18 changes: 9 additions & 9 deletions src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::sync::OnceLock;
use std::thread;

use anyhow::Result;
use chirpstack_api::prost::Message;
use log::{debug, error, info, trace};
use once_cell::sync::OnceCell;
use tokio::sync::{mpsc, oneshot, Mutex};

use crate::config::Configuration;
use crate::{helpers, mesh, proxy};
use chirpstack_api::gw;

static GATEWAY_ID: OnceCell<Mutex<[u8; 8]>> = OnceCell::new();
static RELAY_ID: OnceCell<Mutex<[u8; 4]>> = OnceCell::new();
static GATEWAY_ID: OnceLock<Mutex<[u8; 8]>> = OnceLock::new();
static RELAY_ID: OnceLock<Mutex<[u8; 4]>> = OnceLock::new();

static CONCENTRATORD_CMD_CHAN: OnceCell<CommandChannel> = OnceCell::new();
static MESH_CONCENTRATORD_CMD_CHAN: OnceCell<CommandChannel> = OnceCell::new();
static CONCENTRATORD_CMD_CHAN: OnceLock<CommandChannel> = OnceLock::new();
static MESH_CONCENTRATORD_CMD_CHAN: OnceLock<CommandChannel> = OnceLock::new();

type Event = (String, Vec<u8>);
type Command = ((String, Vec<u8>), oneshot::Sender<Result<Vec<u8>>>);
Expand Down Expand Up @@ -66,13 +66,13 @@ async fn setup_concentratord(conf: &Configuration) -> Result<()> {
info!("Retrieved Gateway ID: {}", hex::encode(gateway_id));
GATEWAY_ID
.set(Mutex::new(gateway_id))
.map_err(|e| anyhow!("OnceCell error: {:?}", e))?;
.map_err(|e| anyhow!("OnceLock error: {:?}", e))?;

// Set CMD channel.

CONCENTRATORD_CMD_CHAN
.set(cmd_tx)
.map_err(|e| anyhow!("OnceCell error: {:?}", e))?;
.map_err(|e| anyhow!("OnceLock error: {:?}", e))?;

// Setup ZMQ event.

Expand Down Expand Up @@ -163,13 +163,13 @@ async fn setup_mesh_conncentratord(conf: &Configuration) -> Result<()> {
relay_id.copy_from_slice(&resp[4..]);
RELAY_ID
.set(Mutex::new(relay_id))
.map_err(|e| anyhow!("OnceCell error: {:?}", e))?;
.map_err(|e| anyhow!("OnceLock error: {:?}", e))?;

// set CMD channel.

MESH_CONCENTRATORD_CMD_CHAN
.set(cmd_tx)
.map_err(|e| anyhow!("OnceCell error: {:?}", e))?;
.map_err(|e| anyhow!("OnceLock error: {:?}", e))?;

// Setup ZMQ event.

Expand Down
9 changes: 4 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::fs;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;

use anyhow::Result;
use once_cell::sync::OnceCell;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::aes128::Aes128Key;

static CONFIG: OnceCell<Mutex<Arc<Configuration>>> = OnceCell::new();
static CONFIG: OnceLock<Mutex<Arc<Configuration>>> = OnceLock::new();

#[derive(Serialize, Deserialize, Default)]
#[serde(default)]
Expand Down Expand Up @@ -225,13 +224,13 @@ impl<'de> Deserialize<'de> for CodeRate {
pub fn set(c: Configuration) -> Result<()> {
CONFIG
.set(Mutex::new(Arc::new(c)))
.map_err(|_| anyhow!("Set OnceCell error"))
.map_err(|_| anyhow!("Set OnceLock error"))
}

pub fn get() -> Arc<Configuration> {
let conf = CONFIG
.get()
.ok_or_else(|| anyhow!("OnceCell is not set"))
.ok_or_else(|| anyhow!("OnceLock is not set"))
.unwrap();

conf.lock().unwrap().clone()
Expand Down
9 changes: 5 additions & 4 deletions src/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::HashMap;
use std::sync::LazyLock;
use std::sync::Mutex;

use anyhow::Result;
use chirpstack_api::gw;
use log::{info, trace, warn};
use once_cell::sync::Lazy;
use rand::random;

use crate::{
Expand All @@ -22,9 +22,10 @@ use crate::{
static CTX_PREFIX: [u8; 3] = [1, 2, 3];
static MESH_CHANNEL: Mutex<usize> = Mutex::new(0);
static UPLINK_ID: Mutex<u16> = Mutex::new(0);
static UPLINK_CONTEXT: Lazy<Mutex<HashMap<u16, Vec<u8>>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
static PAYLOAD_CACHE: Lazy<Mutex<Cache<PayloadCache>>> = Lazy::new(|| Mutex::new(Cache::new(64)));
static UPLINK_CONTEXT: LazyLock<Mutex<HashMap<u16, Vec<u8>>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
static PAYLOAD_CACHE: LazyLock<Mutex<Cache<PayloadCache>>> =
LazyLock::new(|| Mutex::new(Cache::new(64)));

// Handle LoRaWAN payload (non-proprietary).
pub async fn handle_uplink(border_gateway: bool, pl: gw::UplinkFrame) -> Result<()> {
Expand Down
6 changes: 3 additions & 3 deletions src/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::sync::OnceLock;
use std::thread;

use anyhow::Result;
use chirpstack_api::gw;
use chirpstack_api::prost::Message;
use log::{error, info, trace};
use once_cell::sync::OnceCell;
use tokio::sync::{mpsc, oneshot};

use crate::backend;
use crate::config::Configuration;
use crate::helpers;
use crate::mesh;

static EVENT_CHAN: OnceCell<EventChannel> = OnceCell::new();
static EVENT_CHAN: OnceLock<EventChannel> = OnceLock::new();

type Event = (String, Vec<u8>);
type Command = ((String, Vec<u8>), oneshot::Sender<Vec<u8>>);
Expand Down Expand Up @@ -54,7 +54,7 @@ pub async fn setup(conf: &Configuration) -> Result<()> {

EVENT_CHAN
.set(event_tx)
.map_err(|e| anyhow!("OnceCell error: {:?}", e))?;
.map_err(|e| anyhow!("OnceLock error: {:?}", e))?;

// Setup ZMQ command.

Expand Down
26 changes: 13 additions & 13 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use std::sync::OnceLock;
use std::time::Duration;

use once_cell::sync::OnceCell;
use tokio::fs::remove_file;
use tokio::sync::Mutex;
use tokio::time::sleep;
use zeromq::{Socket, SocketRecv, SocketSend};

use chirpstack_gateway_mesh::config::{self, Configuration};

pub static FORWARDER_EVENT_SOCK: OnceCell<Mutex<zeromq::SubSocket>> = OnceCell::new();
pub static FORWARDER_COMMAND_SOCK: OnceCell<Mutex<zeromq::ReqSocket>> = OnceCell::new();
pub static FORWARDER_EVENT_SOCK: OnceLock<Mutex<zeromq::SubSocket>> = OnceLock::new();
pub static FORWARDER_COMMAND_SOCK: OnceLock<Mutex<zeromq::ReqSocket>> = OnceLock::new();

pub static BACKEND_EVENT_SOCK: OnceCell<Mutex<zeromq::PubSocket>> = OnceCell::new();
pub static BACKEND_COMMAND_SOCK: OnceCell<Mutex<zeromq::RepSocket>> = OnceCell::new();
pub static BACKEND_EVENT_SOCK: OnceLock<Mutex<zeromq::PubSocket>> = OnceLock::new();
pub static BACKEND_COMMAND_SOCK: OnceLock<Mutex<zeromq::RepSocket>> = OnceLock::new();

pub static MESH_BACKEND_EVENT_SOCK: OnceCell<Mutex<zeromq::PubSocket>> = OnceCell::new();
pub static MESH_BACKEND_COMMAND_SOCK: OnceCell<Mutex<zeromq::RepSocket>> = OnceCell::new();
pub static MESH_BACKEND_EVENT_SOCK: OnceLock<Mutex<zeromq::PubSocket>> = OnceLock::new();
pub static MESH_BACKEND_COMMAND_SOCK: OnceLock<Mutex<zeromq::RepSocket>> = OnceLock::new();

pub async fn setup(border_gateway: bool) {
let conf = get_config(border_gateway);
Expand Down Expand Up @@ -87,7 +87,7 @@ async fn init_forwarder(border_gateway: bool) {

FORWARDER_EVENT_SOCK
.set(Mutex::new(event_sock))
.map_err(|_| anyhow!("OnceCell error"))
.map_err(|_| anyhow!("OnceLock error"))
.unwrap();

let mut cmd_sock = zeromq::ReqSocket::new();
Expand All @@ -98,7 +98,7 @@ async fn init_forwarder(border_gateway: bool) {

FORWARDER_COMMAND_SOCK
.set(Mutex::new(cmd_sock))
.map_err(|_| anyhow!("OnceCell error"))
.map_err(|_| anyhow!("OnceLock error"))
.unwrap();

sleep(Duration::from_millis(100)).await;
Expand All @@ -116,7 +116,7 @@ async fn init_backend(border_gateway: bool) {

BACKEND_EVENT_SOCK
.set(Mutex::new(event_sock))
.map_err(|_| anyhow!("OnceCell error"))
.map_err(|_| anyhow!("OnceLock error"))
.unwrap();

let mut cmd_sock = zeromq::RepSocket::new();
Expand All @@ -128,7 +128,7 @@ async fn init_backend(border_gateway: bool) {

BACKEND_COMMAND_SOCK
.set(Mutex::new(cmd_sock))
.map_err(|_| anyhow!("OnceCell error"))
.map_err(|_| anyhow!("OnceLock error"))
.unwrap();

let mut event_sock = zeromq::PubSocket::new();
Expand All @@ -140,7 +140,7 @@ async fn init_backend(border_gateway: bool) {

MESH_BACKEND_EVENT_SOCK
.set(Mutex::new(event_sock))
.map_err(|_| anyhow!("OnceCell error"))
.map_err(|_| anyhow!("OnceLock error"))
.unwrap();

let mut cmd_sock = zeromq::RepSocket::new();
Expand All @@ -152,7 +152,7 @@ async fn init_backend(border_gateway: bool) {

MESH_BACKEND_COMMAND_SOCK
.set(Mutex::new(cmd_sock))
.map_err(|_| anyhow!("OnceCell error"))
.map_err(|_| anyhow!("OnceLock error"))
.unwrap();

sleep(Duration::from_millis(300)).await;
Expand Down

0 comments on commit 4eb94ca

Please sign in to comment.