Componentes.Terceros.RemObj.../internal/5.0.29.665/1/RemObjects SDK for Delphi/Samples/Async/AsyncClientMain.pas

240 lines
6.9 KiB
ObjectPascal

unit AsyncClientMain;
interface
{$I RemObjects.inc}
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, uROWinInetHttpChannel,
uROClient, uROBINMessage,
AsyncLibrary_Async, ExtCtrls, uROIndyUDPChannel, uROPostMessage,
uROIndyEmailChannel;
type
TAsyncClientMainForm = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
lbl_Status: TLabel;
ed_Value1: TEdit;
ed_Value2: TEdit;
CalcLiveButton: TButton;
CalcAsyncHttpButton: TButton;
GetResultButton: TButton;
ed_Result: TEdit;
CheckForAnswerBytton: TButton;
CalcAsyncUdpButton: TButton;
ROBINMessage1: TROBinMessage;
ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
ROIndyUDPChannel1: TROIndyUDPChannel;
btn_CalcAsyncEmail: TButton;
bSetupEmailSettings: TButton;
ROEmailChannel: TROEmailChannel;
ROPostMessage: TROPostMessage;
procedure CalcLiveButtonClick(Sender: TObject);
procedure CalcAsyncHttpButtonClick(Sender: TObject);
procedure GetResultButtonClick(Sender: TObject);
procedure CheckForAnswerByttonClick(Sender: TObject);
procedure CalcAsyncUdpButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btn_CalcAsyncEmailClick(Sender: TObject);
procedure bSetupEmailSettingsClick(Sender: TObject);
protected
fAsyncService: IAsyncService_Async;
procedure AdjustButtons;
procedure SetupEmailClient;
public
{ Public declarations }
end;
var
AsyncClientMainForm: TAsyncClientMainForm;
implementation
uses
AsyncLibrary_Intf, IdStack, async_Emailsettings, IdSMTP;
{$R *.dfm}
procedure TAsyncClientMainForm.CalcLiveButtonClick(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
try
with CoAsyncService.Create(ROBINMessage1, ROWinInetHTTPChannel1) do begin
ed_Result.Text := '';
ed_Result.Repaint();
ed_Result.Text := IntToStr(Sum(StrToInt(ed_Value1.Text), StrToInt(ed_Value2.Text)));
end; { with }
finally
Screen.Cursor := crDefault;
end; { try/finally }
end;
procedure TAsyncClientMainForm.CalcAsyncHttpButtonClick(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
try
if not Assigned(fAsyncService) then
fAsyncService := CoAsyncService_Async.Create(ROBINMessage1, ROWinInetHTTPChannel1);
ed_Result.Text := '';
ed_Result.Repaint();
fAsyncService.Invoke_Sum(StrToInt(ed_Value1.Text), StrToInt(ed_Value2.Text));
AdjustButtons();
finally
Screen.Cursor := crDefault;
end;
ShowMessage('The request has been sent to the Server.'#13'Click "Retrieve Result" to check if an answer has been received yet.');
end;
procedure TAsyncClientMainForm.CalcAsyncUdpButtonClick(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
try
if not Assigned(fAsyncService) then
fAsyncService := CoAsyncService_Async.Create(ROBINMessage1, ROIndyUDPChannel1);
ed_Result.Text := '';
ed_Result.Repaint();
fAsyncService.Invoke_Sum(StrToInt(ed_Value1.Text), StrToInt(ed_Value2.Text));
AdjustButtons();
finally
Screen.Cursor := crDefault;
end;
ShowMessage('The request has been sent to the Server.'#13'Click "Retrieve Result" to check if an answer has been received yet.');
end;
procedure TAsyncClientMainForm.GetResultButtonClick(Sender: TObject);
begin
if not Assigned(fAsyncService) then exit;
Screen.Cursor := crHourGlass;
try
if fAsyncService.AnswerReceived then begin
ed_Result.Text := IntToStr(fAsyncService.Retrieve_Sum());
fAsyncService := nil;
lbl_Status.Caption := 'Idle';
end
else begin
ShowMessage('Sorry, no answer yet.');
end;
AdjustButtons();
finally
Screen.Cursor := crDefault;
end;
end;
procedure TAsyncClientMainForm.CheckForAnswerByttonClick(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
try
if Assigned(fAsyncService) then begin
if fAsyncService.AnswerReceived then begin
lbl_Status.Caption := 'Answer Received';
ShowMessage('Answer received!');
end
else if fAsyncService.Busy then begin
lbl_Status.Caption := 'Busy';
ShowMessage('Sorry, no answer yet.');
end
else begin
lbl_Status.Caption := 'Idle';
ShowMessage('Sorry, no answer yet.');
end;
end;
finally
Screen.Cursor := crDefault;
end;
end;
procedure TAsyncClientMainForm.AdjustButtons;
begin
CalcAsyncHttpButton.Enabled := not Assigned(fAsyncService);
CalcAsyncUdpButton.Enabled := not Assigned(fAsyncService);
GetResultButton.Enabled := Assigned(fAsyncService);
CheckForAnswerBytton.Enabled := Assigned(fAsyncService);
btn_CalcAsyncEmail.Enabled := not Assigned(fAsyncService);
end;
procedure TAsyncClientMainForm.SetupEmailClient;
begin
ROEmailChannel.ServerEmail := ServerEmail;
ROEmailChannel.ClientEmail := ClientEmail;
{$IFDEF RemObjects_INDY8}
ROEmailChannel.Pop3Client.UserId := POP3UserName;
ROEmailChannel.SmtpClient.UserId := SMTPUserName;
{$ELSE}
ROEmailChannel.Pop3Client.Username := POP3UserName;
ROEmailChannel.SmtpClient.Username := SMTPUserName;
{$ENDIF}
{$IFDEF RemObjects_INDY10}
if SMTPUserName <> '' then
ROEmailChannel.SmtpClient.AuthType := atSASL
else
ROEmailChannel.SmtpClient.AuthType := atNone;
{$ELSE}
if SMTPUserName <> '' then
ROEmailChannel.SmtpClient.AuthenticationType := atLogin
else
ROEmailChannel.SmtpClient.AuthenticationType := atNone;
{$ENDIF}
ROEmailChannel.Pop3Client.Password := Pop3Password;
ROEmailChannel.Pop3Client.Host := POP3host;
ROEmailChannel.Pop3Client.Port := POP3Port;
ROEmailChannel.SmtpClient.Password := SMTPPassword;
ROEmailChannel.SmtpClient.Host := SMTPhost;
ROEmailChannel.SmtpClient.Port := SMTPPort;
end;
procedure TAsyncClientMainForm.FormCreate(Sender: TObject);
begin
async_Emailsettings.LoadEmailSettings;
end;
procedure TAsyncClientMainForm.bSetupEmailSettingsClick(Sender: TObject);
begin
async_Emailsettings.SetupEmailSettings(True);
SetupEmailClient;
end;
procedure TAsyncClientMainForm.btn_CalcAsyncEmailClick(Sender: TObject);
begin
if SMTPhost = 'smtphost' then async_Emailsettings.SetupEmailSettings(True);
SetupEmailClient;
Screen.Cursor := crHourGlass;
try
if not Assigned(fAsyncService) then
fAsyncService := CoAsyncService_Async.Create(ROPostMessage, ROEmailChannel);
ed_Result.Text := '';
ed_Result.Repaint();
fAsyncService.Invoke_Sum(StrToInt(ed_Value1.Text), StrToInt(ed_Value2.Text));
lbl_Status.Caption := 'Busy';
AdjustButtons();
finally
Screen.Cursor := crDefault;
end;
ShowMessage('The request has been sent to the Server.'#13'Click "Retrieve Result" to check if an answer has been received yet.');
end;
end.