xref: /haiku/src/servers/launch/Job.h (revision 7cd19b7e5c0e66b06a3064275dd7519e50a7f89f)
1 /*
2  * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef JOB_H
6 #define JOB_H
7 
8 
9 #include "BaseJob.h"
10 
11 #include <map>
12 #include <set>
13 #include <vector>
14 
15 #include <OS.h>
16 #include <StringList.h>
17 
18 #include <locks.h>
19 
20 
21 using namespace BSupportKit;
22 class BMessage;
23 
24 class Finder;
25 class Target;
26 
27 struct entry_ref;
28 
29 
30 typedef std::map<BString, BMessage> PortMap;
31 
32 
33 class Job : public BaseJob {
34 public:
35 								Job(const char* name);
36 								Job(const Job& other);
37 	virtual						~Job();
38 
39 			bool				IsEnabled() const;
40 			void				SetEnabled(bool enable);
41 
42 			bool				IsService() const;
43 			void				SetService(bool service);
44 
45 			bool				CreateDefaultPort() const;
46 			void				SetCreateDefaultPort(bool createPort);
47 
48 			void				AddPort(BMessage& data);
49 
50 			const BStringList&	Arguments() const;
51 			BStringList&		Arguments();
52 			void				AddArgument(const char* argument);
53 
54 			::Target*			Target() const;
55 			void				SetTarget(::Target* target);
56 
57 			const BStringList&	Requirements() const;
58 			BStringList&		Requirements();
59 			void				AddRequirement(const char* requirement);
60 
61 	virtual	bool				CheckCondition(ConditionContext& context) const;
62 
63 			status_t			Init(const Finder& jobs,
64 									std::set<BString>& dependencies);
65 			status_t			InitCheck() const;
66 
67 			team_id				Team() const;
68 
69 			const PortMap&		Ports() const;
70 			port_id				Port(const char* name = NULL) const;
71 
72 			status_t			Launch();
73 			bool				IsLaunched() const;
74 			bool				IsRunning() const;
75 
76 			status_t			HandleGetLaunchData(BMessage* message);
77 
78 	virtual	status_t			Run();
79 
80 protected:
81 	virtual	status_t			Execute();
82 
83 private:
84 			Job&				operator=(const Job& other);
85 			void				_DeletePorts();
86 			status_t			_AddRequirement(BJob* dependency);
87 			void				_AddStringList(std::vector<const char*>& array,
88 									const BStringList& list);
89 
90 			void				_SetLaunchStatus(status_t launchStatus);
91 
92 			status_t			_SendLaunchDataReply(BMessage* message);
93 			void				_SendPendingLaunchDataReplies();
94 
95 			status_t			_CreateAndTransferPorts();
96 
97 			status_t			_Launch(const char* signature, entry_ref* ref,
98 									int argCount, const char* const* args,
99 									const char** environment);
100 
101 private:
102 			BStringList			fArguments;
103 			BStringList			fRequirements;
104 			bool				fEnabled;
105 			bool				fService;
106 			bool				fCreateDefaultPort;
107 			PortMap				fPortMap;
108 			status_t			fInitStatus;
109 			team_id				fTeam;
110 			status_t			fLaunchStatus;
111 			mutex				fLaunchStatusLock;
112 			::Target*			fTarget;
113 			::Condition*		fCondition;
114 			BObjectList<BMessage>
115 								fPendingLaunchDataReplies;
116 };
117 
118 
119 class Finder {
120 public:
121 	virtual	Job*				FindJob(const char* name) const = 0;
122 	virtual	Target*				FindTarget(const char* name) const = 0;
123 };
124 
125 
126 #endif // JOB_H
127