Server Code for an Internet Access Control Application
In the preceding article we dealt with the functional requirements of the server application as well as with the components that make up the server interface. In this article we will continue to build on the concepts that we discussed there and at the same time explore the code that runs the server application.
Server Code for an Internet Access Control Application (Page 1 of 4 )
The server code
The code starts off with declaring some classes that we are going to use, including the definition for the context class, so let's kick off with them:
TMyContext = class(TIdContext)
You already have some background about the TidContext class, so I will not go into it here. The above code declares a descendant of the TidContext class as TMyContext and then declares some variables that will hold some of the information we will need. We collect this information when the client tries to connect to our server.
The next class declaration deals with synchronization. Now, TidSync is needed here because it gives us the ability to pass parameters between synchronized methods:
TLog = class(TIdSync)
procedure DoSynchronize; override;
constructor Create(const AMsg: String);
class procedure AddMsg(const AMsg: String);
Both the TLog and TMyContext objects are going to help in facilitating requests from the client machine. The Tlog object has an FMsg string variable, which will catch all communications that are going to come from a connected client. This variable will then be passed between the three methods of the TLog object. One of those methods updates the user interface. Both of these two classes separate the communication between the server and a client, away from the main VCL thread of the server. This ensures that the server can handle many client connections at the same time, because it does not actually handle the communication, it simply passes it on to the said objects for processing. The implementation of these two classes is very easy to understand:
constructor TForm1.Create(AOwner: TComponent);
ts.ContextClass := TMyContext;
The TMyContext class is created and inherits the properties and methods of the context class. You can then access a connected client's data by simply using the dot notation, as in "TMyContext.data." The DoSynchronization procedure simply updates the memo control of the form. Without this particular procedure, the application will try to update the control directly from the main thread. This will cause your application to crash:
The AddMsg procedure makes the client communication string available to the DoSynchronize procedure for processing:
class procedure TLog.AddMsg(const AMsg: String);
with Create(AMsg) do try
That's all there is for dealing with a potential future bottleneck situation. Because of the minimal communication between the server and client applications, I've not optimized this application as much as I otherwise would have.