Commit 2ef03410 authored by dns2utf8's avatar dns2utf8

Distributing metadata starts to work now

parent 2d6260f0
......@@ -23,6 +23,11 @@ impl Handler<Subscribe> for Hub {
name: msg.name,
image_paths: vec![],
});
/*let message = ClientResponse::Pods();
for pod in self.pods.values() {
pod.addr.do_send(message.clone())
}*/
self.handle(ClientRequest::ListAllPods, _ctx);
}
}
impl Handler<Unsubscribe> for Hub {
......@@ -131,15 +136,16 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
//self.client_request(message, ctx);
self.hub.send(message)
.into_actor(self)
.then(|response, _, ctx| {
//self.do_send(response);
ctx.text(serde_json::to_string(&response.unwrap()).expect("unable to serialize internal state"));
.then(|response, this, ctx| {
let response: ClientResponse = response.unwrap();
actix::Handler::handle(this, response, ctx);
fut::ok( () )
})
.spawn(ctx);
}
Ok(JsonProtocol::PodRequest(message)) => {
//self.pod_request(message, ctx);
actix::Handler::handle(self, message, ctx);
}
_invalid => {
ctx.text("\"invalid request\"".to_string());
......@@ -156,6 +162,7 @@ impl Handler<ClientResponse> for Ws {
type Result = MessageResult<ClientResponse>;
fn handle(&mut self, message: ClientResponse, ctx: &mut Self::Context) -> Self::Result {
let message = crate::protocols::JsonProtocol::ClientResponse(message);
ctx.text(serde_json::to_string(&message).expect("unable to serialize internal state"));
MessageResult(())
}
......@@ -164,11 +171,30 @@ impl Handler<PodResponse> for Ws {
type Result = MessageResult<PodResponse>;
fn handle(&mut self, message: PodResponse, ctx: &mut Self::Context) -> Self::Result {
let message = crate::protocols::JsonProtocol::PodResponse(message);
ctx.text(serde_json::to_string(&message).expect("unable to serialize internal state"));
MessageResult(())
}
}
impl Handler<PodRequest> for Ws {
type Result = MessageResult<PodRequest>;
fn handle(&mut self, message: PodRequest, ctx: &mut Self::Context) -> Self::Result {
use PodRequest::*;
match message {
RegisterSelf { proposed_id, name } => {
actix::Handler::handle(self, PodResponse::Registered { global_id: self.id }, ctx);
}
UpdateTitle { name } => {
}
UpdatePaths { paths } => {
}
};
MessageResult(())
}
}
#[derive(Message)]
pub struct Subscribe {
id: PodId,
......
......@@ -9,7 +9,7 @@
<body>
<h1>A Distributed Gallery</h1>
<div id="galleries"></div>
<section id="galleries"></section>
<h1>Share yourself</h1>
<form action="#">
......
const log = console.log;
const log = console.log
, error = console.error;
let html_logger = undefined;
let ws = undefined;
......@@ -24,11 +25,22 @@ function update_ui() {
if (galleries.length === 0) {
galleries_element.innerHTML = "No Gallery connected";
} else {
let html = 'TODO display galleries';
galleries_element.innerHTML = '';
galleries.forEach(x => log(x));
galleries.forEach(x => {
const div = document.createElement('div');
const title = document.createElement('h3');
const text = document.createElement('div');
title.innerHTML = x.name || `unnamed Gallery #${x.id}`;
text.innerHTML = 'loading tree...';
div.appendChild(title);
div.appendChild(text);
galleries_element.appendChild(div)
});
galleries_element.innerHTML = html;
}
}
......@@ -70,14 +82,16 @@ function setup_ws() {
ws.onmessage = msg => {
log(msg.data);
html_logger('< ' + msg.data);
const data = JSON.parse(msg.data);
if (typeof data.ClientResponse !== 'undefined') {
client_response(data.ClientResponse);
} if (typeof data.PodResponse !== 'undefined') {
} else
if (typeof data.PodResponse !== 'undefined') {
Pod.message_handler(data.PodResponse);
} else {
console.error(["unimplemented message", data]);
html_logger('< ' + msg.data);
html_logger('! ' + msg.data);
}
};
......@@ -91,5 +105,14 @@ function setup_ws() {
}
function client_response(message) {
if (typeof message.PodGone !== 'undefined') {
galleries = galleries.filter(x => x.id !== message.PodGone);
} else
if (typeof message.Pods !== 'undefined') {
galleries = message.Pods;
galleries.sort((a, b) => a.name.localeCompare(b.name))
update_ui();
} else {
error(['client_response unimplemented', message]);
}
}
......@@ -30,7 +30,11 @@ window.addEventListener('load', function() {
function message_handler(message) {
console.log(['Pod::message_handler()', message]);
throw message;
if (typeof message.Registered !== 'undefined') {
Pod.id = message.Registered.global_id;
} else {
error(['pod_response unimplemented', message]);
}
}
function reconnect_handler(connected) {
console.log(['Pod::reconnect_handler()', connected]);
......
......@@ -7,7 +7,7 @@
transition: 0.3s ease all !important;
}
pre { background: #999; }
pre { background: #bbb; }
section { border: 1px solid #999; border-radius: 1rem; padding: 1rem; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment