I have a Manager class that starts one of several tasks (Poco::Util::TimerTask) that are executed periodically in a separate thread using Poco::Util::Timer. When a certain condition is met, this task should stop and tell the manager to start another task, and so on.
To be more specific, I need to send data to the server every n seconds, and if the data cannot be sent, then save it to a file every m seconds.
- Do I need to design it differently? how can I design classes to get rid of the circular dependency (between Manager and tasks)
- Maybe do you have ideas how to send and save more simplier?
I can show this at the moment
class Manager {
Poco::AutoPtr<Poco::Util::TimerTask> sendTask;
Poco::AutoPtr<Poco::Util::TimerTask> saveTask;
Poco::Util::Timer timer;
public:
void start() {
timer.scheduleAtFixedRate(sendTask, 15000, 15000);
}
void setSendTask() {
timer.cancel();
sendTaskPtr_.reset(new SendTimerTask( sender_, saver_, *this, agent_ ));
timer.scheduleAtFixedRate(sendTaskPtr_, 0, 15000);
}
void setSaveTask() {
timer.cancel();
saveTaskPtr_.reset(new SaveTimerTask( saver_, sender_, *this, agent_ ));
timer.scheduleAtFixedRate(saveTaskPtr_, 0, 20000);
}
};
class SendTask : public Poco::Util::TimerTask{
public:
void run() override {
// sending to server
if (condition) {
manager.setSaveTask();
}
}
private:
Manager manager;
};
2