Queue.cpp 47 KB

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