cpp-ipfs-http-client
IPFS C++ client library
Loading...
Searching...
No Matches
threading_example.cc

An example of how to use IPFS Client with threads, using the Abort() and Reset() methods.

An example of how to use IPFS Client with threads, using the Abort() and Reset() methods.

#include <ipfs/client.h>
// Or when you use IPFS locally:
//#include "ipfs/client.h"
#include <iostream>
#include <sstream>
#include <thread>
void startThread() {
try {
// Create IPFS Client object, with 2 minutes time-out.
ipfs::Client client("localhost", 5001, "2m");
// Only start a single thread
std::thread thread([&client]() {
std::stringstream contents;
try {
// File should not exists, takes forever (until time-out)
client.FilesGet("QmZp1rrtGTictR2rpNcx4673R7qU9Jdr9DQ6Z7F6Wgo2bQ",
&contents);
// Code below will never be reached, since we abort the request
std::cout << "Output: " << contents.str() << std::endl;
} catch (const std::runtime_error& e) {
// Run-time error will be thrown because of the aborting request.
std::cerr << "Error: " << e.what() << std::endl;
}
});
if (thread.joinable()) {
std::cout << "Directly try to abort the request and stop the thread."
<< std::endl;
// Try to remove the Abort() and Reset() calls,
// and see the difference yourself :)
client.Abort();
thread.join(); // Should not be blocking now
client.Reset();
}
} catch (const std::exception& e) {
std::cerr << "General error: " << e.what() << std::endl;
}
}
int main() {
std::cout << "Starting thread.." << std::endl;
// Start a request inside a thread
startThread();
std::cout << "Done!" << std::endl;
return 0;
}
IPFS client.
Definition client.h:48
void Reset()
Resets the abort call, allowing to execute new API requests again.
Definition client.cc:483
void FilesGet(const std::string &path, std::iostream *response)
Get a file from IPFS.
Definition client.cc:206
void Abort()
Abort any current running IPFS API request.
Definition client.cc:476