Last active
August 29, 2015 14:18
-
-
Save vozhyk-/800b1591dea1a6f60804 to your computer and use it in GitHub Desktop.
MEGA API: Streaming download test
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
# github/gitignore/CMake.gitignore | |
CMakeCache.txt | |
CMakeFiles | |
Makefile | |
cmake_install.cmake | |
install_manifest.txt |
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
cmake_minimum_required (VERSION 2.6) | |
project (mega-streaming-test) | |
find_package(PkgConfig) | |
pkg_check_modules(LIBMEGA libmega REQUIRED) | |
add_executable(mega-streaming-test main.cpp) | |
set_property(TARGET mega-streaming-test PROPERTY CXX_STANDARD 11) | |
set_property(TARGET mega-streaming-test PROPERTY CXX_STANDARD_REQIRED ON) | |
target_link_libraries(mega-streaming-test ${LIBMEGA_LIBRARIES}) |
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
#ifndef LISTENER_BASE_H | |
#define LISTENER_BASE_H | |
#include <thread> | |
#include <chrono> | |
template<typename result_type> class listener_base | |
{ | |
public: | |
bool done; | |
result_type result; | |
listener_base() | |
{ | |
done = false; | |
result = {}; | |
} | |
result_type wait_for_result() | |
{ | |
while (!done) | |
{ | |
std::this_thread::sleep_for(std::chrono::seconds(1)); | |
} | |
return result; | |
} | |
}; | |
#endif // LISTENER_BASE_H |
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
#ifndef LISTENERS_H | |
#define LISTENERS_H | |
#include <iostream> | |
#include "listener_base.h" | |
using namespace std; | |
using namespace mega; | |
class get_public_node_listener | |
: public listener_base<MegaNode *>, public MegaRequestListener | |
{ | |
public: | |
virtual void onRequestFinish(MegaApi *api, | |
MegaRequest *request, | |
MegaError *error) | |
{ | |
if (error->getErrorCode() == MegaError::API_OK) | |
result = request->getPublicMegaNode(); | |
done = true; | |
} | |
}; | |
class streaming_listener | |
: public listener_base<bool>, public MegaTransferListener | |
{ | |
public: | |
virtual void onTransferUpdate(MegaApi *api, MegaTransfer *transfer) | |
{ | |
auto size = transfer->getDeltaSize(); | |
char *data = transfer->getLastBytes(); | |
cout << "onTransferUpdate: buf " << (long long)data | |
<< " size " << size << endl; | |
} | |
virtual void onTransferFinish(MegaApi *api, MegaTransfer *transfer, | |
MegaError *error) | |
{ | |
cout << "onTransferFinish: " << error->getErrorString() << endl; | |
result = error->getErrorCode() == MegaError::API_OK; | |
done = true; | |
} | |
}; | |
#endif // LISTENERS_H |
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
/* | |
* MEGA API: Streaming download test | |
* | |
* Compile: | |
* cmake -DCMAKE_BUILD_TYPE=Debug . && make | |
* Run: | |
* ./mega-streaming-test | |
*/ | |
#include <iostream> | |
#include <megaapi.h> | |
#include "listeners.h" | |
using namespace std; | |
using namespace mega; | |
const char *APP_KEY = "HUc2iQaJ"; | |
MegaApi *mega_api; | |
MegaNode *get_mega_public_node(string url) | |
{ | |
get_public_node_listener listener; | |
mega_api->getPublicNode(url.c_str(), &listener); | |
return listener.wait_for_result(); | |
} | |
void test_download() | |
{ | |
string mega_url = | |
"https://mega.co.nz/#!rAdmmBDQ!NMSKeDukFB9vpT42tbXEl0_GNNzrkMx7Vuvz4anuo2U"; | |
MegaNode *node = get_mega_public_node(mega_url); | |
auto size = node->getSize(); | |
cout << "node size: " << size << endl; | |
cout << "---- Normal download:" << endl; | |
streaming_listener listener; | |
mega_api->startDownload(node, "/tmp/test", &listener); | |
listener.wait_for_result(); | |
cout << endl << "---- Streaming download:" << endl; | |
/* | |
* It seems the error is returned from src/megaapi_impl.cpp:6452 | |
* | |
* (gdb output) | |
* Breakpoint 13, mega::MegaApiImpl::sendPendingTransfers (this=0x61c060) | |
* at src/megaapi_impl.cpp:6452 | |
* 6452 if(!node && !publicNode) { e = API_EARGS; break; } | |
*/ | |
listener = streaming_listener(); | |
mega_api->startStreaming(node, 0, size, &listener); | |
listener.wait_for_result(); | |
} | |
int main() | |
{ | |
mega_api = new MegaApi(APP_KEY); | |
mega_api->setLogLevel(MegaApi::LOG_LEVEL_INFO); | |
test_download(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment