xref: /haiku/src/apps/haikudepot/server/AbstractServerProcess.h (revision efafab643ce980e3f3c916795ed302599f6b4f66)
1 /*
2  * Copyright 2017-2018, 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 typedef enum process_options {
19 	SERVER_PROCESS_NO_NETWORKING	= 1 << 0,
20 	SERVER_PROCESS_PREFER_CACHE		= 1 << 1,
21 	SERVER_PROCESS_DROP_CACHE		= 1 << 2
22 } process_options;
23 
24 
25 typedef enum process_state {
26 	SERVER_PROCESS_INITIAL			= 1,
27 	SERVER_PROCESS_RUNNING			= 2,
28 	SERVER_PROCESS_COMPLETE			= 3
29 } process_state;
30 
31 
32 /*! Clients are able to subclass from this 'interface' in order to accept
33     call-backs when a process has exited; either through success or through
34     failure.
35  */
36 
37 class AbstractServerProcessListener {
38 public:
39 	virtual	void				ServerProcessExited() = 0;
40 };
41 
42 
43 class AbstractServerProcess : public Stoppable {
44 public:
45 								AbstractServerProcess(
46 									AbstractServerProcessListener* listener,
47 									uint32 options);
48 	virtual						~AbstractServerProcess();
49 
50 	virtual	const char*				Name() = 0;
51 			status_t			Run();
52 			status_t			Stop();
53 			status_t			ErrorStatus();
54 			bool				IsRunning();
55 			bool				WasStopped();
56 
57 protected:
58 	virtual status_t			RunInternal() = 0;
59 	virtual status_t			StopInternal();
60 
61 	virtual	void				GetStandardMetaDataPath(
62 									BPath& path) const = 0;
63 	virtual	void				GetStandardMetaDataJsonPath(
64 									BString& jsonPath) const = 0;
65 
66 			status_t			IfModifiedSinceHeaderValue(
67 									BString& headerValue) const;
68 			status_t			IfModifiedSinceHeaderValue(
69 									BString& headerValue,
70 									const BPath& metaDataPath,
71 									const BString& jsonPath) const;
72 
73 			status_t			PopulateMetaData(
74 									StandardMetaData& metaData,
75 									const BPath& path,
76 									const BString& jsonPath) const;
77 
78 			status_t			ParseJsonFromFileWithListener(
79 									BJsonEventListener *listener,
80 									const BPath& path) const;
81 
82 			status_t			DownloadToLocalFileAtomically(
83 									const BPath& targetFilePath,
84 									const BUrl& url);
85 
86 			status_t			DeleteLocalFile(const BPath& filePath);
87 			status_t			MoveDamagedFileAside(const BPath& filePath);
88 
89 			bool				HasOption(uint32 flag);
90 			bool				ShouldAttemptNetworkDownload(
91 									bool hasDataAlready);
92 
93 	static	bool				IsSuccess(status_t e);
94 
95 private:
96 			BLocker				fLock;
97 			AbstractServerProcessListener*
98 								fListener;
99 			bool				fWasStopped;
100 			process_state		fProcessState;
101 			status_t			fErrorStatus;
102 			uint32				fOptions;
103 
104 			BHttpRequest*		fRequest;
105 
106 			process_state		ProcessState();
107 			void				SetErrorStatus(status_t value);
108 			void				SetProcessState(process_state value);
109 
110 			status_t			DownloadToLocalFile(
111 									const BPath& targetFilePath,
112 									const BUrl& url,
113 									uint32 redirects, uint32 failures);
114 
115 			bool				LooksLikeGzip(const char *pathStr) const;
116 
117 };
118 
119 #endif // ABSTRACT_SERVER_PROCESS_H
120