Queue.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "Terminal.h"
  6. #include "Queue.h"
  7. #include "Exceptions.h"
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. //---------------------------------------------------------------------------
  11. class TBackgroundTerminal;
  12. //---------------------------------------------------------------------------
  13. class TUserAction
  14. {
  15. public:
  16. virtual __fastcall ~TUserAction() {}
  17. virtual void __fastcall Execute(TTerminalQueue * Queue, void * Arg) = 0;
  18. };
  19. //---------------------------------------------------------------------------
  20. class TQueryUserAction : public TUserAction
  21. {
  22. public:
  23. virtual void __fastcall Execute(TTerminalQueue * Queue, void * Arg)
  24. {
  25. Queue->DoQueryUser(Sender, Query, MoreMessages, Answers, Params, Answer, Type, Arg);
  26. }
  27. TObject * Sender;
  28. AnsiString Query;
  29. TStrings * MoreMessages;
  30. int Answers;
  31. const TQueryParams * Params;
  32. int Answer;
  33. TQueryType Type;
  34. };
  35. //---------------------------------------------------------------------------
  36. class TPromptUserAction : public TUserAction
  37. {
  38. public:
  39. __fastcall TPromptUserAction() :
  40. Results(new TStringList())
  41. {
  42. }
  43. virtual __fastcall ~TPromptUserAction()
  44. {
  45. delete Results;
  46. }
  47. virtual void __fastcall Execute(TTerminalQueue * Queue, void * Arg)
  48. {
  49. Queue->DoPromptUser(Terminal, Kind, Name, Instructions, Prompts, Results, Result, Arg);
  50. }
  51. TTerminal * Terminal;
  52. TPromptKind Kind;
  53. AnsiString Name;
  54. AnsiString Instructions;
  55. TStrings * Prompts;
  56. TStrings * Results;
  57. bool Result;
  58. };
  59. //---------------------------------------------------------------------------
  60. class TShowExtendedExceptionAction : public TUserAction
  61. {
  62. public:
  63. virtual void __fastcall Execute(TTerminalQueue * Queue, void * Arg)
  64. {
  65. Queue->DoShowExtendedException(Terminal, E, Arg);
  66. }
  67. TTerminal * Terminal;
  68. Exception * E;
  69. };
  70. //---------------------------------------------------------------------------
  71. class TTerminalItem : public TSignalThread
  72. {
  73. friend class TQueueItem;
  74. friend class TBackgroundTerminal;
  75. public:
  76. __fastcall TTerminalItem(TTerminalQueue * Queue, int Index);
  77. __fastcall ~TTerminalItem();
  78. void __fastcall Process(TQueueItem * Item);
  79. bool __fastcall ProcessUserAction(void * Arg);
  80. void __fastcall Cancel();
  81. void __fastcall Idle();
  82. bool __fastcall Pause();
  83. bool __fastcall Resume();
  84. protected:
  85. TTerminalQueue * FQueue;
  86. TBackgroundTerminal * FTerminal;
  87. TQueueItem * FItem;
  88. TCriticalSection * FCriticalSection;
  89. TUserAction * FUserAction;
  90. bool FCancel;
  91. bool FPause;
  92. virtual void __fastcall ProcessEvent();
  93. virtual void __fastcall Finished();
  94. bool __fastcall WaitForUserAction(TQueueItem::TStatus ItemStatus, TUserAction * UserAction);
  95. bool __fastcall OverrideItemStatus(TQueueItem::TStatus & ItemStatus);
  96. void __fastcall TerminalQueryUser(TObject * Sender,
  97. const AnsiString Query, TStrings * MoreMessages, int Answers,
  98. const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg);
  99. void __fastcall TerminalPromptUser(TTerminal * Terminal, TPromptKind Kind,
  100. AnsiString Name, AnsiString Instructions,
  101. TStrings * Prompts, TStrings * Results, bool & Result, void * Arg);
  102. void __fastcall TerminalShowExtendedException(TTerminal * Terminal,
  103. Exception * E, void * Arg);
  104. void __fastcall OperationFinished(TFileOperation Operation, TOperationSide Side,
  105. bool Temp, const AnsiString & FileName, bool Success,
  106. TOnceDoneOperation & OnceDoneOperation);
  107. void __fastcall OperationProgress(TFileOperationProgressType & ProgressData,
  108. TCancelStatus & Cancel);
  109. };
  110. //---------------------------------------------------------------------------
  111. // TSignalThread
  112. //---------------------------------------------------------------------------
  113. int __fastcall TSimpleThread::ThreadProc(void * Thread)
  114. {
  115. TSimpleThread * SimpleThread = reinterpret_cast<TSimpleThread*>(Thread);
  116. assert(SimpleThread != NULL);
  117. try
  118. {
  119. SimpleThread->Execute();
  120. }
  121. catch(...)
  122. {
  123. // we do not expect thread to be terminated with exception
  124. assert(false);
  125. }
  126. SimpleThread->FFinished = true;
  127. SimpleThread->Finished();
  128. return 0;
  129. }
  130. //---------------------------------------------------------------------------
  131. __fastcall TSimpleThread::TSimpleThread() :
  132. FThread(NULL), FFinished(true)
  133. {
  134. unsigned ThreadID;
  135. FThread = reinterpret_cast<HANDLE>(
  136. StartThread(NULL, 0, ThreadProc, this, CREATE_SUSPENDED, ThreadID));
  137. }
  138. //---------------------------------------------------------------------------
  139. __fastcall TSimpleThread::~TSimpleThread()
  140. {
  141. Close();
  142. if (FThread != NULL)
  143. {
  144. CloseHandle(FThread);
  145. }
  146. }
  147. //---------------------------------------------------------------------------
  148. bool __fastcall TSimpleThread::IsFinished()
  149. {
  150. return FFinished;
  151. }
  152. //---------------------------------------------------------------------------
  153. void __fastcall TSimpleThread::Start()
  154. {
  155. if (ResumeThread(FThread) == 1)
  156. {
  157. FFinished = false;
  158. }
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TSimpleThread::Finished()
  162. {
  163. }
  164. //---------------------------------------------------------------------------
  165. void __fastcall TSimpleThread::Close()
  166. {
  167. if (!FFinished)
  168. {
  169. Terminate();
  170. WaitFor();
  171. }
  172. }
  173. //---------------------------------------------------------------------------
  174. void __fastcall TSimpleThread::WaitFor(unsigned int Milliseconds)
  175. {
  176. WaitForSingleObject(FThread, Milliseconds);
  177. }
  178. //---------------------------------------------------------------------------
  179. // TSignalThread
  180. //---------------------------------------------------------------------------
  181. __fastcall TSignalThread::TSignalThread() :
  182. TSimpleThread(),
  183. FTerminated(true), FEvent(NULL)
  184. {
  185. FEvent = CreateEvent(NULL, false, false, NULL);
  186. assert(FEvent != NULL);
  187. ::SetThreadPriority(FThread, THREAD_PRIORITY_BELOW_NORMAL);
  188. }
  189. //---------------------------------------------------------------------------
  190. __fastcall TSignalThread::~TSignalThread()
  191. {
  192. // cannot leave closing to TSimpleThread as we need to close it before
  193. // destroying the event
  194. Close();
  195. if (FEvent)
  196. {
  197. CloseHandle(FEvent);
  198. }
  199. }
  200. //---------------------------------------------------------------------------
  201. void __fastcall TSignalThread::Start()
  202. {
  203. FTerminated = false;
  204. TSimpleThread::Start();
  205. }
  206. //---------------------------------------------------------------------------
  207. void __fastcall TSignalThread::TriggerEvent()
  208. {
  209. SetEvent(FEvent);
  210. }
  211. //---------------------------------------------------------------------------
  212. bool __fastcall TSignalThread::WaitForEvent()
  213. {
  214. return (WaitForSingleObject(FEvent, INFINITE) == WAIT_OBJECT_0) &&
  215. !FTerminated;
  216. }
  217. //---------------------------------------------------------------------------
  218. void __fastcall TSignalThread::Execute()
  219. {
  220. while (!FTerminated)
  221. {
  222. if (WaitForEvent())
  223. {
  224. ProcessEvent();
  225. }
  226. }
  227. }
  228. //---------------------------------------------------------------------------
  229. void __fastcall TSignalThread::Terminate()
  230. {
  231. FTerminated = true;
  232. TriggerEvent();
  233. }
  234. //---------------------------------------------------------------------------
  235. // TTerminalQueue
  236. //---------------------------------------------------------------------------
  237. __fastcall TTerminalQueue::TTerminalQueue(TTerminal * Terminal,
  238. TConfiguration * Configuration) :
  239. FTerminal(Terminal), FTransfersLimit(2),
  240. FConfiguration(Configuration), FSessionData(NULL), FItems(NULL),
  241. FTerminals(NULL), FItemsSection(NULL), FFreeTerminals(0),
  242. FItemsInProcess(0), FTemporaryTerminals(0), FOverallTerminals(0)
  243. {
  244. FOnQueryUser = NULL;
  245. FOnPromptUser = NULL;
  246. FOnShowExtendedException = NULL;
  247. FOnQueueItemUpdate = NULL;
  248. FOnListUpdate = NULL;
  249. FOnEvent = NULL;
  250. FLastIdle = Now();
  251. FIdleInterval = EncodeTime(0, 0, 2, 0);
  252. assert(Terminal != NULL);
  253. FSessionData = new TSessionData("");
  254. FSessionData->Assign(Terminal->SessionData);
  255. FItems = new TList();
  256. FTerminals = new TList();
  257. FItemsSection = new TCriticalSection();
  258. Start();
  259. }
  260. //---------------------------------------------------------------------------
  261. __fastcall TTerminalQueue::~TTerminalQueue()
  262. {
  263. Close();
  264. {
  265. TGuard Guard(FItemsSection);
  266. TTerminalItem * TerminalItem;
  267. while (FTerminals->Count > 0)
  268. {
  269. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
  270. FTerminals->Delete(0);
  271. TerminalItem->Terminate();
  272. TerminalItem->WaitFor();
  273. delete TerminalItem;
  274. }
  275. delete FTerminals;
  276. for (int Index = 0; Index < FItems->Count; Index++)
  277. {
  278. delete GetItem(Index);
  279. }
  280. delete FItems;
  281. }
  282. delete FItemsSection;
  283. delete FSessionData;
  284. }
  285. //---------------------------------------------------------------------------
  286. void __fastcall TTerminalQueue::TerminalFinished(TTerminalItem * TerminalItem)
  287. {
  288. if (!FTerminated)
  289. {
  290. {
  291. TGuard Guard(FItemsSection);
  292. int Index = FTerminals->IndexOf(TerminalItem);
  293. assert(Index >= 0);
  294. if (Index < FFreeTerminals)
  295. {
  296. FFreeTerminals--;
  297. }
  298. // Index may be >= FTransfersLimit also when the transfer limit was
  299. // recently decresed, then
  300. // FTemporaryTerminals < FTerminals->Count - FTransfersLimit
  301. if ((FTransfersLimit > 0) && (Index >= FTransfersLimit) && (FTemporaryTerminals > 0))
  302. {
  303. FTemporaryTerminals--;
  304. }
  305. FTerminals->Extract(TerminalItem);
  306. delete TerminalItem;
  307. }
  308. TriggerEvent();
  309. }
  310. }
  311. //---------------------------------------------------------------------------
  312. bool __fastcall TTerminalQueue::TerminalFree(TTerminalItem * TerminalItem)
  313. {
  314. bool Result = true;
  315. if (!FTerminated)
  316. {
  317. {
  318. TGuard Guard(FItemsSection);
  319. int Index = FTerminals->IndexOf(TerminalItem);
  320. assert(Index >= 0);
  321. assert(Index >= FFreeTerminals);
  322. Result = (FTransfersLimit <= 0) || (Index < FTransfersLimit);
  323. if (Result)
  324. {
  325. FTerminals->Move(Index, 0);
  326. FFreeTerminals++;
  327. }
  328. }
  329. TriggerEvent();
  330. }
  331. return Result;
  332. }
  333. //---------------------------------------------------------------------------
  334. void __fastcall TTerminalQueue::AddItem(TQueueItem * Item)
  335. {
  336. assert(!FTerminated);
  337. Item->SetStatus(TQueueItem::qsPending);
  338. {
  339. TGuard Guard(FItemsSection);
  340. FItems->Add(Item);
  341. Item->FQueue = this;
  342. }
  343. DoListUpdate();
  344. TriggerEvent();
  345. }
  346. //---------------------------------------------------------------------------
  347. void __fastcall TTerminalQueue::RetryItem(TQueueItem * Item)
  348. {
  349. if (!FTerminated)
  350. {
  351. {
  352. TGuard Guard(FItemsSection);
  353. int Index = FItems->Remove(Item);
  354. assert(Index < FItemsInProcess);
  355. USEDPARAM(Index);
  356. FItemsInProcess--;
  357. FItems->Add(Item);
  358. }
  359. DoListUpdate();
  360. TriggerEvent();
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. void __fastcall TTerminalQueue::DeleteItem(TQueueItem * Item)
  365. {
  366. if (!FTerminated)
  367. {
  368. bool Empty;
  369. bool Monitored;
  370. {
  371. TGuard Guard(FItemsSection);
  372. // does this need to be within guard?
  373. Monitored = (Item->CompleteEvent != INVALID_HANDLE_VALUE);
  374. int Index = FItems->Remove(Item);
  375. assert(Index < FItemsInProcess);
  376. USEDPARAM(Index);
  377. FItemsInProcess--;
  378. delete Item;
  379. Empty = true;
  380. Index = 0;
  381. while (Empty && (Index < FItems->Count))
  382. {
  383. Empty = (GetItem(Index) != INVALID_HANDLE_VALUE);
  384. Index++;
  385. }
  386. }
  387. DoListUpdate();
  388. // report empty, if queue is empty or only monitored items are pending.
  389. // do not report if current item was the last, but was monitored.
  390. if (!Monitored && Empty)
  391. {
  392. DoEvent(qeEmpty);
  393. }
  394. }
  395. }
  396. //---------------------------------------------------------------------------
  397. TQueueItem * __fastcall TTerminalQueue::GetItem(int Index)
  398. {
  399. return reinterpret_cast<TQueueItem*>(FItems->Items[Index]);
  400. }
  401. //---------------------------------------------------------------------------
  402. TTerminalQueueStatus * __fastcall TTerminalQueue::CreateStatus(TTerminalQueueStatus * Current)
  403. {
  404. TTerminalQueueStatus * Status = new TTerminalQueueStatus();
  405. try
  406. {
  407. try
  408. {
  409. TGuard Guard(FItemsSection);
  410. TQueueItem * Item;
  411. TQueueItemProxy * ItemProxy;
  412. for (int Index = 0; Index < FItems->Count; Index++)
  413. {
  414. Item = GetItem(Index);
  415. if (Current != NULL)
  416. {
  417. ItemProxy = Current->FindByQueueItem(Item);
  418. }
  419. else
  420. {
  421. ItemProxy = NULL;
  422. }
  423. if (ItemProxy != NULL)
  424. {
  425. Current->Delete(ItemProxy);
  426. Status->Add(ItemProxy);
  427. ItemProxy->Update();
  428. }
  429. else
  430. {
  431. Status->Add(new TQueueItemProxy(this, Item));
  432. }
  433. }
  434. }
  435. __finally
  436. {
  437. if (Current != NULL)
  438. {
  439. delete Current;
  440. }
  441. }
  442. }
  443. catch(...)
  444. {
  445. delete Status;
  446. throw;
  447. }
  448. return Status;
  449. }
  450. //---------------------------------------------------------------------------
  451. bool __fastcall TTerminalQueue::ItemGetData(TQueueItem * Item,
  452. TQueueItemProxy * Proxy)
  453. {
  454. // to prevent deadlocks when closing queue from other thread
  455. bool Result = !FFinished;
  456. if (Result)
  457. {
  458. TGuard Guard(FItemsSection);
  459. Result = (FItems->IndexOf(Item) >= 0);
  460. if (Result)
  461. {
  462. Item->GetData(Proxy);
  463. }
  464. }
  465. return Result;
  466. }
  467. //---------------------------------------------------------------------------
  468. bool __fastcall TTerminalQueue::ItemProcessUserAction(TQueueItem * Item, void * Arg)
  469. {
  470. // to prevent deadlocks when closing queue from other thread
  471. bool Result = !FFinished;
  472. if (Result)
  473. {
  474. TTerminalItem * TerminalItem;
  475. {
  476. TGuard Guard(FItemsSection);
  477. Result = (FItems->IndexOf(Item) >= 0) &&
  478. TQueueItem::IsUserActionStatus(Item->Status);
  479. if (Result)
  480. {
  481. TerminalItem = Item->FTerminalItem;
  482. }
  483. }
  484. if (Result)
  485. {
  486. Result = TerminalItem->ProcessUserAction(Arg);
  487. }
  488. }
  489. return Result;
  490. }
  491. //---------------------------------------------------------------------------
  492. bool __fastcall TTerminalQueue::ItemMove(TQueueItem * Item, TQueueItem * BeforeItem)
  493. {
  494. // to prevent deadlocks when closing queue from other thread
  495. bool Result = !FFinished;
  496. if (Result)
  497. {
  498. {
  499. TGuard Guard(FItemsSection);
  500. int Index = FItems->IndexOf(Item);
  501. int IndexDest = FItems->IndexOf(BeforeItem);
  502. Result = (Index >= 0) && (IndexDest >= 0) &&
  503. (Item->GetStatus() == TQueueItem::qsPending) &&
  504. (BeforeItem->GetStatus() == TQueueItem::qsPending);
  505. if (Result)
  506. {
  507. FItems->Move(Index, IndexDest);
  508. }
  509. }
  510. if (Result)
  511. {
  512. DoListUpdate();
  513. TriggerEvent();
  514. }
  515. }
  516. return Result;
  517. }
  518. //---------------------------------------------------------------------------
  519. bool __fastcall TTerminalQueue::ItemExecuteNow(TQueueItem * Item)
  520. {
  521. // to prevent deadlocks when closing queue from other thread
  522. bool Result = !FFinished;
  523. if (Result)
  524. {
  525. {
  526. TGuard Guard(FItemsSection);
  527. int Index = FItems->IndexOf(Item);
  528. Result = (Index >= 0) && (Item->GetStatus() == TQueueItem::qsPending) &&
  529. // prevent double-initiation when "execute" is clicked twice too fast
  530. (Index >= FItemsInProcess);
  531. if (Result)
  532. {
  533. if (Index > FItemsInProcess)
  534. {
  535. FItems->Move(Index, FItemsInProcess);
  536. }
  537. if ((FTransfersLimit > 0) && (FTerminals->Count >= FTransfersLimit))
  538. {
  539. FTemporaryTerminals++;
  540. }
  541. }
  542. }
  543. if (Result)
  544. {
  545. DoListUpdate();
  546. TriggerEvent();
  547. }
  548. }
  549. return Result;
  550. }
  551. //---------------------------------------------------------------------------
  552. bool __fastcall TTerminalQueue::ItemDelete(TQueueItem * Item)
  553. {
  554. // to prevent deadlocks when closing queue from other thread
  555. bool Result = !FFinished;
  556. if (Result)
  557. {
  558. bool UpdateList = false;
  559. {
  560. TGuard Guard(FItemsSection);
  561. int Index = FItems->IndexOf(Item);
  562. Result = (Index >= 0);
  563. if (Result)
  564. {
  565. if (Item->Status == TQueueItem::qsPending)
  566. {
  567. FItems->Delete(Index);
  568. UpdateList = true;
  569. }
  570. else
  571. {
  572. Item->FTerminalItem->Cancel();
  573. }
  574. }
  575. }
  576. if (UpdateList)
  577. {
  578. DoListUpdate();
  579. TriggerEvent();
  580. }
  581. }
  582. return Result;
  583. }
  584. //---------------------------------------------------------------------------
  585. bool __fastcall TTerminalQueue::ItemPause(TQueueItem * Item, bool Pause)
  586. {
  587. // to prevent deadlocks when closing queue from other thread
  588. bool Result = !FFinished;
  589. if (Result)
  590. {
  591. TTerminalItem * TerminalItem;
  592. {
  593. TGuard Guard(FItemsSection);
  594. Result = (FItems->IndexOf(Item) >= 0) &&
  595. ((Pause && (Item->Status == TQueueItem::qsProcessing)) ||
  596. (!Pause && (Item->Status == TQueueItem::qsPaused)));
  597. if (Result)
  598. {
  599. TerminalItem = Item->FTerminalItem;
  600. }
  601. }
  602. if (Result)
  603. {
  604. if (Pause)
  605. {
  606. Result = TerminalItem->Pause();
  607. }
  608. else
  609. {
  610. Result = TerminalItem->Resume();
  611. }
  612. }
  613. }
  614. return Result;
  615. }
  616. //---------------------------------------------------------------------------
  617. bool __fastcall TTerminalQueue::ItemSetCPSLimit(TQueueItem * Item, unsigned long CPSLimit)
  618. {
  619. // to prevent deadlocks when closing queue from other thread
  620. bool Result = !FFinished;
  621. if (Result)
  622. {
  623. TGuard Guard(FItemsSection);
  624. Result = (FItems->IndexOf(Item) >= 0);
  625. if (Result)
  626. {
  627. Item->SetCPSLimit(CPSLimit);
  628. }
  629. }
  630. return Result;
  631. }
  632. //---------------------------------------------------------------------------
  633. void __fastcall TTerminalQueue::Idle()
  634. {
  635. if (Now() - FLastIdle > FIdleInterval)
  636. {
  637. FLastIdle = FIdleInterval;
  638. TTerminalItem * TerminalItem = NULL;
  639. if (FFreeTerminals > 0)
  640. {
  641. TGuard Guard(FItemsSection);
  642. if (FFreeTerminals > 0)
  643. {
  644. // take the last free terminal, because TerminalFree() puts it to the
  645. // front, this ensures we cycle thru all free terminals
  646. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[FFreeTerminals - 1]);
  647. FTerminals->Move(FFreeTerminals - 1, FTerminals->Count - 1);
  648. FFreeTerminals--;
  649. }
  650. }
  651. if (TerminalItem != NULL)
  652. {
  653. TerminalItem->Idle();
  654. }
  655. }
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TTerminalQueue::ProcessEvent()
  659. {
  660. TTerminalItem * TerminalItem;
  661. TQueueItem * Item;
  662. do
  663. {
  664. TerminalItem = NULL;
  665. Item = NULL;
  666. if (FItems->Count > FItemsInProcess)
  667. {
  668. TGuard Guard(FItemsSection);
  669. if ((FFreeTerminals == 0) &&
  670. ((FTransfersLimit <= 0) ||
  671. (FTerminals->Count < FTransfersLimit + FTemporaryTerminals)))
  672. {
  673. FOverallTerminals++;
  674. TerminalItem = new TTerminalItem(this, FOverallTerminals);
  675. FTerminals->Add(TerminalItem);
  676. }
  677. else if (FFreeTerminals > 0)
  678. {
  679. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
  680. FTerminals->Move(0, FTerminals->Count - 1);
  681. FFreeTerminals--;
  682. }
  683. if (TerminalItem != NULL)
  684. {
  685. Item = GetItem(FItemsInProcess);
  686. FItemsInProcess++;
  687. }
  688. }
  689. if (TerminalItem != NULL)
  690. {
  691. TerminalItem->Process(Item);
  692. }
  693. }
  694. while (!FTerminated && (TerminalItem != NULL));
  695. }
  696. //---------------------------------------------------------------------------
  697. void __fastcall TTerminalQueue::DoQueueItemUpdate(TQueueItem * Item)
  698. {
  699. if (OnQueueItemUpdate != NULL)
  700. {
  701. OnQueueItemUpdate(this, Item);
  702. }
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TTerminalQueue::DoListUpdate()
  706. {
  707. if (OnListUpdate != NULL)
  708. {
  709. OnListUpdate(this);
  710. }
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TTerminalQueue::DoEvent(TQueueEvent Event)
  714. {
  715. if (OnEvent != NULL)
  716. {
  717. OnEvent(this, Event);
  718. }
  719. }
  720. //---------------------------------------------------------------------------
  721. void __fastcall TTerminalQueue::DoQueryUser(TObject * Sender,
  722. const AnsiString Query, TStrings * MoreMessages, int Answers,
  723. const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg)
  724. {
  725. if (OnQueryUser != NULL)
  726. {
  727. OnQueryUser(Sender, Query, MoreMessages, Answers, Params, Answer, Type, Arg);
  728. }
  729. }
  730. //---------------------------------------------------------------------------
  731. void __fastcall TTerminalQueue::DoPromptUser(TTerminal * Terminal,
  732. TPromptKind Kind, AnsiString Name, AnsiString Instructions,
  733. TStrings * Prompts, TStrings * Results, bool & Result, void * Arg)
  734. {
  735. if (OnPromptUser != NULL)
  736. {
  737. OnPromptUser(Terminal, Kind, Name, Instructions, Prompts, Results, Result, Arg);
  738. }
  739. }
  740. //---------------------------------------------------------------------------
  741. void __fastcall TTerminalQueue::DoShowExtendedException(
  742. TTerminal * Terminal, Exception * E, void * Arg)
  743. {
  744. if (OnShowExtendedException != NULL)
  745. {
  746. OnShowExtendedException(Terminal, E, Arg);
  747. }
  748. }
  749. //---------------------------------------------------------------------------
  750. void __fastcall TTerminalQueue::SetTransfersLimit(int value)
  751. {
  752. if (FTransfersLimit != value)
  753. {
  754. {
  755. TGuard Guard(FItemsSection);
  756. if ((value > 0) && (value < FItemsInProcess))
  757. {
  758. FTemporaryTerminals = (FItemsInProcess - value);
  759. }
  760. else
  761. {
  762. FTemporaryTerminals = 0;
  763. }
  764. FTransfersLimit = value;
  765. }
  766. TriggerEvent();
  767. }
  768. }
  769. //---------------------------------------------------------------------------
  770. bool __fastcall TTerminalQueue::GetIsEmpty()
  771. {
  772. TGuard Guard(FItemsSection);
  773. return (FItems->Count == 0);
  774. }
  775. //---------------------------------------------------------------------------
  776. // TBackgroundItem
  777. //---------------------------------------------------------------------------
  778. class TBackgroundTerminal : public TSecondaryTerminal
  779. {
  780. friend class TTerminalItem;
  781. public:
  782. __fastcall TBackgroundTerminal(TTerminal * MainTerminal,
  783. TSessionData * SessionData, TConfiguration * Configuration,
  784. TTerminalItem * Item, const AnsiString & Name);
  785. protected:
  786. virtual bool __fastcall DoQueryReopen(Exception * E);
  787. private:
  788. TTerminalItem * FItem;
  789. };
  790. //---------------------------------------------------------------------------
  791. __fastcall TBackgroundTerminal::TBackgroundTerminal(TTerminal * MainTerminal,
  792. TSessionData * SessionData, TConfiguration * Configuration, TTerminalItem * Item,
  793. const AnsiString & Name) :
  794. TSecondaryTerminal(MainTerminal, SessionData, Configuration, Name), FItem(Item)
  795. {
  796. }
  797. //---------------------------------------------------------------------------
  798. bool __fastcall TBackgroundTerminal::DoQueryReopen(Exception * /*E*/)
  799. {
  800. bool Result;
  801. if (FItem->FTerminated || FItem->FCancel)
  802. {
  803. // avoid reconnection if we are closing
  804. Result = false;
  805. }
  806. else
  807. {
  808. Sleep(Configuration->SessionReopenBackground);
  809. Result = true;
  810. }
  811. return Result;
  812. }
  813. //---------------------------------------------------------------------------
  814. // TTerminalItem
  815. //---------------------------------------------------------------------------
  816. __fastcall TTerminalItem::TTerminalItem(TTerminalQueue * Queue, int Index) :
  817. TSignalThread(), FQueue(Queue), FTerminal(NULL), FItem(NULL),
  818. FCriticalSection(NULL), FUserAction(NULL)
  819. {
  820. FCriticalSection = new TCriticalSection();
  821. FTerminal = new TBackgroundTerminal(FQueue->FTerminal, Queue->FSessionData,
  822. FQueue->FConfiguration, this, FORMAT("Background %d", (Index)));
  823. try
  824. {
  825. FTerminal->UseBusyCursor = false;
  826. FTerminal->OnQueryUser = TerminalQueryUser;
  827. FTerminal->OnPromptUser = TerminalPromptUser;
  828. FTerminal->OnShowExtendedException = TerminalShowExtendedException;
  829. FTerminal->OnProgress = OperationProgress;
  830. FTerminal->OnFinished = OperationFinished;
  831. }
  832. catch(...)
  833. {
  834. delete FTerminal;
  835. throw;
  836. }
  837. Start();
  838. }
  839. //---------------------------------------------------------------------------
  840. __fastcall TTerminalItem::~TTerminalItem()
  841. {
  842. Close();
  843. assert(FItem == NULL);
  844. delete FTerminal;
  845. delete FCriticalSection;
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TTerminalItem::Process(TQueueItem * Item)
  849. {
  850. {
  851. TGuard Guard(FCriticalSection);
  852. assert(FItem == NULL);
  853. FItem = Item;
  854. }
  855. TriggerEvent();
  856. }
  857. //---------------------------------------------------------------------------
  858. void __fastcall TTerminalItem::ProcessEvent()
  859. {
  860. TGuard Guard(FCriticalSection);
  861. bool Retry = true;
  862. FCancel = false;
  863. FPause = false;
  864. FItem->FTerminalItem = this;
  865. try
  866. {
  867. assert(FItem != NULL);
  868. if (!FTerminal->Active)
  869. {
  870. FItem->SetStatus(TQueueItem::qsConnecting);
  871. FTerminal->SessionData->RemoteDirectory = FItem->StartupDirectory();
  872. FTerminal->Open();
  873. }
  874. Retry = false;
  875. if (!FCancel)
  876. {
  877. FItem->SetStatus(TQueueItem::qsProcessing);
  878. FItem->Execute(this);
  879. }
  880. }
  881. catch(Exception & E)
  882. {
  883. // do not show error messages, if task was canceled anyway
  884. // (for example if transfer is cancelled during reconnection attempts)
  885. if (!FCancel &&
  886. (FTerminal->QueryUserException("", &E, qaOK | qaCancel, NULL, qtError) == qaCancel))
  887. {
  888. FCancel = true;
  889. }
  890. }
  891. FItem->SetStatus(TQueueItem::qsDone);
  892. FItem->FTerminalItem = NULL;
  893. TQueueItem * Item = FItem;
  894. FItem = NULL;
  895. if (Retry && !FCancel)
  896. {
  897. FQueue->RetryItem(Item);
  898. }
  899. else
  900. {
  901. FQueue->DeleteItem(Item);
  902. }
  903. if (!FTerminal->Active ||
  904. !FQueue->TerminalFree(this))
  905. {
  906. Terminate();
  907. }
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TTerminalItem::Idle()
  911. {
  912. TGuard Guard(FCriticalSection);
  913. assert(FTerminal->Active);
  914. try
  915. {
  916. FTerminal->Idle();
  917. }
  918. catch(...)
  919. {
  920. }
  921. if (!FTerminal->Active ||
  922. !FQueue->TerminalFree(this))
  923. {
  924. Terminate();
  925. }
  926. }
  927. //---------------------------------------------------------------------------
  928. void __fastcall TTerminalItem::Cancel()
  929. {
  930. FCancel = true;
  931. if ((FItem->GetStatus() == TQueueItem::qsPaused) ||
  932. TQueueItem::IsUserActionStatus(FItem->GetStatus()))
  933. {
  934. TriggerEvent();
  935. }
  936. }
  937. //---------------------------------------------------------------------------
  938. bool __fastcall TTerminalItem::Pause()
  939. {
  940. assert(FItem != NULL);
  941. bool Result = (FItem->GetStatus() == TQueueItem::qsProcessing) && !FPause;
  942. if (Result)
  943. {
  944. FPause = true;
  945. }
  946. return Result;
  947. }
  948. //---------------------------------------------------------------------------
  949. bool __fastcall TTerminalItem::Resume()
  950. {
  951. assert(FItem != NULL);
  952. bool Result = (FItem->GetStatus() == TQueueItem::qsPaused);
  953. if (Result)
  954. {
  955. TriggerEvent();
  956. }
  957. return Result;
  958. }
  959. //---------------------------------------------------------------------------
  960. bool __fastcall TTerminalItem::ProcessUserAction(void * Arg)
  961. {
  962. // When status is changed twice quickly, the controller when responding
  963. // to the first change (non-user-action) can be so slow to check only after
  964. // the second (user-action) change occurs. Thus it responds it.
  965. // Then as reaction to the second (user-action) change there will not be
  966. // any outstanding user-action.
  967. bool Result = (FUserAction != NULL);
  968. if (Result)
  969. {
  970. assert(FItem != NULL);
  971. FUserAction->Execute(FQueue, Arg);
  972. FUserAction = NULL;
  973. TriggerEvent();
  974. }
  975. return Result;
  976. }
  977. //---------------------------------------------------------------------------
  978. bool __fastcall TTerminalItem::WaitForUserAction(
  979. TQueueItem::TStatus ItemStatus, TUserAction * UserAction)
  980. {
  981. assert(FItem != NULL);
  982. assert((FItem->GetStatus() == TQueueItem::qsProcessing) ||
  983. (FItem->GetStatus() == TQueueItem::qsConnecting));
  984. bool Result;
  985. TQueueItem::TStatus PrevStatus = FItem->GetStatus();
  986. try
  987. {
  988. FUserAction = UserAction;
  989. FItem->SetStatus(ItemStatus);
  990. FQueue->DoEvent(qePendingUserAction);
  991. Result = !FTerminated && WaitForEvent() && !FCancel;
  992. }
  993. __finally
  994. {
  995. FUserAction = NULL;
  996. FItem->SetStatus(PrevStatus);
  997. }
  998. return Result;
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TTerminalItem::Finished()
  1002. {
  1003. TSignalThread::Finished();
  1004. FQueue->TerminalFinished(this);
  1005. }
  1006. //---------------------------------------------------------------------------
  1007. void __fastcall TTerminalItem::TerminalQueryUser(TObject * Sender,
  1008. const AnsiString Query, TStrings * MoreMessages, int Answers,
  1009. const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg)
  1010. {
  1011. // so far query without queue item can occur only for key cofirmation
  1012. // on re-key with non-cached host key. make it fail.
  1013. if (FItem != NULL)
  1014. {
  1015. USEDPARAM(Arg);
  1016. assert(Arg == NULL);
  1017. TQueryUserAction Action;
  1018. Action.Sender = Sender;
  1019. Action.Query = Query;
  1020. Action.MoreMessages = MoreMessages;
  1021. Action.Answers = Answers;
  1022. Action.Params = Params;
  1023. Action.Answer = Answer;
  1024. Action.Type = Type;
  1025. // if the query is "error", present it as an "error" state in UI,
  1026. // however it is still handled as query by the action.
  1027. TQueueItem::TStatus ItemStatus =
  1028. (Action.Type == qtError ? TQueueItem::qsError : TQueueItem::qsQuery);
  1029. if (WaitForUserAction(ItemStatus, &Action))
  1030. {
  1031. Answer = Action.Answer;
  1032. }
  1033. }
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. void __fastcall TTerminalItem::TerminalPromptUser(TTerminal * Terminal,
  1037. TPromptKind Kind, AnsiString Name, AnsiString Instructions, TStrings * Prompts,
  1038. TStrings * Results, bool & Result, void * Arg)
  1039. {
  1040. if (FItem == NULL)
  1041. {
  1042. // sanity, should not occur
  1043. assert(false);
  1044. Result = false;
  1045. }
  1046. else
  1047. {
  1048. USEDPARAM(Arg);
  1049. assert(Arg == NULL);
  1050. TPromptUserAction Action;
  1051. Action.Terminal = Terminal;
  1052. Action.Kind = Kind;
  1053. Action.Name = Name;
  1054. Action.Instructions = Instructions;
  1055. Action.Prompts = Prompts;
  1056. Action.Results->AddStrings(Results);
  1057. if (WaitForUserAction(TQueueItem::qsPrompt, &Action))
  1058. {
  1059. Results->Clear();
  1060. Results->AddStrings(Action.Results);
  1061. Result = Action.Result;
  1062. }
  1063. }
  1064. }
  1065. //---------------------------------------------------------------------------
  1066. void __fastcall TTerminalItem::TerminalShowExtendedException(
  1067. TTerminal * Terminal, Exception * E, void * Arg)
  1068. {
  1069. USEDPARAM(Arg);
  1070. assert(Arg == NULL);
  1071. AnsiString Message; // not used
  1072. if ((FItem != NULL) &&
  1073. ExceptionMessage(E, Message))
  1074. {
  1075. TShowExtendedExceptionAction Action;
  1076. Action.Terminal = Terminal;
  1077. Action.E = E;
  1078. WaitForUserAction(TQueueItem::qsError, &Action);
  1079. }
  1080. }
  1081. //---------------------------------------------------------------------------
  1082. void __fastcall TTerminalItem::OperationFinished(TFileOperation /*Operation*/,
  1083. TOperationSide /*Side*/, bool /*Temp*/, const AnsiString & /*FileName*/,
  1084. bool /*Success*/, TOnceDoneOperation & /*OnceDoneOperation*/)
  1085. {
  1086. // nothing
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. void __fastcall TTerminalItem::OperationProgress(
  1090. TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
  1091. {
  1092. if (FPause && !FTerminated && !FCancel)
  1093. {
  1094. TQueueItem::TStatus PrevStatus = FItem->GetStatus();
  1095. assert(PrevStatus == TQueueItem::qsProcessing);
  1096. // must be set before TFileOperationProgressType::Suspend(), because
  1097. // it invokes this method back
  1098. FPause = false;
  1099. ProgressData.Suspend();
  1100. try
  1101. {
  1102. FItem->SetStatus(TQueueItem::qsPaused);
  1103. WaitForEvent();
  1104. }
  1105. __finally
  1106. {
  1107. FItem->SetStatus(PrevStatus);
  1108. ProgressData.Resume();
  1109. }
  1110. }
  1111. if (FTerminated || FCancel)
  1112. {
  1113. if (ProgressData.TransferingFile)
  1114. {
  1115. Cancel = csCancelTransfer;
  1116. }
  1117. else
  1118. {
  1119. Cancel = csCancel;
  1120. }
  1121. }
  1122. assert(FItem != NULL);
  1123. FItem->SetProgress(ProgressData);
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. bool __fastcall TTerminalItem::OverrideItemStatus(TQueueItem::TStatus & ItemStatus)
  1127. {
  1128. assert(FTerminal != NULL);
  1129. bool Result = (FTerminal->Status < ssOpened) && (ItemStatus == TQueueItem::qsProcessing);
  1130. if (Result)
  1131. {
  1132. ItemStatus = TQueueItem::qsConnecting;
  1133. }
  1134. return Result;
  1135. }
  1136. //---------------------------------------------------------------------------
  1137. // TQueueItem
  1138. //---------------------------------------------------------------------------
  1139. __fastcall TQueueItem::TQueueItem() :
  1140. FStatus(qsPending), FTerminalItem(NULL), FSection(NULL), FProgressData(NULL),
  1141. FQueue(NULL), FInfo(NULL), FCompleteEvent(INVALID_HANDLE_VALUE),
  1142. FCPSLimit(-1)
  1143. {
  1144. FSection = new TCriticalSection();
  1145. FInfo = new TInfo();
  1146. }
  1147. //---------------------------------------------------------------------------
  1148. __fastcall TQueueItem::~TQueueItem()
  1149. {
  1150. if (FCompleteEvent != INVALID_HANDLE_VALUE)
  1151. {
  1152. SetEvent(FCompleteEvent);
  1153. }
  1154. delete FSection;
  1155. delete FInfo;
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. bool __fastcall TQueueItem::IsUserActionStatus(TStatus Status)
  1159. {
  1160. return (Status == qsQuery) || (Status == qsError) || (Status == qsPrompt);
  1161. }
  1162. //---------------------------------------------------------------------------
  1163. TQueueItem::TStatus __fastcall TQueueItem::GetStatus()
  1164. {
  1165. TGuard Guard(FSection);
  1166. return FStatus;
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TQueueItem::SetStatus(TStatus Status)
  1170. {
  1171. {
  1172. TGuard Guard(FSection);
  1173. FStatus = Status;
  1174. }
  1175. assert((FQueue != NULL) || (Status == qsPending));
  1176. if (FQueue != NULL)
  1177. {
  1178. FQueue->DoQueueItemUpdate(this);
  1179. }
  1180. }
  1181. //---------------------------------------------------------------------------
  1182. void __fastcall TQueueItem::SetProgress(
  1183. TFileOperationProgressType & ProgressData)
  1184. {
  1185. {
  1186. TGuard Guard(FSection);
  1187. assert(FProgressData != NULL);
  1188. *FProgressData = ProgressData;
  1189. FProgressData->Reset();
  1190. if (FCPSLimit >= 0)
  1191. {
  1192. ProgressData.CPSLimit = static_cast<unsigned long>(FCPSLimit);
  1193. FCPSLimit = -1;
  1194. }
  1195. }
  1196. FQueue->DoQueueItemUpdate(this);
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. void __fastcall TQueueItem::GetData(TQueueItemProxy * Proxy)
  1200. {
  1201. TGuard Guard(FSection);
  1202. assert(Proxy->FProgressData != NULL);
  1203. if (FProgressData != NULL)
  1204. {
  1205. *Proxy->FProgressData = *FProgressData;
  1206. }
  1207. else
  1208. {
  1209. Proxy->FProgressData->Clear();
  1210. }
  1211. *Proxy->FInfo = *FInfo;
  1212. Proxy->FStatus = FStatus;
  1213. if (FTerminalItem != NULL)
  1214. {
  1215. FTerminalItem->OverrideItemStatus(Proxy->FStatus);
  1216. }
  1217. }
  1218. //---------------------------------------------------------------------------
  1219. void __fastcall TQueueItem::Execute(TTerminalItem * TerminalItem)
  1220. {
  1221. try
  1222. {
  1223. {
  1224. assert(FProgressData == NULL);
  1225. TGuard Guard(FSection);
  1226. FProgressData = new TFileOperationProgressType();
  1227. }
  1228. DoExecute(TerminalItem->FTerminal);
  1229. }
  1230. __finally
  1231. {
  1232. {
  1233. TGuard Guard(FSection);
  1234. delete FProgressData;
  1235. FProgressData = NULL;
  1236. }
  1237. }
  1238. }
  1239. //---------------------------------------------------------------------------
  1240. void __fastcall TQueueItem::SetCPSLimit(unsigned long CPSLimit)
  1241. {
  1242. FCPSLimit = static_cast<long>(CPSLimit);
  1243. }
  1244. //---------------------------------------------------------------------------
  1245. // TQueueItemProxy
  1246. //---------------------------------------------------------------------------
  1247. __fastcall TQueueItemProxy::TQueueItemProxy(TTerminalQueue * Queue,
  1248. TQueueItem * QueueItem) :
  1249. FQueue(Queue), FQueueItem(QueueItem), FProgressData(NULL),
  1250. FQueueStatus(NULL), FInfo(NULL),
  1251. FProcessingUserAction(false), FUserData(NULL)
  1252. {
  1253. FProgressData = new TFileOperationProgressType();
  1254. FInfo = new TQueueItem::TInfo();
  1255. Update();
  1256. }
  1257. //---------------------------------------------------------------------------
  1258. __fastcall TQueueItemProxy::~TQueueItemProxy()
  1259. {
  1260. delete FProgressData;
  1261. delete FInfo;
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. TFileOperationProgressType * __fastcall TQueueItemProxy::GetProgressData()
  1265. {
  1266. return (FProgressData->Operation == foNone) ? NULL : FProgressData;
  1267. }
  1268. //---------------------------------------------------------------------------
  1269. bool __fastcall TQueueItemProxy::Update()
  1270. {
  1271. assert(FQueueItem != NULL);
  1272. TQueueItem::TStatus PrevStatus = Status;
  1273. bool Result = FQueue->ItemGetData(FQueueItem, this);
  1274. if ((FQueueStatus != NULL) && (PrevStatus != Status))
  1275. {
  1276. FQueueStatus->ResetStats();
  1277. }
  1278. return Result;
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. bool __fastcall TQueueItemProxy::ExecuteNow()
  1282. {
  1283. return FQueue->ItemExecuteNow(FQueueItem);
  1284. }
  1285. //---------------------------------------------------------------------------
  1286. bool __fastcall TQueueItemProxy::Move(bool Sooner)
  1287. {
  1288. bool Result = false;
  1289. int I = Index;
  1290. if (Sooner)
  1291. {
  1292. if (I > 0)
  1293. {
  1294. Result = Move(FQueueStatus->Items[I - 1]);
  1295. }
  1296. }
  1297. else
  1298. {
  1299. if (I < FQueueStatus->Count - 1)
  1300. {
  1301. Result = FQueueStatus->Items[I + 1]->Move(this);
  1302. }
  1303. }
  1304. return Result;
  1305. }
  1306. //---------------------------------------------------------------------------
  1307. bool __fastcall TQueueItemProxy::Move(TQueueItemProxy * BeforeItem)
  1308. {
  1309. return FQueue->ItemMove(FQueueItem, BeforeItem->FQueueItem);
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. bool __fastcall TQueueItemProxy::Delete()
  1313. {
  1314. return FQueue->ItemDelete(FQueueItem);
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. bool __fastcall TQueueItemProxy::Pause()
  1318. {
  1319. return FQueue->ItemPause(FQueueItem, true);
  1320. }
  1321. //---------------------------------------------------------------------------
  1322. bool __fastcall TQueueItemProxy::Resume()
  1323. {
  1324. return FQueue->ItemPause(FQueueItem, false);
  1325. }
  1326. //---------------------------------------------------------------------------
  1327. bool __fastcall TQueueItemProxy::ProcessUserAction(void * Arg)
  1328. {
  1329. assert(FQueueItem != NULL);
  1330. bool Result;
  1331. FProcessingUserAction = true;
  1332. try
  1333. {
  1334. Result = FQueue->ItemProcessUserAction(FQueueItem, Arg);
  1335. }
  1336. __finally
  1337. {
  1338. FProcessingUserAction = false;
  1339. }
  1340. return Result;
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. bool __fastcall TQueueItemProxy::SetCPSLimit(unsigned long CPSLimit)
  1344. {
  1345. return FQueue->ItemSetCPSLimit(FQueueItem, CPSLimit);
  1346. }
  1347. //---------------------------------------------------------------------------
  1348. int __fastcall TQueueItemProxy::GetIndex()
  1349. {
  1350. assert(FQueueStatus != NULL);
  1351. int Index = FQueueStatus->FList->IndexOf(this);
  1352. assert(Index >= 0);
  1353. return Index;
  1354. }
  1355. //---------------------------------------------------------------------------
  1356. // TTerminalQueueStatus
  1357. //---------------------------------------------------------------------------
  1358. __fastcall TTerminalQueueStatus::TTerminalQueueStatus() :
  1359. FList(NULL)
  1360. {
  1361. FList = new TList();
  1362. ResetStats();
  1363. }
  1364. //---------------------------------------------------------------------------
  1365. __fastcall TTerminalQueueStatus::~TTerminalQueueStatus()
  1366. {
  1367. for (int Index = 0; Index < FList->Count; Index++)
  1368. {
  1369. delete GetItem(Index);
  1370. }
  1371. delete FList;
  1372. FList = NULL;
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. void __fastcall TTerminalQueueStatus::ResetStats()
  1376. {
  1377. FActiveCount = -1;
  1378. }
  1379. //---------------------------------------------------------------------------
  1380. int __fastcall TTerminalQueueStatus::GetActiveCount()
  1381. {
  1382. if (FActiveCount < 0)
  1383. {
  1384. FActiveCount = 0;
  1385. while ((FActiveCount < FList->Count) &&
  1386. (GetItem(FActiveCount)->Status != TQueueItem::qsPending))
  1387. {
  1388. FActiveCount++;
  1389. }
  1390. }
  1391. return FActiveCount;
  1392. }
  1393. //---------------------------------------------------------------------------
  1394. void __fastcall TTerminalQueueStatus::Add(TQueueItemProxy * ItemProxy)
  1395. {
  1396. ItemProxy->FQueueStatus = this;
  1397. FList->Add(ItemProxy);
  1398. ResetStats();
  1399. }
  1400. //---------------------------------------------------------------------------
  1401. void __fastcall TTerminalQueueStatus::Delete(TQueueItemProxy * ItemProxy)
  1402. {
  1403. FList->Extract(ItemProxy);
  1404. ItemProxy->FQueueStatus = NULL;
  1405. ResetStats();
  1406. }
  1407. //---------------------------------------------------------------------------
  1408. int __fastcall TTerminalQueueStatus::GetCount()
  1409. {
  1410. return FList->Count;
  1411. }
  1412. //---------------------------------------------------------------------------
  1413. TQueueItemProxy * __fastcall TTerminalQueueStatus::GetItem(int Index)
  1414. {
  1415. return reinterpret_cast<TQueueItemProxy *>(FList->Items[Index]);
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. TQueueItemProxy * __fastcall TTerminalQueueStatus::FindByQueueItem(
  1419. TQueueItem * QueueItem)
  1420. {
  1421. TQueueItemProxy * Item;
  1422. for (int Index = 0; Index < FList->Count; Index++)
  1423. {
  1424. Item = GetItem(Index);
  1425. if (Item->FQueueItem == QueueItem)
  1426. {
  1427. return Item;
  1428. }
  1429. }
  1430. return NULL;
  1431. }
  1432. //---------------------------------------------------------------------------
  1433. // TLocatedQueueItem
  1434. //---------------------------------------------------------------------------
  1435. __fastcall TLocatedQueueItem::TLocatedQueueItem(TTerminal * Terminal) :
  1436. TQueueItem()
  1437. {
  1438. assert(Terminal != NULL);
  1439. FCurrentDir = Terminal->CurrentDirectory;
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. AnsiString __fastcall TLocatedQueueItem::StartupDirectory()
  1443. {
  1444. return FCurrentDir;
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. void __fastcall TLocatedQueueItem::DoExecute(TTerminal * Terminal)
  1448. {
  1449. assert(Terminal != NULL);
  1450. Terminal->CurrentDirectory = FCurrentDir;
  1451. }
  1452. //---------------------------------------------------------------------------
  1453. // TTransferQueueItem
  1454. //---------------------------------------------------------------------------
  1455. __fastcall TTransferQueueItem::TTransferQueueItem(TTerminal * Terminal,
  1456. TStrings * FilesToCopy, const AnsiString & TargetDir,
  1457. const TCopyParamType * CopyParam, int Params, TOperationSide Side) :
  1458. TLocatedQueueItem(Terminal), FFilesToCopy(NULL), FCopyParam(NULL)
  1459. {
  1460. FInfo->Operation = (Params & cpDelete ? foMove : foCopy);
  1461. FInfo->Side = Side;
  1462. assert(FilesToCopy != NULL);
  1463. FFilesToCopy = new TStringList();
  1464. for (int Index = 0; Index < FilesToCopy->Count; Index++)
  1465. {
  1466. FFilesToCopy->AddObject(FilesToCopy->Strings[Index],
  1467. ((FilesToCopy->Objects[Index] == NULL) || (Side == osLocal)) ? NULL :
  1468. dynamic_cast<TRemoteFile*>(FilesToCopy->Objects[Index])->Duplicate());
  1469. }
  1470. FTargetDir = TargetDir;
  1471. assert(CopyParam != NULL);
  1472. FCopyParam = new TCopyParamType(*CopyParam);
  1473. FParams = Params;
  1474. }
  1475. //---------------------------------------------------------------------------
  1476. __fastcall TTransferQueueItem::~TTransferQueueItem()
  1477. {
  1478. for (int Index = 0; Index < FFilesToCopy->Count; Index++)
  1479. {
  1480. delete FFilesToCopy->Objects[Index];
  1481. }
  1482. delete FFilesToCopy;
  1483. delete FCopyParam;
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. // TUploadQueueItem
  1487. //---------------------------------------------------------------------------
  1488. __fastcall TUploadQueueItem::TUploadQueueItem(TTerminal * Terminal,
  1489. TStrings * FilesToCopy, const AnsiString & TargetDir,
  1490. const TCopyParamType * CopyParam, int Params) :
  1491. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osLocal)
  1492. {
  1493. if (FilesToCopy->Count > 1)
  1494. {
  1495. if (FLAGSET(Params, cpTemporary))
  1496. {
  1497. FInfo->Source = "";
  1498. FInfo->ModifiedLocal = "";
  1499. }
  1500. else
  1501. {
  1502. ExtractCommonPath(FilesToCopy, FInfo->Source);
  1503. // this way the trailing backslash is preserved for root directories like D:\\
  1504. FInfo->Source = ExtractFileDir(IncludeTrailingBackslash(FInfo->Source));
  1505. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1506. IncludeTrailingBackslash(FInfo->Source);
  1507. }
  1508. }
  1509. else
  1510. {
  1511. if (FLAGSET(Params, cpTemporary))
  1512. {
  1513. FInfo->Source = ExtractFileName(FilesToCopy->Strings[0]);
  1514. FInfo->ModifiedLocal = "";
  1515. }
  1516. else
  1517. {
  1518. assert(FilesToCopy->Count > 0);
  1519. FInfo->Source = FilesToCopy->Strings[0];
  1520. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1521. IncludeTrailingBackslash(ExtractFilePath(FInfo->Source));
  1522. }
  1523. }
  1524. FInfo->Destination =
  1525. UnixIncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  1526. FInfo->ModifiedRemote = UnixIncludeTrailingBackslash(TargetDir);
  1527. }
  1528. //---------------------------------------------------------------------------
  1529. void __fastcall TUploadQueueItem::DoExecute(TTerminal * Terminal)
  1530. {
  1531. TTransferQueueItem::DoExecute(Terminal);
  1532. assert(Terminal != NULL);
  1533. Terminal->CopyToRemote(FFilesToCopy, FTargetDir, FCopyParam, FParams);
  1534. }
  1535. //---------------------------------------------------------------------------
  1536. // TDownloadQueueItem
  1537. //---------------------------------------------------------------------------
  1538. __fastcall TDownloadQueueItem::TDownloadQueueItem(TTerminal * Terminal,
  1539. TStrings * FilesToCopy, const AnsiString & TargetDir,
  1540. const TCopyParamType * CopyParam, int Params) :
  1541. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osRemote)
  1542. {
  1543. if (FilesToCopy->Count > 1)
  1544. {
  1545. if (!UnixExtractCommonPath(FilesToCopy, FInfo->Source))
  1546. {
  1547. FInfo->Source = Terminal->CurrentDirectory;
  1548. }
  1549. FInfo->Source = UnixExcludeTrailingBackslash(FInfo->Source);
  1550. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1551. UnixIncludeTrailingBackslash(FInfo->Source);
  1552. }
  1553. else
  1554. {
  1555. assert(FilesToCopy->Count > 0);
  1556. FInfo->Source = FilesToCopy->Strings[0];
  1557. if (UnixExtractFilePath(FInfo->Source).IsEmpty())
  1558. {
  1559. FInfo->Source = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  1560. FInfo->Source;
  1561. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1562. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  1563. }
  1564. else
  1565. {
  1566. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1567. UnixExtractFilePath(FInfo->Source);
  1568. }
  1569. }
  1570. if (FLAGSET(Params, cpTemporary))
  1571. {
  1572. FInfo->Destination = "";
  1573. }
  1574. else
  1575. {
  1576. FInfo->Destination =
  1577. IncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  1578. }
  1579. FInfo->ModifiedLocal = IncludeTrailingBackslash(TargetDir);
  1580. }
  1581. //---------------------------------------------------------------------------
  1582. void __fastcall TDownloadQueueItem::DoExecute(TTerminal * Terminal)
  1583. {
  1584. TTransferQueueItem::DoExecute(Terminal);
  1585. assert(Terminal != NULL);
  1586. Terminal->CopyToLocal(FFilesToCopy, FTargetDir, FCopyParam, FParams);
  1587. }