Skip to content

Commit

Permalink
Fetching (and exposing) peer info is implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Mar 7, 2016
1 parent d66f9c3 commit 24719d7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 28 deletions.
47 changes: 38 additions & 9 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ documentation = "http://verwalter.readthedocs.org"
authors = ["Paul Colomiets <paul@colomiets.name>"]

[dependencies]
rotor = "0.6.0"
rotor = "0.6.1"
rotor-http = "0.6.0"
rotor-tools = "0.3.1"
rotor-cantal = { rev="f772b26", git="git://github.com/tailhook/rotor-cantal" }
lua = { rev="e76065b", git = "https://github.com/jcmoyer/rust-lua53" }
quick-error = "0.2.1"
liquid = "0.2"
Expand Down
36 changes: 25 additions & 11 deletions src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use std::time::{Duration};
use rotor::{Scope, Time};
use rotor_http::server::{Server, Response, RecvMode, Head};
use rotor_http::server::{Context as HttpContext};
use rustc_serialize::json::{ToJson, as_pretty_json};
use rustc_serialize::Encodable;
use rustc_serialize::json::{ToJson, as_json, as_pretty_json};

use net::Context;

#[derive(Clone, Debug)]
pub enum ApiRoute {
Config,
Peers,
}

#[derive(Clone, Debug, Copy)]
Expand Down Expand Up @@ -85,25 +87,20 @@ fn parse_api(path: &str) -> Option<Route> {
match path_component(path) {
("config", "") => Some(Api(Config,
if suffix(path) == "pretty" { Plain } else { Json })),
("peers", "") => Some(Api(Peers,
if suffix(path) == "pretty" { Plain } else { Json })),
_ => None,
}
}

fn serve_api(context: &Context, route: &ApiRoute, format: Format,
res: &mut Response)
fn respond<T: Encodable>(res: &mut Response, format: Format, data: T)
-> Result<(), io::Error>
{
use self::ApiRoute::*;
use self::Format::*;
let data = match *route {
Config => context.config.read().unwrap().to_json(),
};

res.status(200, "OK");
res.add_header("Content-Type", b"application/json").unwrap();
let data = match format {
Json => format!("{}", data),
Plain => format!("{}", as_pretty_json(&data)),
Format::Json => format!("{}", as_json(&data)),
Format::Plain => format!("{}", as_pretty_json(&data)),
};
res.add_length(data.as_bytes().len() as u64).unwrap();
res.done_headers().unwrap();
Expand All @@ -112,6 +109,23 @@ fn serve_api(context: &Context, route: &ApiRoute, format: Format,
Ok(())
}

fn serve_api(context: &Context, route: &ApiRoute, format: Format,
res: &mut Response)
-> Result<(), io::Error>
{
match *route {
ApiRoute::Config => {
respond(res, format, &context.config.read()
.expect("live configuration")
.to_json())
}
ApiRoute::Peers => {
respond(res, format, &context.schedule.get_peers().as_ref()
.map(|x| &x.peers))
}
}
}

impl HttpContext for Context {
// Defaults for now
}
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ extern crate libc;
extern crate lua;
extern crate scan_dir;
extern crate yaml_rust;
extern crate rotor;
extern crate hyper;
#[macro_use] extern crate rotor;
extern crate rotor_http;
extern crate rotor_tools;
extern crate rotor_cantal;
#[macro_use] extern crate log;
#[macro_use] extern crate matches;
#[macro_use] extern crate quick_error;
Expand Down
25 changes: 20 additions & 5 deletions src/net.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
use std::io;
use std::net::SocketAddr;
use std::sync::{Arc, RwLock};
use std::time::Duration;

use rotor;
use rotor_http::ServerFsm;
use rotor::mio::tcp::TcpListener;
use rotor::mio::tcp::{TcpListener};
use rotor_cantal::{Schedule, connect_localhost, Fsm as CantalFsm};
use rotor_tools::loop_ext::LoopExt;

use config::Config;
use frontend::Public;


rotor_compose!(pub enum Fsm/Seed<Context> {
Frontend(ServerFsm<Public, TcpListener>),
Cantal(CantalFsm<Context>),
});


pub struct Context {
pub config: Arc<RwLock<Config>>,
pub schedule: Schedule,
}


pub fn main(addr: &SocketAddr, cfg: Arc<RwLock<Config>>)
-> Result<(), io::Error>
{
let loop_creator = rotor::Loop::new(&rotor::Config::new())
.expect("Can't create loop");
let mut loop_inst = loop_creator.instantiate(Context {
let mut creator = rotor::Loop::new(&rotor::Config::new())
.expect("create loop");
let schedule = creator.add_and_fetch(Fsm::Cantal, |scope| {
connect_localhost(scope)
}).expect("create cantal endpoint");
schedule.set_peers_interval(Duration::new(10, 0));
let mut loop_inst = creator.instantiate(Context {
config: cfg,
schedule: schedule,
});
let listener = TcpListener::bind(&addr).expect("Can't bind address");
loop_inst.add_machine_with(|scope| {
ServerFsm::<Public, _>::new(listener, scope)
ServerFsm::<Public, _>::new(listener, scope).wrap(Fsm::Frontend)
}).expect("Can't add a state machine");
loop_inst.run()
}

0 comments on commit 24719d7

Please sign in to comment.