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 75 status_t HandleGetLaunchData(BMessage* message); 76 77 protected: 78 virtual status_t Execute(); 79 80 private: 81 Job& operator=(const Job& other); 82 void _DeletePorts(); 83 status_t _AddRequirement(BJob* dependency); 84 void _AddStringList(std::vector<const char*>& array, 85 const BStringList& list); 86 87 void _SetLaunchStatus(status_t launchStatus); 88 89 status_t _SendLaunchDataReply(BMessage* message); 90 void _SendPendingLaunchDataReplies(); 91 92 status_t _CreateAndTransferPorts(); 93 94 status_t _Launch(const char* signature, entry_ref* ref, 95 int argCount, const char* const* args, 96 const char** environment); 97 98 private: 99 BStringList fArguments; 100 BStringList fRequirements; 101 bool fEnabled; 102 bool fService; 103 bool fCreateDefaultPort; 104 PortMap fPortMap; 105 status_t fInitStatus; 106 team_id fTeam; 107 status_t fLaunchStatus; 108 mutex fLaunchStatusLock; 109 ::Target* fTarget; 110 ::Condition* fCondition; 111 BObjectList<BMessage> 112 fPendingLaunchDataReplies; 113 }; 114 115 116 class Finder { 117 public: 118 virtual Job* FindJob(const char* name) const = 0; 119 virtual Target* FindTarget(const char* name) const = 0; 120 }; 121 122 123 #endif // JOB_H 124