home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / VDLib / source / Job.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  4.2 KB  |  194 lines

  1. #include "stdafx.h"
  2. #include <vd2/system/thread.h>
  3. #include <vd2/VDLib/Job.h>
  4.  
  5. VDJob::VDJob()
  6.     : mpJobQueue(NULL)
  7.     , mState(VDJob::kStateWaiting)
  8.     , mId(0)
  9.     , mRunnerId(0)
  10.     , mDateStart(0)
  11.     , mDateEnd(0)
  12.     , mbContainsReloadMarker(false)
  13. {
  14. }
  15.  
  16. VDJob::~VDJob() {
  17. }
  18.  
  19. bool VDJob::operator==(const VDJob& job) const {
  20. #define TEST(field) if (field != job.field) return false
  21.     TEST(mCreationRevision);
  22.     TEST(mChangeRevision);
  23.     TEST(mId);
  24.     TEST(mDateStart);
  25.     TEST(mDateEnd);
  26.     TEST(mRunnerId);
  27.     TEST(mRunnerName);
  28.     TEST(mName);
  29.     TEST(mInputFile);
  30.     TEST(mOutputFile);
  31.     TEST(mError);
  32.     TEST(mScript);
  33.     TEST(mState);
  34.     TEST(mRunnerId);
  35.     TEST(mbContainsReloadMarker);
  36. #undef TEST
  37.  
  38.     return true;
  39. }
  40.  
  41. void VDJob::SetInputFile(const wchar_t *file) {
  42.     mInputFile = VDTextWToA(file);
  43. }
  44.  
  45. void VDJob::SetOutputFile(const wchar_t *file) {
  46.     mOutputFile = VDTextWToA(file);
  47. }
  48.  
  49. void VDJob::SetState(int state) {
  50.     mState = state;
  51.     mbModified = true;
  52.  
  53.     switch(state) {
  54.     case kStateStarting:
  55.     case kStateInProgress:
  56.     case kStateAborted:
  57.     case kStateAborting:
  58.     case kStateCompleted:
  59.     case kStateError:
  60.         break;
  61.     default:
  62.         mRunnerName.clear();
  63.         mRunnerId = 0;
  64.         break;
  65.     }
  66. }
  67.  
  68. void VDJob::SetRunner(uint64 id, const char *name) {
  69.     mRunnerId = id;
  70.     mRunnerName = name;
  71. }
  72.  
  73. void VDJob::SetScript(const void *script, uint32 len, bool reloadable) {
  74.     mScript.assign((const char *)script, (const char *)script + len);
  75.     mbContainsReloadMarker = reloadable;
  76. }
  77.  
  78. void VDJob::Refresh() {
  79.     if (mpJobQueue)
  80.         mpJobQueue->Refresh(this);
  81. }
  82.  
  83. void VDJob::Run() {
  84.     if (mpJobQueue)
  85.         mpJobQueue->Run(this);
  86. }
  87.  
  88. void VDJob::Reload() {
  89.     if (mpJobQueue)
  90.         mpJobQueue->Reload(this);
  91. }
  92.  
  93. bool VDJob::Merge(const VDJob& src) {
  94.     const int rev1 = mChangeRevision;
  95.     const int rev2 = src.mChangeRevision;
  96.     const bool mod1 = mbModified;
  97.     const bool mod2 = src.mChangeRevision > mChangeRevision;
  98.  
  99.     if (mChangeRevision < src.mChangeRevision)
  100.         mChangeRevision = src.mChangeRevision;
  101.  
  102.     if (operator==(src))
  103.         return false;
  104.  
  105.     // Okay, the jobs aren't the same. Which ones are modified?
  106.     bool acceptTheirs = false;
  107.  
  108.     do {
  109.         // only yours modified -- accept yours
  110.         if (mod1 && !mod2) {
  111.             acceptTheirs = false;
  112.             break;
  113.         }
  114.  
  115.         // only theirs modified -- accept theirs
  116.         if (!mod1 && mod2) {
  117.             acceptTheirs = true;
  118.             break;
  119.         }
  120.  
  121.         // Two cases left:
  122.         //    - neither modified, but mismatch (bad, but should recover)
  123.         //    - both modified, but mismatch (the dreaded three-way merge)
  124.  
  125.         // We resolve with the following priority:
  126.         //
  127.         //    Starting < Aborting < Aborted < Error < Waiting < Postponed < Completed < In Progress
  128.         //
  129.         // Ties are broken via Accept Theirs. This is critical for In Progress mode.
  130.  
  131.         static const int kPriority[]={
  132.             4,
  133.             7,
  134.             6,
  135.             5,
  136.             2,
  137.             3,
  138.             1,
  139.             0
  140.         };
  141.  
  142.         VDASSERTCT(sizeof(kPriority)/sizeof(kPriority[0]) == VDJob::kStateCount);
  143.  
  144.         int pri1 = kPriority[mState];
  145.         int pri2 = kPriority[src.mState];
  146.  
  147.         acceptTheirs = (pri1 <= pri2);
  148.  
  149.     } while(false);
  150.  
  151.     VDDEBUG("Yours[%3d%c]:  %s\n", rev1, mod1 ? '*' : ' ', ToString().c_str());
  152.     VDDEBUG("Theirs[%3d%c]: %s\n", rev2, mod2 ? '*' : ' ', src.ToString().c_str());
  153.  
  154.     // We should never accept a local copy when it corresponds to an In Progress from a
  155.     // different machine.
  156.     VDASSERT(acceptTheirs || mState != kStateInProgress || (uint32)mRunnerId == VDGetCurrentProcessId());
  157.  
  158.     if (acceptTheirs) {
  159.         mName        = src.mName;
  160.         mError        = src.mError;
  161.         mRunnerName    = src.mRunnerName;
  162.         mState        = src.mState;
  163.         mRunnerId    = src.mRunnerId;
  164.         mDateStart    = src.mDateStart;
  165.         mDateEnd    = src.mDateEnd;
  166.  
  167.         VDDEBUG("  Resolving accept theirs.\n");
  168.         return true;
  169.     }
  170.  
  171.     VDDEBUG("  Resolving accept yours.\n");
  172.     return false;
  173. }
  174.  
  175. VDStringA VDJob::ToString() const {
  176.     VDStringA s;
  177.  
  178.     static const char *const kStateNames[]={
  179.         "Waiting",
  180.         "In progress",
  181.         "Completed",
  182.         "Postponed",
  183.         "Aborted",
  184.         "Error",
  185.         "Aborting",
  186.         "Starting",
  187.     };
  188.  
  189.     VDASSERTCT(sizeof(kStateNames) / sizeof(kStateNames[0]) == kStateCount);
  190.  
  191.     s.sprintf("%s | %s | %-11s (%s:%d)", mInputFile.c_str(), mOutputFile.c_str(), kStateNames[mState], mRunnerName.c_str(), (uint32)mRunnerId);
  192.     return s;
  193. }
  194.