Repository navigation
Expand file tree
/
Copy pathCallInternal.cpp
More file actions
123 lines (95 loc) · 2.91 KB
/
Copy pathCallInternal.cpp
File metadata and controls
123 lines (95 loc) · 2.91 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2017 Sony Corporation
*/
#include <istream>
#include <ostream>
#include "easyhttpcpp/common/CoreLogger.h"
#include "easyhttpcpp/HttpException.h"
#include "CallInternal.h"
#include "HttpAsyncExecutionTask.h"
namespace easyhttpcpp {
static const std::string Tag = "CallInternal";
CallInternal::CallInternal(EasyHttpContext::Ptr pContext, Request::Ptr pRequest) : m_pContext(pContext),
m_pUserRequest(pRequest), m_executed(false), m_cancelled(false)
{
}
CallInternal::~CallInternal()
{
}
Response::Ptr CallInternal::execute()
{
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
if (m_executed) {
EASYHTTPCPP_LOG_D(Tag, "Can not execute because already executed.");
throw HttpIllegalStateException("Can not execute because already executed.");
}
m_executed = true;
m_pRequestExecutor = new HttpRequestExecutor(m_pContext, m_pUserRequest);
if (m_cancelled) {
m_pRequestExecutor->cancel();
}
}
return m_pRequestExecutor->execute();
}
void CallInternal::executeAsync(ResponseCallback::Ptr pResponseCallback)
{
HttpAsyncExecutionTask::Ptr pAsyncExecutionTask;
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
if (m_executed) {
EASYHTTPCPP_LOG_D(Tag, "Can not execute because already executed.");
throw HttpIllegalStateException("Can not execute because already executed.");
}
if (!pResponseCallback) {
EASYHTTPCPP_LOG_D(Tag, "ResponseCallback is NULL.");
throw HttpIllegalArgumentException("ResponseCallback can not be NULL.");
}
m_executed = true;
m_pRequestExecutor = new HttpRequestExecutor(m_pContext, m_pUserRequest);
pAsyncExecutionTask = new HttpAsyncExecutionTask(m_pContext, m_pRequestExecutor,
pResponseCallback);
if (m_cancelled) {
m_pRequestExecutor->cancel();
}
}
m_pContext->getHttpExecutionTaskManager()->start(pAsyncExecutionTask);
}
bool CallInternal::isExecuted() const
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
return m_executed;
}
Request::Ptr CallInternal::getRequest() const
{
return m_pUserRequest;
}
bool CallInternal::cancel()
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
m_cancelled = true;
if (m_pRequestExecutor) {
return m_pRequestExecutor->cancel();
} else {
return true;
}
}
bool CallInternal::isCancelled() const
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
if (m_pRequestExecutor) {
return m_pRequestExecutor->isCancelled();
} else {
return m_cancelled;
}
}
HttpEngine::Ptr CallInternal::getHttpEngine()
{
Poco::FastMutex::ScopedLock lock(m_instanceMutex);
if (m_pRequestExecutor) {
return m_pRequestExecutor->getHttpEngine();
} else {
return NULL;
}
}
} /* namespace easyhttpcpp */