Server Code for an Internet Access Control Application - Timing Procedure
(Page 3 of 4 )
The timing procedure is perhaps the most important code of the three custom procedures in the application. It is responsible for updating the server and keeping staff members updated on the status of various workstations. More importantly, it activates workstations when staff members allocate sessions for users, by sending the "activate" command in the manner that we discussed before. Let's look in detail at the functional logic for the timing procedure:
procedure TForm1.timing(theStation,charge,thetime:string);
var
List: TList;
I:integer;
Context: TMyContext;
currtime,end_time:tdatetime;
li:tlistitem;
begin
currtime:=now;
longTimeFormat:='hh:nn:ss';
end_time:=IncMinute(currtime,strtoint(alloctime));
form1.endtime:=end_time;
// tAll:=alloctime;
//Search for workstation name in lv list
//if the name is there, then just add the allocated time and charge etc.
li:=lv.FindCaption(0,theStation,true,true,true);
if li <> nil then begin
memo1.lines.add(theStation+'...found...');
form1.li.ImageIndex:=8;
li.SubItems.Strings[0]:=formatdatetime('hh:mm:ss',currtime);
li.SubItems.Strings[1]:=timetostr(end_time);
li.SubItems.Strings[2]:=alloctime + ' minutes';
li.SubItems.Strings[3]:='$'+charge;
li.SubItems.Strings[4]:='Busy';
end
else begin
//the computer name is not on the listview, so add it!
memo1.lines.add('Timing...Caption does not exists.');
//update the lview
form1.li:=form1.lv.Items.Add;
form1.li.ImageIndex:=8;
form1.li.Caption:=theStation;
form1.li.SubItems.Add(formatdatetime('hh:mm:ss',currtime));
form1.li.SubItems.Add(timetostr(end_time));
form1.li.SubItems.Add(alloctime + ' minutes');
form1.li.SubItems.Add('N$'+charge);
form1.li.SubItems.Add('Busy');
end;
List := form1.ts.Contexts.LockList;
try
for I := 0 to List.Count-1 do
begin
Context :=TMyContext(List[I]);
if Context.compname = theStation then
begin
try
Context.Connection.IOHandler.WriteLn('activate:'+theStation+'@'+alloctime);
except
end;
Exit;
end;
end;
finally
form1.ts.Contexts.UnlockList;
end;
form1.memo1.Lines.add('error.');
end;
One of the primary aims of this procedure is to keep staff members updated on the status of an Internet session. It does this by automatically calculating the length of time that a user will be on the Internet using the actual time that a staff member allocates to that user. For example if a user is allocated thirty minutes at exactly one 'o clock then the procedure will automatically calculate and visually indicate that the user's session should end at half past one.
I hard coded the time units, which are fifteen, thirty and sixty minutes. This is of course not good practice, but in this case it is okay, because any time a user wants more time, a staff member will simply re-allocate the required time unit.
In addition to time keeping, this procedure also shows the total cost of any allocated time unit; for example, a fifteen minute session will cost around five dollars, a thirty minute session will cost ten dollars, and so on. The code starts by setting up the different variables that it will use to keep time. The variable names should give you an idea of their function. For example, the "currtime" variable stores the current time, and so on:
currtime:=now;
longTimeFormat:='hh:nn:ss';
end_time:=IncMinute(currtime,strtoint(alloctime));
form1.endtime:=end_time;
The "alloctime" variable will contain a time unit that is used to add to the current time. This is programmatically achieved by using the "IncMinute()" function as shown above, which then gives us the time that the session should end. The main control on the form that shows us the status of the connected workstations is the list view control; I choose it because it is highly customizable and is perfect for real time updates. It also therefore occupies a lot of time in this procedure.
The next section of the procedure does two things: first, it checks to see if the name of the workstation that the staff member allocates a session to is actually shown on the list view; if so, it adds the required information such as the allocated time and cost of the session. The workstation name search is done using the list view's search function called "FindCaption()":
//if the name is there, then just add the allocated time and charge etc.
li:=lv.FindCaption(0,theStation,true,true,true);
if li <> nil then begin
memo1.lines.add(theStation+'...found...');
form1.li.ImageIndex:=8;
li.SubItems.Strings[0]:=formatdatetime('hh:mm:ss',currtime);
li.SubItems.Strings[1]:=timetostr(end_time);
li.SubItems.Strings[2]:=alloctime + ' minutes';
li.SubItems.Strings[3]:='$'+charge;
li.SubItems.Strings[4]:='Busy';
Second, it checks if the name of the workstation is on the list. If not, it adds the workstation name and all related session information:
end
else begin
//the computer name is not on the listview, so add it!
memo1.lines.add('Timing...Caption does not exists.');
//update the lview
form1.li:=form1.lv.Items.Add;
form1.li.ImageIndex:=8;
form1.li.Caption:=theStation;
form1.li.SubItems.Add(formatdatetime('hh:mm:ss',et));
form1.li.SubItems.Add(timetostr(et));
form1.li.SubItems.Add(thetime + ' minutes');
form1.li.SubItems.Add('N$'+charge);
form1.li.SubItems.Add('Busy');
end;
Next: Final Part of the Procedure >>
More Delphi-Kylix Articles
More By David Web