xref: /haiku/src/apps/haikudepot/server/AbstractServerProcess.h (revision 3995592cdf304335132305e27c40cbb0b1ac46e3)
1 /*
2  * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef ABSTRACT_SERVER_PROCESS_H
7 #define ABSTRACT_SERVER_PROCESS_H
8 
9 #include <HttpRequest.h>
10 #include <Json.h>
11 #include <String.h>
12 #include <Url.h>
13 
14 #include "StandardMetaData.h"
15 #include "Stoppable.h"
16 
17 
18 #define APP_ERR_NOT_MODIFIED		(B_APP_ERROR_BASE + 452)
19 #define APP_ERR_NO_DATA				(B_APP_ERROR_BASE + 453)
20 
21 
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
26 } process_options;
27 
28 
29 typedef enum process_state {
30 	SERVER_PROCESS_INITIAL			= 1,
31 	SERVER_PROCESS_RUNNING			= 2,
32 	SERVER_PROCESS_COMPLETE			= 3
33 } process_state;
34 
35 
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
38     failure.
39  */
40 
41 class AbstractServerProcessListener {
42 public:
43 	virtual	void				ServerProcessExited() = 0;
44 };
45 
46 
47 class AbstractServerProcess : public Stoppable {
48 public:
49 								AbstractServerProcess(
50 									AbstractServerProcessListener* listener,
51 									uint32 options);
52 	virtual						~AbstractServerProcess();
53 
54 	virtual	const char*				Name() = 0;
55 			status_t			Run();
56 			status_t			Stop();
57 			status_t			ErrorStatus();
58 			bool				IsRunning();
59 			bool				WasStopped();
60 
61 protected:
62 	virtual status_t			RunInternal() = 0;
63 	virtual status_t			StopInternal();
64 
65 	virtual	void				GetStandardMetaDataPath(
66 									BPath& path) const = 0;
67 	virtual	void				GetStandardMetaDataJsonPath(
68 									BString& jsonPath) const = 0;
69 
70 			status_t			IfModifiedSinceHeaderValue(
71 									BString& headerValue) const;
72 			status_t			IfModifiedSinceHeaderValue(
73 									BString& headerValue,
74 									const BPath& metaDataPath,
75 									const BString& jsonPath) const;
76 
77 			status_t			PopulateMetaData(
78 									StandardMetaData& metaData,
79 									const BPath& path,
80 									const BString& jsonPath) const;
81 
82 			status_t			ParseJsonFromFileWithListener(
83 									BJsonEventListener *listener,
84 									const BPath& path) const;
85 
86 			status_t			DownloadToLocalFileAtomically(
87 									const BPath& targetFilePath,
88 									const BUrl& url);
89 
90 			status_t			DeleteLocalFile(const BPath& currentFilePath);
91 
92 			status_t			MoveDamagedFileAside(
93 									const BPath& currentFilePath);
94 
95 			bool				HasOption(uint32 flag);
96 			bool				ShouldAttemptNetworkDownload(
97 									bool hasDataAlready);
98 
99 	static	bool				IsSuccess(status_t e);
100 
101 private:
102 			BLocker				fLock;
103 			AbstractServerProcessListener*
104 								fListener;
105 			bool				fWasStopped;
106 			process_state		fProcessState;
107 			status_t			fErrorStatus;
108 			uint32				fOptions;
109 
110 			BHttpRequest*		fRequest;
111 
112 			process_state		ProcessState();
113 			void				SetErrorStatus(status_t value);
114 			void				SetProcessState(process_state value);
115 
116 			status_t			DownloadToLocalFile(
117 									const BPath& targetFilePath,
118 									const BUrl& url,
119 									uint32 redirects, uint32 failures);
120 
121 			bool				LooksLikeGzip(const char *pathStr) const;
122 
123 };
124 
125 #endif // ABSTRACT_SERVER_PROCESS_H
126