Skip to content

Commit

Permalink
Implement daemonizer component (#9)
Browse files Browse the repository at this point in the history
* integrate daemonizer component

* fix dependency conflicts between windows and unix builds

* add canape project for daemon example

* rewrite deamonizer component with nix

* Minor changes

* Minor changes

* install daemon with shell script

* add logir cfg item for daemonization

* split syslog logging from process logging

* minor fix

* update cargo lock

* make xcp loglvl configurable in xcp daemon example

* append to daemon logfile instead of overwrite

* prevent unintentional daemonizer builds outside unix platforms

* re-export xcp dependencies

* ignore snake case warnings in IdlGenrator output

* fix xcp daemon example

---------

Co-authored-by: Rainer Zaiser <[email protected]>
  • Loading branch information
GjergjiShkurti and RainerZ authored Dec 3, 2024
1 parent eebe736 commit 45eff80
Show file tree
Hide file tree
Showing 24 changed files with 50 additions and 88 deletions.
22 changes: 0 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions examples/hello_xcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description/"}
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }



xcp = { path = "../../", features = [] }
1 change: 0 additions & 1 deletion examples/hello_xcp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use log::{debug, error, info, trace, warn};
use std::{fmt::Debug, thread, time::Duration};

use xcp::*;
use xcp_type_description::prelude::*;

//-----------------------------------------------------------------------------
// Calibration parameters
Expand Down
4 changes: 1 addition & 3 deletions examples/multi_thread_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description/"}
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }
xcp = { path = "../../", features = [] }
1 change: 0 additions & 1 deletion examples/multi_thread_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::{
use anyhow::Result;

use xcp::*;
use xcp_type_description::prelude::*;

// Static application start time
lazy_static::lazy_static! {
Expand Down
4 changes: 0 additions & 4 deletions examples/point_cloud_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ lazy_static = "1.4.0"
cdr = "0.2.4"

xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description/"}
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }
xcp_idl_generator = { path = "../../xcp_idl_generator/"}
xcp_idl_generator_derive = { path = "../../xcp_idl_generator/xcp_idl_generator_derive/"}



2 changes: 0 additions & 2 deletions examples/point_cloud_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use std::{
// XCP

use xcp::*;
use xcp_idl_generator::prelude::*;
use xcp_type_description::prelude::*;

//-----------------------------------------------------------------------------
// Defaults
Expand Down
4 changes: 0 additions & 4 deletions examples/protobuf_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ prost-types = "0.13.1"


xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description/"}
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }



4 changes: 0 additions & 4 deletions examples/rayon_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,5 @@ serde_json = "1.0"
lazy_static = "1.4.0"

xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description/"}
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }




1 change: 0 additions & 1 deletion examples/rayon_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use num::Complex;
use rayon::prelude::*;
use std::{thread, time::Duration};
use xcp::*;
use xcp_type_description::prelude::*;

//---------------------------------------------------------------------------------------
// Calibratable parameters
Expand Down
5 changes: 1 addition & 4 deletions examples/scoped_threads/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@ lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description/"}
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }

xcp = { path = "../../", features = [] }
1 change: 0 additions & 1 deletion examples/scoped_threads/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use log::{debug, error, info, trace, warn};
use std::{fmt::Debug, thread, time::Duration};

use xcp::*;
use xcp_type_description::prelude::*;

//-----------------------------------------------------------------------------

Expand Down
7 changes: 1 addition & 6 deletions examples/single_thread_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,4 @@ lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description/"}
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }
xcp_idl_generator = { path = "../../xcp_idl_generator/"}
xcp_idl_generator_derive = { path = "../../xcp_idl_generator/xcp_idl_generator_derive/"}

xcp = { path = "../../", features = [] }
1 change: 0 additions & 1 deletion examples/single_thread_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::{
};

use xcp::*;
use xcp_type_description::prelude::*;

//-----------------------------------------------------------------------------
// Demo calibration parameters
Expand Down
3 changes: 0 additions & 3 deletions examples/tokio_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description/"}
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }

1 change: 0 additions & 1 deletion examples/tokio_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use core::f64::consts::PI;
use std::error::Error;

use xcp::*;
use xcp_type_description::prelude::*;

include!("../../../tests/xcp_server_task.rs");

Expand Down
4 changes: 0 additions & 4 deletions examples/type_description_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@ edition = "2021"

[dependencies]
xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description" }
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }



3 changes: 1 addition & 2 deletions examples/type_description_demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//use xcp::*;
use xcp_type_description::prelude::*;
use xcp::*;

#[derive(Clone, Copy, XcpTypeDescription, Debug)]
struct Parent {
Expand Down
8 changes: 6 additions & 2 deletions examples/xcp_daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ name = "xcp_daemon"
version = "0.1.0"
edition = "2021"

# In windows we simply log an error message and exit
# So we don't need to link any dependencies
[dependencies]
log = "0.4.22"

# In unix we link our dependencies as per usual
[target.'cfg(unix)'.dependencies]
xcp = { path = "../../", features = [] }
xcp_type_description = { path = "../../xcp_type_description/"}
xcp_type_description_derive = { path = "../../xcp_type_description/xcp_type_description_derive/" }

log = "0.4.22"
signal-hook = "0.3.17"
Expand Down
2 changes: 1 addition & 1 deletion examples/xcp_daemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sudo chmod +x ./install.sh; # Make the install script executable
sudo ./install.sh; # Install the example (binaries and configuration files)

# edit parameters
sudo nano /etc/xcpd/xcpd.conf
sudo nano /etc/xcpd/xcpd.conf

# run
sudo systemctl start xcpd; # Start the daemon
Expand Down
37 changes: 24 additions & 13 deletions examples/xcp_daemon/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use xcp::*;
use xcp_type_description::prelude::*;

#[cfg(unix)]
mod platform;
#[cfg(unix)]
use platform::*;
#[cfg(unix)]
use log::info;
use serde::{Deserialize, Serialize};
use signal_hook::{
consts::{SIGHUP, SIGINT, SIGTERM},
iterator::Signals,
};
use thiserror::Error;

use std::{thread, time::Duration};
use log::error;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, XcpTypeDescription)]
#[cfg(unix)]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, XcpTypeDescription)]
struct CalPage1 {
#[type_description(comment = "Max counter value", min = "0", max = "1023")]
counter_max: u32,
Expand All @@ -21,13 +18,15 @@ struct CalPage1 {
delay: u32,
}

#[cfg(unix)]
// Default value for the calibration parameters
const CAL_PAGE: CalPage1 = CalPage1 {
counter_min: 5,
counter_max: 10,
delay: 100000,
};

#[cfg(unix)]
#[derive(Error, Debug)]
enum XcpProcessError {
#[error("An XCP error occurred: {0}")]
Expand All @@ -38,16 +37,19 @@ enum XcpProcessError {
IoError(#[from] std::io::Error),
}

#[cfg(unix)]
struct XcpProcess {
cfg: ProcessConfig,
}

#[cfg(unix)]
impl XcpProcess {
fn new(config: ProcessConfig) -> Self {
XcpProcess { cfg: config }
}
}

#[cfg(unix)]
impl Process for XcpProcess {
type Error = XcpProcessError;

Expand Down Expand Up @@ -179,8 +181,8 @@ impl Process for XcpProcess {
&self.cfg
}
}

fn main() {
#[cfg(unix)]
fn _main() {
let cfg = ProcessConfig::new(
"xcpd",
"/var/run/xcpd.pid",
Expand All @@ -195,3 +197,12 @@ fn main() {
let mut daemon = Daemon::new(XcpProcess::new(cfg));
daemon.run().expect("Failed to run daemon");
}

#[cfg(not(unix))]
fn _main() {
error!("Daemonization is only supported for Unix platforms.");
}

fn main() {
_main();
}
9 changes: 9 additions & 0 deletions examples/xcp_daemon/src/platform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![cfg(unix)]

pub use signal_hook::{
consts::{SIGHUP, SIGINT, SIGTERM},
iterator::Signals,
};
pub use std::{thread, time::Duration};
pub use thiserror::Error;
pub use xcp::*;
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ mod daemon;
#[cfg(unix)]
pub use daemon::unix::*;

pub use xcp_type_description::prelude::*;
pub use xcp_idl_generator::prelude::*;

//----------------------------------------------------------------------------------------------
// Manually register a static measurement and calibration variables

Expand Down
4 changes: 2 additions & 2 deletions xcp_idl_generator/xcp_idl_generator_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub fn idl_generator_derive(input: TokenStream) -> TokenStream {
.fields
.iter()
.map(|field| {
//TODO Error handling
let field_name = &field.ident.as_ref().unwrap();
let field_type = &field.ty;

Expand All @@ -44,14 +43,15 @@ pub fn idl_generator_derive(input: TokenStream) -> TokenStream {
}

#[ctor::ctor]
#[allow(non_snake_case)]
fn #register_function_name() {
let mut struct_fields = FieldList::new();
#(#field_handlers)*

static mut STRUCT_INSTANCE: Option<Struct> = None;
static mut INITIALIZED: bool = false;

// @@@@ - unsafe - Mutable static, todo
// @@@@ - unsafe - Mutable static
unsafe {
// Prevent the user from calling the register function multiple times
if INITIALIZED {
Expand Down

0 comments on commit 45eff80

Please sign in to comment.