unit uExampleMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, uMain_TPS_ChunkUploader, Vcl.StdCtrls, Vcl.ComCtrls,
  System.SyncObjs, Vcl.AppEvnts;

type
  TfExampleMain = class(TForm)
    bTest: TButton;
    ProgressBar: TProgressBar;
    OpenDialog: TOpenDialog;
    Memo: TMemo;
    bTestViaActions: TButton;
    bTest_in_Thread: TButton;
    ApplicationEvents: TApplicationEvents;
    PS_ChunkUploader1: TPS_ChunkUploader;
    procedure FormCreate(Sender: TObject);
    procedure bTestClick(Sender: TObject);
    procedure bTestViaActionsClick(Sender: TObject);
    procedure bTest_in_ThreadClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ApplicationEventsException(Sender: TObject; E: Exception);
  private

   FFileSizeDivideInChunks:Int64;
   FFileSizeUploadInChunks:Int64;

    FCS:TCriticalSection;

    procedure OnUploadInChunks(Sender: TObject; ACountOfBytes: Int64);
    procedure OnDivideInChunksOnClientBegin(Sender: TObject; ACountOfBytes: Int64);
    procedure OnDivideInChunksOnClient(Sender: TObject; ACountOfBytes: Int64);
    procedure OnGatherChunksOnServer(Sender: TObject; ACountOfBytes: Int64);
    { Private declarations }
  public
    { Public declarations }

    procedure OnUploadInChunksBegin(Sender:TObject; ACountOfBytes:Int64);
    property  CS:TCriticalSection read FCS write FCS;

  end;

var
  fExampleMain: TfExampleMain;

implementation

{$R *.dfm}

uses uChunkUploaderThread; // !!! <<< must be exactlu here to avoid problems, in uses after implementation

procedure TfExampleMain.ApplicationEventsException(Sender: TObject; E: Exception);
begin

if (Sender is TChunkUploaderThread) then

begin

      ShowMessage(
      'Вызвана ошибка '+E.ClassName+'с сообщением : '+
                  E.Message

                  );

end else

begin

      ShowMessage(
            'Вызвана ошибка '+E.ClassName+'с сообщением : '+
                        E.Message

                        );



end;




end;

procedure TfExampleMain.bTestClick(Sender: TObject);
begin

// PS_ChunkUploader1.VisualFrame_TPS_ChunkUploader.Test();

ShowMessage('Please use little files up to 100-200 KB because this test is in mainthread');

 if OpenDialog.Execute then begin


 PS_ChunkUploader1.VisualFrame_TPS_ChunkUploader.SendFileInChunks.
 SendFileInChunks(OpenDialog.FileName,1024);

 end;



end;

procedure TfExampleMain.bTestViaActionsClick(Sender: TObject);
begin

ShowMessage('Please use little files up to 100-200 KB because this test is in mainthread');

 if OpenDialog.Execute then begin


     with PS_ChunkUploader1.VisualFrame_TPS_ChunkUploader do begin

     // setting params
     FilePath:=OpenDialog.FileName;
     ChunkSize:=1024;

     // starting action
     aSendFileInChunksExecute(Self);

     end;



 end;


end;



procedure TfExampleMain.bTest_in_ThreadClick(Sender: TObject);

var ChunkUploaderThread:TChunkUploaderThread;

begin

//ShowMessage('Use any files you want. It is Separate thread here');

if OpenDialog.Execute then begin

ChunkUploaderThread:=TChunkUploaderThread.Create(true);
ChunkUploaderThread.ExampleMain:=Self;
ChunkUploaderThread.FileName:=OpenDialog.FileName;
ChunkUploaderThread.ChunkSize:=1024*1024*4; // 4 Mb
ChunkUploaderThread.Start


end;



end;

procedure TfExampleMain.FormCreate(Sender: TObject);
begin

FCS:=TCriticalSection.Create;

 with PS_ChunkUploader1.VisualFrame_TPS_ChunkUploader do begin


      with HTTP_ConnectionParams do begin

      Protocol:='http';
      Host:='localhost';//'40.85.142.196';//'192.168.1.102';//'localhost';
      Port:='40000';
      IdUser:='1'; // << Critical important to use ID that are in DataBase coffeetest.users
      User:='SomeUser';

      DefineServerToRequest();

      end;


  // works much faster without setting HTTPRequests.IdHTTP.ConnectTimeout
  //  HTTPRequests.IdHTTP.ConnectTimeout:=5*60000; // 5 minutes for big files

  HTTPRequests.IdHTTP.ReadTimeout:=5*60000;  // 5 minutes for big files

 end;


// Events settings
with PS_ChunkUploader1.VisualFrame_TPS_ChunkUploader.SendFileInChunks do begin

OnDivideInChunksOnClientBegin:=Self.OnDivideInChunksOnClientBegin;
OnDivideInChunksOnClient:=Self.OnDivideInChunksOnClient;


OnUploadInChunksBegin:=Self.OnUploadInChunksBegin;
OnUploadInChunks:=Self.OnUploadInChunks;

OnGatherChunksOnServer:=Self.OnGatherChunksOnServer;

end;


end;




procedure TfExampleMain.FormDestroy(Sender: TObject);
begin
FCS.Free;
FCS:=nil;
end;

procedure TfExampleMain.OnDivideInChunksOnClientBegin(Sender: TObject; ACountOfBytes: Int64);
begin

ProgressBar.Position:=0;
ProgressBar.Max:=100;
FFileSizeDivideInChunks:=ACountOfBytes;

Memo.Lines.add('OnDivideInChunksOnClientBegin');

end;


procedure TfExampleMain.OnDivideInChunksOnClient(Sender: TObject; ACountOfBytes: Int64);
var Position:integer;
begin

Position:=Trunc( (ACountOfBytes/FFileSizeDivideInChunks)*100);
ProgressBar.Position:=Position;
Memo.Lines.add('OnDivideInChunksOnClient '+ProgressBar.Position.ToString()+' %');

end;




procedure TfExampleMain.OnUploadInChunksBegin(Sender: TObject; ACountOfBytes: Int64);
begin

ProgressBar.Position:=0;
ProgressBar.Max:=100;
FFileSizeUploadInChunks:=ACountOfBytes;

Memo.Lines.add('OnUploadInChunksBegin');

end;

procedure TfExampleMain.OnUploadInChunks(Sender: TObject; ACountOfBytes: Int64);

var Position:integer;

begin


Position:=Trunc( (ACountOfBytes/FFileSizeUploadInChunks)*100);
ProgressBar.Position:=Position;
Memo.Lines.add('OnUploadInChunks '+ProgressBar.Position.ToString()+' %');

end;


procedure TfExampleMain.OnGatherChunksOnServer(Sender: TObject; ACountOfBytes: Int64);
begin
//
Memo.Lines.add('OnGatherChunksOnServer Wait some time...');

end;



end.
