75 lines
1.6 KiB
C++
75 lines
1.6 KiB
C++
#include <atomic>
|
|
#include <functional>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
namespace clanguml {
|
|
namespace t20029 {
|
|
std::string encode_b64(std::string &&content) { return std::move(content); }
|
|
|
|
template <typename T> class Encoder : public T {
|
|
public:
|
|
bool send(std::string &&msg)
|
|
{
|
|
return T::send(std::move(
|
|
// Encode the message using Base64 encoding and pass it to the next
|
|
// layer
|
|
encode(std::move(msg))));
|
|
}
|
|
|
|
protected:
|
|
std::string encode(std::string &&msg) { return encode_b64(std::move(msg)); }
|
|
};
|
|
|
|
template <typename T> class Retrier : public T {
|
|
public:
|
|
bool send(std::string &&msg)
|
|
{
|
|
std::string buffer{std::move(msg)};
|
|
|
|
int retryCount = 5;
|
|
|
|
// Repeat until send() succeeds or retry count is exceeded
|
|
while (retryCount--) {
|
|
if (T::send(buffer))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
};
|
|
|
|
class ConnectionPool {
|
|
public:
|
|
void connect()
|
|
{
|
|
if (!is_connected_.load())
|
|
connect_impl();
|
|
}
|
|
|
|
bool send(const std::string &msg) { return true; }
|
|
|
|
private:
|
|
void connect_impl() { is_connected_ = true; }
|
|
|
|
std::atomic<bool> is_connected_;
|
|
};
|
|
|
|
int tmain()
|
|
{
|
|
auto pool = std::make_shared<Encoder<Retrier<ConnectionPool>>>();
|
|
|
|
// Establish connection to the remote server synchronously
|
|
pool->connect();
|
|
|
|
// Repeat for each line in the input stream
|
|
for (std::string line; std::getline(std::cin, line);) {
|
|
if (!pool->send(std::move(line)))
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
} |