Broadcast to all stacks?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Broadcast to all stacks?

Post by AxWald » Wed Oct 14, 2020 9:53 am

Hi,
seems I got a knot in my brain again. Maybe someone can help?

I have a "mainStack" as a standalone. There's a number of other stacks, opened by the mainStack as required. These are plain LC stacks, no subStacks.
Now I want to dispatch/ send/ push/ whatever a message from the mainStack to every other open stack that has a handler to respond. How to do this?

I don't want to go lengths to achieve this - this should happen often, so it must be simple, fast & frugal. Using "topStack" doesn't work, this isn't necessarily the (only) target. I'm sure there's a simple solution, but it seems I'm in blackout here.

Any ideas? Thank you for any help!

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Broadcast to all stacks?

Post by ClipArtGuy » Wed Oct 14, 2020 10:35 am

The openstacks function might help?

https://livecode.com/resources/api/#liv ... openstacks

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Broadcast to all stacks?

Post by AxWald » Wed Oct 14, 2020 11:16 am

Yes,

I could loop through the openStacks, ignore all that aren't mine, determine which of mine can respond & dispatch explicitly to each of those.
And thus create a "hickUp" each time this happens. Guess I should explain more:

My "mainStack" has a handler running that periodically calls itself (send "upDateClock" to me in myVar seconds). It sets a clock field, and checks if we still have internet, and sets some cProps accordingly.
Now I just want to dispatch (?) a msg from there, "into the blue", not to a certain stack.
So that any stack that has a suitable handler can respond - for instance, adjust an icon to show the IN status.

I'm taking much care in the "upDateClock" handler to be as unobtrusive as possible (watching the pendingMessages etc. ...). No way to do a lot of calculating, or sending more than 1 message. I'd rather scrap the whole idea.

So I need to "push" a message among the stacks, without knowing who'll receive it. Without polling from the receiver side. Is this possible? Any ideas?

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Broadcast to all stacks?

Post by FourthWorld » Wed Oct 14, 2020 1:32 pm

The message path is very flexible, but there is no built-in way to do a broadcast.

A loop that uses dispatch is your best bet here, and you'd be in good company since that's pretty much what the IDE does for a similar need.

You could shorten the broadcast time a little by having stacks register themselves to the broadcaster when they open, so the broadcast has fewer things to loop through.

But even without that I think you'll find the loop pretty fast. I'd be surprised if it takes more than a few microseconds, maybe less unless you have a ginormous number of stack files open.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Broadcast to all stacks?

Post by dunbarx » Wed Oct 14, 2020 2:06 pm

What Richard said.

But I am confused. Your mainStack explicitly opens other stacks. Don't you therefore have a precise list of those stacks, to which you can send a message?

Craig

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Broadcast to all stacks?

Post by AxWald » Wed Oct 14, 2020 4:22 pm

Hi,

Craig, maintaining a list of stacks I have opened doesn't make much sense to me:
Such a list must be correct, else it causes mistakes - what if for whatever reason a stack in background dies quietly? So this list would need to be periodically verified. At each "Open" I had to add, at each "Close" to subtract. And then still I had only a list of stacks that I'd need to send messages to individually. That I can have already with "the openStacks".

Richard, ClipArtGuy, I tested your proposals, and it would actually work!

Code: Select all

   put the openStacks into myStax
   repeat for each line L in myStax
      if (L begins with "awa") and not (L ends with "Main") then
         put L & CR after myVar
      end if
   end repeat
   return char 1 to -2 of myVar
This gives me a list of stacks that I may have open (they all begin with "awa", and the mainStack end with "Main" ...). Getting this is about 1 ms on my machine, nothing compared to the ~ 140 ms my "upDateClock" already uses.


But, realizing that my idea of a "broadcast" may be impossible as I had envisioned it, I thought some more. And came to the conclusion that it would be nice to have, but that actually the topStack is the only one that really needs it. So:

Code: Select all

   dispatch "ourNetStatus" to the topStack with \
         (the cNetStat of this stack) & comma & (the cHaveNet of this stack)
must do it. "cNetStat" is the mode the program is in: "onLine mode" or "offLine mode". "cHaveNet" shows if we have internet, or not. The monitoring is done by the mainStack in the background, but the stack actually active should be able to respond if one of these parameters changes. And this works now.

Thx & have fun!

PS: I wonder if there wouldn't be demand for such a "broadcast" function?
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Broadcast to all stacks?

Post by dunbarx » Wed Oct 14, 2020 6:02 pm

Axwald.

It never occurred to me that a stack might quietly die. It is a matter of style, I suppose, whether keeping track of stacks that are explicitly opened or closed is an issue. But I know you can find a way that suits you.

Craig

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Broadcast to all stacks?

Post by AxWald » Wed Oct 14, 2020 6:59 pm

Craig,

this is meant to run on Win & Android, both. The same program.
On Win, it's easy - this is rock solid. Stacks rarely ever crash, as long as you use a stable version for development & test sufficiently. Basically all of my customers will have Win10 meanwhile, and I haven't had a reported crash for years.
On Android, it's different. I need to cover versions from 4.1 up, with widely different features/ bloatware. I need to cover various architectures, and for some of these, I'm forced to use LC versions less stable than desirable to compile with.

When I use "Test" & send my current (flawlessly running on Win ...) version to Android, I'm most often flabbergasted what all can go wrong. For fun & recreation, my newest, just found madness (on Android):
When my mainStack is active, I can switch to other apps, or have the tablet go to sleep - it just stays open, as desired.
When any other stack is active & I do this, Android will close my app ;-)

So, I cannot take anything as granted, and try to avoid anything that even remotely could, maybe, smell dubiously. Getting the openStacks via above mentioned function is clean. Manually keeping & maintaining a list of open stacks by far less, IMHO.

Thx, and have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Broadcast to all stacks?

Post by FourthWorld » Wed Oct 14, 2020 7:02 pm

AxWald wrote:
Wed Oct 14, 2020 4:22 pm
I wonder if there wouldn't be demand for such a "broadcast" function?
Indeed there is. And since it takes only a few lines of code, we have it in just a minute's work whenever we need it. :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mimu
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 76
Joined: Tue Mar 05, 2013 7:00 pm
Location: Berlin
Contact:

Re: Broadcast to all stacks?

Post by mimu » Wed Oct 14, 2020 7:42 pm

Hi axwald,

for me it sounds as if you are talking about the publish/subscribe pattern.

Andre Garzia has put together a simple lib for this, its called:
aagPubSubLib

Usage is explained in his book "LiveCode Advanced Application Architecture"
You can get it here:
https://andregarzia.com/livecode/

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Broadcast to all stacks?

Post by stam » Fri Nov 13, 2020 3:56 pm

mimu wrote:
Wed Oct 14, 2020 7:42 pm
Hi axwald,

for me it sounds as if you are talking about the publish/subscribe pattern.

Andre Garzia has put together a simple lib for this, its called:
aagPubSubLib

Usage is explained in his book "LiveCode Advanced Application Architecture"
You can get it here:
https://andregarzia.com/livecode/
This exactly. I came across Andre’s publish/subscribe library and was initially quite excited as a newcomer to LC, before I realised that most of the time this extra work isn’t really needed.

But your use-case sounds like it fits the bill, you should definitely have a look at using an observer pattern like this...

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Broadcast to all stacks?

Post by stam » Wed Mar 06, 2024 10:00 am

As a belated answer to the OP as well as for anyone searching for Publish-Subscribe functionality in LC (not always needed, but good to have if it is): https://github.com/stam66/skPubSub

Post Reply

Return to “Talking LiveCode”