Rust implementation of Dataspaces, Syndicate, and a high-speed networked dataspace broker.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.3 KiB

2 months ago
2 months ago
1 year ago
1 year ago
1 year ago
1 year ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. use std::path::PathBuf;
  2. use std::sync::Arc;
  3. use structopt::StructOpt;
  4. use syndicate::actor::*;
  5. use syndicate::convert::from_io_value;
  6. use syndicate::dataspace::*;
  7. use syndicate::relay;
  8. use syndicate::schemas::service;
  9. use syndicate::schemas::transport_address;
  10. use syndicate::value::NestedValue;
  11. mod gatekeeper;
  12. mod protocol;
  13. mod services;
  14. mod schemas {
  15. include!(concat!(env!("OUT_DIR"), "/src/schemas/mod.rs"));
  16. }
  17. use schemas::internal_services;
  18. #[derive(Clone, StructOpt)]
  19. struct ServerConfig {
  20. #[structopt(short = "p", long = "port")]
  21. ports: Vec<u16>,
  22. #[structopt(short = "s", long = "socket")]
  23. sockets: Vec<PathBuf>,
  24. #[structopt(long)]
  25. inferior: bool,
  26. #[structopt(long)]
  27. debt_reporter: bool,
  28. #[structopt(short = "c", long)]
  29. config: Vec<PathBuf>,
  30. }
  31. #[tokio::main]
  32. async fn main() -> Result<(), Box<dyn std::error::Error>> {
  33. syndicate::convenient_logging()?;
  34. let config = Arc::new(ServerConfig::from_args());
  35. {
  36. const BRIGHT_GREEN: &str = "\x1b[92m";
  37. const RED: &str = "\x1b[31m";
  38. const GREEN: &str = "\x1b[32m";
  39. const NORMAL: &str = "\x1b[0m";
  40. const BRIGHT_YELLOW: &str = "\x1b[93m";
  41. tracing::info!(r"{} ______ {}", GREEN, NORMAL);
  42. tracing::info!(r"{} / {}\_{}\{} ", GREEN, BRIGHT_GREEN, GREEN, NORMAL);
  43. tracing::info!(r"{} / {},{}__/{} \ {} ____ __", GREEN, RED, BRIGHT_GREEN, GREEN, NORMAL);
  44. tracing::info!(r"{} /{}\__/ \{},{} \{} _______ ______ ____/ /_/________ / /____", GREEN, BRIGHT_GREEN, RED, GREEN, NORMAL);
  45. tracing::info!(r"{} \{}/ \__/ {}/{} / ___/ / / / __ \/ __ / / ___/ __ \/ __/ _ \", GREEN, BRIGHT_GREEN, GREEN, NORMAL);
  46. tracing::info!(r"{} \ {}'{} \__{}/ {} _\_ \/ /_/ / / / / /_/ / / /__/ /_/ / /_/ __/", GREEN, RED, BRIGHT_GREEN, GREEN, NORMAL);
  47. tracing::info!(r"{} \____{}/{}_/ {} /____/\__, /_/ /_/\____/_/\___/\__/_/\__/\___/", GREEN, BRIGHT_GREEN, GREEN, NORMAL);
  48. tracing::info!(r" /____/");
  49. tracing::info!(r"");
  50. tracing::info!(r" {}version {}{}", BRIGHT_YELLOW, env!("CARGO_PKG_VERSION"), NORMAL);
  51. tracing::info!(r"");
  52. tracing::info!(r" documentation & reference material: https://syndicate-lang.org/");
  53. tracing::info!(r" source code & bugs: https://git.syndicate-lang.org/syndicate-lang/syndicate-rs");
  54. tracing::info!(r"");
  55. }
  56. tracing::trace!("startup");
  57. Actor::new().boot(tracing::Span::current(), move |t| {
  58. let root_ds = Cap::new(&t.create(Dataspace::new()));
  59. if config.inferior {
  60. tracing::info!("inferior server instance");
  61. let root_ds = Arc::clone(&root_ds);
  62. t.spawn(syndicate::name!("parent"), move |t| protocol::run_io_relay(
  63. t,
  64. relay::Input::Bytes(Box::pin(tokio::io::stdin())),
  65. relay::Output::Bytes(Box::pin(tokio::io::stdout())),
  66. root_ds));
  67. }
  68. let server_config_ds = Cap::new(&t.create(Dataspace::new()));
  69. gatekeeper::bind(t, &root_ds, AnyValue::new("syndicate"), [0; 16],
  70. Arc::clone(&root_ds));
  71. gatekeeper::bind(t, &root_ds, AnyValue::new("server-config"), [0; 16],
  72. Arc::clone(&server_config_ds));
  73. let gateway = Cap::guard(&t.create(
  74. syndicate::entity(Arc::clone(&root_ds)).on_asserted(gatekeeper::handle_resolve)));
  75. services::debt_reporter::on_demand(t, Arc::clone(&server_config_ds));
  76. services::tcp_relay_listener::on_demand(t, Arc::clone(&server_config_ds), Arc::clone(&gateway));
  77. services::unix_relay_listener::on_demand(t, Arc::clone(&server_config_ds), Arc::clone(&gateway));
  78. services::config_watcher::on_demand(t, Arc::clone(&server_config_ds));
  79. if config.debt_reporter {
  80. server_config_ds.assert(t, &service::RequireService {
  81. service_name: from_io_value(&internal_services::DebtReporter)?,
  82. });
  83. }
  84. for port in config.ports.clone() {
  85. server_config_ds.assert(t, &service::RequireService {
  86. service_name: from_io_value(
  87. &internal_services::TcpRelayListener {
  88. addr: transport_address::Tcp {
  89. host: "0.0.0.0".to_owned(),
  90. port: (port as i32).into(),
  91. }
  92. })?,
  93. });
  94. }
  95. for path in config.sockets.clone() {
  96. server_config_ds.assert(t, &service::RequireService {
  97. service_name: from_io_value(
  98. &internal_services::UnixRelayListener {
  99. addr: transport_address::Unix {
  100. path: path.to_str().expect("representable UnixListener path").to_owned(),
  101. }
  102. })?,
  103. });
  104. }
  105. for path in config.config.clone() {
  106. server_config_ds.assert(t, &service::RequireService {
  107. service_name: from_io_value(
  108. &internal_services::ConfigWatcher {
  109. path: path.to_str().expect("representable ConfigWatcher path").to_owned(),
  110. })?,
  111. });
  112. }
  113. Ok(())
  114. }).await??;
  115. Ok(())
  116. }