I am trying to send file from one terminal to other terminal by using the filesharing example. libp2p 0.52.2 version but the code keeps on running and doesn’t give error on receiver side. In receiver i don’t get the provider is empty but both these nodes keep on running
Sender code
let secretkeyseed: std::option::Option<u8>=Some(1);//get seed
let (mut network_client, mut network_events, network_event_loop) =
network::new(secretkeyseed).await?;
// Spawn the network task for it to run in the background.
spawn(network_event_loop.run());
//network_client.get_providers("public1.txt".to_string()).await;
let mut name="public1.txt".to_string();
network_client.start_providing(name.clone()).await;
//loop {
match network_events.next().await {
// Reply with the content of the file on incoming requests.
Some(network::Event::InboundRequest { request, channel }) => {
if request == name {
network_client
.respond_file(std::fs::read("/home/rusty/code/datafile.bin".to_string())?, channel)
.await;
}
}
e => todo!("{:?}", e),
}
// }
Receiver Code
let secretkeyseed: std::option::Option<u8>=Some(10);
let (mut network_client, mut network_events, network_event_loop) =
network::new(secretkeyseed).await?;
// Spawn the network task for it to run in the background.
spawn(network_event_loop.run());
let mut name="public1.txt".to_string();
network_client.get_providers(name.clone()).await;
let providers = network_client.get_providers(name.clone()).await;
if providers.is_empty() {
return Err(format!("Could not find provider for file {name}.").into());
}
// Request the content of the file from each node.
let requests = providers.into_iter().map(|p| {
let mut network_client = network_client.clone();
let name = name.clone();
async move { network_client.request_file(p, name).await }.boxed()
});
// Await the requests, ignore the remaining once a single one succeeds.
let file_content = futures::future::select_ok(requests)
.await
.map_err(|_| "None of the providers returned file.")?
.0;
println!("{:?}",file_content);