74 lines
No EOL
1.8 KiB
C++
74 lines
No EOL
1.8 KiB
C++
#ifndef BUFFERED_REQUEST_H
|
|
#define BUFFERED_REQUEST_H
|
|
|
|
#include <string>
|
|
#include <map>
|
|
#include "camcam_httpclient.h"
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class IRequestBuffer
|
|
{
|
|
public:
|
|
|
|
typedef unsigned int ObjectId;
|
|
|
|
virtual ~IRequestBuffer() {};
|
|
static IRequestBuffer* factory(const std::string& destServerScript);
|
|
|
|
virtual bool addRequest(IRequestBuffer::ObjectId objectId, const char *propertyValue) = 0;
|
|
virtual std::string getRequestLine(bool consume=true) = 0;
|
|
|
|
};
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class BufferedRequester : public CamCamHttpClient
|
|
{
|
|
public:
|
|
BufferedRequester() : _BufferedSendingPeriodS(60) {}
|
|
bool addRequest(const char *destServerScript, IRequestBuffer::ObjectId objectId, const char *propertyValue);
|
|
std::string getLineToSend(bool consume=true);
|
|
bool sendBatch(const std::string& lineToSend);
|
|
|
|
protected:
|
|
|
|
void clear();
|
|
int _BufferedSendingPeriodS;
|
|
|
|
private:
|
|
|
|
typedef std::map<std::string, IRequestBuffer*> BufferedRequesterMap;
|
|
BufferedRequesterMap _BRMap;
|
|
};
|
|
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class BckgndBufferedRequester : public BufferedRequester, public CBackgroundTask
|
|
{
|
|
public:
|
|
BckgndBufferedRequester() : _ExpediteNextSending(false), _WantExit(false) {}
|
|
~BckgndBufferedRequester();
|
|
bool init(const SimpleString<16> *pIdAccount);
|
|
void setInitialPeriod(int seconds) { _BufferedSendingPeriodS = seconds; } // only call before starting the thread!
|
|
bool addRequest(const char *destServerScript, IRequestBuffer::ObjectId objectId, const char *propertyValue, bool expediteSending=false);
|
|
bool flushAndExit(); // asynchronous, use with waitForExit()
|
|
|
|
protected:
|
|
bool sendBatch(const std::string& lineToSend) { return BufferedRequester::sendBatch(lineToSend); }
|
|
|
|
private:
|
|
|
|
/// The code of the background task.
|
|
void run();
|
|
|
|
MUTEX_TYPE _MutexToRequest;
|
|
volatile bool _ExpediteNextSending;
|
|
volatile bool _WantExit;
|
|
};
|
|
|
|
#endif // BUFFERED_REQUEST_H
|