Jonathan H
2016-Nov-27 14:07 UTC
[asterisk-users] Non-global variable that follows channel?
Thanks, Max. Yes, of course, you are right, and I am an idiot because I was tired and putting underscores before the variable name when I read it back! Then I forgot to post the followup email to say I had figured it out. Now, this SHARED was not something I was aware of, but looked like an ideal solution to passing variables BACK from to the parent channel. However, it does not seem to be very reliable. Code: [svtest1] exten => s,1,Answer() same => n,Verbose(1,Answered channel:${CHANNEL}) same => n,Dial(Local/s at svtest2,,g) same => n,Verbose(1,***In channel:${CHANNEL} sharedVar: ${SHARED(sharedVar,Local/s at svtest2)} ) same => n,Hangup() [svtest2] exten => s,1,NoOp() same => n,Set(SHARED(sharedVar,Local/s at svtest2)="I have been set in svtest2") same => n,Verbose(1,***In channel:${CHANNEL} sharedVar: ${SHARED(sharedVar,Local/s at svtest2)}) same => n,Answer() same => n,Hangup() Output, 4 times from console, 4 times from a normal PJSIP dialin: Of the 8 attempts. in only 3 instances the variable survives the journey back to the parent channel, and seemingly randomly, too. Am I using it incorrectly? CLI> originate local/s at svtest1 application echo Answered channel:Local/s at svtest1-00000027;2 ***In channel:Local/s at svtest2-00000028;2 sharedVar: "I have been set in svtest2" ***In channel:Local/s at svtest1-00000027;2 sharedVar: CLI> originate local/s at svtest1 application echo Answered channel:Local/s at svtest1-00000029;2 ***In channel:Local/s at svtest2-0000002a;2 sharedVar: "I have been set in svtest2" ***In channel:Local/s at svtest1-00000029;2 sharedVar: CLI> originate local/s at svtest1 application echo Answered channel:Local/s at svtest1-0000002b;2 ***In channel:Local/s at svtest2-0000002c;2 sharedVar: "I have been set in svtest2" ***In channel:Local/s at svtest1-0000002b;2 sharedVar: "I have been set in svtest2" CLI> originate local/s at svtest1 application echo Answered channel:Local/s at svtest1-0000002d;2 ***In channel:Local/s at svtest2-0000002e;2 sharedVar: "I have been set in svtest2" ***In channel:Local/s at svtest1-0000002d;2 sharedVar: Answered channel:PJSIP/6001-00000007 ***In channel:Local/s at svtest2-0000002f;2 sharedVar: "I have been set in svtest2" ***In channel:PJSIP/6001-00000007 sharedVar: "I have been set in svtest2" Answered channel:PJSIP/6001-00000008 ***In channel:Local/s at svtest2-00000030;2 sharedVar: "I have been set in svtest2" ***In channel:PJSIP/6001-00000008 sharedVar: Answered channel:PJSIP/6001-00000009 ***In channel:Local/s at svtest2-00000031;2 sharedVar: "I have been set in svtest2" ***In channel:PJSIP/6001-00000009 sharedVar: Answered channel:PJSIP/6001-0000000a ***In channel:Local/s at svtest2-00000032;2 sharedVar: "I have been set in svtest2" ***In channel:PJSIP/6001-0000000a sharedVar: "I have been set in svtest2" On 27 November 2016 at 11:39, Max Grobecker <max.grobecker at ml.grobecker.info> wrote:> Hi, > > is channel variable inheritance working for your setup? > Passing variables to other channels can normally simply be done by naming the variable with one or two prefixed undersorces > to make it available to the channel that is created from that one defining the variable. > But I have no idea if it's getting inherited to Gosub called from a Dial command... > -> https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+Application_Set > > If that is not working for you, you might use the SHARED() variables which are kind of global accessible by the channel ID. > So you might call your Gosub with only the (unique) reference name of the variables you wish to pass and then call it from your Gosub. > -> https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+Function_SHARED > > > Greetings, > Max > > > Am 23.11.2016 um 13:06 schrieb Jonathan H: >> Related to http://lists.digium.com/pipermail/asterisk-users/2016-November/290384.html, >> at the moment I'm passing one variable via DIAL. >> >> Now I'd like to pass a whole bunch, and my idea was to rather than >> having a great string of >> >> b(synctest3b^setVar^1(something)^2(more things)^3(etc)) >> >> and then get them with ARG1..ARGn etc, I could bundle the whole lot >> into a HASH and then unbundle them at the called channel. >> >> Passing the HASH as a var isn't working (I wasn't expecting it to!) >> but is there any other way of doing this, or is it setVar for each >> one? >> > > > -- > _____________________________________________________________________ > -- Bandwidth and Colocation Provided by http://www.api-digital.com -- > > Check out the new Asterisk community forum at: https://community.asterisk.org/ > > New to Asterisk? Start here: > https://wiki.asterisk.org/wiki/display/AST/Getting+Started > > asterisk-users mailing list > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users
Richard Mudgett
2016-Nov-27 16:16 UTC
[asterisk-users] Non-global variable that follows channel?
On Sun, Nov 27, 2016 at 8:07 AM, Jonathan H <lardconcepts at gmail.com> wrote:> Thanks, Max. > > Yes, of course, you are right, and I am an idiot because I was tired > and putting underscores before the variable name when I read it back! > Then I forgot to post the followup email to say I had figured it out. > > Now, this SHARED was not something I was aware of, but looked like an > ideal solution to passing variables BACK from to the parent channel. > > However, it does not seem to be very reliable. > > Code: > > [svtest1] > exten => s,1,Answer() > same => n,Verbose(1,Answered channel:${CHANNEL}) > same => n,Dial(Local/s at svtest2,,g) > same => n,Verbose(1,***In channel:${CHANNEL} sharedVar: > ${SHARED(sharedVar,Local/s at svtest2)} ) > same => n,Hangup() > > [svtest2] > exten => s,1,NoOp() > same => n,Set(SHARED(sharedVar,Local/s at svtest2)="I have been set > in svtest2") > same => n,Verbose(1,***In channel:${CHANNEL} sharedVar: > ${SHARED(sharedVar,Local/s at svtest2)}) > same => n,Answer() > same => n,Hangup() >There are a few problems with the way you are trying to use SHARED here. 1) Dial with the g option continues in the dialplan when the called channel hangs up and you are accessing a SHARED variable on the called channel while it is being destroyed. Thus it may or may not still exist when you attempt to access it. 2) You are using local channels. Remember local channels always come in pairs. The local channel name you are using to reference the SHARED variable is ambiguous. Not only do you not know which half of a local channel pair you will get, you may not even get one of the local channels the Dial created for this call if you have more than one of these calls happening in parallel. Executing this CLI command: CLI> originate local/s at svtest1 application echo Your dialplan creates this channel chain when Local/s at svtest2 executes the Answer: Echo() -- Local/s at svtest1-00000000;1 -- Local/s at svtest1-00000000;2 -- Local/s at svtest2-00000001;1 -- Local/s at svtest2-00000001;2 -- Answer() These problems can be avoided if we make a couple of changes. 1) Change which channel maintains the SHARED variables to the calling channel. 2) Pass the calling channel name to the called channel using variable inheritance. [svtest1] exten = s,1,NoOp() same = n,Answer() same = n,Set(__MY_CALLER=${CHANNEL(name)}) same = n,Dial(Local/s at svtest2,,g) same = n,NoOp(Returned SHARED(sharedVar) = '${SHARED(sharedVar)'} same = n,Hangup() [svtest2] exten = s,1,NoOp() exten = n,Set(SHARED(sharedVar,MY_CALLER)="I have been set in svtest2 by ${CHANNEL(name)}") exten = n,Answer() exten = n,Hangup() Richard -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.digium.com/pipermail/asterisk-users/attachments/20161127/cb534a2f/attachment.html>
Jonathan H
2016-Nov-27 17:13 UTC
[asterisk-users] Non-global variable that follows channel?
Thanks, Richard - your code does indeed work reliably 100% of the time, and thank you for that explanation. I do think the docs at https://wiki.asterisk.org/wiki/display/AST/Asterisk+14+Function_SHARED could do with more clarification. BTW, there were a couple of typos in your code, so for anyone who wants to copy/paste, here's Richard's code WITH the curly braces ( Set(SHARED(sharedVar,MY_CALLER) should be Set(SHARED(sharedVar,${MY_CALLER}) ) [svtest1] exten => s,1,Answer() same => n,Verbose(1,Answered channel:${CHANNEL}) same => n,Set(__MY_CALLER=${CHANNEL(name)}) same => n,Dial(Local/s at svtest2,,g) same => n,Verbose(1,***In channel:${CHANNEL} sharedVar:${SHARED(sharedVar)}) same => n,Hangup() [svtest2] exten => s,1,NoOp() same => n,Set(SHARED(sharedVar,${MY_CALLER})="I have been set in svtest2 by ${CHANNEL(name)}") same => n,Verbose(1,***In channel:${CHANNEL} sharedVar: ${SHARED(sharedVar,Local/s at svtest2)}) same => n,Answer() same => n,Hangup() Thanks again. (BTW, looking forward to the day when Asterisk gets some kind of asynchronous dialplan application - all this "dialling a local channel" is just to get via music on hold so there's not silence while a long operation happening, as per http://lists.digium.com/pipermail/asterisk-users/2016-November/290384.html :) ) On 27 November 2016 at 16:16, Richard Mudgett <rmudgett at digium.com> wrote:> [svtest1] > exten = s,1,NoOp() > same = n,Answer() > same = n,Set(__MY_CALLER=${CHANNEL(name)}) > same = n,Dial(Local/s at svtest2,,g) > same = n,NoOp(Returned SHARED(sharedVar) = '${SHARED(sharedVar)'} > same = n,Hangup() > > [svtest2] > exten = s,1,NoOp() > exten = n,Set(SHARED(sharedVar,MY_CALLER)="I have been set in svtest2 by > ${CHANNEL(name)}") > exten = n,Answer() > exten = n,Hangup()