| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Common.h"
- #include "Terminal.h"
- #include "Queue.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- class TTerminalItem : public TSignalThread
- {
- friend class TQueueItem;
- public:
- __fastcall TTerminalItem(TTerminalQueue * Queue);
- __fastcall ~TTerminalItem();
- void __fastcall Process(TQueueItem * Item);
- bool __fastcall ProcessUserAction(void * Arg);
- void __fastcall Cancel();
- void __fastcall Idle();
- bool __fastcall Pause();
- bool __fastcall Resume();
- protected:
- struct TQueryUserRec
- {
- TObject * Sender;
- AnsiString Query;
- TStrings * MoreMessages;
- int Answers;
- const TQueryParams * Params;
- int Answer;
- TQueryType Type;
- };
- struct TPromptUserRec
- {
- TSecureShell * SecureShell;
- AnsiString Prompt;
- TPromptKind Kind;
- AnsiString Response;
- bool Result;
- };
- struct TShowExtendedExceptionRec
- {
- TSecureShell * SecureShell;
- Exception * E;
- };
- TTerminalQueue * FQueue;
- TTerminal * FTerminal;
- TQueueItem * FItem;
- TCriticalSection * FCriticalSection;
- void * FUserActionParams;
- bool FCancel;
- bool FPause;
- virtual void __fastcall ProcessEvent();
- virtual void __fastcall Finished();
- bool __fastcall WaitForUserAction(TQueueItem::TStatus ItemStatus, void * Params);
- void __fastcall TerminalClose(TObject * Sender);
- void __fastcall TerminalQueryUser(TObject * Sender,
- const AnsiString Query, TStrings * MoreMessages, int Answers,
- const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg);
- void __fastcall TerminalPromptUser(TSecureShell * SecureShell,
- AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result, void * Arg);
- void __fastcall TerminalShowExtendedException(TSecureShell * SecureShell,
- Exception * E, void * Arg);
- void __fastcall OperationFinished(TFileOperation Operation, TOperationSide Side,
- bool Temp, const AnsiString FileName, bool Success,
- bool & DisconnectWhenFinished);
- void __fastcall OperationProgress(TFileOperationProgressType & ProgressData,
- TCancelStatus & Cancel);
- };
- //---------------------------------------------------------------------------
- // TSignalThread
- //---------------------------------------------------------------------------
- int __fastcall ThreadProc(void * Thread)
- {
- TSimpleThread * SimpleThread = reinterpret_cast<TSimpleThread*>(Thread);
- assert(SimpleThread != NULL);
- try
- {
- SimpleThread->Execute();
- }
- __finally
- {
- SimpleThread->FFinished = true;
- SimpleThread->Finished();
- EndThread(0);
- }
- return 0;
- }
- //---------------------------------------------------------------------------
- __fastcall TSimpleThread::TSimpleThread() :
- FThread(NULL), FFinished(true)
- {
- unsigned ThreadID;
- FThread = reinterpret_cast<HANDLE>(
- BeginThread(NULL, 0, ThreadProc, this, CREATE_SUSPENDED, ThreadID));
- }
- //---------------------------------------------------------------------------
- __fastcall TSimpleThread::~TSimpleThread()
- {
- Close();
- if (FThread != NULL)
- {
- CloseHandle(FThread);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSimpleThread::Start()
- {
- if (ResumeThread(FThread) == 1)
- {
- FFinished = false;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSimpleThread::Finished()
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TSimpleThread::Close()
- {
- if (!FFinished)
- {
- Terminate();
- WaitFor();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSimpleThread::WaitFor()
- {
- WaitForSingleObject(FThread, INFINITE);
- }
- //---------------------------------------------------------------------------
- // TSignalThread
- //---------------------------------------------------------------------------
- __fastcall TSignalThread::TSignalThread() :
- TSimpleThread(),
- FTerminated(true), FEvent(NULL)
- {
- FEvent = CreateEvent(NULL, false, false, NULL);
- assert(FEvent != NULL);
- ::SetThreadPriority(FThread, THREAD_PRIORITY_BELOW_NORMAL);
- }
- //---------------------------------------------------------------------------
- __fastcall TSignalThread::~TSignalThread()
- {
- // cannot leave closing to TSimpleThread as we need to close it before
- // destroying the event
- Close();
- if (FEvent)
- {
- CloseHandle(FEvent);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSignalThread::Start()
- {
- FTerminated = false;
- TSimpleThread::Start();
- }
- //---------------------------------------------------------------------------
- void __fastcall TSignalThread::TriggerEvent()
- {
- SetEvent(FEvent);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSignalThread::WaitForEvent()
- {
- return (WaitForSingleObject(FEvent, INFINITE) == WAIT_OBJECT_0) &&
- !FTerminated;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSignalThread::Execute()
- {
- while (!FTerminated)
- {
- if (WaitForEvent())
- {
- ProcessEvent();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSignalThread::Terminate()
- {
- FTerminated = true;
- TriggerEvent();
- }
- //---------------------------------------------------------------------------
- // TTerminalQueue
- //---------------------------------------------------------------------------
- __fastcall TTerminalQueue::TTerminalQueue(TTerminal * Terminal,
- TConfiguration * Configuration) :
- FTerminal(Terminal), FTransfersLimit(2),
- FConfiguration(Configuration), FSessionData(NULL), FItems(NULL),
- FTerminals(NULL), FItemsSection(NULL), FFreeTerminals(0),
- FItemsInProcess(0), FTemporaryTerminals(0)
- {
- FOnQueryUser = NULL;
- FOnPromptUser = NULL;
- FOnShowExtendedException = NULL;
- FOnQueueItemUpdate = NULL;
- FOnListUpdate = NULL;
- FLastIdle = Now();
- FIdleInterval = EncodeTime(0, 0, 2, 0);
- assert(Terminal != NULL);
- FSessionData = new TSessionData("");
- FSessionData->Assign(Terminal->SessionData);
- FItems = new TList();
- FTerminals = new TList();
- FItemsSection = new TCriticalSection();
- Start();
- }
- //---------------------------------------------------------------------------
- __fastcall TTerminalQueue::~TTerminalQueue()
- {
- Close();
- {
- TGuard Guard(FItemsSection);
- TTerminalItem * TerminalItem;
- while (FTerminals->Count > 0)
- {
- TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
- FTerminals->Delete(0);
- TerminalItem->Terminate();
- TerminalItem->WaitFor();
- delete TerminalItem;
- }
- delete FTerminals;
- for (int Index = 0; Index < FItems->Count; Index++)
- {
- delete GetItem(Index);
- }
- }
- delete FItemsSection;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::TerminalFinished(TTerminalItem * TerminalItem)
- {
- if (!FTerminated)
- {
- {
- TGuard Guard(FItemsSection);
- int Index = FTerminals->IndexOf(TerminalItem);
- assert(Index >= 0);
- if (Index < FFreeTerminals)
- {
- FFreeTerminals--;
- }
- // Index may be >= FTransfersLimit also when the transfer limit was
- // recently decresed, then
- // FTemporaryTerminals < FTerminals->Count - FTransfersLimit
- if ((FTransfersLimit > 0) && (Index >= FTransfersLimit) && (FTemporaryTerminals > 0))
- {
- FTemporaryTerminals--;
- }
- FTerminals->Extract(TerminalItem);
- delete TerminalItem;
- }
- TriggerEvent();
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::TerminalFree(TTerminalItem * TerminalItem)
- {
- bool Result = true;
-
- if (!FTerminated)
- {
- {
- TGuard Guard(FItemsSection);
- int Index = FTerminals->IndexOf(TerminalItem);
- assert(Index >= 0);
- assert(Index >= FFreeTerminals);
- Result = (FTransfersLimit <= 0) || (Index < FTransfersLimit);
- if (Result)
- {
- FTerminals->Move(Index, 0);
- FFreeTerminals++;
- }
- }
- TriggerEvent();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::AddItem(TQueueItem * Item)
- {
- assert(!FTerminated);
- Item->SetStatus(TQueueItem::qsPending);
- {
- TGuard Guard(FItemsSection);
- FItems->Add(Item);
- Item->FQueue = this;
- }
- DoListUpdate();
- TriggerEvent();
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::RetryItem(TQueueItem * Item)
- {
- if (!FTerminated)
- {
- {
- TGuard Guard(FItemsSection);
- int Index = FItems->Remove(Item);
- assert(Index < FItemsInProcess);
- USEDPARAM(Index);
- FItemsInProcess--;
- FItems->Add(Item);
- }
- DoListUpdate();
- TriggerEvent();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::DeleteItem(TQueueItem * Item)
- {
- if (!FTerminated)
- {
- {
- TGuard Guard(FItemsSection);
- int Index = FItems->Remove(Item);
- assert(Index < FItemsInProcess);
- USEDPARAM(Index);
- FItemsInProcess--;
- delete Item;
- }
- DoListUpdate();
- }
- }
- //---------------------------------------------------------------------------
- TQueueItem * __fastcall TTerminalQueue::GetItem(int Index)
- {
- return reinterpret_cast<TQueueItem*>(FItems->Items[Index]);
- }
- //---------------------------------------------------------------------------
- TTerminalQueueStatus * __fastcall TTerminalQueue::CreateStatus(TTerminalQueueStatus * Current)
- {
- TTerminalQueueStatus * Status = new TTerminalQueueStatus();
- try
- {
- try
- {
- TGuard Guard(FItemsSection);
- TQueueItem * Item;
- TQueueItemProxy * ItemProxy;
- for (int Index = 0; Index < FItems->Count; Index++)
- {
- Item = GetItem(Index);
- if (Current != NULL)
- {
- ItemProxy = Current->FindByQueueItem(Item);
- }
- else
- {
- ItemProxy = NULL;
- }
- if (ItemProxy != NULL)
- {
- Current->Delete(ItemProxy);
- Status->Add(ItemProxy);
- ItemProxy->Update();
- }
- else
- {
- Status->Add(new TQueueItemProxy(this, Item));
- }
- }
- }
- __finally
- {
- if (Current != NULL)
- {
- delete Current;
- }
- }
- }
- catch(...)
- {
- delete Status;
- throw;
- }
- return Status;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::ItemGetData(TQueueItem * Item,
- TQueueItemProxy * Proxy)
- {
- // to prevent deadlocks when closing queue from other thread
- bool Result = !FFinished;
- if (Result)
- {
- TGuard Guard(FItemsSection);
- Result = (FItems->IndexOf(Item) >= 0);
- if (Result)
- {
- Item->GetData(Proxy);
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::ItemProcessUserAction(TQueueItem * Item, void * Arg)
- {
- // to prevent deadlocks when closing queue from other thread
- bool Result = !FFinished;
- if (Result)
- {
- TTerminalItem * TerminalItem;
- {
- TGuard Guard(FItemsSection);
- Result = (FItems->IndexOf(Item) >= 0) &&
- TQueueItem::IsUserActionStatus(Item->Status);
- if (Result)
- {
- TerminalItem = Item->FTerminalItem;
- }
- }
- if (Result)
- {
- Result = TerminalItem->ProcessUserAction(Arg);
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::ItemMove(TQueueItem * Item, TQueueItem * BeforeItem)
- {
- // to prevent deadlocks when closing queue from other thread
- bool Result = !FFinished;
- if (Result)
- {
- {
- TGuard Guard(FItemsSection);
- int Index = FItems->IndexOf(Item);
- int IndexDest = FItems->IndexOf(BeforeItem);
- Result = (Index >= 0) && (IndexDest >= 0) &&
- (Item->GetStatus() == TQueueItem::qsPending) &&
- (BeforeItem->GetStatus() == TQueueItem::qsPending);
- if (Result)
- {
- FItems->Move(Index, IndexDest);
- }
- }
- if (Result)
- {
- DoListUpdate();
- TriggerEvent();
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::ItemExecuteNow(TQueueItem * Item)
- {
- // to prevent deadlocks when closing queue from other thread
- bool Result = !FFinished;
- if (Result)
- {
- {
- TGuard Guard(FItemsSection);
- int Index = FItems->IndexOf(Item);
- Result = (Index >= 0) && (Item->GetStatus() == TQueueItem::qsPending) &&
- // prevent double-initiation when "execute" is clicked twice too fast
- (Index >= FItemsInProcess);
- if (Result)
- {
- if (Index > FItemsInProcess)
- {
- FItems->Move(Index, FItemsInProcess);
- }
- if ((FTransfersLimit > 0) && (FTerminals->Count >= FTransfersLimit))
- {
- FTemporaryTerminals++;
- }
- }
- }
- if (Result)
- {
- DoListUpdate();
- TriggerEvent();
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::ItemDelete(TQueueItem * Item)
- {
- // to prevent deadlocks when closing queue from other thread
- bool Result = !FFinished;
- if (Result)
- {
- bool UpdateList = false;
- {
- TGuard Guard(FItemsSection);
- int Index = FItems->IndexOf(Item);
- Result = (Index >= 0);
- if (Result)
- {
- if (Item->Status == TQueueItem::qsPending)
- {
- FItems->Delete(Index);
- UpdateList = true;
- }
- else
- {
- Item->FTerminalItem->Cancel();
- }
- }
- }
- if (UpdateList)
- {
- DoListUpdate();
- TriggerEvent();
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::ItemPause(TQueueItem * Item, bool Pause)
- {
- // to prevent deadlocks when closing queue from other thread
- bool Result = !FFinished;
- if (Result)
- {
- TTerminalItem * TerminalItem;
- {
- TGuard Guard(FItemsSection);
- Result = (FItems->IndexOf(Item) >= 0) &&
- ((Pause && (Item->Status == TQueueItem::qsProcessing)) ||
- (!Pause && (Item->Status == TQueueItem::qsPaused)));
- if (Result)
- {
- TerminalItem = Item->FTerminalItem;
- }
- }
- if (Result)
- {
- if (Pause)
- {
- Result = TerminalItem->Pause();
- }
- else
- {
- Result = TerminalItem->Resume();
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::Idle()
- {
- if (Now() - FLastIdle > FIdleInterval)
- {
- FLastIdle = FIdleInterval;
- TTerminalItem * TerminalItem = NULL;
- if (FFreeTerminals > 0)
- {
- TGuard Guard(FItemsSection);
- if (FFreeTerminals > 0)
- {
- // take the last free terminal, because TerminalFree() puts it to the
- // front, this ensures we cycle thru all free terminals
- TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[FFreeTerminals - 1]);
- FTerminals->Move(FFreeTerminals - 1, FTerminals->Count - 1);
- FFreeTerminals--;
- }
- }
- if (TerminalItem != NULL)
- {
- TerminalItem->Idle();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::ProcessEvent()
- {
- TTerminalItem * TerminalItem;
- TQueueItem * Item;
- do
- {
- TerminalItem = NULL;
- Item = NULL;
- if (FItems->Count > FItemsInProcess)
- {
- TGuard Guard(FItemsSection);
- if ((FFreeTerminals == 0) &&
- ((FTransfersLimit <= 0) ||
- (FTerminals->Count < FTransfersLimit + FTemporaryTerminals)))
- {
- TerminalItem = new TTerminalItem(this);
- FTerminals->Add(TerminalItem);
- }
- else if (FFreeTerminals > 0)
- {
- TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
- FTerminals->Move(0, FTerminals->Count - 1);
- FFreeTerminals--;
- }
- if (TerminalItem != NULL)
- {
- Item = GetItem(FItemsInProcess);
- FItemsInProcess++;
- }
- }
- if (TerminalItem != NULL)
- {
- TerminalItem->Process(Item);
- }
- }
- while (!FTerminated && (TerminalItem != NULL));
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::DoQueueItemUpdate(TQueueItem * Item)
- {
- if (OnQueueItemUpdate != NULL)
- {
- OnQueueItemUpdate(this, Item);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::DoListUpdate()
- {
- if (OnListUpdate != NULL)
- {
- OnListUpdate(this);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::DoQueryUser(TObject * Sender,
- const AnsiString Query, TStrings * MoreMessages, int Answers,
- const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg)
- {
- if (OnQueryUser != NULL)
- {
- OnQueryUser(Sender, Query, MoreMessages, Answers, Params, Answer, Type, Arg);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::DoPromptUser(TSecureShell * SecureShell,
- AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result, void * Arg)
- {
- if (OnPromptUser != NULL)
- {
- OnPromptUser(SecureShell, Prompt, Kind, Response, Result, Arg);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::DoShowExtendedException(
- TSecureShell * SecureShell, Exception * E, void * Arg)
- {
- if (OnShowExtendedException != NULL)
- {
- OnShowExtendedException(SecureShell, E, Arg);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::SetTransfersLimit(int value)
- {
- if (FTransfersLimit != value)
- {
- {
- TGuard Guard(FItemsSection);
- if ((value > 0) && (value < FItemsInProcess))
- {
- FTemporaryTerminals = (FItemsInProcess - value);
- }
- else
- {
- FTemporaryTerminals = 0;
- }
- FTransfersLimit = value;
- }
- TriggerEvent();
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::GetIsEmpty()
- {
- TGuard Guard(FItemsSection);
- return (FItems->Count == 0);
- }
- //---------------------------------------------------------------------------
- // TTerminalItem
- //---------------------------------------------------------------------------
- __fastcall TTerminalItem::TTerminalItem(TTerminalQueue * Queue) :
- TSignalThread(), FQueue(Queue), FTerminal(NULL), FItem(NULL),
- FCriticalSection(NULL), FUserActionParams(NULL)
- {
- FCriticalSection = new TCriticalSection();
- FTerminal = new TSecondaryTerminal(FQueue->FTerminal);
- try
- {
- FTerminal->UseBusyCursor = false;
-
- FTerminal->Configuration = Queue->FConfiguration;
- FTerminal->SessionData = Queue->FSessionData;
- FTerminal->OnClose = TerminalClose;
- FTerminal->OnQueryUser = TerminalQueryUser;
- FTerminal->OnPromptUser = TerminalPromptUser;
- FTerminal->OnShowExtendedException = TerminalShowExtendedException;
- FTerminal->OnProgress = OperationProgress;
- FTerminal->OnFinished = OperationFinished;
- }
- catch(...)
- {
- delete FTerminal;
- throw;
- }
- Start();
- }
- //---------------------------------------------------------------------------
- __fastcall TTerminalItem::~TTerminalItem()
- {
- Close();
- assert(FItem == NULL);
- delete FTerminal;
- delete FCriticalSection;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::Process(TQueueItem * Item)
- {
- {
- TGuard Guard(FCriticalSection);
- assert(FItem == NULL);
- FItem = Item;
- }
- TriggerEvent();
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::ProcessEvent()
- {
- TGuard Guard(FCriticalSection);
- bool Retry = true;
- FCancel = false;
- FPause = false;
- FItem->FTerminalItem = this;
- try
- {
- assert(FItem != NULL);
- if (!FTerminal->Active)
- {
- FItem->SetStatus(TQueueItem::qsConnecting);
- FTerminal->SessionData->RemoteDirectory = FItem->StartupDirectory();
- FTerminal->Open();
- FTerminal->DoStartup();
- }
- Retry = false;
- if (!FCancel)
- {
- FItem->SetStatus(TQueueItem::qsProcessing);
- FItem->Execute(this);
- }
- }
- catch(Exception & E)
- {
- FTerminal->DoShowExtendedException(&E);
- }
- FItem->SetStatus(TQueueItem::qsDone);
-
- FItem->FTerminalItem = NULL;
- TQueueItem * Item = FItem;
- FItem = NULL;
- if (Retry && !FCancel)
- {
- FQueue->RetryItem(Item);
- }
- else
- {
- FQueue->DeleteItem(Item);
- }
- if (!FTerminal->Active ||
- !FQueue->TerminalFree(this))
- {
- Terminate();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::Idle()
- {
- TGuard Guard(FCriticalSection);
- assert(FTerminal->Active);
- try
- {
- FTerminal->Idle();
- }
- catch(...)
- {
- }
- if (!FTerminal->Active ||
- !FQueue->TerminalFree(this))
- {
- Terminate();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::Cancel()
- {
- FCancel = true;
- if (FItem->GetStatus() == TQueueItem::qsPaused)
- {
- TriggerEvent();
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalItem::Pause()
- {
- assert(FItem != NULL);
- bool Result = (FItem->GetStatus() == TQueueItem::qsProcessing) && !FPause;
- if (Result)
- {
- FPause = true;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalItem::Resume()
- {
- assert(FItem != NULL);
- bool Result = (FItem->GetStatus() == TQueueItem::qsPaused);
- if (Result)
- {
- TriggerEvent();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalItem::ProcessUserAction(void * Arg)
- {
- // When status is changed twice quickly, the controller when responding
- // to the first change (non-user-action) can be so slow to check only after
- // the second (user-action) change occurs. Thus it responds it.
- // Then as reaction to the second (user-action) change there will not be
- // any outstanding user-action.
- bool Result = (FUserActionParams != NULL);
- if (Result)
- {
- assert(FItem != NULL);
- if (FItem->GetStatus() == TQueueItem::qsQuery)
- {
- TQueryUserRec * Params;
- Params = reinterpret_cast<TQueryUserRec *>(FUserActionParams);
- FQueue->DoQueryUser(Params->Sender, Params->Query, Params->MoreMessages,
- Params->Answers, Params->Params, Params->Answer, Params->Type, Arg);
- }
- else if (FItem->GetStatus() == TQueueItem::qsPrompt)
- {
- TPromptUserRec * Params;
- Params = reinterpret_cast<TPromptUserRec *>(FUserActionParams);
- FQueue->DoPromptUser(Params->SecureShell, Params->Prompt,
- Params->Kind, Params->Response, Params->Result, Arg);
- }
- else if (FItem->GetStatus() == TQueueItem::qsError)
- {
- TShowExtendedExceptionRec * Params;
- Params = reinterpret_cast<TShowExtendedExceptionRec *>(FUserActionParams);
- FQueue->DoShowExtendedException(Params->SecureShell, Params->E, Arg);
- }
- else
- {
- assert(false);
- }
- FUserActionParams = NULL;
- TriggerEvent();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalItem::WaitForUserAction(
- TQueueItem::TStatus ItemStatus, void * Params)
- {
- assert(FItem != NULL);
- assert((FItem->GetStatus() == TQueueItem::qsProcessing) ||
- (FItem->GetStatus() == TQueueItem::qsConnecting));
- bool Result;
- TQueueItem::TStatus PrevStatus = FItem->GetStatus();
- try
- {
- FUserActionParams = Params;
- FItem->SetStatus(ItemStatus);
- Result = !FTerminated && WaitForEvent() && !FCancel;
- }
- __finally
- {
- FUserActionParams = NULL;
- FItem->SetStatus(PrevStatus);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::Finished()
- {
- TSignalThread::Finished();
- FQueue->TerminalFinished(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::TerminalClose(TObject * /*Sender*/)
- {
- // nothing
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::TerminalQueryUser(TObject * Sender,
- const AnsiString Query, TStrings * MoreMessages, int Answers,
- const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg)
- {
- USEDPARAM(Arg);
- assert(Arg == NULL);
- TQueryUserRec QueryUserRec;
- QueryUserRec.Sender = Sender;
- QueryUserRec.Query = Query;
- QueryUserRec.MoreMessages = MoreMessages;
- QueryUserRec.Answers = Answers;
- QueryUserRec.Params = Params;
- QueryUserRec.Answer = Answer;
- QueryUserRec.Type = Type;
- if (WaitForUserAction(TQueueItem::qsQuery, &QueryUserRec))
- {
- Answer = QueryUserRec.Answer;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::TerminalPromptUser(TSecureShell * SecureShell,
- AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result,
- void * Arg)
- {
- USEDPARAM(Arg);
- assert(Arg == NULL);
- TPromptUserRec PromptUserRec;
- PromptUserRec.SecureShell = SecureShell;
- PromptUserRec.Prompt = Prompt;
- PromptUserRec.Kind = Kind;
- PromptUserRec.Response = Response.c_str();
- PromptUserRec.Result = Result;
- if (WaitForUserAction(TQueueItem::qsPrompt, &PromptUserRec))
- {
- Response = PromptUserRec.Response.c_str();
- Result = PromptUserRec.Result;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::TerminalShowExtendedException(
- TSecureShell * SecureShell, Exception * E, void * Arg)
- {
- USEDPARAM(Arg);
- assert(Arg == NULL);
- if (!E->Message.IsEmpty() &&
- (dynamic_cast<EAbort*>(E) == NULL))
- {
- TShowExtendedExceptionRec ShowExtendedExceptionRec;
- ShowExtendedExceptionRec.SecureShell = SecureShell;
- ShowExtendedExceptionRec.E = E;
- WaitForUserAction(TQueueItem::qsError, &ShowExtendedExceptionRec);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::OperationFinished(TFileOperation /*Operation*/,
- TOperationSide /*Side*/, bool /*Temp*/, const AnsiString /*FileName*/,
- bool /*Success*/, bool & /*DisconnectWhenFinished*/)
- {
- // nothing
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::OperationProgress(
- TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
- {
- if (FPause && !FTerminated && !FCancel)
- {
- TQueueItem::TStatus PrevStatus = FItem->GetStatus();
- assert(PrevStatus == TQueueItem::qsProcessing);
- // must be set before TFileOperationProgressType::Suspend(), because
- // it invokes this method back
- FPause = false;
- ProgressData.Suspend();
- try
- {
- FItem->SetStatus(TQueueItem::qsPaused);
- WaitForEvent();
- }
- __finally
- {
- FItem->SetStatus(PrevStatus);
- ProgressData.Resume();
- }
- }
- if (FTerminated || FCancel)
- {
- if (ProgressData.TransferingFile)
- {
- Cancel = csCancelTransfer;
- }
- else
- {
- Cancel = csCancel;
- }
- }
- assert(FItem != NULL);
- FItem->SetProgress(ProgressData);
- }
- //---------------------------------------------------------------------------
- // TQueueItem
- //---------------------------------------------------------------------------
- __fastcall TQueueItem::TQueueItem() :
- FStatus(qsPending), FTerminalItem(NULL), FSection(NULL), FProgressData(NULL),
- FQueue(NULL), FInfo(NULL), FCompleteEvent(INVALID_HANDLE_VALUE)
- {
- FSection = new TCriticalSection();
- FInfo = new TInfo();
- }
- //---------------------------------------------------------------------------
- __fastcall TQueueItem::~TQueueItem()
- {
- if (FCompleteEvent != INVALID_HANDLE_VALUE)
- {
- SetEvent(FCompleteEvent);
- }
- delete FSection;
- delete FInfo;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItem::IsUserActionStatus(TStatus Status)
- {
- return (Status == qsQuery) || (Status == qsError) || (Status == qsPrompt);
- }
- //---------------------------------------------------------------------------
- TQueueItem::TStatus __fastcall TQueueItem::GetStatus()
- {
- TGuard Guard(FSection);
- return FStatus;
- }
- //---------------------------------------------------------------------------
- void __fastcall TQueueItem::SetStatus(TStatus Status)
- {
- {
- TGuard Guard(FSection);
- FStatus = Status;
- }
- assert((FQueue != NULL) || (Status == qsPending));
- if (FQueue != NULL)
- {
- FQueue->DoQueueItemUpdate(this);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TQueueItem::SetProgress(
- const TFileOperationProgressType & ProgressData)
- {
- {
- TGuard Guard(FSection);
- assert(FProgressData != NULL);
- *FProgressData = ProgressData;
- FProgressData->Reset();
- }
- FQueue->DoQueueItemUpdate(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TQueueItem::GetData(TQueueItemProxy * Proxy)
- {
- TGuard Guard(FSection);
- assert(Proxy->FProgressData != NULL);
- if (FProgressData != NULL)
- {
- *Proxy->FProgressData = *FProgressData;
- }
- else
- {
- Proxy->FProgressData->Clear();
- }
- *Proxy->FInfo = *FInfo;
- Proxy->FStatus = FStatus;
- }
- //---------------------------------------------------------------------------
- void __fastcall TQueueItem::Execute(TTerminalItem * TerminalItem)
- {
- try
- {
- {
- assert(FProgressData == NULL);
- TGuard Guard(FSection);
- FProgressData = new TFileOperationProgressType();
- }
- DoExecute(TerminalItem->FTerminal);
- }
- __finally
- {
- {
- TGuard Guard(FSection);
- delete FProgressData;
- FProgressData = NULL;
- }
- }
- }
- //---------------------------------------------------------------------------
- // TQueueItemProxy
- //---------------------------------------------------------------------------
- __fastcall TQueueItemProxy::TQueueItemProxy(TTerminalQueue * Queue,
- TQueueItem * QueueItem) :
- FQueue(Queue), FQueueItem(QueueItem), FProgressData(NULL),
- FQueueStatus(NULL), FInfo(NULL),
- FProcessingUserAction(false), FUserData(NULL)
- {
- FProgressData = new TFileOperationProgressType();
- FInfo = new TQueueItem::TInfo();
- Update();
- }
- //---------------------------------------------------------------------------
- __fastcall TQueueItemProxy::~TQueueItemProxy()
- {
- delete FProgressData;
- delete FInfo;
- }
- //---------------------------------------------------------------------------
- TFileOperationProgressType * __fastcall TQueueItemProxy::GetProgressData()
- {
- return (FProgressData->Operation == foNone) ? NULL : FProgressData;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::Update()
- {
- assert(FQueueItem != NULL);
- TQueueItem::TStatus PrevStatus = Status;
- bool Result = FQueue->ItemGetData(FQueueItem, this);
- if ((FQueueStatus != NULL) && (PrevStatus != Status))
- {
- FQueueStatus->ResetStats();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::ExecuteNow()
- {
- return FQueue->ItemExecuteNow(FQueueItem);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::Move(bool Sooner)
- {
- bool Result = false;
- int I = Index;
- if (Sooner)
- {
- if (I > 0)
- {
- Result = Move(FQueueStatus->Items[I - 1]);
- }
- }
- else
- {
- if (I < FQueueStatus->Count - 1)
- {
- Result = FQueueStatus->Items[I + 1]->Move(this);
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::Move(TQueueItemProxy * BeforeItem)
- {
- return FQueue->ItemMove(FQueueItem, BeforeItem->FQueueItem);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::Delete()
- {
- return FQueue->ItemDelete(FQueueItem);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::Pause()
- {
- return FQueue->ItemPause(FQueueItem, true);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::Resume()
- {
- return FQueue->ItemPause(FQueueItem, false);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::ProcessUserAction(void * Arg)
- {
- assert(FQueueItem != NULL);
- bool Result;
- FProcessingUserAction = true;
- try
- {
- Result = FQueue->ItemProcessUserAction(FQueueItem, Arg);
- }
- __finally
- {
- FProcessingUserAction = false;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall TQueueItemProxy::GetIndex()
- {
- assert(FQueueStatus != NULL);
- int Index = FQueueStatus->FList->IndexOf(this);
- assert(Index >= 0);
- return Index;
- }
- //---------------------------------------------------------------------------
- // TTerminalQueueStatus
- //---------------------------------------------------------------------------
- __fastcall TTerminalQueueStatus::TTerminalQueueStatus() :
- FList(NULL)
- {
- FList = new TList();
- ResetStats();
- }
- //---------------------------------------------------------------------------
- __fastcall TTerminalQueueStatus::~TTerminalQueueStatus()
- {
- for (int Index = 0; Index < FList->Count; Index++)
- {
- delete GetItem(Index);
- }
- delete FList;
- FList = NULL;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueueStatus::ResetStats()
- {
- FActiveCount = -1;
- }
- //---------------------------------------------------------------------------
- int __fastcall TTerminalQueueStatus::GetActiveCount()
- {
- if (FActiveCount < 0)
- {
- FActiveCount = 0;
- while ((FActiveCount < FList->Count) &&
- (GetItem(FActiveCount)->Status != TQueueItem::qsPending))
- {
- FActiveCount++;
- }
- }
- return FActiveCount;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueueStatus::Add(TQueueItemProxy * ItemProxy)
- {
- ItemProxy->FQueueStatus = this;
- FList->Add(ItemProxy);
- ResetStats();
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueueStatus::Delete(TQueueItemProxy * ItemProxy)
- {
- FList->Extract(ItemProxy);
- ItemProxy->FQueueStatus = NULL;
- ResetStats();
- }
- //---------------------------------------------------------------------------
- int __fastcall TTerminalQueueStatus::GetCount()
- {
- return FList->Count;
- }
- //---------------------------------------------------------------------------
- TQueueItemProxy * __fastcall TTerminalQueueStatus::GetItem(int Index)
- {
- return reinterpret_cast<TQueueItemProxy *>(FList->Items[Index]);
- }
- //---------------------------------------------------------------------------
- TQueueItemProxy * __fastcall TTerminalQueueStatus::FindByQueueItem(
- TQueueItem * QueueItem)
- {
- TQueueItemProxy * Item;
- for (int Index = 0; Index < FList->Count; Index++)
- {
- Item = GetItem(Index);
- if (Item->FQueueItem == QueueItem)
- {
- return Item;
- }
- }
- return NULL;
- }
- //---------------------------------------------------------------------------
- // TLocatedQueueItem
- //---------------------------------------------------------------------------
- __fastcall TLocatedQueueItem::TLocatedQueueItem(TTerminal * Terminal) :
- TQueueItem()
- {
- assert(Terminal != NULL);
- FCurrentDir = Terminal->CurrentDirectory;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TLocatedQueueItem::StartupDirectory()
- {
- return FCurrentDir;
- }
- //---------------------------------------------------------------------------
- void __fastcall TLocatedQueueItem::DoExecute(TTerminal * Terminal)
- {
- assert(Terminal != NULL);
- Terminal->CurrentDirectory = FCurrentDir;
- }
- //---------------------------------------------------------------------------
- // TTransferQueueItem
- //---------------------------------------------------------------------------
- __fastcall TTransferQueueItem::TTransferQueueItem(TTerminal * Terminal,
- TStrings * FilesToCopy, const AnsiString & TargetDir,
- const TCopyParamType * CopyParam, int Params, TOperationSide Side) :
- TLocatedQueueItem(Terminal), FFilesToCopy(NULL), FCopyParam(NULL)
- {
- FInfo->Operation = (Params & cpDelete ? foMove : foCopy);
- FInfo->Side = Side;
- assert(FilesToCopy != NULL);
- FFilesToCopy = new TStringList();
- for (int Index = 0; Index < FilesToCopy->Count; Index++)
- {
- FFilesToCopy->AddObject(FilesToCopy->Strings[Index],
- ((FilesToCopy->Objects[Index] == NULL) || (Side == osLocal)) ? NULL :
- dynamic_cast<TRemoteFile*>(FilesToCopy->Objects[Index])->Duplicate());
- }
- FTargetDir = TargetDir;
- assert(CopyParam != NULL);
- FCopyParam = new TCopyParamType(*CopyParam);
- FParams = Params;
- }
- //---------------------------------------------------------------------------
- __fastcall TTransferQueueItem::~TTransferQueueItem()
- {
- delete FFilesToCopy;
- delete FCopyParam;
- }
- //---------------------------------------------------------------------------
- // TUploadQueueItem
- //---------------------------------------------------------------------------
- __fastcall TUploadQueueItem::TUploadQueueItem(TTerminal * Terminal,
- TStrings * FilesToCopy, const AnsiString & TargetDir,
- const TCopyParamType * CopyParam, int Params) :
- TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osLocal)
- {
- if (FilesToCopy->Count > 1)
- {
- if (FLAGSET(Params, cpTemporary))
- {
- FInfo->Source = "";
- FInfo->ModifiedLocal = "";
- }
- else
- {
- ExtractCommonPath(FilesToCopy, FInfo->Source);
- // this way the trailing backslash is preserved for root directories like D:\\
- FInfo->Source = ExtractFileDir(IncludeTrailingBackslash(FInfo->Source));
- FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
- IncludeTrailingBackslash(FInfo->Source);
- }
- }
- else
- {
- if (FLAGSET(Params, cpTemporary))
- {
- FInfo->Source = ExtractFileName(FilesToCopy->Strings[0]);
- FInfo->ModifiedLocal = "";
- }
- else
- {
- assert(FilesToCopy->Count > 0);
- FInfo->Source = FilesToCopy->Strings[0];
- FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
- IncludeTrailingBackslash(ExtractFilePath(FInfo->Source));
- }
- }
- FInfo->Destination =
- UnixIncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
- FInfo->ModifiedRemote = UnixIncludeTrailingBackslash(TargetDir);
- }
- //---------------------------------------------------------------------------
- void __fastcall TUploadQueueItem::DoExecute(TTerminal * Terminal)
- {
- TTransferQueueItem::DoExecute(Terminal);
- assert(Terminal != NULL);
- Terminal->CopyToRemote(FFilesToCopy, FTargetDir, FCopyParam, FParams);
- }
- //---------------------------------------------------------------------------
- // TDownloadQueueItem
- //---------------------------------------------------------------------------
- __fastcall TDownloadQueueItem::TDownloadQueueItem(TTerminal * Terminal,
- TStrings * FilesToCopy, const AnsiString & TargetDir,
- const TCopyParamType * CopyParam, int Params) :
- TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osRemote)
- {
- if (FilesToCopy->Count > 1)
- {
- if (!UnixExtractCommonPath(FilesToCopy, FInfo->Source))
- {
- FInfo->Source = Terminal->CurrentDirectory;
- }
- FInfo->Source = UnixExcludeTrailingBackslash(FInfo->Source);
- FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
- UnixIncludeTrailingBackslash(FInfo->Source);
- }
- else
- {
- assert(FilesToCopy->Count > 0);
- FInfo->Source = FilesToCopy->Strings[0];
- if (UnixExtractFilePath(FInfo->Source).IsEmpty())
- {
- FInfo->Source = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
- FInfo->Source;
- FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
- UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
- }
- else
- {
- FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
- UnixExtractFilePath(FInfo->Source);
- }
- }
- if (FLAGSET(Params, cpTemporary))
- {
- FInfo->Destination = "";
- }
- else
- {
- FInfo->Destination =
- IncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
- }
- FInfo->ModifiedLocal = IncludeTrailingBackslash(TargetDir);
- }
- //---------------------------------------------------------------------------
- void __fastcall TDownloadQueueItem::DoExecute(TTerminal * Terminal)
- {
- TTransferQueueItem::DoExecute(Terminal);
- assert(Terminal != NULL);
- Terminal->CopyToLocal(FFilesToCopy, FTargetDir, FCopyParam, FParams);
- }
|