Queue.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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 TTerminalItem : public TSignalThread
  11. {
  12. friend class TQueueItem;
  13. public:
  14. __fastcall TTerminalItem(TTerminalQueue * Queue);
  15. __fastcall ~TTerminalItem();
  16. void __fastcall Process(TQueueItem * Item);
  17. void __fastcall ProcessUserAction();
  18. void __fastcall Cancel();
  19. protected:
  20. struct TQueryUserRec
  21. {
  22. TObject * Sender;
  23. AnsiString Query;
  24. TStrings * MoreMessages;
  25. int Answers;
  26. const TQueryParams * Params;
  27. int Answer;
  28. TQueryType Type;
  29. };
  30. struct TPromptUserRec
  31. {
  32. TSecureShell * SecureShell;
  33. AnsiString Prompt;
  34. TPromptKind Kind;
  35. AnsiString Response;
  36. bool Result;
  37. };
  38. struct TShowExtendedExceptionRec
  39. {
  40. TSecureShell * SecureShell;
  41. Exception * E;
  42. };
  43. TTerminalQueue * FQueue;
  44. TTerminal * FTerminal;
  45. TQueueItem * FItem;
  46. TCriticalSection * FCriticalSection;
  47. void * FUserActionParams;
  48. bool FCancel;
  49. virtual void __fastcall ProcessEvent();
  50. virtual void __fastcall Finished();
  51. bool __fastcall WaitForUserAction(TQueueItem::TStatus ItemStatus, void * Params);
  52. void __fastcall TerminalClose(TObject * Sender);
  53. void __fastcall TerminalQueryUser(TObject * Sender,
  54. const AnsiString Query, TStrings * MoreMessages, int Answers,
  55. const TQueryParams * Params, int & Answer, TQueryType Type);
  56. void __fastcall TerminalPromptUser(TSecureShell * SecureShell,
  57. AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result);
  58. void __fastcall TerminalShowExtendedException(TSecureShell * SecureShell,
  59. Exception * E);
  60. void __fastcall OperationFinished(TFileOperation Operation, TOperationSide Side,
  61. bool DragDrop, const AnsiString FileName, bool Success,
  62. bool & DisconnectWhenFinished);
  63. void __fastcall OperationProgress(TFileOperationProgressType & ProgressData,
  64. TCancelStatus & Cancel);
  65. };
  66. //---------------------------------------------------------------------------
  67. // TSignalThread
  68. //---------------------------------------------------------------------------
  69. int __fastcall ThreadProc(void * Thread)
  70. {
  71. TSignalThread * SimpleThread = reinterpret_cast<TSignalThread*>(Thread);
  72. assert(SimpleThread != NULL);
  73. try
  74. {
  75. SimpleThread->Execute();
  76. }
  77. __finally
  78. {
  79. SimpleThread->FFinished = true;
  80. SimpleThread->Finished();
  81. EndThread(0);
  82. }
  83. return 0;
  84. }
  85. //---------------------------------------------------------------------------
  86. __fastcall TSignalThread::TSignalThread() :
  87. FTerminated(true), FFinished(true), FThread(NULL), FEvent(NULL)
  88. {
  89. FEvent = CreateEvent(NULL, false, false, NULL);
  90. assert(FEvent != NULL);
  91. unsigned ThreadID;
  92. FThread = reinterpret_cast<HANDLE>(
  93. BeginThread(NULL, 0, ThreadProc, this, CREATE_SUSPENDED, ThreadID));
  94. ::SetThreadPriority(FThread, THREAD_PRIORITY_BELOW_NORMAL);
  95. }
  96. //---------------------------------------------------------------------------
  97. __fastcall TSignalThread::~TSignalThread()
  98. {
  99. Close();
  100. if (FThread != NULL)
  101. {
  102. CloseHandle(FThread);
  103. }
  104. if (FEvent)
  105. {
  106. CloseHandle(FEvent);
  107. }
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TSignalThread::Start()
  111. {
  112. FTerminated = false;
  113. if (ResumeThread(FThread) == 1)
  114. {
  115. FFinished = false;
  116. }
  117. }
  118. //---------------------------------------------------------------------------
  119. void __fastcall TSignalThread::Finished()
  120. {
  121. }
  122. //---------------------------------------------------------------------------
  123. void __fastcall TSignalThread::TriggerEvent()
  124. {
  125. SetEvent(FEvent);
  126. }
  127. //---------------------------------------------------------------------------
  128. bool __fastcall TSignalThread::WaitForEvent()
  129. {
  130. return (WaitForSingleObject(FEvent, INFINITE) == WAIT_OBJECT_0) &&
  131. !FTerminated;
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TSignalThread::Execute()
  135. {
  136. while (!FTerminated)
  137. {
  138. if (WaitForEvent())
  139. {
  140. ProcessEvent();
  141. }
  142. }
  143. }
  144. //---------------------------------------------------------------------------
  145. void __fastcall TSignalThread::Close()
  146. {
  147. if (!FFinished)
  148. {
  149. Terminate();
  150. WaitFor();
  151. }
  152. }
  153. //---------------------------------------------------------------------------
  154. void __fastcall TSignalThread::Terminate()
  155. {
  156. FTerminated = true;
  157. TriggerEvent();
  158. }
  159. //---------------------------------------------------------------------------
  160. void __fastcall TSignalThread::WaitFor()
  161. {
  162. WaitForSingleObject(FThread, INFINITE);
  163. }
  164. //---------------------------------------------------------------------------
  165. // TTerminalQueue
  166. //---------------------------------------------------------------------------
  167. __fastcall TTerminalQueue::TTerminalQueue(TTerminal * Terminal,
  168. TConfiguration * Configuration) :
  169. FTerminal(Terminal), FTransfersLimit(2),
  170. FConfiguration(Configuration), FSessionData(NULL), FItems(NULL),
  171. FTerminals(NULL), FItemsSection(NULL), FFreeTerminals(0),
  172. FItemsInProcess(0), FTemporaryTerminals(0)
  173. {
  174. FOnQueryUser = NULL;
  175. FOnPromptUser = NULL;
  176. FOnShowExtendedException = NULL;
  177. FOnQueueItemUpdate = NULL;
  178. FOnListUpdate = NULL;
  179. assert(Terminal != NULL);
  180. FSessionData = new TSessionData("");
  181. FSessionData->Assign(Terminal->SessionData);
  182. FItems = new TList();
  183. FTerminals = new TList();
  184. FItemsSection = new TCriticalSection();
  185. Start();
  186. }
  187. //---------------------------------------------------------------------------
  188. __fastcall TTerminalQueue::~TTerminalQueue()
  189. {
  190. Close();
  191. {
  192. TGuard Guard(FItemsSection);
  193. TTerminalItem * TerminalItem;
  194. while (FTerminals->Count > 0)
  195. {
  196. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
  197. FTerminals->Delete(0);
  198. TerminalItem->Terminate();
  199. TerminalItem->WaitFor();
  200. delete TerminalItem;
  201. }
  202. delete FTerminals;
  203. for (int Index = 0; Index < FItems->Count; Index++)
  204. {
  205. delete GetItem(Index);
  206. }
  207. }
  208. delete FItemsSection;
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TTerminalQueue::TerminalFinished(TTerminalItem * TerminalItem)
  212. {
  213. if (!FTerminated)
  214. {
  215. {
  216. TGuard Guard(FItemsSection);
  217. int Index = FTerminals->IndexOf(TerminalItem);
  218. assert(Index >= 0);
  219. if (Index < FFreeTerminals)
  220. {
  221. FFreeTerminals--;
  222. }
  223. if (Index >= FTransfersLimit)
  224. {
  225. assert(FTemporaryTerminals > 0);
  226. FTemporaryTerminals--;
  227. }
  228. FTerminals->Extract(TerminalItem);
  229. delete TerminalItem;
  230. }
  231. TriggerEvent();
  232. }
  233. }
  234. //---------------------------------------------------------------------------
  235. bool __fastcall TTerminalQueue::TerminalFree(TTerminalItem * TerminalItem)
  236. {
  237. bool Result = true;
  238. if (!FTerminated)
  239. {
  240. {
  241. TGuard Guard(FItemsSection);
  242. int Index = FTerminals->IndexOf(TerminalItem);
  243. assert(Index >= 0);
  244. assert(Index >= FFreeTerminals);
  245. Result = (Index < FTransfersLimit);
  246. if (Result)
  247. {
  248. FTerminals->Move(Index, 0);
  249. FFreeTerminals++;
  250. }
  251. }
  252. TriggerEvent();
  253. }
  254. return Result;
  255. }
  256. //---------------------------------------------------------------------------
  257. void __fastcall TTerminalQueue::AddItem(TQueueItem * Item)
  258. {
  259. assert(!FTerminated);
  260. Item->SetStatus(TQueueItem::qsPending);
  261. {
  262. TGuard Guard(FItemsSection);
  263. FItems->Add(Item);
  264. Item->FQueue = this;
  265. }
  266. DoListUpdate();
  267. TriggerEvent();
  268. }
  269. //---------------------------------------------------------------------------
  270. void __fastcall TTerminalQueue::RetryItem(TQueueItem * Item)
  271. {
  272. if (!FTerminated)
  273. {
  274. {
  275. TGuard Guard(FItemsSection);
  276. int Index = FItems->Remove(Item);
  277. assert(Index < FItemsInProcess);
  278. USEDPARAM(Index);
  279. FItemsInProcess--;
  280. FItems->Add(Item);
  281. }
  282. DoListUpdate();
  283. TriggerEvent();
  284. }
  285. }
  286. //---------------------------------------------------------------------------
  287. void __fastcall TTerminalQueue::DeleteItem(TQueueItem * Item)
  288. {
  289. if (!FTerminated)
  290. {
  291. {
  292. TGuard Guard(FItemsSection);
  293. int Index = FItems->Remove(Item);
  294. assert(Index < FItemsInProcess);
  295. USEDPARAM(Index);
  296. FItemsInProcess--;
  297. delete Item;
  298. }
  299. DoListUpdate();
  300. }
  301. }
  302. //---------------------------------------------------------------------------
  303. TQueueItem * __fastcall TTerminalQueue::GetItem(int Index)
  304. {
  305. return reinterpret_cast<TQueueItem*>(FItems->Items[Index]);
  306. }
  307. //---------------------------------------------------------------------------
  308. TTerminalQueueStatus * __fastcall TTerminalQueue::CreateStatus(TTerminalQueueStatus * Current)
  309. {
  310. TTerminalQueueStatus * Status = new TTerminalQueueStatus();
  311. try
  312. {
  313. try
  314. {
  315. TGuard Guard(FItemsSection);
  316. TQueueItem * Item;
  317. TQueueItemProxy * ItemProxy;
  318. for (int Index = 0; Index < FItems->Count; Index++)
  319. {
  320. Item = GetItem(Index);
  321. if (Current != NULL)
  322. {
  323. ItemProxy = Current->FindByQueueItem(Item);
  324. }
  325. else
  326. {
  327. ItemProxy = NULL;
  328. }
  329. if (ItemProxy != NULL)
  330. {
  331. Current->Delete(ItemProxy);
  332. Status->Add(ItemProxy);
  333. ItemProxy->Update();
  334. }
  335. else
  336. {
  337. Status->Add(new TQueueItemProxy(this, Item));
  338. }
  339. }
  340. }
  341. __finally
  342. {
  343. if (Current != NULL)
  344. {
  345. delete Current;
  346. }
  347. }
  348. }
  349. catch(...)
  350. {
  351. delete Status;
  352. throw;
  353. }
  354. return Status;
  355. }
  356. //---------------------------------------------------------------------------
  357. bool __fastcall TTerminalQueue::ItemGetData(TQueueItem * Item,
  358. TQueueItemProxy * Proxy)
  359. {
  360. TGuard Guard(FItemsSection);
  361. bool Result = (FItems->IndexOf(Item) >= 0);
  362. if (Result)
  363. {
  364. Item->GetData(Proxy);
  365. }
  366. return Result;
  367. }
  368. //---------------------------------------------------------------------------
  369. bool __fastcall TTerminalQueue::ItemProcessUserAction(TQueueItem * Item)
  370. {
  371. bool Result;
  372. TTerminalItem * TerminalItem;
  373. {
  374. TGuard Guard(FItemsSection);
  375. Result = (FItems->IndexOf(Item) >= 0) &&
  376. TQueueItem::IsUserActionStatus(Item->Status);
  377. if (Result)
  378. {
  379. TerminalItem = Item->FTerminalItem;
  380. }
  381. }
  382. if (Result)
  383. {
  384. TerminalItem->ProcessUserAction();
  385. }
  386. return Result;
  387. }
  388. //---------------------------------------------------------------------------
  389. bool __fastcall TTerminalQueue::ItemMove(TQueueItem * Item, TQueueItem * BeforeItem)
  390. {
  391. bool Result;
  392. {
  393. TGuard Guard(FItemsSection);
  394. int Index = FItems->IndexOf(Item);
  395. int IndexDest = FItems->IndexOf(BeforeItem);
  396. Result = (Index >= 0) && (IndexDest >= 0) &&
  397. (Item->GetStatus() == TQueueItem::qsPending) &&
  398. (BeforeItem->GetStatus() == TQueueItem::qsPending);
  399. if (Result)
  400. {
  401. FItems->Move(Index, IndexDest);
  402. }
  403. }
  404. if (Result)
  405. {
  406. DoListUpdate();
  407. TriggerEvent();
  408. }
  409. return Result;
  410. }
  411. //---------------------------------------------------------------------------
  412. bool __fastcall TTerminalQueue::ItemExecuteNow(TQueueItem * Item)
  413. {
  414. bool Result;
  415. {
  416. TGuard Guard(FItemsSection);
  417. int Index = FItems->IndexOf(Item);
  418. Result = (Index >= 0) && (Item->GetStatus() == TQueueItem::qsPending);
  419. if (Result)
  420. {
  421. if (Index != FItemsInProcess)
  422. {
  423. assert(Index > FItemsInProcess);
  424. FItems->Move(Index, FItemsInProcess);
  425. }
  426. if (FTerminals->Count >= FTransfersLimit)
  427. {
  428. FTemporaryTerminals++;
  429. }
  430. }
  431. }
  432. if (Result)
  433. {
  434. DoListUpdate();
  435. TriggerEvent();
  436. }
  437. return Result;
  438. }
  439. //---------------------------------------------------------------------------
  440. bool __fastcall TTerminalQueue::ItemDelete(TQueueItem * Item)
  441. {
  442. bool Result;
  443. bool UpdateList = false;
  444. {
  445. TGuard Guard(FItemsSection);
  446. int Index = FItems->IndexOf(Item);
  447. Result = (Index >= 0);
  448. if (Result)
  449. {
  450. if (Item->Status == TQueueItem::qsPending)
  451. {
  452. FItems->Delete(Index);
  453. UpdateList = true;
  454. }
  455. else
  456. {
  457. Item->FTerminalItem->Cancel();
  458. }
  459. }
  460. }
  461. if (UpdateList)
  462. {
  463. DoListUpdate();
  464. TriggerEvent();
  465. }
  466. return Result;
  467. }
  468. //---------------------------------------------------------------------------
  469. void __fastcall TTerminalQueue::ProcessEvent()
  470. {
  471. TTerminalItem * TerminalItem;
  472. TQueueItem * Item;
  473. do
  474. {
  475. TerminalItem = NULL;
  476. Item = NULL;
  477. if (FItems->Count > FItemsInProcess)
  478. {
  479. TGuard Guard(FItemsSection);
  480. if ((FFreeTerminals == 0) &&
  481. (FTerminals->Count < FTransfersLimit + FTemporaryTerminals))
  482. {
  483. TerminalItem = new TTerminalItem(this);
  484. FTerminals->Add(TerminalItem);
  485. }
  486. else if (FFreeTerminals > 0)
  487. {
  488. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
  489. FTerminals->Move(0, FTerminals->Count - 1);
  490. FFreeTerminals--;
  491. }
  492. if (TerminalItem != NULL)
  493. {
  494. Item = GetItem(FItemsInProcess);
  495. FItemsInProcess++;
  496. }
  497. }
  498. if (TerminalItem != NULL)
  499. {
  500. TerminalItem->Process(Item);
  501. }
  502. }
  503. while (!FTerminated && (TerminalItem != NULL));
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TTerminalQueue::DoQueueItemUpdate(TQueueItem * Item)
  507. {
  508. if (OnQueueItemUpdate != NULL)
  509. {
  510. OnQueueItemUpdate(this, Item);
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TTerminalQueue::DoListUpdate()
  515. {
  516. if (OnListUpdate != NULL)
  517. {
  518. OnListUpdate(this);
  519. }
  520. }
  521. //---------------------------------------------------------------------------
  522. void __fastcall TTerminalQueue::DoQueryUser(TObject * Sender,
  523. const AnsiString Query, TStrings * MoreMessages, int Answers,
  524. const TQueryParams * Params, int & Answer, TQueryType Type)
  525. {
  526. if (OnQueryUser != NULL)
  527. {
  528. OnQueryUser(Sender, Query, MoreMessages, Answers, Params, Answer, Type);
  529. }
  530. }
  531. //---------------------------------------------------------------------------
  532. void __fastcall TTerminalQueue::DoPromptUser(TSecureShell * SecureShell,
  533. AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result)
  534. {
  535. if (OnPromptUser != NULL)
  536. {
  537. OnPromptUser(SecureShell, Prompt, Kind, Response, Result);
  538. }
  539. }
  540. //---------------------------------------------------------------------------
  541. void __fastcall TTerminalQueue::DoShowExtendedException(
  542. TSecureShell * SecureShell, Exception * E)
  543. {
  544. if (OnShowExtendedException != NULL)
  545. {
  546. OnShowExtendedException(SecureShell, E);
  547. }
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TTerminalQueue::SetTransfersLimit(int value)
  551. {
  552. if (FTransfersLimit != value)
  553. {
  554. {
  555. TGuard Guard(FItemsSection);
  556. if (value < FItemsInProcess)
  557. {
  558. FTemporaryTerminals = (FItemsInProcess - value);
  559. }
  560. else
  561. {
  562. FTemporaryTerminals = 0;
  563. }
  564. FTransfersLimit = value;
  565. }
  566. TriggerEvent();
  567. }
  568. }
  569. //---------------------------------------------------------------------------
  570. bool __fastcall TTerminalQueue::GetIsEmpty()
  571. {
  572. TGuard Guard(FItemsSection);
  573. return (FItems->Count == 0);
  574. }
  575. //---------------------------------------------------------------------------
  576. // TTerminalItem
  577. //---------------------------------------------------------------------------
  578. __fastcall TTerminalItem::TTerminalItem(TTerminalQueue * Queue) :
  579. TSignalThread(), FQueue(Queue), FTerminal(NULL), FItem(NULL),
  580. FCriticalSection(NULL), FUserActionParams(NULL)
  581. {
  582. FCriticalSection = new TCriticalSection();
  583. FTerminal = new TSecondaryTerminal(FQueue->FTerminal);
  584. try
  585. {
  586. FTerminal->UseBusyCursor = false;
  587. FTerminal->Configuration = Queue->FConfiguration;
  588. FTerminal->SessionData = Queue->FSessionData;
  589. FTerminal->OnClose = TerminalClose;
  590. FTerminal->OnQueryUser = TerminalQueryUser;
  591. FTerminal->OnPromptUser = TerminalPromptUser;
  592. FTerminal->OnShowExtendedException = TerminalShowExtendedException;
  593. FTerminal->OnProgress = OperationProgress;
  594. FTerminal->OnFinished = OperationFinished;
  595. }
  596. catch(...)
  597. {
  598. delete FTerminal;
  599. throw;
  600. }
  601. Start();
  602. }
  603. //---------------------------------------------------------------------------
  604. __fastcall TTerminalItem::~TTerminalItem()
  605. {
  606. Close();
  607. assert(FItem == NULL);
  608. delete FTerminal;
  609. delete FCriticalSection;
  610. }
  611. //---------------------------------------------------------------------------
  612. void __fastcall TTerminalItem::Process(TQueueItem * Item)
  613. {
  614. {
  615. TGuard Guard(FCriticalSection);
  616. assert(FItem == NULL);
  617. FItem = Item;
  618. }
  619. TriggerEvent();
  620. }
  621. //---------------------------------------------------------------------------
  622. void __fastcall TTerminalItem::ProcessEvent()
  623. {
  624. TGuard Guard(FCriticalSection);
  625. bool Retry = true;
  626. FCancel = false;
  627. FItem->FTerminalItem = this;
  628. try
  629. {
  630. assert(FItem != NULL);
  631. if (!FTerminal->Active)
  632. {
  633. FItem->SetStatus(TQueueItem::qsConnecting);
  634. FTerminal->SessionData->RemoteDirectory = FItem->StartupDirectory();
  635. FTerminal->Open();
  636. FTerminal->DoStartup();
  637. }
  638. Retry = false;
  639. if (!FCancel)
  640. {
  641. FItem->SetStatus(TQueueItem::qsProcessing);
  642. FItem->Execute(this);
  643. }
  644. }
  645. catch(Exception & E)
  646. {
  647. FTerminal->DoShowExtendedException(&E);
  648. }
  649. FItem->SetStatus(TQueueItem::qsDone);
  650. FItem->FTerminalItem = NULL;
  651. TQueueItem * Item = FItem;
  652. FItem = NULL;
  653. if (Retry && !FCancel)
  654. {
  655. FQueue->RetryItem(Item);
  656. }
  657. else
  658. {
  659. FQueue->DeleteItem(Item);
  660. }
  661. if (!FTerminal->Active ||
  662. !FQueue->TerminalFree(this))
  663. {
  664. Terminate();
  665. }
  666. }
  667. //---------------------------------------------------------------------------
  668. void __fastcall TTerminalItem::Cancel()
  669. {
  670. FCancel = true;
  671. }
  672. //---------------------------------------------------------------------------
  673. void __fastcall TTerminalItem::ProcessUserAction()
  674. {
  675. assert(FItem != NULL);
  676. assert(FUserActionParams != NULL);
  677. if (FItem->GetStatus() == TQueueItem::qsQuery)
  678. {
  679. TQueryUserRec * Params;
  680. Params = reinterpret_cast<TQueryUserRec *>(FUserActionParams);
  681. FQueue->DoQueryUser(Params->Sender, Params->Query, Params->MoreMessages,
  682. Params->Answers, Params->Params, Params->Answer, Params->Type);
  683. }
  684. else if (FItem->GetStatus() == TQueueItem::qsPrompt)
  685. {
  686. TPromptUserRec * Params;
  687. Params = reinterpret_cast<TPromptUserRec *>(FUserActionParams);
  688. FQueue->DoPromptUser(Params->SecureShell, Params->Prompt,
  689. Params->Kind, Params->Response, Params->Result);
  690. }
  691. else if (FItem->GetStatus() == TQueueItem::qsError)
  692. {
  693. TShowExtendedExceptionRec * Params;
  694. Params = reinterpret_cast<TShowExtendedExceptionRec *>(FUserActionParams);
  695. FQueue->DoShowExtendedException(Params->SecureShell, Params->E);
  696. }
  697. else
  698. {
  699. assert(false);
  700. }
  701. TriggerEvent();
  702. }
  703. //---------------------------------------------------------------------------
  704. bool __fastcall TTerminalItem::WaitForUserAction(
  705. TQueueItem::TStatus ItemStatus, void * Params)
  706. {
  707. assert(FItem != NULL);
  708. assert((FItem->GetStatus() == TQueueItem::qsProcessing) ||
  709. (FItem->GetStatus() == TQueueItem::qsConnecting));
  710. bool Result;
  711. TQueueItem::TStatus PrevStatus = FItem->GetStatus();
  712. try
  713. {
  714. FUserActionParams = Params;
  715. FItem->SetStatus(ItemStatus);
  716. Result = !FTerminated && WaitForEvent() && !FCancel;
  717. }
  718. __finally
  719. {
  720. FUserActionParams = NULL;
  721. FItem->SetStatus(PrevStatus);
  722. }
  723. return Result;
  724. }
  725. //---------------------------------------------------------------------------
  726. void __fastcall TTerminalItem::Finished()
  727. {
  728. TSignalThread::Finished();
  729. FQueue->TerminalFinished(this);
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TTerminalItem::TerminalClose(TObject * /*Sender*/)
  733. {
  734. // nothing
  735. }
  736. //---------------------------------------------------------------------------
  737. void __fastcall TTerminalItem::TerminalQueryUser(TObject * Sender,
  738. const AnsiString Query, TStrings * MoreMessages, int Answers,
  739. const TQueryParams * Params, int & Answer, TQueryType Type)
  740. {
  741. TQueryUserRec QueryUserRec;
  742. QueryUserRec.Sender = Sender;
  743. QueryUserRec.Query = Query;
  744. QueryUserRec.MoreMessages = MoreMessages;
  745. QueryUserRec.Answers = Answers;
  746. QueryUserRec.Params = Params;
  747. QueryUserRec.Answer = Answer;
  748. QueryUserRec.Type = Type;
  749. if (WaitForUserAction(TQueueItem::qsQuery, &QueryUserRec))
  750. {
  751. Answer = QueryUserRec.Answer;
  752. }
  753. }
  754. //---------------------------------------------------------------------------
  755. void __fastcall TTerminalItem::TerminalPromptUser(TSecureShell * SecureShell,
  756. AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result)
  757. {
  758. TPromptUserRec PromptUserRec;
  759. PromptUserRec.SecureShell = SecureShell;
  760. PromptUserRec.Prompt = Prompt;
  761. PromptUserRec.Kind = Kind;
  762. PromptUserRec.Response = Response;
  763. PromptUserRec.Result = Result;
  764. if (WaitForUserAction(TQueueItem::qsPrompt, &PromptUserRec))
  765. {
  766. Response = PromptUserRec.Response;
  767. Result = PromptUserRec.Result;
  768. }
  769. }
  770. //---------------------------------------------------------------------------
  771. void __fastcall TTerminalItem::TerminalShowExtendedException(
  772. TSecureShell * SecureShell, Exception * E)
  773. {
  774. if (!E->Message.IsEmpty() &&
  775. (dynamic_cast<EAbort*>(E) == NULL))
  776. {
  777. TShowExtendedExceptionRec ShowExtendedExceptionRec;
  778. ShowExtendedExceptionRec.SecureShell = SecureShell;
  779. ShowExtendedExceptionRec.E = E;
  780. WaitForUserAction(TQueueItem::qsError, &ShowExtendedExceptionRec);
  781. }
  782. }
  783. //---------------------------------------------------------------------------
  784. void __fastcall TTerminalItem::OperationFinished(TFileOperation /*Operation*/,
  785. TOperationSide /*Side*/, bool /*DragDrop*/, const AnsiString /*FileName*/,
  786. bool /*Success*/, bool & /*DisconnectWhenFinished*/)
  787. {
  788. // nothing
  789. }
  790. //---------------------------------------------------------------------------
  791. void __fastcall TTerminalItem::OperationProgress(
  792. TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
  793. {
  794. if (FTerminated || FCancel)
  795. {
  796. if (ProgressData.TransferingFile)
  797. {
  798. Cancel = csCancelTransfer;
  799. }
  800. else
  801. {
  802. Cancel = csCancel;
  803. }
  804. }
  805. assert(FItem != NULL);
  806. FItem->SetProgress(ProgressData);
  807. }
  808. //---------------------------------------------------------------------------
  809. // TQueueItem
  810. //---------------------------------------------------------------------------
  811. __fastcall TQueueItem::TQueueItem() :
  812. FStatus(qsPending), FTerminalItem(NULL), FSection(NULL), FProgressData(NULL),
  813. FQueue(NULL), FInfo(NULL), FCompleteEvent(INVALID_HANDLE_VALUE)
  814. {
  815. FSection = new TCriticalSection();
  816. FInfo = new TInfo();
  817. }
  818. //---------------------------------------------------------------------------
  819. __fastcall TQueueItem::~TQueueItem()
  820. {
  821. if (FCompleteEvent != INVALID_HANDLE_VALUE)
  822. {
  823. SetEvent(FCompleteEvent);
  824. }
  825. delete FSection;
  826. delete FInfo;
  827. }
  828. //---------------------------------------------------------------------------
  829. bool __fastcall TQueueItem::IsUserActionStatus(TStatus Status)
  830. {
  831. return (Status == qsQuery) || (Status == qsError) || (Status == qsPrompt);
  832. }
  833. //---------------------------------------------------------------------------
  834. TQueueItem::TStatus __fastcall TQueueItem::GetStatus()
  835. {
  836. TGuard Guard(FSection);
  837. return FStatus;
  838. }
  839. //---------------------------------------------------------------------------
  840. void __fastcall TQueueItem::SetStatus(TStatus Status)
  841. {
  842. {
  843. TGuard Guard(FSection);
  844. FStatus = Status;
  845. }
  846. assert((FQueue != NULL) || (Status == qsPending));
  847. if (FQueue != NULL)
  848. {
  849. FQueue->DoQueueItemUpdate(this);
  850. }
  851. }
  852. //---------------------------------------------------------------------------
  853. void __fastcall TQueueItem::SetProgress(
  854. const TFileOperationProgressType & ProgressData)
  855. {
  856. {
  857. TGuard Guard(FSection);
  858. assert(FProgressData != NULL);
  859. *FProgressData = ProgressData;
  860. FProgressData->Reset();
  861. }
  862. FQueue->DoQueueItemUpdate(this);
  863. }
  864. //---------------------------------------------------------------------------
  865. void __fastcall TQueueItem::GetData(TQueueItemProxy * Proxy)
  866. {
  867. TGuard Guard(FSection);
  868. assert(Proxy->FProgressData != NULL);
  869. if (FProgressData != NULL)
  870. {
  871. *Proxy->FProgressData = *FProgressData;
  872. }
  873. else
  874. {
  875. Proxy->FProgressData->Clear();
  876. }
  877. *Proxy->FInfo = *FInfo;
  878. Proxy->FStatus = FStatus;
  879. }
  880. //---------------------------------------------------------------------------
  881. void __fastcall TQueueItem::Execute(TTerminalItem * TerminalItem)
  882. {
  883. try
  884. {
  885. {
  886. assert(FProgressData == NULL);
  887. TGuard Guard(FSection);
  888. FProgressData = new TFileOperationProgressType();
  889. }
  890. DoExecute(TerminalItem->FTerminal);
  891. }
  892. __finally
  893. {
  894. {
  895. TGuard Guard(FSection);
  896. delete FProgressData;
  897. FProgressData = NULL;
  898. }
  899. }
  900. }
  901. //---------------------------------------------------------------------------
  902. // TQueueItemProxy
  903. //---------------------------------------------------------------------------
  904. __fastcall TQueueItemProxy::TQueueItemProxy(TTerminalQueue * Queue,
  905. TQueueItem * QueueItem) :
  906. FQueue(Queue), FQueueItem(QueueItem), FProgressData(NULL),
  907. FQueueStatus(NULL), FInfo(NULL), FFlag(false),
  908. FProcessingUserAction(false)
  909. {
  910. FProgressData = new TFileOperationProgressType();
  911. FInfo = new TQueueItem::TInfo();
  912. Update();
  913. }
  914. //---------------------------------------------------------------------------
  915. __fastcall TQueueItemProxy::~TQueueItemProxy()
  916. {
  917. delete FProgressData;
  918. delete FInfo;
  919. }
  920. //---------------------------------------------------------------------------
  921. TFileOperationProgressType * __fastcall TQueueItemProxy::GetProgressData()
  922. {
  923. return (FProgressData->Operation == foNone) ? NULL : FProgressData;
  924. }
  925. //---------------------------------------------------------------------------
  926. bool __fastcall TQueueItemProxy::Update()
  927. {
  928. assert(FQueueItem != NULL);
  929. TQueueItem::TStatus PrevStatus = Status;
  930. bool Result = FQueue->ItemGetData(FQueueItem, this);
  931. if ((FQueueStatus != NULL) && (PrevStatus != Status))
  932. {
  933. FQueueStatus->ResetStats();
  934. }
  935. return Result;
  936. }
  937. //---------------------------------------------------------------------------
  938. bool __fastcall TQueueItemProxy::ExecuteNow()
  939. {
  940. return FQueue->ItemExecuteNow(FQueueItem);
  941. }
  942. //---------------------------------------------------------------------------
  943. bool __fastcall TQueueItemProxy::Move(bool Sooner)
  944. {
  945. bool Result = false;
  946. int I = Index;
  947. if (Sooner)
  948. {
  949. if (I > 0)
  950. {
  951. Result = Move(FQueueStatus->Items[I - 1]);
  952. }
  953. }
  954. else
  955. {
  956. if (I < FQueueStatus->Count - 1)
  957. {
  958. Result = FQueueStatus->Items[I + 1]->Move(this);
  959. }
  960. }
  961. return Result;
  962. }
  963. //---------------------------------------------------------------------------
  964. bool __fastcall TQueueItemProxy::Move(TQueueItemProxy * BeforeItem)
  965. {
  966. return FQueue->ItemMove(FQueueItem, BeforeItem->FQueueItem);
  967. }
  968. //---------------------------------------------------------------------------
  969. bool __fastcall TQueueItemProxy::Delete()
  970. {
  971. return FQueue->ItemDelete(FQueueItem);
  972. }
  973. //---------------------------------------------------------------------------
  974. bool __fastcall TQueueItemProxy::ProcessUserAction()
  975. {
  976. assert(FQueueItem != NULL);
  977. bool Result;
  978. FProcessingUserAction = true;
  979. try
  980. {
  981. Result = FQueue->ItemProcessUserAction(FQueueItem);
  982. }
  983. __finally
  984. {
  985. FProcessingUserAction = false;
  986. }
  987. return Result;
  988. }
  989. //---------------------------------------------------------------------------
  990. int __fastcall TQueueItemProxy::GetIndex()
  991. {
  992. assert(FQueueStatus != NULL);
  993. int Index = FQueueStatus->FList->IndexOf(this);
  994. assert(Index >= 0);
  995. return Index;
  996. }
  997. //---------------------------------------------------------------------------
  998. // TTerminalQueueStatus
  999. //---------------------------------------------------------------------------
  1000. __fastcall TTerminalQueueStatus::TTerminalQueueStatus() :
  1001. FList(NULL)
  1002. {
  1003. FList = new TList();
  1004. ResetStats();
  1005. }
  1006. //---------------------------------------------------------------------------
  1007. __fastcall TTerminalQueueStatus::~TTerminalQueueStatus()
  1008. {
  1009. for (int Index = 0; Index < FList->Count; Index++)
  1010. {
  1011. delete GetItem(Index);
  1012. }
  1013. delete FList;
  1014. FList = NULL;
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TTerminalQueueStatus::ResetStats()
  1018. {
  1019. FActiveCount = -1;
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. int __fastcall TTerminalQueueStatus::GetActiveCount()
  1023. {
  1024. if (FActiveCount < 0)
  1025. {
  1026. FActiveCount = 0;
  1027. while ((FActiveCount < FList->Count) &&
  1028. (GetItem(FActiveCount)->Status != TQueueItem::qsPending))
  1029. {
  1030. FActiveCount++;
  1031. }
  1032. }
  1033. return FActiveCount;
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. void __fastcall TTerminalQueueStatus::Add(TQueueItemProxy * ItemProxy)
  1037. {
  1038. ItemProxy->FQueueStatus = this;
  1039. FList->Add(ItemProxy);
  1040. ResetStats();
  1041. }
  1042. //---------------------------------------------------------------------------
  1043. void __fastcall TTerminalQueueStatus::Delete(TQueueItemProxy * ItemProxy)
  1044. {
  1045. FList->Extract(ItemProxy);
  1046. ItemProxy->FQueueStatus = NULL;
  1047. ResetStats();
  1048. }
  1049. //---------------------------------------------------------------------------
  1050. int __fastcall TTerminalQueueStatus::GetCount()
  1051. {
  1052. return FList->Count;
  1053. }
  1054. //---------------------------------------------------------------------------
  1055. TQueueItemProxy * __fastcall TTerminalQueueStatus::GetItem(int Index)
  1056. {
  1057. return reinterpret_cast<TQueueItemProxy *>(FList->Items[Index]);
  1058. }
  1059. //---------------------------------------------------------------------------
  1060. TQueueItemProxy * __fastcall TTerminalQueueStatus::FindByQueueItem(
  1061. TQueueItem * QueueItem)
  1062. {
  1063. TQueueItemProxy * Item;
  1064. for (int Index = 0; Index < FList->Count; Index++)
  1065. {
  1066. Item = GetItem(Index);
  1067. if (Item->FQueueItem == QueueItem)
  1068. {
  1069. return Item;
  1070. }
  1071. }
  1072. return NULL;
  1073. }
  1074. //---------------------------------------------------------------------------
  1075. // TLocatedQueueItem
  1076. //---------------------------------------------------------------------------
  1077. __fastcall TLocatedQueueItem::TLocatedQueueItem(TTerminal * Terminal) :
  1078. TQueueItem()
  1079. {
  1080. assert(Terminal != NULL);
  1081. FCurrentDir = Terminal->CurrentDirectory;
  1082. }
  1083. //---------------------------------------------------------------------------
  1084. AnsiString __fastcall TLocatedQueueItem::StartupDirectory()
  1085. {
  1086. return FCurrentDir;
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. void __fastcall TLocatedQueueItem::DoExecute(TTerminal * Terminal)
  1090. {
  1091. assert(Terminal != NULL);
  1092. Terminal->CurrentDirectory = FCurrentDir;
  1093. }
  1094. //---------------------------------------------------------------------------
  1095. // TTransferQueueItem
  1096. //---------------------------------------------------------------------------
  1097. __fastcall TTransferQueueItem::TTransferQueueItem(TTerminal * Terminal,
  1098. TStrings * FilesToCopy, const AnsiString & TargetDir,
  1099. const TCopyParamType * CopyParam, int Params, TOperationSide Side) :
  1100. TLocatedQueueItem(Terminal), FFilesToCopy(NULL), FCopyParam(NULL)
  1101. {
  1102. FInfo->Operation = (Params & cpDelete ? foMove : foCopy);
  1103. FInfo->Side = Side;
  1104. assert(FilesToCopy != NULL);
  1105. FFilesToCopy = new TStringList();
  1106. for (int Index = 0; Index < FilesToCopy->Count; Index++)
  1107. {
  1108. FFilesToCopy->AddObject(FilesToCopy->Strings[Index],
  1109. ((FilesToCopy->Objects[Index] == NULL) || (Side == osLocal)) ? NULL :
  1110. dynamic_cast<TRemoteFile*>(FilesToCopy->Objects[Index])->Duplicate());
  1111. }
  1112. FTargetDir = TargetDir;
  1113. assert(CopyParam != NULL);
  1114. FCopyParam = new TCopyParamType(*CopyParam);
  1115. FParams = Params;
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. __fastcall TTransferQueueItem::~TTransferQueueItem()
  1119. {
  1120. delete FFilesToCopy;
  1121. delete FCopyParam;
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. // TUploadQueueItem
  1125. //---------------------------------------------------------------------------
  1126. __fastcall TUploadQueueItem::TUploadQueueItem(TTerminal * Terminal,
  1127. TStrings * FilesToCopy, const AnsiString & TargetDir,
  1128. const TCopyParamType * CopyParam, int Params) :
  1129. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osLocal)
  1130. {
  1131. if (FilesToCopy->Count > 1)
  1132. {
  1133. ExtractCommonPath(FilesToCopy, FInfo->Source);
  1134. FInfo->Source = ExcludeTrailingBackslash(FInfo->Source);
  1135. }
  1136. else
  1137. {
  1138. assert(FilesToCopy->Count > 0);
  1139. FInfo->Source = FilesToCopy->Strings[0];
  1140. }
  1141. FInfo->Destination =
  1142. UnixIncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  1143. FInfo->ModifiesLocal = ((Params & cpDelete) != 0);
  1144. FInfo->ModifiesRemote = true;
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TUploadQueueItem::DoExecute(TTerminal * Terminal)
  1148. {
  1149. TTransferQueueItem::DoExecute(Terminal);
  1150. assert(Terminal != NULL);
  1151. Terminal->CopyToRemote(FFilesToCopy, FTargetDir, FCopyParam, FParams);
  1152. }
  1153. //---------------------------------------------------------------------------
  1154. // TDownloadQueueItem
  1155. //---------------------------------------------------------------------------
  1156. __fastcall TDownloadQueueItem::TDownloadQueueItem(TTerminal * Terminal,
  1157. TStrings * FilesToCopy, const AnsiString & TargetDir,
  1158. const TCopyParamType * CopyParam, int Params) :
  1159. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osRemote)
  1160. {
  1161. if (FilesToCopy->Count > 1)
  1162. {
  1163. if (!UnixExtractCommonPath(FilesToCopy, FInfo->Source))
  1164. {
  1165. FInfo->Source = Terminal->CurrentDirectory;
  1166. }
  1167. FInfo->Source = UnixExcludeTrailingBackslash(FInfo->Source);
  1168. }
  1169. else
  1170. {
  1171. assert(FilesToCopy->Count > 0);
  1172. FInfo->Source = FilesToCopy->Strings[0];
  1173. if (UnixExtractFilePath(FInfo->Source).IsEmpty())
  1174. {
  1175. FInfo->Source = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  1176. FInfo->Source;
  1177. }
  1178. }
  1179. FInfo->Destination =
  1180. IncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  1181. FInfo->ModifiesLocal = true;
  1182. FInfo->ModifiesRemote = ((Params & cpDelete) != 0);
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. void __fastcall TDownloadQueueItem::DoExecute(TTerminal * Terminal)
  1186. {
  1187. TTransferQueueItem::DoExecute(Terminal);
  1188. assert(Terminal != NULL);
  1189. Terminal->CopyToLocal(FFilesToCopy, FTargetDir, FCopyParam, FParams);
  1190. }