summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShaun Taheri <shaun@advancedtelematic.com>2016-09-08 14:55:33 +0200
committerShaun Taheri <shaun@advancedtelematic.com>2016-09-08 15:39:23 +0200
commit5b69a479dd4ebf6f8c6fddb3ef48823b59bb0a17 (patch)
tree3587b057ebb1f40c0e5ecd2fb75b58197e8a65b1 /src
parent56a59856ff89195aff896242048a50ddc47a423c (diff)
downloadrvi_sota_client-5b69a479dd4ebf6f8c6fddb3ef48823b59bb0a17.tar.gz
Send an UpdateReport after a DownloadFailed Event
Diffstat (limited to 'src')
-rw-r--r--src/gateway/socket.rs56
-rw-r--r--src/interpreter.rs5
2 files changed, 41 insertions, 20 deletions
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<Mutex<Sender<Interpret>>>) ->
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<E: Encodable> {
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<DownloadComplete> = 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<Event, Command> 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));
}