Queue.cpp 47 KB

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