Title: How to implement automatic asynchronous data exchange in the company
Question: How to implement automatic asynchronous data exchange in the company that sells goods. All business information of this company is stored in the database serviced by a server application. There are a lot of distant branches and independent agents that use client applications.
Answer:
How to implement automatic asynchronous data exchange in the company
The Clever Internet Library contains a set of components designed for asynchronous operations with Internet resources. One of these components - TclNewsChecker - allows you to check the presence and up-to-date state of a certain remote resource in the background mode in order to download it, if necessary.
This functionality can be used not only for checking news or updates on a developer's site but for more complicated, non-trivial tasks. Let us explore the following situation. There is a company that sells goods. All business information of this company is stored in the database serviced by a server application. There are a lot of distant branches and independent agents that use client applications.
It is clear that one of the main tasks is data replication between the main office and its branches. So, the server application produces special script files containing replicated data. The challenge is to automate the process of delivering these script files to all the clients.
The TclNewsChecker can help you to solve this problem. Let us consider that the server application automatically creates replication files on a certain schedule and places them into folder that can be accessed via the Internet. At the same time for each client we'll use the TclNewsChecker component. The client side should remember the last replicates script. We'll put this information into the file name to simplify our task: script1.dat, script2.dat... The last figure in the file name represents the number of the last replicated file. This value can be stored in the Registry, for instance.
function TForm1.GetScriptNumber: Integer;
var
reg: TRegistry;
begin
Result := 0;
reg := TRegistry.Create();
try
if (reg.OpenKey(' \Software\MyProgram', False)) and reg.ValueExists('ScriptNo') then
begin
Result := reg.ReadInteger('ScriptNo');
end;
finally
reg.Free();
end;
end;
procedure TForm1.PutScriptNumber(ANumber: Integer);
var
reg: TRegistry;
begin
reg := TRegistry.Create();
try
if (reg.OpenKey(\Software\MyProgram, True)) then
begin
reg.WriteInteger('ScriptNo', ANumber);
end;
finally
reg.Free();
end;
end;
Now we should add the event handler of the main form display - OnShow. The initialization of the downloading process (StartCheckNews) should be implemented in it.
procedure TForm1.FormShow(Sender: TObject);
begin
StartCheckNews();
end;
procedure TForm1.StartCheckNews;
begin
FScriptNo := GetScriptNumber();
clNewsChecker.URL := ' http:www.myurl/resources/script' + IntToStr(FScriptNo) + '.dat';
clNewsChecker.Start(True);
end;
You also may add the OnDataItemProceed event handler and implement progress display or the asynchronous operation mode in it.
procedure TForm1.clNewsCheckerDataItemProceed(Sender: TObject;
ResourceInfo: TclResourceInfo; BytesProceed: Integer; CurrentData: PChar;
CurrentDataSize: Integer);
begin
Caption := IntToStr(BytesProceed) + ' of ' + IntToStr(ResourceInfo.Size) + ' downloaded';
end;
It is necessary to implement the increment and saving of the next script number (FscriptNo) and response on the downloaded resource in the OnNewsExist event handler:
procedure TForm1.clNewsCheckerNewsExist(Sender: TObject);
begin
Inc(FScriptNo);
PutScriptNumber(FScriptNo);
StartReplication();
end;
procedure TForm1.StartReplication;
begin
ShowMessage('Here code for processing the replication script ' + clNewsChecker.LocalFile);
end;
You also can enhance the functionality and check presence of the next script file in the Internet. It can be necessary if a client hasn't downloaded scripts from the server for a long time:
procedure TForm1.clNewsCheckerNewsExist(Sender: TObject);
begin
Inc(FScriptNo);
PutScriptNumber(FScriptNo);
StartReplication();
StartCheckNews();
end;
These few steps allow you to implement automatic asynchronous data exchange in the company.