1/*
2 * Copyright 2015-2018, 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
21using namespace BSupportKit;
22class BMessage;
23class BMessenger;
24
25class Finder;
26class Job;
27class Target;
28
29struct entry_ref;
30
31
32typedef std::map<BString, BMessage> PortMap;
33
34
35class TeamListener {
36public:
37	virtual	void				TeamLaunched(Job* job, status_t status) = 0;
38};
39
40
41class Job : public BaseJob {
42public:
43								Job(const char* name);
44								Job(const Job& other);
45	virtual						~Job();
46
47			::TeamListener*		TeamListener() const;
48			void				SetTeamListener(::TeamListener* listener);
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			const BStringList&	Pending() const;
73			BStringList&		Pending();
74			void				AddPending(const char* pending);
75
76	virtual	bool				CheckCondition(ConditionContext& context) const;
77
78			status_t			Init(const Finder& jobs,
79									std::set<BString>& dependencies);
80			status_t			InitCheck() const;
81
82			team_id				Team() const;
83
84			const PortMap&		Ports() const;
85			port_id				Port(const char* name = NULL) const;
86
87			port_id				DefaultPort() const;
88			void				SetDefaultPort(port_id port);
89
90			status_t			Launch();
91			bool				IsLaunched() const;
92			bool				IsRunning() const;
93			void				TeamDeleted();
94			bool				CanBeLaunched() const;
95
96			bool				IsLaunching() const;
97			void				SetLaunching(bool launching);
98
99			status_t			HandleGetLaunchData(BMessage* message);
100			status_t			GetMessenger(BMessenger& messenger);
101
102	virtual	status_t			Run();
103
104protected:
105	virtual	status_t			Execute();
106
107private:
108			Job&				operator=(const Job& other);
109			void				_DeletePorts();
110			status_t			_AddRequirement(BJob* dependency);
111			void				_AddStringList(std::vector<const char*>& array,
112									const BStringList& list);
113
114			void				_SetLaunchStatus(status_t launchStatus);
115
116			status_t			_SendLaunchDataReply(BMessage* message);
117			void				_SendPendingLaunchDataReplies();
118
119			status_t			_CreateAndTransferPorts();
120			port_id				_CreateAndTransferPort(const char* name,
121									int32 capacity);
122
123			status_t			_Launch(const char* signature, entry_ref* ref,
124									int argCount, const char* const* args,
125									const char** environment);
126
127private:
128			BStringList			fArguments;
129			BStringList			fRequirements;
130			bool				fEnabled;
131			bool				fService;
132			bool				fCreateDefaultPort;
133			bool				fLaunching;
134			PortMap				fPortMap;
135			status_t			fInitStatus;
136			team_id				fTeam;
137			port_id				fDefaultPort;
138			uint32				fToken;
139			status_t			fLaunchStatus;
140			mutex				fLaunchStatusLock;
141			::Target*			fTarget;
142			::Condition*		fCondition;
143			BStringList			fPendingJobs;
144			BObjectList<BMessage>
145								fPendingLaunchDataReplies;
146			::TeamListener*		fTeamListener;
147};
148
149
150class Finder {
151public:
152	virtual	Job*				FindJob(const char* name) const = 0;
153	virtual	Target*				FindTarget(const char* name) const = 0;
154};
155
156
157#endif // JOB_H
158