Server Code for an Internet Access Control Application - Custom Procedures and Functions
(Page 2 of 4 )
There are a few custom procedures and functions that I've created to facilitate the processing of communication and updating of the user interface. These are very important for the entire application and warrant a discussion. The first procedure is called "broadcastall" and is the medium that the program uses to communicate with all the workstations simultaneously:
procedure TForm1.broadcastAll;
var
List: TList;
Context: TMyContext;
I: Integer;
begin
// List is inherited from TIdContext
List := form1.ts.Contexts.LockList;
try
for I := 0 to List.Count-1 do
begin
Context := TMyContext(List[I]);
try
Context.Connection.IOHandler.WriteLn('shutdown:end@plchldr');
except end;
end;
finally
form1.ts.Contexts.UnlockList;
end;
end;
The procedure defines a couple of variables, one of which is called Context. This variable inherits all of the properties of the TMyContext class and is extensively used throughout the procedure. The other variable that I want to draw your attention to is the "list" variable that is of type TList. This variable is used to inherit all the information that is contained in the TidContext class list:
List := form1.ts.Contexts.LockList;
The TidContext class contains a list of connected clients which is automatically generated and updated when a client connects or disconnects. The procedure starts by transferring the contents of the context object to the declared TList variable and locks the list, so that no new clients can be added while the procedure is busy broadcasting:
List := form1.ts.Contexts.LockList;
Then it counts the number of clients on the list and sets up a loop:
try
for I := 0 to List.Count-1 do
begin
Context := TMyContext(List[I]);
The procedure then writes to each client on the list. The communication sends a command called shutdown as shown below:
try
Context.Connection.IOHandler.WriteLn('shutdown:end@plchldr');
except end;
end;
and finally unlocks the list so it can be updated if needed:
finally
form1.ts.Contexts.UnlockList;
end;
end;
Next: Timing Procedure >>
More Delphi-Kylix Articles
More By David Web