2021-02-08 19:28:27 +01:00
|
|
|
// FIXME: No.
|
2021-09-19 18:17:42 +02:00
|
|
|
#![allow(dead_code)]
|
2021-10-07 16:44:01 +02:00
|
|
|
#![forbid(unused_imports)]
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2021-10-06 13:53:14 +02:00
|
|
|
/*
|
2020-02-14 12:20:17 +01:00
|
|
|
mod modules;
|
|
|
|
mod log;
|
|
|
|
mod config;
|
2020-05-10 17:23:43 +02:00
|
|
|
mod connection;
|
2020-10-23 16:35:10 +02:00
|
|
|
mod db;
|
2020-11-17 12:09:45 +01:00
|
|
|
mod machine;
|
2020-11-24 14:16:22 +01:00
|
|
|
mod builtin;
|
2020-11-30 15:05:25 +01:00
|
|
|
mod server;
|
2020-12-01 09:44:18 +01:00
|
|
|
mod network;
|
|
|
|
mod actor;
|
|
|
|
mod initiator;
|
2021-09-09 21:50:11 +02:00
|
|
|
mod space;
|
2021-10-06 13:53:14 +02:00
|
|
|
*/
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2021-10-13 14:15:52 +02:00
|
|
|
use rkyv::ser::serializers::AllocSerializer;
|
2021-10-18 10:39:31 +02:00
|
|
|
use rkyv::{SerializeUnsized, archived_value, Infallible, Deserialize};
|
|
|
|
use crate::oid::ObjectIdentifier;
|
|
|
|
use std::str::FromStr;
|
2021-10-13 04:57:40 +02:00
|
|
|
|
2021-09-30 10:07:42 +02:00
|
|
|
mod resource;
|
2021-10-06 13:53:14 +02:00
|
|
|
mod schema;
|
|
|
|
mod state;
|
|
|
|
mod db;
|
2021-10-13 04:57:40 +02:00
|
|
|
mod network;
|
2021-10-18 10:39:31 +02:00
|
|
|
pub mod oid;
|
|
|
|
mod varint;
|
2021-10-06 13:53:14 +02:00
|
|
|
|
|
|
|
/*
|
2021-09-30 10:07:42 +02:00
|
|
|
|
2020-02-18 01:30:40 +01:00
|
|
|
use clap::{App, Arg};
|
2020-02-14 12:20:17 +01:00
|
|
|
|
2020-02-18 01:30:40 +01:00
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-12-01 09:44:18 +01:00
|
|
|
use smol::Executor;
|
|
|
|
|
2020-02-18 01:30:40 +01:00
|
|
|
use error::Error;
|
|
|
|
|
2020-12-01 09:44:18 +01:00
|
|
|
use slog::Logger;
|
|
|
|
|
2020-12-14 11:02:46 +01:00
|
|
|
use paho_mqtt::AsyncClient;
|
2021-08-27 21:51:44 +02:00
|
|
|
use crate::config::Config;
|
2021-10-06 13:53:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn main() {
|
2021-10-13 04:57:40 +02:00
|
|
|
let db = db::StateDB::init("/tmp/state").unwrap();
|
|
|
|
println!("{:#?}", db);
|
|
|
|
|
|
|
|
let b = true;
|
2021-10-18 10:39:31 +02:00
|
|
|
//println!("{}", b.archived_type_oid());
|
2021-10-13 04:57:40 +02:00
|
|
|
|
2021-10-18 10:39:31 +02:00
|
|
|
let boid = &state::value::OID_BOOL;
|
|
|
|
let b2 = false;
|
|
|
|
let ent = state::Entry { oid: boid, val: &b2 };
|
2021-10-13 04:57:40 +02:00
|
|
|
println!("ent {:?}", &ent);
|
|
|
|
let s = serde_json::to_string(&ent).unwrap();
|
|
|
|
println!("{}", &s);
|
2021-10-18 10:39:31 +02:00
|
|
|
let ent2: state::OwnedEntry = serde_json::from_str(&s).unwrap();
|
2021-10-13 04:57:40 +02:00
|
|
|
println!("ent2: {:?}", ent2);
|
2021-10-13 14:15:52 +02:00
|
|
|
|
2021-10-18 10:39:31 +02:00
|
|
|
println!("Hello");
|
|
|
|
|
|
|
|
let mut ser = AllocSerializer::<32>::default();
|
|
|
|
//let b3 = Box::new(u32::from_ne_bytes([0xDE, 0xAD, 0xBE, 0xEF])) as Box<dyn state::value::SerializeValue>;
|
2021-10-13 14:15:52 +02:00
|
|
|
let b3 = Box::new(true) as Box<dyn state::value::SerializeValue>;
|
2021-10-18 10:39:31 +02:00
|
|
|
let pos3 = b3.serialize_unsized(&mut ser).unwrap();
|
|
|
|
let pos4 = 0;
|
|
|
|
//let pos4 = b4.serialize_unsized(&mut ser).unwrap();
|
2021-10-13 14:15:52 +02:00
|
|
|
let buf = ser.into_serializer().into_inner();
|
2021-10-18 10:39:31 +02:00
|
|
|
println!("({}) {:?} | {:?}", pos3, &buf[..pos3], &buf[pos3..]);
|
|
|
|
//println!("Serialized {} bytes: {:?} | {:?} | {:?}", pos4, &buf[..pos3], &buf[pos3+12..pos4], &buf[pos4+12..]);
|
|
|
|
let r3 = unsafe {
|
|
|
|
archived_value::<Box<dyn state::value::SerializeValue>>(&buf.as_slice(), pos3)
|
|
|
|
};
|
|
|
|
let v3: Box<dyn state::value::SerializeValue> = r3.deserialize(&mut Infallible).unwrap();
|
|
|
|
println!("{:?}", v3);
|
|
|
|
|
|
|
|
let koid = ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.2.1").unwrap();
|
|
|
|
let state = state::State::build()
|
|
|
|
.add(koid, Box::new(0xDEADBEEFu32))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
println!("{:?}", state);
|
|
|
|
let json = serde_json::to_string(&state).unwrap();
|
|
|
|
println!("{}", json);
|
|
|
|
let state_back: state::State = serde_json::from_str(&json).unwrap();
|
|
|
|
println!("{:?}", state_back);
|
|
|
|
let val = state_back.inner;
|
2021-10-06 13:53:14 +02:00
|
|
|
}
|
2020-12-14 11:02:46 +01:00
|
|
|
|
2021-10-06 13:53:14 +02:00
|
|
|
/*fn main() {
|
2020-02-18 01:30:40 +01:00
|
|
|
use clap::{crate_version, crate_description, crate_name};
|
|
|
|
|
|
|
|
// Argument parsing
|
|
|
|
// values for the name, description and version are pulled from `Cargo.toml`.
|
|
|
|
let matches = App::new(crate_name!())
|
|
|
|
.about(crate_description!())
|
|
|
|
.version(crate_version!())
|
|
|
|
.arg(Arg::with_name("config")
|
|
|
|
.help("Path to the config file to use")
|
|
|
|
.long("config")
|
|
|
|
.short("c")
|
|
|
|
.takes_value(true)
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("print default")
|
|
|
|
.help("Print a default config to stdout instead of running")
|
|
|
|
.long("print-default")
|
|
|
|
)
|
2021-08-27 21:51:44 +02:00
|
|
|
.arg(Arg::with_name("check config")
|
|
|
|
.help("Check config for validity")
|
|
|
|
.long("check")
|
|
|
|
)
|
2020-09-11 09:57:03 +02:00
|
|
|
.arg(Arg::with_name("dump")
|
|
|
|
.help("Dump all databases into the given directory")
|
|
|
|
.long("dump")
|
|
|
|
.conflicts_with("load")
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("load")
|
|
|
|
.help("Load databases from the given directory")
|
|
|
|
.long("load")
|
|
|
|
.conflicts_with("dump")
|
|
|
|
.takes_value(true)
|
|
|
|
)
|
2020-02-18 01:30:40 +01:00
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
// Check for the --print-default option first because we don't need to do anything else in that
|
|
|
|
// case.
|
|
|
|
if matches.is_present("print default") {
|
2020-12-14 11:02:46 +01:00
|
|
|
let config = config::Config::default();
|
|
|
|
let encoded = serde_dhall::serialize(&config).to_string().unwrap();
|
2020-02-18 01:30:40 +01:00
|
|
|
|
|
|
|
// Direct writing to fd 1 is faster but also prevents any print-formatting that could
|
|
|
|
// invalidate the generated TOML
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut handle = stdout.lock();
|
2020-12-14 11:02:46 +01:00
|
|
|
handle.write_all(&encoded.as_bytes()).unwrap();
|
2020-02-18 01:30:40 +01:00
|
|
|
|
|
|
|
// Early return to exit.
|
2020-12-01 10:21:39 +01:00
|
|
|
return;
|
2021-08-27 21:51:44 +02:00
|
|
|
} else if matches.is_present("check config") {
|
|
|
|
let configpath = matches.value_of("config").unwrap_or("/etc/diflouroborane.dhall");
|
|
|
|
match config::read(&PathBuf::from_str(configpath).unwrap()) {
|
|
|
|
Ok(cfg) => {
|
2021-08-28 23:03:57 +02:00
|
|
|
//TODO: print a normalized version of the supplied config
|
|
|
|
println!("config is valid");
|
2021-08-27 21:51:44 +02:00
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
std::process::exit(-1);
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 01:30:40 +01:00
|
|
|
}
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-12-01 09:44:18 +01:00
|
|
|
let retval;
|
|
|
|
|
|
|
|
// Scope to drop everything before exiting.
|
|
|
|
{
|
|
|
|
// Initialize the logging subsystem first to be able to better document the progress from now
|
|
|
|
// on.
|
|
|
|
// TODO: Now would be a really good time to close stdin/out and move logging to syslog
|
|
|
|
// Log is in an Arc so we can do very cheap clones in closures.
|
|
|
|
let log = Arc::new(log::init());
|
|
|
|
info!(log, "Starting");
|
|
|
|
|
|
|
|
match maybe(matches, log.clone()) {
|
|
|
|
Ok(_) => retval = 0,
|
|
|
|
Err(e) => {
|
|
|
|
error!(log, "{}", e);
|
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::process::exit(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returning a `Result` from `main` allows us to use the `?` shorthand.
|
|
|
|
// In the case of an Err it will be printed using `fmt::Debug`
|
|
|
|
fn maybe(matches: clap::ArgMatches, log: Arc<Logger>) -> Result<(), Error> {
|
2020-12-16 13:51:47 +01:00
|
|
|
// If no `config` option is given use a preset default.
|
2020-12-16 14:47:13 +01:00
|
|
|
let configpath = matches.value_of("config").unwrap_or("/etc/diflouroborane.dhall");
|
2020-12-16 13:51:47 +01:00
|
|
|
let config = config::read(&PathBuf::from_str(configpath).unwrap())?;
|
|
|
|
debug!(log, "Loaded Config: {:?}", config);
|
2020-02-18 13:06:25 +01:00
|
|
|
|
2020-11-30 16:12:40 +01:00
|
|
|
if matches.is_present("dump") {
|
2021-01-26 15:11:50 +01:00
|
|
|
let db = db::Databases::new(&log, &config)?;
|
2021-09-19 19:47:29 +02:00
|
|
|
let v = db.access.dump_roles().unwrap();
|
|
|
|
for (id, role) in v.iter() {
|
|
|
|
info!(log, "Role {}:\n{}", id, role);
|
|
|
|
}
|
2021-09-21 07:48:19 +02:00
|
|
|
|
|
|
|
let v = db.userdb.list_users()?;
|
|
|
|
for user in v.iter() {
|
|
|
|
info!(log, "User {}:\n{:?}", user.id, user.data);
|
|
|
|
}
|
2020-12-01 09:44:18 +01:00
|
|
|
Ok(())
|
2020-11-30 16:12:40 +01:00
|
|
|
} else if matches.is_present("load") {
|
2020-12-16 13:51:47 +01:00
|
|
|
let db = db::Databases::new(&log, &config)?;
|
2020-12-16 13:30:04 +01:00
|
|
|
let mut dir = PathBuf::from(matches.value_of_os("load").unwrap());
|
2020-12-16 14:04:50 +01:00
|
|
|
|
2020-12-16 13:30:04 +01:00
|
|
|
dir.push("users.toml");
|
2020-12-16 13:51:47 +01:00
|
|
|
let map = db::user::load_file(&dir)?;
|
|
|
|
for (uid,user) in map.iter() {
|
|
|
|
db.userdb.put_user(uid, user)?;
|
|
|
|
}
|
2020-12-16 13:30:04 +01:00
|
|
|
debug!(log, "Loaded users: {:?}", map);
|
|
|
|
dir.pop();
|
2020-12-16 14:04:50 +01:00
|
|
|
|
2020-12-01 09:44:18 +01:00
|
|
|
Ok(())
|
2020-11-30 16:12:40 +01:00
|
|
|
} else {
|
2020-12-01 10:21:39 +01:00
|
|
|
let ex = Executor::new();
|
2021-01-22 16:25:26 +01:00
|
|
|
let db = db::Databases::new(&log, &config)?;
|
2020-12-01 09:44:18 +01:00
|
|
|
|
2021-09-21 07:48:19 +02:00
|
|
|
let machines = machine::load(&config)?;
|
|
|
|
let (actor_map, actors) = actor::load(&log, &config)?;
|
2021-09-30 10:07:42 +02:00
|
|
|
let (init_map, initiators) = initiator::load(&log, &config, db.userdb.clone(), db.access.clone())?;
|
2020-02-18 13:06:25 +01:00
|
|
|
|
2020-12-14 12:39:01 +01:00
|
|
|
let mut network = network::Network::new(machines, actor_map, init_map);
|
2020-12-07 12:11:07 +01:00
|
|
|
|
2020-12-14 12:39:01 +01:00
|
|
|
for (a,b) in config.actor_connections.iter() {
|
|
|
|
if let Err(e) = network.connect_actor(a,b) {
|
|
|
|
error!(log, "{}", e);
|
|
|
|
}
|
2021-09-21 07:48:19 +02:00
|
|
|
info!(log, "[Actor] Connected {} to {}", a, b);
|
2020-12-14 12:39:01 +01:00
|
|
|
}
|
2020-12-01 09:44:18 +01:00
|
|
|
|
2020-12-14 12:39:01 +01:00
|
|
|
for (a,b) in config.init_connections.iter() {
|
|
|
|
if let Err(e) = network.connect_init(a,b) {
|
|
|
|
error!(log, "{}", e);
|
|
|
|
}
|
2021-09-21 07:48:19 +02:00
|
|
|
info!(log, "[Initi] Connected {} to {}", a, b);
|
2020-12-14 12:39:01 +01:00
|
|
|
}
|
2020-12-02 11:31:17 +01:00
|
|
|
|
2020-12-14 14:45:16 +01:00
|
|
|
for actor in actors.into_iter() {
|
|
|
|
ex.spawn(actor).detach();
|
|
|
|
}
|
|
|
|
for init in initiators.into_iter() {
|
|
|
|
ex.spawn(init).detach();
|
|
|
|
}
|
2020-12-02 11:31:17 +01:00
|
|
|
|
2021-09-21 07:48:19 +02:00
|
|
|
server::serve_api_connections(log.clone(), config, db, network, ex)
|
2020-11-30 16:12:40 +01:00
|
|
|
}
|
2020-02-18 13:06:25 +01:00
|
|
|
}
|
2021-10-06 13:53:14 +02:00
|
|
|
*/
|