Unify network strategies with upcoming proxy.
This commit is contained in:
parent
a2f747fb0c
commit
c46c019c83
10 changed files with 110 additions and 45 deletions
|
@ -25,17 +25,28 @@
|
|||
#include "interfaces/IStrategyListener.h"
|
||||
#include "net/Client.h"
|
||||
#include "net/strategies/FailoverStrategy.h"
|
||||
#include "Platform.h"
|
||||
|
||||
|
||||
FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, const char *agent, IStrategyListener *listener) :
|
||||
FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener) :
|
||||
m_release(false),
|
||||
m_retries(retries),
|
||||
m_retryPause(retryPause),
|
||||
m_active(-1),
|
||||
m_index(0),
|
||||
m_remaining(0),
|
||||
m_listener(listener)
|
||||
{
|
||||
for (const Url *url : urls) {
|
||||
add(url, agent);
|
||||
add(url);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
FailoverStrategy::~FailoverStrategy()
|
||||
{
|
||||
for (Client *client : m_pools) {
|
||||
delete client;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,13 +63,25 @@ void FailoverStrategy::connect()
|
|||
}
|
||||
|
||||
|
||||
void FailoverStrategy::release()
|
||||
{
|
||||
m_release = true;
|
||||
|
||||
for (size_t i = 0; i < m_pools.size(); ++i) {
|
||||
if (m_pools[i]->disconnect()) {
|
||||
m_remaining++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FailoverStrategy::resume()
|
||||
{
|
||||
if (!isActive()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_listener->onJob( m_pools[m_active], m_pools[m_active]->job());
|
||||
m_listener->onJob(this, m_pools[m_active], m_pools[m_active]->job());
|
||||
}
|
||||
|
||||
|
||||
|
@ -86,6 +109,14 @@ void FailoverStrategy::tick(uint64_t now)
|
|||
void FailoverStrategy::onClose(Client *client, int failures)
|
||||
{
|
||||
if (failures == -1) {
|
||||
if (m_release) {
|
||||
m_remaining--;
|
||||
|
||||
if (m_remaining == 0) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -107,7 +138,7 @@ void FailoverStrategy::onClose(Client *client, int failures)
|
|||
void FailoverStrategy::onJobReceived(Client *client, const Job &job)
|
||||
{
|
||||
if (m_active == client->id()) {
|
||||
m_listener->onJob(client, job);
|
||||
m_listener->onJob(this, client, job);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -128,20 +159,20 @@ void FailoverStrategy::onLoginSuccess(Client *client)
|
|||
|
||||
if (active >= 0 && active != m_active) {
|
||||
m_index = m_active = active;
|
||||
m_listener->onActive(client);
|
||||
m_listener->onActive(this, client);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FailoverStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error)
|
||||
{
|
||||
m_listener->onResultAccepted(client, result, error);
|
||||
m_listener->onResultAccepted(this, client, result, error);
|
||||
}
|
||||
|
||||
|
||||
void FailoverStrategy::add(const Url *url, const char *agent)
|
||||
void FailoverStrategy::add(const Url *url)
|
||||
{
|
||||
Client *client = new Client((int) m_pools.size(), agent, this);
|
||||
Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this);
|
||||
client->setUrl(url);
|
||||
client->setRetryPause(m_retryPause * 1000);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue