xref: /haiku/src/servers/launch/Job.h (revision e2f83cbd6b2a52abb62fef4407c2a97023055648)
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 Job;
26 class Target;
27 
28 struct entry_ref;
29 
30 
31 typedef std::map<BString, BMessage> PortMap;
32 
33 
34 class TeamRegistrator {
35 public:
36 	virtual	void				RegisterTeam(Job* job) = 0;
37 };
38 
39 
40 class Job : public BaseJob {
41 public:
42 								Job(const char* name);
43 								Job(const Job& other);
44 	virtual						~Job();
45 
46 			::TeamRegistrator*	TeamRegistrator() const;
47 			void				SetTeamRegistrator(
48 									::TeamRegistrator* registrator);
49 
50 			bool				IsEnabled() const;
51 			void				SetEnabled(bool enable);
52 
53 			bool				IsService() const;
54 			void				SetService(bool service);
55 
56 			bool				CreateDefaultPort() const;
57 			void				SetCreateDefaultPort(bool createPort);
58 
59 			void				AddPort(BMessage& data);
60 
61 			const BStringList&	Arguments() const;
62 			BStringList&		Arguments();
63 			void				AddArgument(const char* argument);
64 
65 			::Target*			Target() const;
66 			void				SetTarget(::Target* target);
67 
68 			const BStringList&	Requirements() const;
69 			BStringList&		Requirements();
70 			void				AddRequirement(const char* requirement);
71 
72 	virtual	bool				CheckCondition(ConditionContext& context) const;
73 
74 			status_t			Init(const Finder& jobs,
75 									std::set<BString>& dependencies);
76 			status_t			InitCheck() const;
77 
78 			team_id				Team() const;
79 
80 			const PortMap&		Ports() const;
81 			port_id				Port(const char* name = NULL) const;
82 
83 			status_t			Launch();
84 			bool				IsLaunched() const;
85 			bool				IsRunning() const;
86 			void				TeamDeleted();
87 			bool				CanBeLaunched() const;
88 
89 			bool				IsLaunching() const;
90 			void				SetLaunching(bool launching);
91 
92 			status_t			HandleGetLaunchData(BMessage* message);
93 
94 	virtual	status_t			Run();
95 
96 protected:
97 	virtual	status_t			Execute();
98 
99 private:
100 			Job&				operator=(const Job& other);
101 			void				_DeletePorts();
102 			status_t			_AddRequirement(BJob* dependency);
103 			void				_AddStringList(std::vector<const char*>& array,
104 									const BStringList& list);
105 
106 			void				_SetLaunchStatus(status_t launchStatus);
107 
108 			status_t			_SendLaunchDataReply(BMessage* message);
109 			void				_SendPendingLaunchDataReplies();
110 
111 			status_t			_CreateAndTransferPorts();
112 
113 			status_t			_Launch(const char* signature, entry_ref* ref,
114 									int argCount, const char* const* args,
115 									const char** environment);
116 
117 private:
118 			BStringList			fArguments;
119 			BStringList			fRequirements;
120 			bool				fEnabled;
121 			bool				fService;
122 			bool				fCreateDefaultPort;
123 			bool				fLaunching;
124 			PortMap				fPortMap;
125 			status_t			fInitStatus;
126 			team_id				fTeam;
127 			status_t			fLaunchStatus;
128 			mutex				fLaunchStatusLock;
129 			::Target*			fTarget;
130 			::Condition*		fCondition;
131 			BObjectList<BMessage>
132 								fPendingLaunchDataReplies;
133 			::TeamRegistrator*	fTeamRegistrator;
134 };
135 
136 
137 class Finder {
138 public:
139 	virtual	Job*				FindJob(const char* name) const = 0;
140 	virtual	Target*				FindTarget(const char* name) const = 0;
141 };
142 
143 
144 #endif // JOB_H
145