2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
6 #ifndef ABSTRACT_SERVER_PROCESS_H
7 #define ABSTRACT_SERVER_PROCESS_H
9 #include <HttpRequest.h>
14 #include "StandardMetaData.h"
15 #include "Stoppable.h"
18 #define APP_ERR_NOT_MODIFIED (B_APP_ERROR_BASE + 452)
19 #define APP_ERR_NO_DATA (B_APP_ERROR_BASE + 453)
22 typedef enum process_options
{
23 SERVER_PROCESS_NO_NETWORKING
= 1 << 0,
24 SERVER_PROCESS_PREFER_CACHE
= 1 << 1,
25 SERVER_PROCESS_DROP_CACHE
= 1 << 2
29 typedef enum process_state
{
30 SERVER_PROCESS_INITIAL
= 1,
31 SERVER_PROCESS_RUNNING
= 2,
32 SERVER_PROCESS_COMPLETE
= 3
36 /*! Clients are able to subclass from this 'interface' in order to accept
37 call-backs when a process has exited; either through success or through
41 class AbstractServerProcessListener
{
43 virtual void ServerProcessExited() = 0;
47 class AbstractServerProcess
: public Stoppable
{
49 AbstractServerProcess(
50 AbstractServerProcessListener
* listener
,
52 virtual ~AbstractServerProcess();
54 virtual const char* Name() = 0;
57 status_t
ErrorStatus();
62 virtual status_t
RunInternal() = 0;
63 virtual status_t
StopInternal();
65 virtual void GetStandardMetaDataPath(
66 BPath
& path
) const = 0;
67 virtual void GetStandardMetaDataJsonPath(
68 BString
& jsonPath
) const = 0;
70 status_t
IfModifiedSinceHeaderValue(
71 BString
& headerValue
) const;
72 status_t
IfModifiedSinceHeaderValue(
74 const BPath
& metaDataPath
,
75 const BString
& jsonPath
) const;
77 status_t
PopulateMetaData(
78 StandardMetaData
& metaData
,
80 const BString
& jsonPath
) const;
82 status_t
ParseJsonFromFileWithListener(
83 BJsonEventListener
*listener
,
84 const BPath
& path
) const;
86 status_t
DownloadToLocalFileAtomically(
87 const BPath
& targetFilePath
,
90 status_t
DeleteLocalFile(const BPath
& currentFilePath
);
92 status_t
MoveDamagedFileAside(
93 const BPath
& currentFilePath
);
95 bool HasOption(uint32 flag
);
96 bool ShouldAttemptNetworkDownload(
99 static bool IsSuccess(status_t e
);
103 AbstractServerProcessListener
*
106 process_state fProcessState
;
107 status_t fErrorStatus
;
110 BHttpRequest
* fRequest
;
112 process_state
ProcessState();
113 void SetErrorStatus(status_t value
);
114 void SetProcessState(process_state value
);
116 status_t
DownloadToLocalFile(
117 const BPath
& targetFilePath
,
119 uint32 redirects
, uint32 failures
);
121 bool LooksLikeGzip(const char *pathStr
) const;
125 #endif // ABSTRACT_SERVER_PROCESS_H