Ignore:
Timestamp:
Oct 9, 2012, 1:58:45 PM (12 years ago)
Author:
chronos
Message:
  • Přidáno: Hlavní modul Base, který slouží jako základní rozhraní k aplikaci pro ostatní moduly. Modul System zajistí udržování seznamu instalovaných modulů v perzistentním úložišti v databázi.
  • Upraveno: Správce modulů ModuleManager je nyní použit pro každé komunikační spojení zvlášť.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/ModularSystem/UModularSystem.pas

    r94 r105  
    66
    77uses
    8   Classes, SysUtils, Contnrs;
     8  Classes, SysUtils, Contnrs, URegistry;
    99
    1010type
     
    1919  TModule = class(TComponent)
    2020  private
     21    FEnabled: Boolean;
    2122    FRunning: Boolean;
    2223    FInstalled: Boolean;
    23     Manager: TModuleManager;
     24    FManager: TModuleManager;
    2425    FVersion: string;
    2526    FIdentification: string;
     
    2930    FDependencies: TStringList;
    3031    FDescription: TStringList;
     32    procedure SetEnabled(AValue: Boolean);
    3133    procedure SetInstalled(AValue: Boolean);
    3234    procedure SetRunning(AValue: Boolean);
     35  protected
     36    procedure BeforeStart; virtual;
     37    procedure AfterStart; virtual;
     38    procedure BeforeStop; virtual;
     39    procedure AfterStop; virtual;
    3340  public
    3441    API: TAPI;
    35     MarkForInstall: Boolean;
    3642    procedure Start; virtual;
    3743    procedure Stop; virtual;
     
    4854    property Running: Boolean read FRunning write SetRunning;
    4955    property Installed: Boolean read FInstalled write SetInstalled;
     56    property Enabled: Boolean read FEnabled write SetEnabled;
    5057  published
     58    property Manager: TModuleManager read FManager;
    5159    property Version: string read FVersion write FVersion;
    5260    property Identification: string read FIdentification write FIdentification;
     
    7987    procedure EnumModulesInstall(Dependencies, ModuleList: TStringList);
    8088    procedure EnumModulesUninstall(ModuleName: string; ModuleList: TStringList);
    81     procedure RegisterModule(Module: TModule; MarkForInstall: Boolean = False);
     89    procedure RegisterModule(Module: TModule; Enabled: Boolean = True);
    8290    procedure UnregisterModule(Module: TModule);
    8391    procedure StartInstalled;
    84     procedure InstallMarked;
     92    procedure InstallEnabled;
    8593    procedure StopAll;
    8694    procedure UninstallAll;
     95    procedure LoadFromRegistry(Context: TRegistryContext);
     96    procedure SaveToRegistry(Context: TRegistryContext);
    8797    constructor Create(AOwner: TComponent); override;
    8898    destructor Destroy; override;
     
    143153  for I := 0 to Dependencies.Count - 1 do begin
    144154    Module := FindModuleByName(Dependencies[I]);
    145     if Assigned(Module) then begin
     155    if Assigned(Module) and Module.Enabled then begin
    146156      if not Module.Running then Module.Start;
    147157    end else raise Exception.CreateFmt(SModuleNotFound, [ModuleName, Dependencies[I]]);
     
    199209  for I := 0 to Dependencies.Count - 1 do begin
    200210    Module := FindModuleByName(Dependencies[I]);
    201     if Assigned(Module) then begin
     211    if Assigned(Module) and Module.Enabled then begin
    202212      if not Module.Installed then Module.Install;
    203213    end else raise Exception.CreateFmt(SModuleNotFound, [ModuleName, Dependencies[I]]);
     
    248258
    249259procedure TModuleManager.RegisterModule(Module: TModule;
    250   MarkForInstall: Boolean = False);
     260  Enabled: Boolean = True);
    251261begin
    252262  Modules.Add(Module);
    253   Module.Manager := Self;
     263  Module.FManager := Self;
    254264  Module.API := API;
    255   Module.MarkForInstall := MarkForInstall;
     265  Module.Enabled := Enabled;
    256266end;
    257267
     
    270280end;
    271281
    272 procedure TModuleManager.InstallMarked;
     282procedure TModuleManager.InstallEnabled;
    273283var
    274284  I: Integer;
     
    276286  for I := 0 to Modules.Count - 1 do
    277287  with TModule(Modules[I]) do
    278     if not Installed and MarkForInstall then Install;
     288    if not Installed and Enabled then Install;
    279289end;
    280290
     
    301311  inherited;
    302312  Modules := TObjectList.Create;
     313  //Modules.OwnsObjects := False;
    303314end;
    304315
     
    307318  StopAll;
    308319  FreeAndNil(Modules);
    309   inherited Destroy;
     320  inherited;
     321end;
     322
     323procedure TModuleManager.LoadFromRegistry(Context: TRegistryContext);
     324var
     325  I: Integer;
     326begin
     327  with TRegistryEx.Create do
     328  try
     329    RootKey := Context.RootKey;
     330    for I := 0 to Modules.Count - 1 do
     331    with TModule(Modules[I]) do begin
     332      OpenKey(Context.Key + '\' + Identification, True);
     333      Running := ReadBoolWithDefault('Run',  Enabled);
     334    end;
     335  finally
     336    Free;
     337  end;
     338end;
     339
     340procedure TModuleManager.SaveToRegistry(Context: TRegistryContext);
     341var
     342  I: Integer;
     343begin
     344  with TRegistryEx.Create do
     345  try
     346    RootKey := Context.RootKey;
     347    for I := 0 to Modules.Count - 1 do
     348    with TModule(Modules[I]) do begin
     349      OpenKey(Context.Key + '\' + Identification, True);
     350      WriteBool('Run', Running);
     351    end;
     352  finally
     353    Free;
     354  end;
    310355end;
    311356
     
    318363end;
    319364
     365procedure TModule.BeforeStart;
     366begin
     367  if Running then Exit;
     368  if not Installed then Install;
     369  Manager.StartDependencies(Identification, Dependencies);
     370end;
     371
     372procedure TModule.AfterStart;
     373begin
     374  FRunning := True;
     375end;
     376
     377procedure TModule.BeforeStop;
     378begin
     379  if not Running then Exit;
     380  FRunning := False;
     381  Manager.StopDependencies(Identification);
     382end;
     383
     384procedure TModule.AfterStop;
     385begin
     386end;
     387
    320388procedure TModule.SetInstalled(AValue: Boolean);
    321389begin
     
    324392end;
    325393
     394procedure TModule.SetEnabled(AValue: Boolean);
     395begin
     396  if FEnabled = AValue then Exit;
     397  FEnabled := AValue;
     398  if not FEnabled and FInstalled then Uninstall;
     399end;
     400
    326401procedure TModule.Start;
    327402begin
    328   if Running then Exit;
    329   Manager.StartDependencies(Identification, Dependencies);
    330   FRunning := True;
     403  BeforeStart;
     404  // Do something
     405  AfterStart;
    331406end;
    332407
    333408procedure TModule.Stop;
    334409begin
    335   if not Running then Exit;
    336   Manager.StopDependencies(Identification);
    337   FRunning := False;
     410  BeforeStop;
     411  // Do something
     412  AfterStop;
    338413end;
    339414
     
    403478begin
    404479  Running := False;
    405   Description.Free;
    406   Dependencies.Free;
    407   inherited Destroy;
     480  FreeAndNil(FDescription);
     481  FreeAndNil(FDependencies);
     482  inherited;
    408483end;
    409484
Note: See TracChangeset for help on using the changeset viewer.