Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New unit for NetHTTP #33

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions sample/Examples.groupproj → samples/Examples.groupproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<Projects Include="..\tests\NtfyForDelphiTests.dproj">
<Dependencies/>
</Projects>
<Projects Include="win-service\NtfyService.dproj">
<Dependencies/>
</Projects>
</ItemGroup>
<ProjectExtensions>
<Borland.Personality>Default.Personality.12</Borland.Personality>
Expand Down Expand Up @@ -71,14 +74,23 @@
<Target Name="NtfyForDelphiTests:Make">
<MSBuild Projects="..\tests\NtfyForDelphiTests.dproj" Targets="Make"/>
</Target>
<Target Name="NtfyService">
<MSBuild Projects="win-service\NtfyService.dproj"/>
</Target>
<Target Name="NtfyService:Clean">
<MSBuild Projects="win-service\NtfyService.dproj" Targets="Clean"/>
</Target>
<Target Name="NtfyService:Make">
<MSBuild Projects="win-service\NtfyService.dproj" Targets="Make"/>
</Target>
<Target Name="Build">
<CallTarget Targets="VCLPublisher;VCLSubscriber;ConsolePublisher;ConsoleSubscriber;NtfyForDelphiTests"/>
<CallTarget Targets="VCLPublisher;VCLSubscriber;ConsolePublisher;ConsoleSubscriber;NtfyForDelphiTests;NtfyService"/>
</Target>
<Target Name="Clean">
<CallTarget Targets="VCLPublisher:Clean;VCLSubscriber:Clean;ConsolePublisher:Clean;ConsoleSubscriber:Clean;NtfyForDelphiTests:Clean"/>
<CallTarget Targets="VCLPublisher:Clean;VCLSubscriber:Clean;ConsolePublisher:Clean;ConsoleSubscriber:Clean;NtfyForDelphiTests:Clean;NtfyService:Clean"/>
</Target>
<Target Name="Make">
<CallTarget Targets="VCLPublisher:Make;VCLSubscriber:Make;ConsolePublisher:Make;ConsoleSubscriber:Make;NtfyForDelphiTests:Make"/>
<CallTarget Targets="VCLPublisher:Make;VCLSubscriber:Make;ConsolePublisher:Make;ConsoleSubscriber:Make;NtfyForDelphiTests:Make;NtfyService:Make"/>
</Target>
<Import Project="$(BDS)\Bin\CodeGear.Group.Targets" Condition="Exists('$(BDS)\Bin\CodeGear.Group.Targets')"/>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Provide a topic for publishing messages. You also must have SSL libraries for ru
``` pascal
Ntfy.Notification(
New.Notification
.Tags(['partying_face', 'warning', 'rotating_light'])
.Tags(TArray<String>.Create('partying_face', 'warning', 'rotating_light'))
);
```

Expand Down Expand Up @@ -133,4 +133,7 @@ Provide a topic for publishing messages. You also must have SSL libraries for ru
// to be delivered to unsubscribed clients
Ntfy.Cache(False);

// You can delay messages up to three days
Ntfy.Delay('4hs');

```
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions samples/win-service/NtfyService.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
program NtfyService;

uses
Vcl.SvcMgr,
Service.Module in 'src\Service.Module.pas' {NtfyModule: TService};

{$R *.RES}

begin
// Windows 2003 Server requires StartServiceCtrlDispatcher to be
// called before CoRegisterClassObject, which can be called indirectly
// by Application.Initialize. TServiceApplication.DelayInitialize allows
// Application.Initialize to be called from TService.Main (after
// StartServiceCtrlDispatcher has been called).
//
// Delayed initialization of the Application object may affect
// events which then occur prior to initialization, such as
// TService.OnCreate. It is only recommended if the ServiceApplication
// registers a class object with OLE and is intended for use with
// Windows 2003 Server.
//
// Application.DelayInitialize := True;
//
if not Application.DelayInitialize or Application.Installing then
Application.Initialize;
Application.CreateForm(TNtfyModule, NtfyModule);
Application.Run;
end.
943 changes: 943 additions & 0 deletions samples/win-service/NtfyService.dproj

Large diffs are not rendered by default.

Binary file added samples/win-service/NtfyService.res
Binary file not shown.
9 changes: 9 additions & 0 deletions samples/win-service/src/Service.Module.dfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object NtfyModule: TNtfyModule
OldCreateOrder = False
DisplayName = 'NtfyService'
AfterInstall = ServiceAfterInstall
OnStart = ServiceStart
OnStop = ServiceStop
Height = 150
Width = 215
end
75 changes: 75 additions & 0 deletions samples/win-service/src/Service.Module.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
unit Service.Module;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs, Notify;

type
TNtfyModule = class(TService)
procedure ServiceAfterInstall(Sender: TService);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
public
function GetServiceController: TServiceController; override;
end;

var
NtfyModule: TNtfyModule;

implementation

uses
System.Win.Registry;

{$R *.dfm}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
NtfyModule.Controller(CtrlCode);
end;
function TNtfyModule.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;

procedure TNtfyModule.ServiceAfterInstall(Sender: TService);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create(KEY_READ or KEY_WRITE);
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('\SYSTEM\CurrentControlSet\Services\' + Name, False) then
begin
Reg.WriteString('Description', 'Ntfy Client Service');
Reg.CloseKey;
end;
finally
Reg.Free;
end;
end;

procedure TNtfyModule.ServiceStart(Sender: TService; var Started: Boolean);
begin
// You can log your messages with a service. I could not find a solution
// for pushing windows notifications on win services.
Ntfy.Subscribe('your-very-secret-topic',procedure (AEvent: INotifyEvent)
var LPath: TStringStream;
begin
LPath := TStringStream.Create(AEvent.MessageContent);
try
LPath.SaveToFile(ExtractFilePath(ParamStr(0)) + 'ntfy-logs.txt');
finally
LPath.Free;
end;
end);
end;

procedure TNtfyModule.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
Ntfy.Unsubscribe;
end;

end.
5 changes: 5 additions & 0 deletions src/Notify.Api.Contract.pas
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ interface
function AbortStream: INotifyApi;
end;

INotifyApiFactory = interface
['{66E16F57-69CD-41CC-9457-866A4E5D396E}']
function Api: INotifyApi;
end;

implementation

end.
37 changes: 37 additions & 0 deletions src/Notify.Api.Factory.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
unit Notify.Api.Factory;

interface

uses
Notify.Api.Contract;

type
TNotifyApiFactory = class(TInterfacedObject, INotifyApiFactory)
public
class function New: INotifyApiFactory;
function Api: INotifyApi;
end;

implementation

uses
Notify.Api.NetHTTP,
Notify.Api.Indy;

{ TNotifyApiFactory }

function TNotifyApiFactory.Api: INotifyApi;
begin
{$IFDEF INDY}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs documenting in the README.md. It's effectively a part of your public interface "how to switch between Indy/NetHTTP".

Also, consider renaming this compiler define to NTFY_INDY. Consider, with such a generic define based on the name of a very popular network component set, what would happen if your library was included in other large projects that may have libraries with the same defines for other purposes? You may even consider having a more descriptive name NTFY_DO_A_THING_WITH_INDY so it becomes self-documenting when people include it in their projects.

Copy link
Member Author

@p-samuel p-samuel Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking out for improvement. I'll make the necessary changes. I think NTFY_INDY is more descriptive name as you mentioned as it avoids clashing with other name that a large library might have. I'll also provide documentation of that in the README.md file. I'll update the files with the above review before merging.

Copy link
Member Author

@p-samuel p-samuel Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhatherall, your review has been submitted. Thanks for your contribution.

Result := TNotifyApiIndy.New
{$ELSE}
Result := TNotifyApiNetHTTP.New;
{$IFEND}
end;

class function TNotifyApiFactory.New: INotifyApiFactory;
begin
Result := Self.Create;
end;

end.
6 changes: 3 additions & 3 deletions src/Notify.Api.Indy.pas
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ procedure TSSEThread.DoOnWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount
var
LEventString: UTF8String;
LStrings: TArray<String>;
LString: UTF8String;
LString: String;
begin

if Terminated then
Expand All @@ -353,10 +353,10 @@ procedure TSSEThread.DoOnWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount
if LEventString = FCloseConnectionMessage then
FIdHttp.Socket.Close;

LStrings := SplitString(LEventString, #$A);
LStrings := SplitString(UTF8ToString(LEventString) , #$A);

for LString in LStrings do
NxHorizon.Instance.Post<TNotifySubscriptionEvent>(LString);
NxHorizon.Instance.Post<TNotifySubscriptionEvent>(Utf8String(LString));

end;

Expand Down
Loading