Queue.cpp 48 KB

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