-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
209 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
|
||
#### Check key | ||
|
||
``` | ||
POST /api HTTP/1.1 | ||
Host: localhost:3292 | ||
content-type: application/json | ||
{ | ||
"command": "upload", | ||
"id": null, | ||
"hash": "9854003e54f053e2d911b67bcbf38260", | ||
"timeout": null | ||
} | ||
``` | ||
|
||
``` | ||
400 Bad request | ||
{ | ||
"error":"NotFoundError: Key not found in database [6af750676216564288a098837ad99e965c9e4e8c7df610e1e2095d597c95d3b0]" | ||
} | ||
``` | ||
|
||
#### Check key OK | ||
|
||
``` | ||
POST /api HTTP/1.1 | ||
Host: localhost:3292 | ||
content-type: application/json | ||
{ | ||
"command": "upload", | ||
"id": null, | ||
"hash": "612dd6a00e0e5cd784bdae7de99c78de9bebd7fabd8dfab70adfe2b607c8eccc", | ||
"timeout": 234 | ||
} | ||
``` | ||
|
||
``` | ||
HTTP/1.1 200 OK | ||
Content-Type: application/json | ||
{ | ||
"hash":"612dd6a00e0e5cd784bdae7de99c78de9bebd7fabd8dfab70adfe2b607c8eccc" | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::codec::{Ask, AskReply}; | ||
use crate::connection::Connection; | ||
use crate::database::DatabaseManager; | ||
use crate::error::Error; | ||
use crate::filemap::FileMap; | ||
use crate::flatten::*; | ||
use actix::prelude::*; | ||
use futures::prelude::*; | ||
use std::{io, net}; | ||
use tokio_codec::FramedRead; | ||
use tokio_io::io::WriteHalf; | ||
use tokio_io::AsyncRead; | ||
use tokio_tcp::TcpStream; | ||
|
||
pub fn connect( | ||
db: Addr<DatabaseManager>, | ||
addr: net::SocketAddr, | ||
) -> impl Future<Item = Addr<Connection>, Error = Error> { | ||
TcpStream::connect(&addr) | ||
.from_err() | ||
.and_then(move |c| Ok(Connection::new(db, c, addr))) | ||
} | ||
|
||
pub fn find_peer( | ||
hash: u128, | ||
db: Addr<DatabaseManager>, | ||
addr: Vec<net::SocketAddr>, | ||
) -> impl Future<Item = (Addr<Connection>, Vec<FileMap>), Error = Error> { | ||
let connections = addr.into_iter().map(move |addr| { | ||
let hash = hash; | ||
connect(db.clone(), addr).and_then(move |connection| { | ||
connection | ||
.send(Ask::new(hash)) | ||
.flatten_fut() | ||
.and_then(move |reply: AskReply| match reply.files { | ||
Some(files) => Ok((connection, files)), | ||
None => Err(Error::ResourceNotFound(reply.hash)), | ||
}) | ||
}) | ||
}); | ||
|
||
futures::select_ok(connections).and_then(|(v, _)| Ok(v)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use futures::{future, Async}; | ||
|
||
pub trait FlattenResult<T, E> { | ||
fn flatten_result(self) -> Result<T, E>; | ||
} | ||
|
||
impl<T, E, TE> FlattenResult<T, E> for Result<Result<T, E>, TE> | ||
where | ||
TE: Into<E>, | ||
{ | ||
fn flatten_result(self) -> Result<T, E> { | ||
match self { | ||
Err(e) => Err(e.into()), | ||
Ok(r) => r, | ||
} | ||
} | ||
} | ||
|
||
pub trait FlattenFuture<T, E> { | ||
type Future: future::Future<Item = T, Error = E>; | ||
|
||
fn flatten_fut(self) -> Self::Future; | ||
} | ||
|
||
pub struct FlatFut<F: future::Future> { | ||
inner: F, | ||
} | ||
|
||
impl<T, TE, E, F: future::Future<Item = Result<T, E>, Error = TE>> future::Future for FlatFut<F> | ||
where | ||
TE: Into<E>, | ||
{ | ||
type Item = T; | ||
type Error = E; | ||
|
||
fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> { | ||
match self.inner.poll() { | ||
Err(e) => Err(e.into()), | ||
Ok(Async::NotReady) => Ok(Async::NotReady), | ||
Ok(Async::Ready(Err(e))) => Err(e), | ||
Ok(Async::Ready(Ok(v))) => Ok(Async::Ready(v)), | ||
} | ||
} | ||
} | ||
|
||
impl<T, TE, E, F> FlattenFuture<T, E> for F | ||
where | ||
TE: Into<E>, | ||
F: future::Future<Item = Result<T, E>, Error = TE>, | ||
{ | ||
type Future = FlatFut<F>; | ||
|
||
fn flatten_fut(self) -> Self::Future { | ||
FlatFut { inner: self } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters