Skip to content

Commit

Permalink
Merge pull request maidsafe#613 from benjaminbollen/finalise_api_0.3
Browse files Browse the repository at this point in the history
Finalise api 0.3
  • Loading branch information
Benjamin Bollen committed Aug 18, 2015
2 parents 38993a6 + 30b46ae commit e746a15
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 115 deletions.
13 changes: 6 additions & 7 deletions examples/simple_key_value_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,11 @@ impl Node {
our_authority : Authority,
from_authority : Authority,
response_token : Option<SignedToken>) {
match request.clone() {
match request {
ExternalRequest::Get(data_request) => {
self.handle_get_request(data_request,
our_authority,
from_authority,
request,
response_token);
},
ExternalRequest::Put(data) => {
Expand All @@ -200,7 +199,6 @@ impl Node {
fn handle_get_request(&mut self, data_request : DataRequest,
_our_authority : Authority,
from_authority : Authority,
orig_request : ExternalRequest,
response_token : Option<SignedToken>) {
let name = match data_request {
DataRequest::PlainData(name) => name,
Expand All @@ -214,7 +212,7 @@ impl Node {

self.routing.get_response(from_authority,
Data::PlainData(data),
orig_request,
data_request,
response_token);
}

Expand Down Expand Up @@ -280,7 +278,8 @@ struct Client {
impl Client {
fn new(_bootstrap_peers: Vec<Endpoint>) -> Result<Client, RoutingError> {
let (event_sender, event_receiver) = mpsc::channel::<Event>();
let routing = Routing::new_client(event_sender);

let routing = Routing::new_client(event_sender, None);

let (command_sender, command_receiver) = mpsc::channel::<UserCommand>();

Expand Down Expand Up @@ -360,10 +359,10 @@ impl Client {
match event {
Event::Response{response, our_authority, from_authority} => {
match response {
ExternalResponse::Get(data, external_request, opt_signed_token) => {
ExternalResponse::Get(data, data_request, opt_signed_token) => {

},
ExternalResponse::Put(response_error, external_request, opt_signed_token) => {
ExternalResponse::Put(response_error, opt_signed_token) => {

},
_ => error!("Received external response {:?}, but not handled in example",
Expand Down
23 changes: 17 additions & 6 deletions src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::fmt::{Debug, Formatter, Error};
use messages::{RoutingMessage, Content,
ExternalRequest, ExternalResponse,
InternalRequest, InternalResponse};
use types::Address;

#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Eq, Ord, Clone, Hash)]
pub enum Authority {
Expand Down Expand Up @@ -57,6 +58,16 @@ impl Authority {
&Authority::Client(ref loc, _) => loc,
}
}

pub fn get_address(&self) -> Option<Address> {
match self {
&Authority::ClientManager(_) => None,
&Authority::NaeManager(_) => None,
&Authority::NodeManager(_) => None,
&Authority::ManagedNode(ref name) => Some(Address::Node(name.clone())),
&Authority::Client(_, ref public_key) => Some(Address::Client(public_key.clone())),
}
}
}

impl Debug for Authority {
Expand Down Expand Up @@ -112,19 +123,19 @@ pub fn our_authority(message : &RoutingMessage,
ExternalRequest::Get(ref data_request) => Some(data_request.name().clone()),
ExternalRequest::Put(ref data) => Some(data.name()),
ExternalRequest::Post(ref data) => Some(data.name()),
ExternalRequest::Delete(_) => None,
ExternalRequest::Delete(ref data) => Some(data.name()),
}
},
Content::InternalRequest(ref request) => {
match *request {
InternalRequest::Connect(_) => None,
InternalRequest::RequestNetworkName(ref public_id) => Some(public_id.name()),
InternalRequest::Connect(ref connect_request) => None,
InternalRequest::RequestNetworkName(ref public_id) => Some(public_id.name()),
InternalRequest::CacheNetworkName(ref public_id, _) => Some(public_id.name()),
InternalRequest::Refresh(_, _) => None,
InternalRequest::Refresh(_, _) => None,
}
},
Content::ExternalResponse(_) => None,
Content::InternalResponse(_) => None,
Content::ExternalResponse(_) => None,
Content::InternalResponse(_) => None,
};

let element = match element {
Expand Down
39 changes: 25 additions & 14 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

use std::io;
use std::convert::From;
use cbor::CborError;
use cbor::CborTagEncode;
use cbor::{CborError, CborTagEncode};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use std::error;
use std::fmt;
Expand All @@ -30,20 +29,29 @@ use data::Data;
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, RustcEncodable, RustcDecodable)]
/// represents response errors
pub enum ResponseError {
/// data not found
NoData,
/// Abort is for user to indicate that the state can be dropped;
/// if received by routing, it will drop the state.
Abort,
/// invalid request
InvalidRequest,
/// failure to store data
FailedToStoreData(Data)
InvalidRequest(Data),
/// failure to complete request for data
FailedRequestForData(Data),
/// had to clear Sacrificial Data in order to complete request
HadToClearSacrificial(usize),
}

impl From<CborError> for ResponseError {
fn from(e: CborError) -> ResponseError { ResponseError::Abort }
}

impl error::Error for ResponseError {
fn description(&self) -> &str {
match *self {
ResponseError::NoData => "No Data",
ResponseError::InvalidRequest => "Invalid request",
ResponseError::FailedToStoreData(_) => "Failed to store data",
ResponseError::Abort => "Abort",
ResponseError::InvalidRequest(_) => "Invalid request",
ResponseError::FailedRequestForData(_) => "Failed request for data",
ResponseError::HadToClearSacrificial(size) => "Had to clear {:?} bytes of Sacrificial
data to complete request",
}
}

Expand All @@ -55,10 +63,12 @@ impl error::Error for ResponseError {
impl fmt::Display for ResponseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ResponseError::NoData => fmt::Display::fmt("ResponsError::NoData", f),
ResponseError::InvalidRequest => fmt::Display::fmt("ResponsError::InvalidRequest", f),
ResponseError::FailedToStoreData(_) =>
ResponseError::Abort => fmt::Display::fmt("ResponseError:: Abort", f),
ResponseError::InvalidRequest(_) => fmt::Display::fmt("ResponsError::InvalidRequest", f),
ResponseError::FailedRequestForData(_) =>
fmt::Display::fmt("ResponseError::FailedToStoreData", f),
ResponseError::HadToClearSacrificial(_) =>
fmt::Display::fmt("ResponseError::HadToClearSacrificial", f),
}
}
}
Expand Down Expand Up @@ -233,6 +243,7 @@ impl fmt::Display for RoutingError {

#[cfg(test)]
mod test {
//FIXME (ben 18/08/2015) Tests can be expanded
use super::*;
use rustc_serialize::{Decodable, Encodable};
use cbor;
Expand All @@ -247,6 +258,6 @@ mod test {

#[test]
fn test_response_error() {
test_object(ResponseError::NoData)
test_object(ResponseError::Abort)
}
}
1 change: 0 additions & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,5 @@ pub enum Event {
Disconnected,
FailedRequest(Authority, ExternalRequest, InterfaceError),
FailedResponse(Authority, ExternalResponse, InterfaceError),

Terminated,
}
23 changes: 7 additions & 16 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pub enum ExternalResponse {
// TODO: Technical depth: if the third param here is Some(...) then
// the it shares most of the data with the second argument, which
// needlessly increases bandwidth.
Get (Data, ExternalRequest, Option<SignedToken>),
Put (ResponseError, ExternalRequest, Option<SignedToken>),
Post (ResponseError, ExternalRequest, Option<SignedToken>),
Delete(ResponseError, ExternalRequest, Option<SignedToken>),
Get (Data, DataRequest, Option<SignedToken>),
Put (ResponseError, Option<SignedToken>),
Post (ResponseError, Option<SignedToken>),
Delete(ResponseError, Option<SignedToken>),
}

impl ExternalResponse {
Expand All @@ -92,18 +92,9 @@ impl ExternalResponse {
pub fn get_signed_token(&self) -> &Option<SignedToken> {
match *self {
ExternalResponse::Get(_, _, ref r) => r,
ExternalResponse::Put(_, _, ref r) => r,
ExternalResponse::Post(_, _, ref r) => r,
ExternalResponse::Delete(_, _, ref r) => r,
}
}

pub fn get_orig_request(&self) -> &ExternalRequest {
match *self {
ExternalResponse::Get(_, ref r, _) => r,
ExternalResponse::Put(_, ref r, _) => r,
ExternalResponse::Post(_, ref r, _) => r,
ExternalResponse::Delete(_, ref r, _) => r,
ExternalResponse::Put(_, ref r) => r,
ExternalResponse::Post(_, ref r) => r,
ExternalResponse::Delete(_, ref r) => r,
}
}
}
Expand Down
26 changes: 8 additions & 18 deletions src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type RoutingResult = Result<(), RoutingError>;
/// On constructing a new Routing object a RoutingNode will also be started.
/// Routing objects are clonable for multithreading, or a Routing object can be
/// cloned with a new set of keys while preserving a single RoutingNode.
#[derive(Clone)]
pub struct Routing {
action_sender : mpsc::Sender<Action>,
}
Expand Down Expand Up @@ -70,7 +71,7 @@ impl Routing {
/// Starts a new RoutingIdentity, which will also start a new RoutingNode.
/// The RoutingNode will only bootstrap to the network and not attempt to
/// achieve full routing node status.
pub fn new_client(event_sender : mpsc::Sender<Event>)
pub fn new_client(event_sender : mpsc::Sender<Event>, keys : Option<Id>)
-> Routing {
sodiumoxide::init(); // enable shared global (i.e. safe to multithread now)

Expand All @@ -90,11 +91,6 @@ impl Routing {
}
}

/// Clone the interface while maintaining the same RoutingNode, with a given set of keys.
pub fn clone_with_keys(&self, keys : Id) -> Routing {
unimplemented!()
}

/// Send a Get message with a DataRequest to an Authority, signed with given keys.
pub fn get_request(&self, location : Authority, data_request : DataRequest) {
let _ = self.action_sender.send(Action::SendContent(
Expand Down Expand Up @@ -126,44 +122,38 @@ impl Routing {
/// If we received the request from a group, we'll not get the signed_token.
pub fn get_response(&self, location : Authority,
data : Data,
orig_request : ExternalRequest,
data_request : DataRequest,
signed_token : Option<SignedToken>) {
let _ = self.action_sender.send(Action::SendContent(
location,
Content::ExternalResponse(
ExternalResponse::Get(data, orig_request, signed_token))));
ExternalResponse::Get(data, data_request, signed_token))));
}
/// response error to a put request
pub fn put_response(&self, location : Authority,
response_error : ResponseError,
orig_request : ExternalRequest,
signed_token : Option<SignedToken>) {
let _ = self.action_sender.send(Action::SendContent(
location,
Content::ExternalResponse(
ExternalResponse::Put(response_error, orig_request, signed_token))));
ExternalResponse::Put(response_error, signed_token))));
}
/// Response error to a post request
pub fn post_response(&self, location : Authority,
response_error : ResponseError,
orig_request : ExternalRequest,
signed_token : Option<SignedToken>) {
let _ = self.action_sender.send(Action::SendContent(
location,
Content::ExternalResponse(
ExternalResponse::Post(response_error, orig_request, signed_token))));
ExternalResponse::Post(response_error, signed_token))));
}
/// response error to a delete respons
pub fn delete_response(&self, location : Authority,
response_error : ResponseError,
orig_request : ExternalRequest,
signed_token : Option<SignedToken>) {
let _ = self.action_sender.send(Action::SendContent(
location,
Content::ExternalResponse(
ExternalResponse::Delete(response_error,
orig_request,
signed_token))));
location, Content::ExternalResponse(ExternalResponse::Delete(response_error,
signed_token))));
}

/// Refresh the content in the close group nodes of group address content::name.
Expand Down
Loading

0 comments on commit e746a15

Please sign in to comment.