brookfclhttpdaemonbroker.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. (*
  2. Brook for Free Pascal
  3. Copyright (C) 2014-2019 Silvio Clecio
  4. See the file LICENSE.txt, included in this distribution,
  5. for details about the copyright.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. *)
  10. { FCL HTTP daemon broker. }
  11. unit BrookFCLHttpDaemonBroker;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. {$IFDEF MSWINDOWS}
  16. ServiceManager,
  17. {$ENDIF}
  18. BrookFCLHttpAppBroker, BrookApplication, BrookConsts, DaemonApp, Classes,
  19. SysUtils;
  20. type
  21. { TBrookDaemonApplication }
  22. TBrookDaemonApplication = class(TInterfacedObject, IBrookApplication)
  23. private
  24. function GetTerminated: Boolean;
  25. public
  26. constructor Create;
  27. function Instance: TObject;
  28. procedure CreateForm(AInstanceClass: TComponentClass; out AReference);
  29. procedure Run;
  30. procedure Terminate;
  31. property Terminated: Boolean read GetTerminated;
  32. end;
  33. { TBrookDaemonThread }
  34. TBrookDaemonThread = class(TThread)
  35. public
  36. constructor Create; virtual;
  37. procedure Execute; override;
  38. end;
  39. { TBrookHttpDaemon }
  40. TBrookHttpDaemon = class(TCustomDaemon)
  41. private
  42. FThread: TThread;
  43. public
  44. function Install: Boolean; override;
  45. function Uninstall: Boolean; override;
  46. function Start: Boolean; override;
  47. function Stop: Boolean; override;
  48. end;
  49. { TBrookHttpDaemonMapper }
  50. TBrookHttpDaemonMapper = class(TCustomDaemonMapper)
  51. public
  52. constructor Create(AOwner: TComponent); override;
  53. end;
  54. implementation
  55. var
  56. _BrookHttpApp: IBrookApplication = nil;
  57. function BrookHttpApp: IBrookApplication;
  58. begin
  59. if not Assigned(_BrookHttpApp) then
  60. begin
  61. _BrookHttpApp := TBrookApplication.Create;
  62. TBrookHttpApplication(_BrookHttpApp.Instance).ShowTermMsg := False;
  63. end;
  64. Result := _BrookHttpApp;
  65. end;
  66. { TBrookDaemonApplication }
  67. function TBrookDaemonApplication.GetTerminated: Boolean;
  68. begin
  69. Result := Application.Terminated;
  70. end;
  71. constructor TBrookDaemonApplication.Create;
  72. begin
  73. Application.Title := 'Brook HTTP daemon application';
  74. end;
  75. function TBrookDaemonApplication.Instance: TObject;
  76. begin
  77. Result := BrookHttpApp.Instance;
  78. end;
  79. procedure TBrookDaemonApplication.CreateForm(AInstanceClass: TComponentClass;
  80. out AReference);
  81. var
  82. VInstance: TObject;
  83. VReference: TComponent;
  84. begin
  85. VReference := AInstanceClass.Create(nil);
  86. TComponent(AReference) := VReference;
  87. VInstance := Instance;
  88. if VInstance is TComponent then
  89. TComponent(VInstance).InsertComponent(VReference);
  90. end;
  91. procedure TBrookDaemonApplication.Run;
  92. begin
  93. Application.Run;
  94. end;
  95. procedure TBrookDaemonApplication.Terminate;
  96. begin
  97. Application.Terminate;
  98. end;
  99. { TBrookDaemonThread }
  100. constructor TBrookDaemonThread.Create;
  101. begin
  102. inherited Create(True);
  103. FreeOnTerminate := True;
  104. end;
  105. procedure TBrookDaemonThread.Execute;
  106. begin
  107. BrookHttpApp.Run;
  108. end;
  109. { TBrookHttpDaemon }
  110. function TBrookHttpDaemon.Start: Boolean;
  111. begin
  112. Result := inherited Start;
  113. FThread := TBrookDaemonThread.Create;
  114. FThread.Start;
  115. end;
  116. function TBrookHttpDaemon.Stop: Boolean;
  117. begin
  118. Result := inherited Stop;
  119. FThread.Terminate;
  120. end;
  121. function TBrookHttpDaemon.Install: Boolean;
  122. {$IFDEF MSWINDOWS}
  123. var
  124. VSM: TServiceManager;
  125. {$ENDIF}
  126. begin
  127. Result := inherited Install;
  128. {$IFDEF MSWINDOWS}
  129. VSM := TServiceManager.Create(nil);
  130. try
  131. VSM.Connect;
  132. if VSM.Connected then
  133. VSM.StartService(BROOK_HTTP_DAEMON_NAME, nil);
  134. VSM.Disconnect;
  135. finally
  136. VSM.Free;
  137. end;
  138. {$ENDIF}
  139. WriteLn('Service installed.');
  140. WriteLn(BrookHttpServerTerminalMsg);
  141. end;
  142. function TBrookHttpDaemon.Uninstall: Boolean;
  143. {$IFDEF MSWINDOWS}
  144. var
  145. VSM: TServiceManager;
  146. {$ENDIF}
  147. begin
  148. Result := inherited Uninstall;
  149. {$IFDEF MSWINDOWS}
  150. VSM := TServiceManager.Create(nil);
  151. try
  152. VSM.Connect;
  153. if VSM.Connected then
  154. VSM.StopService(BROOK_HTTP_DAEMON_NAME, True);
  155. VSM.Disconnect;
  156. finally
  157. VSM.Free;
  158. end;
  159. {$ENDIF}
  160. WriteLn('Service uninstalled.');
  161. end;
  162. { TBrookHttpDaemonMapper }
  163. constructor TBrookHttpDaemonMapper.Create(AOwner: TComponent);
  164. var
  165. VDaemonDef: TDaemonDef;
  166. begin
  167. inherited Create(AOwner);
  168. VDaemonDef := DaemonDefs.Add as TDaemonDef;
  169. VDaemonDef.Description := BROOK_HTTP_DAEMON_DESCRIPTION;
  170. VDaemonDef.DisplayName := BROOK_HTTP_DAEMON_DISPLAYNAME;
  171. VDaemonDef.Name := BROOK_HTTP_DAEMON_NAME;
  172. VDaemonDef.DaemonClassName := BROOK_HTTP_DAEMON_CLASSNAME;
  173. VDaemonDef.WinBindings.ServiceType := stWin32;
  174. end;
  175. initialization
  176. RegisterDaemonClass(TBrookHttpDaemon);
  177. RegisterDaemonMapper(TBrookHttpDaemonMapper);
  178. BrookUnregisterApp;
  179. BrookRegisterApp(TBrookDaemonApplication.Create);
  180. end.