Ignore:
Timestamp:
Dec 18, 2009, 12:21:39 PM (15 years ago)
Author:
george
Message:
  • Upraveno: Nedokončené přepracování správy vláken. Použit systém přidělování ze společné sady o dané velikosti namísto přidělování vláken ke každému požadavku samostatně. Vlákna jsou inicializována při spuštění aplikace, jsou opakovaně používána a uvolněna až při ukončení běhu aplikace.
  • Přidáno: Třída pro práci s opakovaně spustitelnými vlákny.
  • Přidáno: Třída přidělování objektů ze zásoby o dané velikosti.
  • Přidáno: Třída pro zobrazování stránkování seznamů položek.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DirectWeb/UWebServer.pas

    r87 r88  
    77uses
    88  Classes, SysUtils, UHTTPServer, UHTTPSessionFile,
    9   UTCPServer,
    10   UCommon,
     9  UTCPServer, Contnrs,
     10  UCommon, syncobjs,
    1111  UMemoryStreamEx,
    1212  UMIMEType, Synautil,
    13   USqlDatabase, DOM, XMLRead;
     13  USqlDatabase, DOM, XMLRead, UHTMLControls;
    1414
    1515const
     
    1717
    1818type
    19   { TWebServer }
    20 
    21   TWebServer = class
     19
     20  { TDatabasePool }
     21
     22  TDatabasePool = class(TObjectList)
    2223  private
    23     HTTPSessionFile: THTTPSessionFile;
    24     function PagesList(URL: string; Page, TotalCount, CountPerPage: Integer
    25       ): string;
    26     procedure ServerInfo(Request: THTTPRequest; Response: THTTPResponse);
     24    Lock: TCriticalSection;
    2725  public
    28     HTTPServer: THTTPServer;
     26    PoolSize: Integer;
    2927    HostName: string;
    3028    Schema: string;
    3129    UserName: string;
    3230    Password: string;
    33 
    34     procedure ViewList(Request: THTTPRequest; Response: THTTPResponse);
    35     procedure ViewItem(Request: THTTPRequest; Response: THTTPResponse);
    36     procedure SendIndex(Request: THTTPRequest; Response: THTTPResponse);
     31    procedure Allocate;
     32    function Acquire: TSqlDatabase;
     33    procedure Release(Database: TSqlDatabase);
     34    constructor Create;
     35    destructor Destroy; override;
     36  end;
     37
     38  { TWebServer }
     39
     40  TWebServer = class
     41  private
     42    SessionStorage: TFileHTTPSessionStorage;
     43    procedure ServerInfo(HandlerData: THTTPHandlerData);
     44  public
     45    HTTPServer: THTTPServer;
     46    DatabasePool: TDatabasePool;
     47
     48    procedure ViewList(HandlerData: THTTPHandlerData);
     49    procedure ViewItem(HandlerData: THTTPHandlerData);
     50    procedure SendIndex(HandlerData: THTTPHandlerData);
    3751    procedure WriteHeader(Stream: TMemoryStreamEx);
    3852    procedure WriteFooter(Stream: TMemoryStreamEx);
     
    6377        with ChildNodes[I3] do begin
    6478          if NodeName = 'hostname' then
    65             HostName := TextContent;
     79            DatabasePool.HostName := TextContent;
    6680          if NodeName = 'schema' then
    67             Schema := TextContent;
     81            DatabasePool.Schema := TextContent;
    6882          if NodeName = 'username' then
    69             UserName := TextContent;
     83            DatabasePool.UserName := TextContent;
    7084          if NodeName = 'password' then
    71             Password := TextContent;
     85            DatabasePool.Password := TextContent;
    7286        end;
    7387        if NodeName = 'server' then
     
    102116end;
    103117
    104 function TWebServer.PagesList(URL: string; Page, TotalCount, CountPerPage: Integer): string;
    105 const
    106   Around: Integer = 10;
    107 var
    108   Count: Integer;
    109   PagesMax: Integer;
    110   PagesMin: Integer;
    111   I: Integer;
    112 begin
    113   Count := Round(TotalCount / CountPerPage);
    114   Result := '';
    115   if Count > 1 then begin
    116     if Page > 0 then begin
    117       Result := Result + '<a href="' + URL + '0">&lt;&lt;</a> ';
    118       Result := Result + '<a href="' + URL + IntToStr(Page - 1) + '">&lt;</a> ';
    119     end;
    120     PagesMax := Count - 1;
    121     PagesMin := 0;
    122     if PagesMax > (Page + Around) then PagesMax := Page + Around;
    123     if PagesMin < (Page - Around) then begin
    124       Result := Result + ' .. ';
    125       PagesMin := Page - Around;
    126     end;
    127     for I := PagesMin to PagesMax do begin
    128       if I = Page then Result := Result + '<strong>';
    129       Result := Result + '<a href="' + URL + IntToStr(I) + '">' + IntToStr(I + 1) + '</a> ';
    130       if I = Page then Result := Result + '</strong>';
    131     end;
    132     if PagesMax < (Count - 1) then Result := Result + ' .. ';
    133     if Page < (Count - 1) then begin
    134       Result := Result + '<a href="' + URL + IntToStr(Page + 1) + '">&gt;</a> ';
    135       Result := Result + '<a href="' + URL + IntToStr(Count - 1) + '">&gt;&gt;</a>';
    136     end;
    137   end;
    138 end;
    139 
    140 procedure TWebServer.ViewList(Request: THTTPRequest; Response: THTTPResponse);
     118procedure TWebServer.ViewList(HandlerData: THTTPHandlerData);
    141119var
    142120  SqlDatabase: TSqlDatabase;
     
    147125  Title: string;
    148126  LinkQuery: TQueryParameterList;
    149 begin
    150   SqlDatabase := TSqlDatabase.Create;
    151   with Response, Stream, SqlDatabase do
     127  PageList: TPageList;
     128begin
     129  SqlDatabase := DatabasePool.Acquire;
     130  with HandlerData, Response, Stream, SqlDatabase do
    152131  begin
    153132    WriteHeader(Stream);
    154     HostName := Self.HostName;
    155     UserName := Self.UserName;
    156     Password := Self.Password;
    157     Database := Self.Schema;
    158     Connect;
    159     WriteString(Request.Query.Values['Table']);
     133
     134    WriteString('<div align="center">');
     135
     136    // Prepare table paging
     137    DbRows := Query('SELECT COUNT(*) FROM ' + Request.Query.Values['Table']);
     138    PageList := TPageList.Create;
     139    PageList.HandlerData := HandlerData;
     140    with PageList do begin
     141      TotalCount := StrToInt(DbRows[0].ValuesAtIndex[0]);
     142      ItemPerPage := 20;
     143      NavigatorVisibleItems := 5;
     144      Process;
     145    end;
     146    DbRows.Destroy;
     147
     148    //WriteString(Request.Query.Values['Table']);
    160149    OrderColumn    := Request.Query.Values['OrderCol'];
    161150    OrderDirection := Request.Query.Values['OrderDir'];
     
    165154      OrderDirection := 'DESC';
    166155    DbRows := Query('SELECT * FROM ' + Request.Query.Values['Table'] +
    167       ' ORDER BY ' + OrderColumn + ' ' + OrderDirection);
     156      ' ORDER BY ' + OrderColumn + ' ' + OrderDirection + PageList.SQLLimit);
    168157
    169158    LinkQuery := TQueryParameterList.Create;
    170159    LinkQuery.Assign(Request.Query);
    171160
    172     WriteString('<strong>Seznam typů karet</strong>');
     161    WriteString('<strong>Seznam typů karet</strong><br/>');
     162    WriteString(PageList.Output);
    173163    WriteString('<table><tr>');
    174164    if DbRows.Count > 0 then
     
    197187    end;
    198188    WriteString('</table>');
     189    WriteString(PageList.Output);
     190    WriteString('</div>');
     191
    199192    DbRows.Destroy;
    200193    Destroy;
    201194    WriteFooter(Stream);
    202195  end;
    203 end;
    204 
    205 procedure TWebServer.ViewItem(Request: THTTPRequest; Response: THTTPResponse);
     196  PageList.Destroy;
     197end;
     198
     199procedure TWebServer.ViewItem(HandlerData: THTTPHandlerData);
    206200var
    207201  SqlDatabase: TSqlDatabase;
     
    214208begin
    215209  SqlDatabase := TSqlDatabase.Create;
    216   with Response, Stream, SqlDatabase do
     210  with HandlerData, Response, Stream, SqlDatabase do
    217211  begin
    218212    WriteHeader(Stream);
    219     HostName := Self.HostName;
    220     UserName := Self.UserName;
    221     Password := Self.Password;
    222     Database := Self.Schema;
    223     Connect;
    224213    DbRows := Query('SELECT * FROM ' + Request.Query.Values['Table'] +
    225214      ' WHERE Id=' + Request.Query.Values['Id']);
     
    239228end;
    240229
    241 procedure TWebServer.ServerInfo(Request: THTTPRequest; Response: THTTPResponse);
     230procedure TWebServer.ServerInfo(HandlerData: THTTPHandlerData);
    242231var
    243232  I: Integer;
    244233begin
    245   with Response.Stream do begin
    246     Response.Cookies.Values['Test'] := 'Halo';
     234  with HandlerData, Response.Stream do begin
     235    //Response.Cookies.Values['Test'] := 'Halo';
    247236    //Response.Cookies.Values['Test2'] := 'Halo2';
    248237
    249     HTTPServer.SessionHandler.Variables.Values['Session1'] := 'Value1';
     238    //HTTPServer.SessionHandler.Variables.Values['Session1'] := 'Value1';
    250239    //HTTPServer.SessionHandler.Variables.Values['Session2'] := 'Value2';
    251240
     
    263252
    264253    WriteString('<h5>Session variables</h5>');
    265     for I := 0 to HTTPServer.SessionHandler.Variables.Count - 1 do begin;
    266       WriteString(HTTPServer.SessionHandler.Variables.Strings[I] + '<br/>');
     254    for I := 0 to Session.Count - 1 do begin;
     255      WriteString(Session.Strings[I] + '<br/>');
    267256    end;
    268257
     
    275264end;
    276265
    277 procedure TWebServer.SendIndex(Request: THTTPRequest; Response: THTTPResponse);
    278 begin
    279   with Response, Stream do
     266procedure TWebServer.SendIndex(HandlerData: THTTPHandlerData);
     267begin
     268  with HandlerData, Response, Stream do
    280269  begin
    281270    WriteHeader(Stream);
    282     WriteString('<img src="logo.png"/><br/>');
    283     WriteString('<strong>Congratulations</strong>');
    284     WriteString('<hr/>');
    285     WriteString('Your PWU LightWebServer based application is running.<br/>');
    286     WriteString('<hr/>');
    287     WriteString('Generated ' + RFC822DateTime(Now) + ' by PWU LightWebServer.');
     271
     272
    288273    WriteFooter(Stream);
    289274  end;
     
    324309begin
    325310  inherited Create;
    326   HTTPSessionFile := THTTPSessionFile.Create;
    327   with HTTPSessionFile do begin
    328   end;
     311  DatabasePool := TDatabasePool.Create;
     312  DatabasePool.Capacity := 20;
     313  DatabasePool.Allocate;
     314
     315  SessionStorage := TFileHTTPSessionStorage.Create;
    329316  HTTPServer := THTTPServer.Create;
    330317  with HTTPServer, Socket do begin
    331     SessionHandler := HTTPSessionFile;
     318    SessionStorage := Self.SessionStorage;
    332319    DocumentRoot := 'Data';
    333320    with RequestHandlerList do begin
     
    346333destructor TWebServer.Destroy;
    347334begin
     335  DatabasePool.Destroy;
    348336  HTTPServer.Destroy;
    349   HTTPSessionFile.Destroy;
     337  SessionStorage.Destroy;
    350338  inherited Destroy;
    351339end;
    352340
     341{ TDatabasePool }
     342
     343procedure TDatabasePool.Allocate;
     344var
     345  I: Integer;
     346begin
     347  for I := 0 to PoolSize - 1 do
     348    with TSqlDatabase(Items[Add(TSqlDatabase.Create)]) do begin
     349      HostName := Self.HostName;
     350      UserName := Self.UserName;
     351      Password := Self.Password;
     352      Database := Self.Schema;
     353      Connect;
     354    end;
     355end;
     356
     357function TDatabasePool.Acquire: TSqlDatabase;
     358begin
     359
     360end;
     361
     362procedure TDatabasePool.Release(Database: TSqlDatabase);
     363begin
     364  try
     365    Lock.Acquire;
     366
     367  finally
     368    Lock.Release;
     369  end;
     370end;
     371
     372constructor TDatabasePool.Create;
     373begin
     374  Lock := TCriticalSection.Create;
     375end;
     376
     377destructor TDatabasePool.Destroy;
     378begin
     379  Lock.Destroy;
     380  inherited Destroy;
     381end;
     382
    353383end.
    354384
Note: See TracChangeset for help on using the changeset viewer.