From 5b69a479dd4ebf6f8c6fddb3ef48823b59bb0a17 Mon Sep 17 00:00:00 2001 From: Shaun Taheri Date: Thu, 8 Sep 2016 14:55:33 +0200 Subject: Send an UpdateReport after a DownloadFailed Event --- Makefile | 7 ++++--- run/sota.toml.env | 4 ++-- src/gateway/socket.rs | 56 +++++++++++++++++++++++++++++++++------------------ src/interpreter.rs | 5 +++++ 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 8cdbfa4..d06e41a 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ RUST_IN_DOCKER := \ advancedtelematic/rust:latest CARGO := $(RUST_IN_DOCKER) cargo +TARGET := x86_64-unknown-linux-gnu # function for building new packages define make-pkg @@ -58,7 +59,7 @@ clean: ## Remove all compiled libraries, builds and temporary files. @rm -rf rust-openssl .cargo test: rust-openssl ## Run all cargo tests. - $(CARGO) test + $(CARGO) test --target=$(TARGET) doc: ## Generate documentation for the sota crate. $(CARGO) doc --lib --no-deps --release @@ -68,8 +69,8 @@ clippy: ## Run clippy lint checks using the nightly compiler. rustup run nightly cargo clippy -- -Dclippy client: rust-openssl src/ ## Compile a new release build of the client. - $(CARGO) build --release --target=x86_64-unknown-linux-gnu - @cp target/x86_64-unknown-linux-gnu/release/sota_client run/ + $(CARGO) build --release --target=$(TARGET) + @cp target/$(TARGET)/release/sota_client run/ image: client ## Build a Docker image for running the client. @docker build --tag advancedtelematic/sota-client run diff --git a/run/sota.toml.env b/run/sota.toml.env index 1ef5619..ecf15b5 100644 --- a/run/sota.toml.env +++ b/run/sota.toml.env @@ -23,8 +23,8 @@ GATEWAY_RVI=false GATEWAY_SOCKET=false GATEWAY_WEBSOCKET=true -NETWORK_HTTP_SERVER=http://127.0.0.1:8888 -NETWORK_RVI_EDGE_SERVER=http://127.0.0.1:9080 +NETWORK_HTTP_SERVER=127.0.0.1:8888 +NETWORK_RVI_EDGE_SERVER=127.0.0.1:9080 NETWORK_SOCKET_COMMANDS_PATH=/tmp/sota-commands.socket NETWORK_SOCKET_EVENTS_PATH=/tmp/sota-events.socket NETWORK_WEBSOCKET_SERVER=127.0.0.1:3012 diff --git a/src/gateway/socket.rs b/src/gateway/socket.rs index af496c9..4022c86 100644 --- a/src/gateway/socket.rs +++ b/src/gateway/socket.rs @@ -1,12 +1,12 @@ use chan; use chan::Sender; -use rustc_serialize::json; +use rustc_serialize::{Encodable, json}; use std::io::{BufReader, Read, Write}; use std::net::Shutdown; use std::sync::{Arc, Mutex}; use std::{fs, thread}; -use datatype::{Command, DownloadComplete, Error, Event}; +use datatype::{Command, Error, Event}; use super::{Gateway, Interpret}; use unix_socket::{UnixListener, UnixStream}; @@ -53,23 +53,32 @@ impl Gateway for Socket { } fn pulse(&self, event: Event) { - match event { + let output = match event { Event::DownloadComplete(dl) => { - let _ = UnixStream::connect(&self.events_path).map(|mut stream| { - let output = DownloadCompleteEvent { - version: "0.1".to_string(), - event: "DownloadComplete".to_string(), - data: dl - }; - stream.write_all(&json::encode(&output).expect("couldn't encode Event").into_bytes()) - .unwrap_or_else(|err| error!("couldn't write to events socket: {}", err)); - stream.shutdown(Shutdown::Write) - .unwrap_or_else(|err| error!("couldn't close events socket: {}", err)); - }).map_err(|err| error!("couldn't open events socket: {}", err)); + json::encode(&EventWrapper { + version: "0.1".to_string(), + event: "DownloadComplete".to_string(), + data: dl + }).expect("couldn't encode DownloadComplete event") + } + + Event::DownloadFailed(id, reason) => { + json::encode(&EventWrapper { + version: "0.1".to_string(), + event: "DownloadFailed".to_string(), + data: DownloadFailedWrapper { update_id: id, reason: reason } + }).expect("couldn't encode DownloadFailed event") } - _ => () - } + _ => return + }; + + let _ = UnixStream::connect(&self.events_path).map(|mut stream| { + stream.write_all(&output.into_bytes()) + .unwrap_or_else(|err| error!("couldn't write to events socket: {}", err)); + stream.shutdown(Shutdown::Write) + .unwrap_or_else(|err| error!("couldn't close events socket: {}", err)); + }).map_err(|err| debug!("couldn't open events socket: {}", err)); } } @@ -89,12 +98,19 @@ fn handle_client(stream: &mut UnixStream, itx: Arc>>) -> erx.recv().ok_or(Error::Socket("internal receiver error".to_string())) } + // FIXME(PRO-1322): create a proper JSON api -#[derive(RustcDecodable, RustcEncodable, PartialEq, Eq, Debug, Clone)] -pub struct DownloadCompleteEvent { +#[derive(RustcEncodable, RustcDecodable, PartialEq, Eq, Debug)] +pub struct EventWrapper { pub version: String, pub event: String, - pub data: DownloadComplete + pub data: E +} + +#[derive(RustcEncodable, RustcDecodable, PartialEq, Eq, Debug)] +pub struct DownloadFailedWrapper { + pub update_id: String, + pub reason: String } @@ -139,7 +155,7 @@ mod tests { let (mut stream, _) = server.accept().expect("couldn't read from events socket"); let mut text = String::new(); stream.read_to_string(&mut text).unwrap(); - let receive: DownloadCompleteEvent = json::decode(&text).expect("couldn't decode DownloadComplete message"); + let receive: EventWrapper = json::decode(&text).expect("couldn't decode Event"); assert_eq!(receive.version, "0.1".to_string()); assert_eq!(receive.event, "DownloadComplete".to_string()); assert_eq!(receive.data, send); diff --git a/src/interpreter.rs b/src/interpreter.rs index 9797456..c009d3c 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -79,6 +79,11 @@ impl Interpreter for EventInterpreter { } } + Event::DownloadFailed(id, reason) => { + let report = UpdateReport::single(id, UpdateResultCode::GENERAL_ERROR, reason); + ctx.send(Command::SendUpdateReport(report)); + } + Event::InstallComplete(report) | Event::InstallFailed(report) => { ctx.send(Command::SendUpdateReport(report)); } -- cgit v1.2.1