Queue.cpp 45 KB

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