-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMegaAppRequestListener.cpp
101 lines (87 loc) · 2.76 KB
/
MegaAppRequestListener.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// Created by zerocool on 12/11/22.
//
#include "megaapi.h"
#include <cstdint>
#include <iostream>
#include "MegaAppRequestListener.h"
#include "log.h"
MegaAppRequestListener::MegaAppRequestListener(mega::MegaApi *api)
{
this->m_notified = false;
this->m_api = api;
}
void MegaAppRequestListener::lockAndNotify()
{
{
std::unique_lock<std::mutex> lock(this->m_mu);
m_notified = true;
}
m_cv.notify_all();
}
void MegaAppRequestListener::onRequestStart(mega::MegaApi* api, mega::MegaRequest *request)
{
VLOG_F(2, "onRequestStart");
}
void MegaAppRequestListener::onRequestUpdate(mega::MegaApi* api,mega::MegaRequest *request)
{
VLOG_F(2, "onRequestUpdate");
}
void MegaAppRequestListener::onRequestFinish(mega::MegaApi* api, mega::MegaRequest *request, mega::MegaError* e)
{
VLOG_F(2, "onRequestFinish");
this->m_err = e->copy();
int requestType = request->getType();
if(e->getErrorCode() != mega::MegaError::API_OK)
{
VLOG_F(2, "onRequestFinish: E: %s", e->getErrorString());
this->lockAndNotify();
} else {
switch(requestType)
{
case mega::MegaRequest::TYPE_LOGIN:
VLOG_F(2, "onRequestFinish: request type is login, starting api->fetchNodes operation");
api->fetchNodes(this);
break;
case mega::MegaRequest::TYPE_FETCH_NODES:
VLOG_F(2, "onRequestFinish: request type is fetchNodes, setting up rootNode");
this->m_publicNode = api->getRootNode()->copy();
break;
case mega::MegaRequest::TYPE_GET_PUBLIC_NODE:
VLOG_F(2, "onRequestFinish: request type is getPublicNode, setting publicNode");
this->m_publicNode = request->getPublicMegaNode()->copy();
break;
}
}
if(requestType != mega::MegaRequest::TYPE_LOGIN && requestType != mega::MegaRequest::TYPE_DELETE)
{
VLOG_F(2, "onRequestFinish: calling lockAndNotify");
this->lockAndNotify();
}
}
void MegaAppRequestListener::onRequestTemporaryError(mega::MegaApi* api, mega::MegaRequest *request, mega::MegaError* error)
{
VLOG_F(2, "onRequestTemporaryError: %s", error->toString());
}
mega::MegaNode* MegaAppRequestListener::GetPublicNode()
{
return this->m_publicNode;
}
void MegaAppRequestListener::SetPublicNode(mega::MegaNode *node)
{
this->m_publicNode = node;
}
mega::MegaError* MegaAppRequestListener::GetError()
{
return this->m_err;
}
void MegaAppRequestListener::Reset()
{
this->m_notified = false;
}
void MegaAppRequestListener::Wait()
{
VLOG_F(2, "Wait: waiting for condition_variable to be notified");
std::unique_lock<std::mutex> lock(this->m_mu);
this->m_cv.wait(lock, [this]{return this->m_notified;});
}