aboutsummaryrefslogtreecommitdiff
path: root/backends/networking/curl/request.h
diff options
context:
space:
mode:
Diffstat (limited to 'backends/networking/curl/request.h')
-rw-r--r--backends/networking/curl/request.h110
1 files changed, 97 insertions, 13 deletions
diff --git a/backends/networking/curl/request.h b/backends/networking/curl/request.h
index d81fe903b8..ff919e02f1 100644
--- a/backends/networking/curl/request.h
+++ b/backends/networking/curl/request.h
@@ -28,40 +28,124 @@
namespace Networking {
-template<typename T> struct RequestIdPair {
- int32 id;
+class Request;
+
+/**
+* Response<T> is a struct to be returned from Request
+* to user's callbacks. It's a type safe way to indicate
+* which "return value" Request has and user awaits.
+*
+* It just keeps a Request pointer together with
+* some T value (which might be a pointer, a reference
+* or a plain type (copied by value)).
+*
+* To make it more convenient, typedefs are used.
+* For example, Response<void *> is called DataResponse
+* and corresponding callback pointer is DataCallback.
+*/
+
+template<typename T> struct Response {
+ Request *request;
T value;
- RequestIdPair(int32 rid, T v) : id(rid), value(v) {}
+ Response(Request *rq, T v) : request(rq), value(v) {}
};
-typedef RequestIdPair<void *> RequestDataPair;
-typedef Common::BaseCallback<RequestDataPair> *DataCallback;
+typedef Response<void *> DataReponse;
+typedef Common::BaseCallback<DataReponse> *DataCallback;
+
+/**
+* RequestState is used to indicate current Request state.
+* ConnectionManager uses it to decide what to do with the Request.
+*
+* PROCESSING state indicates that Request is working.
+* ConnectionManager calls handle() method of Requests in that state.
+*
+* PAUSED state indicates that Request is not working.
+* ConnectionManager keeps Requests in that state and doesn't call any methods of those.
+*
+* RETRY state indicates that Request must restart after a few seconds.
+* ConnectionManager calls handleRetry() method of Requests in that state.
+* Default handleRetry() implementation decreases _retryInSeconds value
+* until it reaches zero. When it does, Request's restart() method is called.
+*
+* FINISHED state indicates that Request did the work and might be deleted.
+* ConnectionManager deletes Requests in that state.
+* After this state is set, but before ConnectionManager deletes the Request,
+* Request calls user's callback. User can ask Request to change its state
+* by calling retry() or pause() methods and Request won't be deleted.
+*/
+
+enum RequestState {
+ PROCESSING,
+ PAUSED,
+ RETRY,
+ FINISHED
+};
class Request {
protected:
/**
- * Callback, which should be called before Request returns true in handle().
+ * Callback, which should be called when Request is finished.
* That's the way Requests pass the result to the code which asked to create this request.
+ *
+ * @note some Requests use their own callbacks to return something but void *.
+ * @note callback must be called in finish() or similar method.
*/
DataCallback _callback;
- int32 _id;
+ /**
+ * Request state, which is used by ConnectionManager to determine
+ * whether request might be deleted or it's still working.
+ *
+ * State might be changed from outside with finish(), pause() or
+ * retry() methods. Override these if you want to react to these
+ * changes correctly.
+ */
+
+ RequestState _state;
+
+ /** In RETRY state this indicates whether it's time to call restart(). */
+ uint32 _retryInSeconds;
public:
- Request(DataCallback cb): _callback(cb), _id(-1) {}
+ Request(DataCallback cb): _callback(cb), _state(PROCESSING), _retryInSeconds(0) {}
virtual ~Request() { delete _callback; }
- /**
- * Method, which does actual work. Depends on what this Request is doing.
- */
-
+ /** Method, which does actual work. Depends on what this Request is doing. */
virtual void handle() = 0;
+ /** Method, which is called by ConnectionManager when Request's state is RETRY. */
+ virtual void handleRetry() {
+ if (_retryInSeconds > 0) --_retryInSeconds;
+ else {
+ _state = PROCESSING;
+ restart();
+ }
+ }
+
+ /** Method, which is used to restart the Request. */
virtual void restart() = 0;
- void setId(int32 id) { _id = id; }
+ /** Method, which is called to pause the Request. */
+ virtual void pause() { _state = PAUSED; }
+
+ /**
+ * Method, which is called to *interrupt* the Request.
+ * When it's called, Request must stop its work and
+ * call the callback to notify user of failure.
+ */
+ virtual void finish() { _state = FINISHED; }
+
+ /** Method, which is called to retry the Request. */
+ virtual void retry(uint32 seconds) {
+ _state = RETRY;
+ _retryInSeconds = seconds;
+ }
+
+ /** Returns Request's current state. */
+ RequestState state() const { return _state; }
};
} //end of namespace Cloud