How can i run another PB application with Pipe ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

How can i run another PB application with Pipe ?

Post by skinkairewalker »

hi everyone !

i am not understand how can i speak with another application compiled with PB , using Pipe ...
how another application will "receive" message from pipe main application ? using input() ?
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: How can i run another PB application with Pipe ?

Post by kenmo »

Try ReadConsoleData().

Here's an example:

Code: Select all

IsSlave = Bool(ProgramParameter(0) = "-slave")
If OpenConsole()
  
  If (IsSlave)
    PrintN("Close this window to finish.")
    PrintN("")
    PrintN("Receiving strings...")
    BSize = 1024
    *Buffer = AllocateMemory(BSize)
    While (#True)
      If ReadConsoleData(*Buffer, BSize) ; This should be in a background thread...
        In.s = PeekS(*Buffer, -1, #PB_UTF8)
        PrintN(In)
      EndIf
      Delay(1)
    Wend
    FreeMemory(*Buffer)
  EndIf
  
  If (Not IsSlave)
    PrintN("Starting slave program...")
    PrintN("")
    *Slave = RunProgram(ProgramFilename(), "-slave", GetCurrentDirectory(), #PB_Program_Open | #PB_Program_Write)
    If (*Slave)
      PrintN("Sending strings...")
      While ProgramRunning(*Slave)
        If WriteProgramString(*Slave, FormatDate("The time is %hh:%ii:%ss", Date()), #PB_UTF8)
          PrintN("Sent!")
        EndIf
        Delay(1000)
      Wend
      PrintN("Done!")
      CloseProgram(*Slave)
    EndIf
  EndIf
  
  CloseConsole()
EndIf
Be warned, ReadConsoleData() blocks the program until some input is available! And there is no AvailableConsoleData() command to check first... So a thread might help.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Re: How can i run another PB application with Pipe ?

Post by skinkairewalker »

kenmo wrote:Try ReadConsoleData().

Here's an example:

Code: Select all

IsSlave = Bool(ProgramParameter(0) = "-slave")
If OpenConsole()
  
  If (IsSlave)
    PrintN("Close this window to finish.")
    PrintN("")
    PrintN("Receiving strings...")
    BSize = 1024
    *Buffer = AllocateMemory(BSize)
    While (#True)
      If ReadConsoleData(*Buffer, BSize) ; This should be in a background thread...
        In.s = PeekS(*Buffer, -1, #PB_UTF8)
        PrintN(In)
      EndIf
      Delay(1)
    Wend
    FreeMemory(*Buffer)
  EndIf
  
  If (Not IsSlave)
    PrintN("Starting slave program...")
    PrintN("")
    *Slave = RunProgram(ProgramFilename(), "-slave", GetCurrentDirectory(), #PB_Program_Open | #PB_Program_Write)
    If (*Slave)
      PrintN("Sending strings...")
      While ProgramRunning(*Slave)
        If WriteProgramString(*Slave, FormatDate("The time is %hh:%ii:%ss", Date()), #PB_UTF8)
          PrintN("Sent!")
        EndIf
        Delay(1000)
      Wend
      PrintN("Done!")
      CloseProgram(*Slave)
    EndIf
  EndIf
  
  CloseConsole()
EndIf
Be warned, ReadConsoleData() blocks the program until some input is available! And there is no AvailableConsoleData() command to check first... So a thread might help.

thanks by you answer !
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Re: How can i run another PB application with Pipe ?

Post by skinkairewalker »

when i add flag parameter in RunProgram "#PB_Program_Read" to receive message from *slave . happens it >

screenshot > http://prntscr.com/dh1sk1


slave dont receives message from master pipe .... why happens it ?
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Re: How can i run another PB application with Pipe ?

Post by skinkairewalker »

well ...
im trying again receive message from slave , but i dont know how can i "send message" to máster ...

máster pipe code >

Code: Select all

Compiler = RunProgram("test.exe", "", GetCurrentDirectory(), #PB_Program_Open | #PB_Program_Read)
  Output$ = ""
  If Compiler
    While ProgramRunning(Compiler)
      If AvailableProgramOutput(Compiler)
        Output$ + ReadProgramString(Compiler) + Chr(13)
      EndIf
    Wend
    Output$ + Chr(13) + Chr(13)
    Output$ + "Exitcode: " + Str(ProgramExitCode(Compiler))
    Debug Output$
    CloseProgram(Compiler) ; Close the connection to the program
  EndIf

slave pipe code >

Code: Select all

*Buffer = AllocateMemory(2048)
OpenConsole()
;PrintN("HelloWorld") // Dont Works ...
PokeS (*Buffer,"Testing !")
WriteConsoleData(*Buffer,2048) ; // dont works 
The result of WriteConsoleData and Printn it just "first" char of message ... > http://prntscr.com/dhazqs
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Re: How can i run another PB application with Pipe ?

Post by pdwyer »

I'm looking at this as well...

I can have the master spawn the slaves (runprogram with #PB_Program_Write|#PB_Program_Open|#PB_Program_Read)
Slaves send master data with printN() all good
master receives data in a loop with AvailableProgramOutput and ReadProgramString
master sends data to the slave with writeprogramstring() (needs #PB_unicode)

Problem is that slave getting data from the master needs to use ReadConsoleData() (I think)
and this is blocking the console!! (as mentioned above)

I tried moving ReadConsoleData to a thread but it still locks the comms so that the slave won't send any more printn()'s until it receives something so it can unlock the consol

There doesn't seem to be any AvailableProgramOutput() for the slave (as mentioned above) and docs for ReadConsoleData() say:
"This function waits until there is some input to read. It will only return without reading data if there was an error or an EOF (End Of File) condition"

I thought the thread would work though in the slave.

I guess I could re-code as a network module and have the slaves spawned by the master reconnect back to it as a tcp server...

Any workarounds for this that people know of? I guess I could have the master send a heartbeat to keep the slaves from locking up....
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How can i run another PB application with Pipe ?

Post by infratec »

Since both programs are from you,
simply use UDP over a local port.
This is much easier to handle.
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How can i run another PB application with Pipe ?

Post by mk-soft »

I think I have something ready ...
STX and ETX are used here for the clean separation of the texts.

UDP Local Network Short Text Sending
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Axolotl
Enthusiast
Enthusiast
Posts: 435
Joined: Wed Dec 31, 2008 3:36 pm

Re: How can i run another PB application with Pipe ?

Post by Axolotl »

I have never used Purebasic and Pipes together.
On Window I use WM_COPYDATA to communicate between two apps on the same computer.
(but now this is a message that does not answer the question, sorry.)
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How can i run another PB application with Pipe ?

Post by mk-soft »

It is better to always work with two channels. UDP server only as receiver and UPD client only as sender. This means that two ports are always required.

Slave = Network Server (receiver)
Master = Network Client (sender)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Re: How can i run another PB application with Pipe ?

Post by pdwyer »

I think I'll switch to networking.

2-way async comms is not what STDIO is good for it seems
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Re: How can i run another PB application with Pipe ?

Post by pdwyer »

sooo, all is not quite fine... I can't stop the two programs talking STDIO to eachother.

The master still spawns all the slaves with RunProgram() (and no flags for connect, open, read write etc)
master and slaves are consol apps and have consols with data on them

but all the slave's printn() data is sent to the master and appears on the masters consol :(

When I started this the master spawned lots of windows and I had to use hide etc but now I'm using no flags and all IO still goes back.

They talk fine on the network, the slaves phone home but when the master sends the slave data, the slave goes to display it on his consol but there is no window for the slave now for some reason and it appears on the masters consol.

It's like the compiler is not realising I removed the flags (or I'm overlooking something really dumb)

Previously the slaves shows as separate apps in task manager, now they show under the master...
bots(i) = RunProgram("slave.exe" ,Str(i), GetPathPart(ProgramFilename()),#PB_Program_UTF8 )
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Re: How can i run another PB application with Pipe ?

Post by pdwyer »

okay fixed,
the slaves need to be compiled as windows and use openconsol() to spawn new windows. if compiled as console then IO goes back to the parent
likewise the processes are now indepenent in task manager
interesting... master compiler options didn't matter though
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Post Reply