🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #2040

Re: Indy 9 TIdFTP Client - when is a Put() finished?

#2040
From: Lesley Anne
Date: Thu, 24 Aug 2006 22:59
129 lines
5799 bytes
Well, I've got my IdFTP client moved off to its own thread.  You mentioned 
earlier about Indy not being an event-driven library, but it does include 
some events and I'm still a little confused on when, whether, and how to use 
them.

It looks as though the Connect() method should include logging in to the 
server so long as I don't override its default value for AAutoLogin, but 
then there's also the component's OnAfterClientLogin and OnStatus events. 
Do I need to look for the occurance of those event triggers, or can I trust 
that if Connect() returned without throwing any exceptions then I should be 
ready to send files?

If I do need to wait for events, I'm not really sure how to do that within a 
secondary thread.  When I'm working in the application's main thread, if I 
need a function to wait for an event to trigger before continuing, I use 
something of the form:
     while (condition)  Application->ProcessMessages;
and then make the event handler set the condition.  I'm not sure what the 
equivalent is in a secondary thread, though.  I don't want it to deal with 
the application's main message queue, but with its own message queue. 
(Though if the answer to my first question was that I don't need to deal 
with the events, that would make this issue irrelevant.)

Well, I'm still not sure whether I have the solution structured correctly, 
but this is what my thread code looks like so far:


// ------------------------------------------------------------------
__fastcall TFTPThread::TFTPThread(const AnsiString &pFileNameMask,
                                  const AnsiString &pFinishedDir,
                                  const AnsiString &pHostAddress,
                                  int pHostPort,
                                  const AnsiString &pUsername,
                                  const AnsiString &pPassword)
                      : TThread(true)
{
   // Set thread properties
   Priority = tpNormal;
   FreeOnTerminate = true;

   // Set input parameters
   FileNameMask = pFileNameMask;
   FinishedDirectory = pFinishedDir;
   HostAddress = pHostAddress;
   HostPort = pHostPort;
   Username = pUsername;
   Password = pPassword;
}
// ------------------------------------------------------------------
void __fastcall TFTPThread::AfterConstruction()
{
   Resume();
}
// ------------------------------------------------------------------
void __fastcall TFTPThread::Execute()
{
   TIdFTP *IdFTP;
   TSearchRec sr;
   String SendDirectory = ExtractFilePath(FileNameMask);

   // initialize output fields
   FilesSent = new TStringList;
   Success = true;
   AppMessage = "";

   // check for files to send
   if (FindFirst(FileNameMask, 0, sr) == 0) {

      try {
         // set up FTP component
         IdFTP = new TIdFTP(NULL);
         IdFTP->Host = HostAddress;
         IdFTP->Port = HostPort;
         IdFTP->Username = Username;
         IdFTP->Password = Password;
         IdFTP->ProxySettings->ProxyType = fpcmNone;

         // Connect to host
         try {
            IdFTP->Connect();
         } catch (...) {
            Success = false;
            AppMessage = "ERROR: Could not connect to FTP Host";
         }

         // Send files
         if (Success) {
            do {
               // Send file
               try {
                  IdFTP->Put(SendDirectory + sr.Name, sr.Name, false);
               } catch (...) {
                  Success = false;
                  AppMessage = "ERROR: failed to transfer " +
                               sr.Name + " to FTP Host";
               }
               // move local file
               if (Success) {
                  if (RenameFile(SendDirectory + sr.Name,
                                 FinishedDirectory + sr.Name)) {
                     FilesSent->Add(sr.Name);
                  } else {
                     Success = false;
                     AppMessage = "ERROR: unable to move " +
                               sr.Name + " to " + FinishedDirectory;
                  }
               }
            } while (Success && (FindNext(sr) == 0));

            IdFTP->Disconnect();
         }

      } __finally {
         FindClose(sr);
         delete IdFTP;
      }
   }

   // Send results back to main application thread
   Synchronize(ReturnResult);

   delete FilesSent;
}
// ------------------------------------------------------------------
void __fastcall TFTPThread::ReturnResult()
{
   MainForm->HandleFTPResult(Success, FilesSent, AppMessage);
}
// ------------------------------------------------------------------
Technical Details
Message-ID: <44ee2f56$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <44ec9e0a@newsgroups.borland.com> <44ecb32d$3@newsgroups.borland.com> <44ecc221$1@newsgroups.borland.com> <44ecd6cd$1@newsgroups.borland.com> <44edc970$1@newsgroups.borland.com> <44edd930$1@newsgroups.borland.com> <44ede279@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,14159208397bbfa4
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Lesley Anne" <mspfila-ng@yahoo.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <44ec9e0a@newsgroups.borland.com> <44ecb32d$3@newsgroups.borland.com> <44ecc221$1@newsgroups.borland.com> <44ecd6cd$1@newsgroups.borland.com> <44edc970$1@newsgroups.borland.com> <44edd930$1@newsgroups.borland.com> <44ede279@newsgroups.borland.com>
Subject: Re: Indy 9 TIdFTP Client - when is a Put() finished?
Date: Thu, 24 Aug 2006 18:59:29 -0400
Lines: 131
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2869
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 144.160.5.25
Message-ID: <44ee2f56$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1156460374 144.160.5.25 (24 Aug 2006 15:59:34 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail