Use log crate for logging.
This commit is contained in:
parent
4d2eb971a5
commit
96bc5d1bdc
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -578,6 +578,7 @@ dependencies = [
|
||||
"fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libp2p 0.3.1",
|
||||
"log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"multibase 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"multihash 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-multiaddr 0.1.0",
|
||||
|
@ -9,6 +9,7 @@ cid = { version = "*", path = "../rust-cid" }
|
||||
fnv = "*"
|
||||
futures = "*"
|
||||
libp2p = { version = "*", path = "../rust-libp2p" }
|
||||
log = "*"
|
||||
multibase = "*"
|
||||
multihash = "*"
|
||||
parity-multiaddr = { version = "*", path = "../rust-libp2p/misc/multiaddr" }
|
||||
|
@ -39,7 +39,7 @@ pub struct Bitswap<TSubstream, TStrategy: Strategy> {
|
||||
impl<TSubstream, TStrategy: Strategy> Bitswap<TSubstream, TStrategy> {
|
||||
/// Creates a `Bitswap`.
|
||||
pub fn new(strategy: TStrategy) -> Self {
|
||||
println!("bitswap: new");
|
||||
debug!("bitswap: new");
|
||||
Bitswap {
|
||||
marker: PhantomData,
|
||||
events: VecDeque::new(),
|
||||
@ -54,47 +54,47 @@ impl<TSubstream, TStrategy: Strategy> Bitswap<TSubstream, TStrategy> {
|
||||
///
|
||||
/// Called from Kademlia behaviour.
|
||||
pub fn connect(&mut self, peer_id: PeerId) {
|
||||
println!("bitswap: connect");
|
||||
debug!("bitswap: connect");
|
||||
if self.wanted_blocks.len() > 0 {
|
||||
if self.target_peers.insert(peer_id.clone()) {
|
||||
println!(" queuing dial_peer to {}", peer_id.to_base58());
|
||||
debug!(" queuing dial_peer to {}", peer_id.to_base58());
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer { peer_id });
|
||||
}
|
||||
}
|
||||
println!("");
|
||||
debug!("");
|
||||
}
|
||||
|
||||
/// Sends a block to the peer.
|
||||
///
|
||||
/// Called from a Strategy.
|
||||
pub fn send_block(&mut self, peer_id: PeerId, block: Block) {
|
||||
println!("bitswap: send_block");
|
||||
debug!("bitswap: send_block");
|
||||
let ledger = self.connected_peers.get_mut(&peer_id)
|
||||
.expect("Peer not in ledger?!");
|
||||
let message = ledger.send_block(block);
|
||||
println!(" queuing block for {}", peer_id.to_base58());
|
||||
debug!(" queuing block for {}", peer_id.to_base58());
|
||||
self.events.push_back(NetworkBehaviourAction::SendEvent {
|
||||
peer_id,
|
||||
event: message,
|
||||
});
|
||||
println!("");
|
||||
debug!("");
|
||||
}
|
||||
|
||||
/// Queues the wanted block for all peers.
|
||||
///
|
||||
/// A user request
|
||||
pub fn want_block(&mut self, cid: Cid, priority: Priority) {
|
||||
println!("bitswap: want_block");
|
||||
debug!("bitswap: want_block");
|
||||
for (peer_id, ledger) in self.connected_peers.iter_mut() {
|
||||
let message = ledger.want_block(&cid, priority);
|
||||
println!(" queuing want for {}", peer_id.to_base58());
|
||||
debug!(" queuing want for {}", peer_id.to_base58());
|
||||
self.events.push_back(NetworkBehaviourAction::SendEvent {
|
||||
peer_id: peer_id.to_owned(),
|
||||
event: message,
|
||||
});
|
||||
}
|
||||
self.wanted_blocks.insert(cid, priority);
|
||||
println!("");
|
||||
debug!("");
|
||||
}
|
||||
|
||||
/// Removes the block from our want list and updates all peers.
|
||||
@ -102,11 +102,11 @@ impl<TSubstream, TStrategy: Strategy> Bitswap<TSubstream, TStrategy> {
|
||||
/// Can be either a user request or be called when the block
|
||||
/// was received.
|
||||
pub fn cancel_block(&mut self, cid: &Cid) {
|
||||
println!("bitswap: cancel_block");
|
||||
debug!("bitswap: cancel_block");
|
||||
for (peer_id, ledger) in self.connected_peers.iter_mut() {
|
||||
let message = ledger.cancel_block(cid);
|
||||
if message.is_some() {
|
||||
println!(" queuing cancel for {}", peer_id.to_base58());
|
||||
debug!(" queuing cancel for {}", peer_id.to_base58());
|
||||
self.events.push_back(NetworkBehaviourAction::SendEvent {
|
||||
peer_id: peer_id.to_owned(),
|
||||
event: message.unwrap(),
|
||||
@ -114,7 +114,7 @@ impl<TSubstream, TStrategy: Strategy> Bitswap<TSubstream, TStrategy> {
|
||||
}
|
||||
}
|
||||
self.wanted_blocks.remove(cid);
|
||||
println!("");
|
||||
debug!("");
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,40 +126,40 @@ where
|
||||
type OutEvent = ();
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
println!("bitswap: new_handler");
|
||||
debug!("bitswap: new_handler");
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn addresses_of_peer(&mut self, _peer_id: &PeerId) -> Vec<Multiaddr> {
|
||||
println!("bitswap: addresses_of_peer");
|
||||
debug!("bitswap: addresses_of_peer");
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn inject_connected(&mut self, peer_id: PeerId, cp: ConnectedPoint) {
|
||||
println!("bitswap: inject_connected");
|
||||
println!(" peer_id: {}", peer_id.to_base58());
|
||||
println!(" connected_point: {:?}", cp);
|
||||
debug!("bitswap: inject_connected");
|
||||
debug!(" peer_id: {}", peer_id.to_base58());
|
||||
debug!(" connected_point: {:?}", cp);
|
||||
let ledger = Ledger::new();
|
||||
if !self.wanted_blocks.is_empty() {
|
||||
let mut message = Message::new();
|
||||
for (cid, priority) in &self.wanted_blocks {
|
||||
message.want_block(cid, *priority);
|
||||
}
|
||||
println!(" queuing wanted blocks");
|
||||
debug!(" queuing wanted blocks");
|
||||
self.events.push_back(NetworkBehaviourAction::SendEvent {
|
||||
peer_id: peer_id.clone(),
|
||||
event: message,
|
||||
});
|
||||
}
|
||||
self.connected_peers.insert(peer_id, ledger);
|
||||
println!("");
|
||||
debug!("");
|
||||
}
|
||||
|
||||
fn inject_disconnected(&mut self, peer_id: &PeerId, cp: ConnectedPoint) {
|
||||
println!("bitswap: inject_disconnected {:?}", cp);
|
||||
println!(" peer_id: {}", peer_id.to_base58());
|
||||
println!(" connected_point: {:?}", cp);
|
||||
println!("");
|
||||
debug!("bitswap: inject_disconnected {:?}", cp);
|
||||
debug!(" peer_id: {}", peer_id.to_base58());
|
||||
debug!(" connected_point: {:?}", cp);
|
||||
debug!("");
|
||||
//self.connected_peers.remove(peer_id);
|
||||
}
|
||||
|
||||
@ -168,8 +168,8 @@ where
|
||||
source: PeerId,
|
||||
event: InnerMessage,
|
||||
) {
|
||||
println!("bitswap: inject_node_event");
|
||||
println!("{:?}", event);
|
||||
debug!("bitswap: inject_node_event");
|
||||
debug!("{:?}", event);
|
||||
let message = match event {
|
||||
InnerMessage::Rx(message) => {
|
||||
message
|
||||
@ -178,7 +178,7 @@ where
|
||||
return;
|
||||
},
|
||||
};
|
||||
println!(" received message");
|
||||
debug!(" received message");
|
||||
|
||||
// Update the ledger.
|
||||
let ledger = self.connected_peers.get_mut(&source)
|
||||
@ -196,7 +196,7 @@ where
|
||||
}
|
||||
// TODO: Remove cancelled `Want` events from the queue.
|
||||
// TODO: Remove cancelled blocks from `SendEvent`.
|
||||
println!("");
|
||||
debug!("");
|
||||
}
|
||||
|
||||
fn poll(
|
||||
@ -204,17 +204,17 @@ where
|
||||
_: &mut PollParameters,
|
||||
) -> Async<NetworkBehaviourAction<
|
||||
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent, Self::OutEvent>> {
|
||||
println!("bitswap: poll");
|
||||
debug!("bitswap: poll");
|
||||
// TODO concat messages to same destination to reduce traffic.
|
||||
if let Some(event) = self.events.pop_front() {
|
||||
if let NetworkBehaviourAction::SendEvent { peer_id, event } = &event {
|
||||
let ledger = self.connected_peers.get_mut(&peer_id)
|
||||
.expect("Peer not in ledger?!");
|
||||
ledger.update_outgoing_stats(&event);
|
||||
println!(" send_message to {}", peer_id.to_base58());
|
||||
debug!(" send_message to {}", peer_id.to_base58());
|
||||
}
|
||||
println!("{:?}", event);
|
||||
println!("");
|
||||
debug!("{:?}", event);
|
||||
debug!("");
|
||||
return Async::Ready(event);
|
||||
}
|
||||
|
||||
@ -226,7 +226,7 @@ where
|
||||
None => {}
|
||||
}
|
||||
|
||||
println!("");
|
||||
debug!("");
|
||||
Async::NotReady
|
||||
}
|
||||
}
|
||||
|
@ -32,9 +32,11 @@ where
|
||||
|
||||
#[inline]
|
||||
fn upgrade_inbound(self, socket: TSocket, info: Self::Info) -> Self::Future {
|
||||
println!("upgrade_inbound: {}", std::str::from_utf8(info).unwrap());
|
||||
debug!("upgrade_inbound: {}", std::str::from_utf8(info).unwrap());
|
||||
upgrade::read_one_then(socket, 2048, |packet| {
|
||||
Ok(Message::from_bytes(&packet)?)
|
||||
let message = Message::from_bytes(&packet)?;
|
||||
debug!("{:?}", message);
|
||||
Ok(message)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -99,7 +101,7 @@ where
|
||||
|
||||
#[inline]
|
||||
fn upgrade_outbound(self, socket: TSocket, info: Self::Info) -> Self::Future {
|
||||
println!("upgrade_outbound: {}", std::str::from_utf8(info).unwrap());
|
||||
debug!("upgrade_outbound: {}", std::str::from_utf8(info).unwrap());
|
||||
let bytes = self.into_bytes();
|
||||
upgrade::write_one(socket, bytes)
|
||||
}
|
||||
|
@ -37,8 +37,8 @@ impl Strategy for AltruisticStrategy {
|
||||
cid: Cid,
|
||||
priority: Priority,
|
||||
) {
|
||||
println!("Peer {} wants block {} with priority {}",
|
||||
source.to_base58(), cid.to_string(), priority);
|
||||
info!("Peer {} wants block {} with priority {}",
|
||||
source.to_base58(), cid.to_string(), priority);
|
||||
let block = self.repo.get(&cid);
|
||||
if block.is_some() {
|
||||
self.events.push_back(StrategyEvent::Send {
|
||||
@ -49,9 +49,9 @@ impl Strategy for AltruisticStrategy {
|
||||
}
|
||||
|
||||
fn process_block(&mut self, source: PeerId, block: Block) {
|
||||
println!("Received block {} from peer {}",
|
||||
block.cid().to_string(),
|
||||
source.to_base58());
|
||||
info!("Received block {} from peer {}",
|
||||
block.cid().to_string(),
|
||||
source.to_base58());
|
||||
self.repo.put(block);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#![deny(missing_docs)]
|
||||
#![deny(warnings)]
|
||||
#![feature(drain_filter)]
|
||||
#[macro_use] extern crate log;
|
||||
use futures::prelude::*;
|
||||
use futures::try_ready;
|
||||
|
||||
@ -40,7 +41,7 @@ impl Ipfs {
|
||||
|
||||
// Listen on all interfaces and whatever port the OS assigns
|
||||
let addr = libp2p::Swarm::listen_on(&mut swarm, "/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
|
||||
println!("Listening on {:?}", addr);
|
||||
info!("Listening on {:?}", addr);
|
||||
|
||||
Ipfs {
|
||||
repo,
|
||||
|
@ -26,14 +26,14 @@ impl<TSubstream: AsyncRead + AsyncWrite, TStrategy: Strategy>
|
||||
match event {
|
||||
MdnsEvent::Discovered(list) => {
|
||||
for (peer, _) in list {
|
||||
println!("mdns: Discovered peer {}", peer.to_base58());
|
||||
debug!("mdns: Discovered peer {}", peer.to_base58());
|
||||
self.bitswap.connect(peer);
|
||||
}
|
||||
}
|
||||
MdnsEvent::Expired(list) => {
|
||||
for (peer, _) in list {
|
||||
if !self.mdns.has_node(&peer) {
|
||||
println!("mdns: Expired peer {}", peer.to_base58());
|
||||
debug!("mdns: Expired peer {}", peer.to_base58());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -88,7 +88,7 @@ impl<TSubstream: AsyncRead + AsyncWrite, TStrategy: Strategy> Behaviour<TSubstre
|
||||
{
|
||||
/// Create a Kademlia behaviour with the IPFS bootstrap nodes.
|
||||
pub fn new(config: NetworkConfig<TStrategy>) -> Self {
|
||||
println!("Local peer id: {}", config.peer_id.to_base58());
|
||||
info!("Local peer id: {}", config.peer_id.to_base58());
|
||||
|
||||
let mdns = Mdns::new().expect("Failed to create mDNS service");
|
||||
|
||||
@ -107,20 +107,20 @@ impl<TSubstream: AsyncRead + AsyncWrite, TStrategy: Strategy> Behaviour<TSubstre
|
||||
}
|
||||
|
||||
pub fn want_block(&mut self, cid: Cid) {
|
||||
println!("Want block {}", cid.to_string());
|
||||
info!("Want block {}", cid.to_string());
|
||||
//let hash = Multihash::from_bytes(cid.to_bytes()).unwrap();
|
||||
//self.kademlia.get_providers(hash);
|
||||
self.bitswap.want_block(cid, 1);
|
||||
}
|
||||
|
||||
pub fn provide_block(&mut self, cid: &Cid) {
|
||||
println!("Providing block {}", cid.to_string());
|
||||
info!("Providing block {}", cid.to_string());
|
||||
//let hash = Multihash::from_bytes(cid.hash.clone()).unwrap();
|
||||
//self.kademlia.add_providing(PeerId::from_multihash(hash).unwrap());
|
||||
}
|
||||
|
||||
pub fn stop_providing_block(&mut self, cid: &Cid) {
|
||||
println!("Finished providing block {}", cid.to_string());
|
||||
info!("Finished providing block {}", cid.to_string());
|
||||
//let hash = Multihash::from_bytes(cid.hash.clone()).unwrap();
|
||||
//self.kademlia.remove_providing(&hash);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user