+/*****************************************************************************
+** FILE IDENTIFICATION
+**
+** Name: backgroundsupr.h
+** Purpose: Header file for background supervisors
+** Programmer: Kevin Rosenberg
+** Date Started: February 2001
+**
+** This is part of the CTSim program
+** Copyright (C) 1983-2001 Kevin Rosenberg
+**
+** $Id: backgroundsupr.h,v 1.1 2001/02/25 06:32:12 kevin Exp $
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License (version 2) as
+** published by the Free Software Foundation.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+******************************************************************************/
+
+#ifndef _BACKGROUNDSUPR_H
+#define _BACKGROUNDSUPR_H
+
+#include <vector>
+#include <wx/thread.h>
+#include <wx/progdlg.h>
+#include "timer.h"
+
+// This thread creates a SupervisorTask event handler object
+// The thread is detached and terminates when SupervisorTask terminates
+class SupervisorThread : public wxThread {
+private:
+
+public:
+ SupervisorThread()
+ : wxThread(wxTHREAD_DETACHED)
+ {}
+
+};
+
+
+// Pure virtual class for BackgroundSupervisor that can communication
+// with BackgroundManager via messages
+class BackgroundWorkerThread;
+
+class BackgroundSupervisor : public wxEvtHandler {
+private:
+ DECLARE_DYNAMIC_CLASS(BackgroundSupervisor)
+
+ typedef std::vector<BackgroundWorkerThread*> ThreadContainer;
+ ThreadContainer m_vecpThreads;
+ wxCriticalSection m_critsectThreadContainer;
+ wxFrame* m_pParentFrame;
+ wxDocument* m_pDocument;
+ const std::string m_strProcessTitle;
+ wxProgressDialog* m_pDialogProgress;
+
+ volatile bool m_bFail;
+ std::string m_strFailMessage;
+ int m_iNumThreads;
+ volatile int m_iRunning;
+ unsigned int m_iUnitsDone;
+ const unsigned int m_iTotalUnits;
+ bool m_bCancelled;
+ bool m_bDone;
+ Timer* m_pTimer;
+ bool m_bBackgroundTaskAdded;
+
+public:
+ enum {
+ MSG_BACKGROUND_SUPERVISOR_ADD = 7500, // sends to BackgroundManager and Document
+ MSG_BACKGROUND_SUPERVISOR_REMOVE = 7501, // sends to BackgroundManager and Document
+ MSG_BACKGROUND_SUPERVISOR_UNIT_TICK = 7502, // sends to BackgroundManager for progress bars
+ MSG_BACKGROUND_SUPERVISOR_CANCEL = 7503, // *sent* to Supervisor to cancel process
+ MSG_DOCUMENT_ACK_REMOVE = 7504,
+
+ MSG_WORKER_THREAD_UNIT_TICK = 7505,
+ MSG_WORKER_THREAD_DONE = 7506,
+ MSG_WORKER_THREAD_FAIL = 7507, // sent by workers when they fail
+ };
+
+ BackgroundSupervisor (wxFrame* pParentFrame, wxDocument* pDocument, const char* const pszProcessTitle,
+ int iTotalUnits);
+
+ BackgroundSupervisor ()
+ : m_iTotalUnits(0), wxEvtHandler()
+ {}
+
+ virtual ~BackgroundSupervisor();
+
+ virtual BackgroundWorkerThread* createWorker (int iThread, int iStartUnit, int iNumUnits)
+ { return NULL; }
+
+ bool start();
+ void deleteAnyWorkers();
+ virtual void onDone() {};
+
+ virtual void OnWorkerFail(wxCommandEvent& event);
+ virtual void OnWorkerUnitTick(wxCommandEvent& event);
+ virtual void OnWorkerDone(wxCommandEvent& event);
+ virtual void OnCancel(wxCommandEvent& event);
+ virtual void OnAckDocumentRemove(wxCommandEvent& event);
+
+ bool anyWorkersRunning() const { return m_iRunning > 0 ? true : false; }
+ bool isDone() const {return m_bDone;}
+ void setDone() { m_bDone = true; }
+ bool fail() const {return m_bFail;}
+ const std::string& getFailMessage() const { return m_strFailMessage; }
+
+ int getNumWorkers() const { return m_iNumThreads; }
+ double getTimerEnd() { return m_pTimer->timerEnd(); }
+
+ static void cancelSupervisor (BackgroundSupervisor* pSupervisor);
+
+ DECLARE_EVENT_TABLE()
+};
+
+
+class BackgroundWorkerThread : public wxThread {
+protected:
+ BackgroundSupervisor* m_pSupervisor;
+ const int m_iStartUnit;
+ const int m_iNumUnits;
+ const int m_iThread;
+
+public:
+ BackgroundWorkerThread (BackgroundSupervisor* pSupervisor, int iThread, int iStartUnit, int iNumUnits)
+ : m_pSupervisor(pSupervisor), m_iThread(iThread), m_iStartUnit(iStartUnit), m_iNumUnits(iNumUnits),
+ wxThread (wxTHREAD_DETACHED)
+ {}
+};
+
+#endif // _BACKGROUNDSUPR_H_
\ No newline at end of file