Recent

Author Topic: SimpleIPC - should it work ?  (Read 2082 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
SimpleIPC - should it work ?
« on: December 07, 2019, 02:55:22 am »
Hi Folks, I have been playing with the two SimpleIPC components on the Lazarus System Tab without any success. In its simplest form, here is what I am doing -
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. begin
  3.     SimpleIPCClient1.ServerID:='tomboy-ng';
  4.     SimpleIPCServer1.ServerID:='tomboy-ng';
  5.     if SimpleIPCClient1.ServerRunning then begin
  6.         Label1.Caption := 'I am a client';
  7.         SimpleIPCClient1.Active := true;
  8.         SimpleIPCClient1.SendStringMessage('SHOWSEARCH');
  9.     end else begin
  10.         Label1.Caption := 'I am a server';
  11.         SimpleIPCServer1.Global:=True;                  // anyone can connect
  12.         SimpleIPCServer1.Active := true;                // start listening
  13.     end;
  14. end;                  
  15.  
  16. procedure TForm1.SimpleIPCServer1Message(Sender: TObject);
  17. begin
  18.     SimpleIPCServer1.ReadMessage;
  19.     Label1.Caption := SimpleIPCServer1.StringMessage;
  20. end;            

I have used the Object Inspector to create the event. Running two instances, the first becomes the 'server' and the second becomes the 'client'. The client sends a message to the server. In practice, the client does recognise that there is a server present, good, and apparently sends it's message but the Server never responds.

Have I missed something ?  Documentation is a bit sparse but seems to indicate thats all I need.  I am wondering if more modern forms of Linux block what ever communication channel these components use ? Likely given our interests in security. I can do the same thing with DBus (only on linux) but it seems overkill if this simple approach does work.

I have attached my minimalist project.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: SimpleIPC - should it work ?
« Reply #1 on: December 07, 2019, 03:07:47 am »

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: SimpleIPC - should it work ?
« Reply #2 on: December 07, 2019, 03:11:38 am »
Hi Folks, I have been playing with the two SimpleIPC components on the Lazarus System Tab without any success. In its simplest form, here is what I am doing -
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. begin
  3.     SimpleIPCClient1.ServerID:='tomboy-ng';
  4.     SimpleIPCServer1.ServerID:='tomboy-ng';
  5.     if SimpleIPCClient1.ServerRunning then begin
  6.         Label1.Caption := 'I am a client';
  7.         SimpleIPCClient1.Active := true;
  8.         SimpleIPCClient1.SendStringMessage('SHOWSEARCH');
  9.     end else begin
  10.         Label1.Caption := 'I am a server';
  11.         SimpleIPCServer1.Global:=True;                  // anyone can connect
  12.         SimpleIPCServer1.Active := true;                // start listening
  13.     end;
  14. end;                  
  15.  
  16. procedure TForm1.SimpleIPCServer1Message(Sender: TObject);
  17. begin
  18.     SimpleIPCServer1.ReadMessage;
  19.     Label1.Caption := SimpleIPCServer1.StringMessage;
  20. end;            

I have used the Object Inspector to create the event. Running two instances, the first becomes the 'server' and the second becomes the 'client'. The client sends a message to the server. In practice, the client does recognise that there is a server present, good, and apparently sends it's message but the Server never responds.

Have I missed something ?  Documentation is a bit sparse but seems to indicate thats all I need.  I am wondering if more modern forms of Linux block what ever communication channel these components use ? Likely given our interests in security. I can do the same thing with DBus (only on linux) but it seems overkill if this simple approach does work.

I have attached my minimalist project.

Davo

Both client and server are used in units in:

components/chmhelp/lhelp/
tools/debugserver/

Should give a good representation of their usage.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: SimpleIPC - should it work ?
« Reply #3 on: December 07, 2019, 03:53:02 am »
... Do you know the github demo page? .....

Thanks winin, no, did not know about it.   But they do not compile. The client is an easy fix but the server seems to need something not there. But maybe a look over the code will help.

@dsiders, also a good suggestion. If its used in lhelp, must still be usable. I'll have a good poke around in there. Thanks.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: SimpleIPC - should it work ?
« Reply #4 on: December 07, 2019, 09:05:17 am »
OK, this does work as expected -

Code: Pascal  [Select][+][-]
  1. procedure TForm1.MessageQueued(Sender : TObject);
  2. begin
  3.     SimpleIPCServer1.ReadMessage;
  4.     Label1.Caption := SimpleIPCServer1.StringMessage;
  5. end;
  6.  
  7. procedure TForm1.FormShow(Sender: TObject);
  8. begin
  9.     SimpleIPCClient1.ServerID:='tomboy-ng';
  10.     SimpleIPCServer1.ServerID:='tomboy-ng';
  11.     if SimpleIPCClient1.ServerRunning then begin
  12.         Label1.Caption := 'I am a client';
  13.         SimpleIPCClient1.Active := true;
  14.         SimpleIPCClient1.SendStringMessage('SHOWSEARCH');
  15.         SimpleIPCClient1.Active := false;
  16.     end else begin
  17.         Label1.Caption := 'I am a server';
  18.         SimpleIPCServer1.OnMessageQueued:=@MessageQueued;
  19.         SimpleIPCServer1.Global:=True;                  // anyone can connect
  20.         //SimpleIPCServer1.Active := true;
  21.         SimpleIPCServer1.StartServer(True);             // start listening, threaded
  22.     end;
  23. end;                  

Note two changes from my first attempt -
* To start the server we call 'StartServer', setting it to Active is not the same thing !
* Don't use the Object Inspector to set a call back function. Just pass SimpleIPCServer1.OnMessageQueued the address of the function you want to call. I have no idea why .....

Thanks folks for pointing me in right direction. The demos in the fpc source was particularly helpful, in my case /usr/share/fpcsrc/3.0.4/packages/fcl-process/examples

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018