Recent

Author Topic: Brook web service embedded in Lazarus GUI program  (Read 3944 times)

SirTwist

  • New Member
  • *
  • Posts: 21
Brook web service embedded in Lazarus GUI program
« on: October 21, 2017, 08:42:28 pm »
Hi all,

I want to setup a brook service within a GUI program. My first attempts with the framework resulted in standalone console programs. I created a new project with a simple main window, and tried to add another unit "Brooker", but I do not know what to select then. I tried "embedded server" or "embedded daemon server", but don't know how to access the daemon.

Anyone who had this problem before and could give me a hint?

Thanks in advance!

Kind regards,
Michael

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Brook web service embedded in Lazarus GUI program
« Reply #1 on: October 22, 2017, 11:11:43 pm »
Both LCL's Application and Brook's BrookApp are TCustomApplication descendant which uses infinite loop as main program flow. Hence, running both in a single (main) thread seems impossible. Since GUI can only be run from main thread, Brook must be run on second thread, prior to Application.Run or through some action (e.g. button click) in the GUI. Everything else could stay the same.

Note that Brook doesn't seem to be designed to be started and stopped indefinitely inside one program flow (I just tested it). There are intended memory leaks (which don't hurt in a way it's designed) and some other quirks like terminating the server can only be done once, subsequent termination won't do anything.
« Last Edit: October 23, 2017, 12:33:36 am by Leledumbo »

SirTwist

  • New Member
  • *
  • Posts: 21
Re: Brook web service embedded in Lazarus GUI program
« Reply #2 on: October 31, 2017, 02:58:17 pm »
That was a good hint, thank you!

I created a small component (TmsBrookCompoment). When this component is set to active, a simple thread and a TBrookHttpServerHandler is created. Within the thread's Execute method, the handler's run method is called:

Code: [Select]
procedure TmsBrookDaemonThread.Execute;
begin
  if Assigned(fHandler)
    then fHandler.Run
    else { report some error here }   
end;

This seems to be working fine, I will do a lot more testing - current status is "hooray it compiles, let's ship it". But I will not ship it now. When I finished some further testing, I will attach it this post.

Bit I do have a memory leakage reported in the line "fHandler.Run": one block of 152 bytes, the other one 24 bytes. I cannot trace further into it. Perhaps some socket allocation within TFPCustomHttpServer? I will check this against an original BrookApplication.





Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Brook web service embedded in Lazarus GUI program
« Reply #3 on: November 01, 2017, 05:22:14 am »
Attached is how I do it. It is narrower than your implementation, though.
Bit I do have a memory leakage reported in the line "fHandler.Run": one block of 152 bytes, the other one 24 bytes. I cannot trace further into it. Perhaps some socket allocation within TFPCustomHttpServer? I will check this against an original BrookApplication.
Like I said eerlier, it does have intended memory leaks. I think that author doesn't care about it since by design it will be run just once so even if it leaks, it will be at the program end, which doesn't matter. Now it matters when we want to be able to start and stop the server in a single program indefinitely by some GUI action.

 

TinyPortal © 2005-2018