Ignore:
Timestamp:
Sep 17, 2012, 8:06:51 AM (12 years ago)
Author:
chronos
Message:
  • Modified: ModularSystem now allow installation, uninstallation and start and stop.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ModularSystem/UModularSystem.pas

    r404 r421  
    1919  TModule = class(TComponent)
    2020  private
     21    FRunning: Boolean;
    2122    FInstalled: Boolean;
    2223    Manager: TModuleManager;
     
    2930    FDescription: TStringList;
    3031    procedure SetInstalled(AValue: Boolean);
     32    procedure SetRunning(AValue: Boolean);
    3133  public
    3234    API: TAPI;
    3335    MarkForInstall: Boolean;
     36    procedure Start; virtual;
     37    procedure Stop; virtual;
    3438    procedure Install; virtual;
    3539    procedure Uninstall; virtual;
    36     procedure Update; virtual;
     40    procedure Upgrade; virtual;
     41    procedure EnumModulesStart(ModuleList: TStringList);
     42    procedure EnumModulesStop(ModuleList: TStringList);
    3743    procedure EnumModulesInstall(ModuleList: TStringList);
    3844    procedure EnumModulesUninstall(ModuleList: TStringList);
    39     constructor Create(Owner: TComponent); virtual;
     45    procedure SetInstalledState(Value: Boolean);
     46    constructor Create(Owner: TComponent); override;
    4047    destructor Destroy; override;
     48    property Running: Boolean read FRunning write SetRunning;
    4149    property Installed: Boolean read FInstalled write SetInstalled;
    4250  published
     
    5058  end;
    5159
     60  TModuleEvent = procedure (Sender: TObject; Module: TModule) of object;
     61
    5262  { TModuleManager }
    5363
     
    5565  private
    5666    FAPI: TAPI;
     67    FOnModuleChange: TModuleEvent;
    5768    procedure SetAPI(AValue: TAPI);
    5869  public
    5970    Modules: TObjectList; // TObjectList<TModule>
    6071    function FindModuleByName(Name: string): TModule;
     72    function ModuleRunning(Name: string): Boolean;
     73    procedure StartDependencies(ModuleName: string; Dependencies: TStringList);
     74    procedure StopDependencies(ModuleName: string);
     75    procedure EnumModulesStart(Dependencies, ModuleList: TStringList);
     76    procedure EnumModulesStop(ModuleName: string; ModuleList: TStringList);
    6177    procedure InstallDependencies(ModuleName: string; Dependencies: TStringList);
    6278    procedure UninstallDependencies(ModuleName: string);
     
    6581    procedure RegisterModule(Module: TModule; MarkForInstall: Boolean = False);
    6682    procedure UnregisterModule(Module: TModule);
     83    procedure StartInstalled;
    6784    procedure InstallMarked;
     85    procedure StopAll;
    6886    procedure UninstallAll;
    6987    constructor Create(AOwner: TComponent); override;
    7088    destructor Destroy; override;
    7189    property API: TAPI read FAPI write SetAPI;
     90    property OnModuleChange: TModuleEvent read FOnModuleChange write FOnModuleChange;
    7291  end;
    7392
     
    107126end;
    108127
    109 procedure TModuleManager.InstallDependencies(ModuleName: string; Dependencies: TStringList);
     128function TModuleManager.ModuleRunning(Name: string): Boolean;
     129var
     130  Module: TModule;
     131begin
     132  Module := FindModuleByName(Name);
     133  if Assigned(Module) then begin
     134    Result := Module.Running;
     135  end else Result := False;
     136end;
     137
     138procedure TModuleManager.StartDependencies(ModuleName: string; Dependencies: TStringList);
     139var
     140  Module: TModule;
     141  I: Integer;
     142begin
     143  for I := 0 to Dependencies.Count - 1 do begin
     144    Module := FindModuleByName(Dependencies[I]);
     145    if Assigned(Module) then begin
     146      if not Module.Running then Module.Start;
     147    end else raise Exception.CreateFmt(SModuleNotFound, [ModuleName, Dependencies[I]]);
     148  end;
     149end;
     150
     151procedure TModuleManager.StopDependencies(ModuleName: string);
     152var
     153  I: Integer;
     154begin
     155  for I := 0 to Modules.Count - 1 do
     156  with TModule(Modules[I]) do begin
     157    if (Dependencies.IndexOf(ModuleName) <> - 1) and Running then Stop;
     158  end;
     159end;
     160
     161procedure TModuleManager.EnumModulesStart(Dependencies,
     162  ModuleList: TStringList);
     163var
     164  Module: TModule;
     165  I: Integer;
     166begin
     167  for I := 0 to Dependencies.Count - 1 do begin
     168    Module := FindModuleByName(Dependencies[I]);
     169    if Assigned(Module) then begin
     170      if not Module.Running and (ModuleList.IndexOf(Module.Identification) = -1) then begin
     171        ModuleList.Add(Module.Identification);
     172        EnumModulesStart(Module.Dependencies, ModuleList);
     173      end;
     174    end else raise Exception.CreateFmt(SModuleNotFound, [Module.Identification]);
     175  end;
     176end;
     177
     178procedure TModuleManager.EnumModulesStop(ModuleName: string;
     179  ModuleList: TStringList);
     180var
     181  I: Integer;
     182begin
     183  for I := 0 to Modules.Count - 1 do
     184  with TModule(Modules[I]) do begin
     185    if (Dependencies.IndexOf(ModuleName) <> -1) and Running and
     186      (ModuleList.IndexOf(Identification) = -1) then begin
     187      ModuleList.Add(Identification);
     188      Self.EnumModulesStop(Identification, ModuleList);
     189    end;
     190  end;
     191end;
     192
     193procedure TModuleManager.InstallDependencies(ModuleName: string;
     194  Dependencies: TStringList);
    110195var
    111196  Module: TModule;
     
    176261end;
    177262
     263procedure TModuleManager.StartInstalled;
     264var
     265  I: Integer;
     266begin
     267  for I := 0 to Modules.Count - 1 do
     268  with TModule(Modules[I]) do
     269    if not Running and Installed then Start;
     270end;
     271
    178272procedure TModuleManager.InstallMarked;
    179273var
     
    185279end;
    186280
     281procedure TModuleManager.StopAll;
     282var
     283  I: Integer;
     284begin
     285  for I := 0 to Modules.Count - 1 do
     286  with TModule(Modules[I]) do
     287    if Running then Stop;
     288end;
     289
    187290procedure TModuleManager.UninstallAll;
    188291var
     
    202305destructor TModuleManager.Destroy;
    203306begin
    204   UninstallAll;
     307  StopAll;
    205308  FreeAndNil(Modules);
    206309  inherited Destroy;
     
    209312{ TModule }
    210313
     314procedure TModule.SetRunning(AValue: Boolean);
     315begin
     316  if FRunning = AValue then Exit;
     317  if AValue then Start else Stop;
     318end;
     319
    211320procedure TModule.SetInstalled(AValue: Boolean);
    212321begin
    213322  if FInstalled = AValue then Exit;
    214323  if AValue then Install else Uninstall;
     324end;
     325
     326procedure TModule.Start;
     327begin
     328  if Running then Exit;
     329  Manager.StartDependencies(Identification, Dependencies);
     330  FRunning := True;
     331end;
     332
     333procedure TModule.Stop;
     334begin
     335  if not Running then Exit;
     336  Manager.StopDependencies(Identification);
     337  FRunning := False;
    215338end;
    216339
     
    220343  Manager.InstallDependencies(Identification, Dependencies);
    221344  FInstalled := True;
     345  if Assigned(Manager.FOnModuleChange) then
     346    Manager.FOnModuleChange(Manager, Self);
    222347end;
    223348
     
    225350begin
    226351  if not Installed then Exit;
     352  if Running then Stop;
    227353  Manager.UninstallDependencies(Identification);
    228354  FInstalled := False;
    229 end;
    230 
    231 procedure TModule.Update;
    232 begin
    233   if not Installed then Exit;
     355  if Assigned(Manager.FOnModuleChange) then
     356    Manager.FOnModuleChange(Manager, Self);
     357end;
     358
     359procedure TModule.Upgrade;
     360begin
     361  if not Running then Exit;
     362end;
     363
     364procedure TModule.EnumModulesStart(ModuleList: TStringList);
     365begin
     366  ModuleList.Clear;
     367  Manager.EnumModulesStart(Dependencies, ModuleList);
     368end;
     369
     370procedure TModule.EnumModulesStop(ModuleList: TStringList);
     371begin
     372  ModuleList.Clear;
     373  Manager.EnumModulesStop(Identification, ModuleList);
    234374end;
    235375
     
    244384  ModuleList.Clear;
    245385  Manager.EnumModulesUninstall(Identification, ModuleList);
     386end;
     387
     388procedure TModule.SetInstalledState(Value: Boolean);
     389begin
     390  FInstalled := Value;
     391  if Assigned(Manager.FOnModuleChange) then
     392    Manager.FOnModuleChange(Manager, Self);
    246393end;
    247394
     
    255402destructor TModule.Destroy;
    256403begin
    257   Installed := False;
     404  Running := False;
    258405  Description.Free;
    259406  Dependencies.Free;
Note: See TracChangeset for help on using the changeset viewer.