CamLaunchProcess Class Reference

Launch a long-running asynchronous process. More...

#include <camprocess.h>

List of all members.

Public Member Functions

 CamLaunchProcess ()
 CamLaunchProcess constructor.
virtual ~CamLaunchProcess ()
 CamLaunchProcess destructor.
BOOL Execute (const wxString &cmd)
 Execute a command which should launch a long-running process.
wxKillError Terminate ()
 Kill the process.
virtual INT32 Disconnect ()
 Allow a process to continue to run after this object is destroyed.
virtual void OnTerminate (intpid, intstatus)
 Process anything the process writes to the error stream.

Protected Member Functions

virtual void ProcessStdErr ()
 Process anything the process writes to the error stream.

Protected Attributes

bool m_bDead
bool m_bConnected
INT32 m_ReturnCode
INT32 m_pid


Detailed Description

Launch a long-running asynchronous process.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
19/May/2006 Base Class: wxProcess
See also:
CamProcess, wxProcess

Definition at line 148 of file camprocess.h.


Constructor & Destructor Documentation

CamLaunchProcess::CamLaunchProcess  ) 
 

CamLaunchProcess constructor.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
19/May/2006
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
-

Definition at line 365 of file camprocess.cpp.

00366 {
00367     m_pid = 0;
00368     m_bDead = true;
00369     m_ReturnCode = -1;
00370     m_bConnected = false;
00371 }

CamLaunchProcess::~CamLaunchProcess  )  [virtual]
 

CamLaunchProcess destructor.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
19/May/2006
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
-

Definition at line 386 of file camprocess.cpp.

00387 {
00388     if (m_bConnected)
00389     {
00390         TRACEUSER("Phil", _T("Process still connected in ~CamLaunchProcess"));
00391         Disconnect();
00392     }
00393 }


Member Function Documentation

INT32 CamLaunchProcess::Disconnect  )  [virtual]
 

Allow a process to continue to run after this object is destroyed.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
19/May/2006
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
ProcessID of the process we have just allowed to live

Definition at line 457 of file camprocess.cpp.

00458 {
00459     INT32 pid = m_pid;
00460 
00461     m_pid = 0;
00462     m_bConnected = false;
00463     Detach();
00464 
00465     return pid;
00466 }

BOOL CamLaunchProcess::Execute const wxString &  cmd  ) 
 

Execute a command which should launch a long-running process.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
19/May/2006
Parameters:
cmd - The command string including parameters [INPUTS]
- [OUTPUTS]
Returns:
TRUE if the command was launched successfully FALSE otherwise

Definition at line 409 of file camprocess.cpp.

00410 {
00411     m_ReturnCode = 0;       // Assume success until we find otherwise
00412 
00413     // Make sure redirection happens
00414     Redirect();
00415 
00416     TRACEUSER("Phil", _T("Executing %s\n"), (LPCTSTR) cmd);
00417     m_pid = wxExecute(cmd, wxEXEC_ASYNC, this);
00418     if (m_pid==0)
00419     {
00420         // Couldn't even create a process for the command!
00421         m_bDead = true;
00422         return FALSE;
00423     }
00424 
00425     // We're now running
00426     m_bDead = false;
00427     m_bConnected = true;
00428 
00429     // Give the command 100 milliseconds to return an error condition or die...
00430     // After that we will either use the return code it passed back to OnTerminate
00431     // or assume it is running successfully...
00432     MonotonicTime graceperiod;
00433     while (!m_bDead && m_ReturnCode==0 && !graceperiod.Elapsed(100))
00434     {
00435         ProcessStdErr();        // Process any output on stderr
00436         wxMilliSleep(1);
00437         wxYield();
00438     }
00439 
00440     TRACEUSER("Phil", _T("Exiting with %d\n"), m_ReturnCode);
00441     return (m_ReturnCode==0);
00442 }

void CamLaunchProcess::OnTerminate int  pid,
int  status
[virtual]
 

Process anything the process writes to the error stream.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
19/May/2006
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
-

Definition at line 534 of file camprocess.cpp.

00534                                                : Correct*/ pid, int /*TYPENOTE: Correct*/ status)
00535 {
00536     ProcessStdErr();
00537 
00538     TRACEUSER("Phil", _T("CamProcess::OnTerminate pid = %d  status = %d\n"), pid, status);
00539     m_bDead = true;
00540     m_ReturnCode = status;
00541     m_bConnected = false;
00542 }

void CamLaunchProcess::ProcessStdErr  )  [protected, virtual]
 

Process anything the process writes to the error stream.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
Date:
19/May/2006
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
-

Definition at line 503 of file camprocess.cpp.

00504 {
00505     if (IsErrorAvailable())
00506     {
00507         wxTextInputStream tis(*GetErrorStream());
00508 
00509         // This assumes that the output is always line buffered
00510         while (!GetErrorStream()->Eof())
00511         {
00512             wxString line;
00513             line << tis.ReadLine();
00514             TRACEUSER("Phil", _T("(stderr):%s\n"), line.c_str());
00515         }
00516 
00517         m_ReturnCode = 42;      // Signal failure
00518     }
00519 }

wxKillError CamLaunchProcess::Terminate  ) 
 

Kill the process.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
19/May/2006
Parameters:
- [INPUTS]
- [OUTPUTS]
Returns:
wxKillError enum giving result of termination

Definition at line 481 of file camprocess.cpp.

00482 {
00483     if (!m_bDead)
00484         return Kill(m_pid, wxSIGTERM);
00485 
00486     return wxKILL_OK;
00487 }


Member Data Documentation

bool CamLaunchProcess::m_bConnected [protected]
 

Definition at line 167 of file camprocess.h.

bool CamLaunchProcess::m_bDead [protected]
 

Definition at line 166 of file camprocess.h.

INT32 CamLaunchProcess::m_pid [protected]
 

Definition at line 169 of file camprocess.h.

INT32 CamLaunchProcess::m_ReturnCode [protected]
 

Definition at line 168 of file camprocess.h.


The documentation for this class was generated from the following files:
Generated on Sat Nov 10 03:51:49 2007 for Camelot by  doxygen 1.4.4