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