[asterisk-dev] [Code Review] Dialplan function for manager account checks - AMI_CLIENT()

Russell Bryant russell at digium.com
Fri Jul 16 08:27:08 CDT 2010



> On 2009-11-03 15:50:16, Russell Bryant wrote:
> > /trunk/main/manager.c, lines 5387-5397
> > <https://reviewboard.asterisk.org/r/421/diff/1/?file=7272#file7272line5387>
> >
> >     Since the operation being performed on each session is so small, it would actually be more efficient to implement this as an ao2_callback().
> 
> Olle E Johansson wrote:
>     I take that as a suggestion, right. I just copied this code from another place in the file.
> 
> Olle E Johansson wrote:
>     Reminder sent to Russell - I need a template to copy from...

While this comment was written back in February, it looks like it was just published, as it just now was sent to the -dev list.  For the sake of the archive, I went and dug up the example I put together in our private email discussion about this:


On 02/04/2010 06:34 AM, Olle E. Johansson wrote:
> https://reviewboard.asterisk.org/r/421/
>
> You requested me to implement something that's not implemented elsewhere in manager, so I have nothing to copy from. I want to move this forward, so either please help me with the needed code in this branch or another place in manager - or approve integration.

The key to doing this is the usage of ao2_callback() or 
ao2_callback_data().  The base usage of an astobj2 callback is to say 
"please call this function on every object in the container".  In this 
case, we want to count the sessions for a given name.  The code would 
look something like ...

(Forgive me if this isn't perfect, as I haven't tried to compile it, but 
hopefully it will be a good demonstration.)

static int session_count_cb(void *obj, void *arg, void *data, int flags)
{
        /* First, we have the object. */
        struct mansession *session = obj;
        /* Second, we have a custom argument, which we set as login */
        const char *login = arg;
        /* Third, we have another custom arg, the counter */
        int *no_sessions = data;

        /* You don't need flags for anything here. */

        if (!strcasecmp(session->username, login)) {
                *no_sessions += 1;
        }

        return 0;
}

...
        /* Count sessions with username 'login' */
        ao2_callback_data(sessions, login, &no_sessions, 0);
...

Done!

The advantages of this approach are with the locking required.  In this 
case, internally in astobj2, the container is only locked a single time. 
  When you use an ao2_iterator, the container is locked and unlocked 
every time you go to get the next element.

In this case, since the entire operation is going to be super fast, 
ao2_callback() is a better choice.

In some other cases, where the operation you need to perform on each 
object may take a while (such as database access or something), then it 
is probably a better choice to use an iterator.  That way, if any other 
threads need to access these objects, they get to have their turn while 
you're off doing database work.


- Russell


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviewboard.asterisk.org/r/421/#review1244
-----------------------------------------------------------


On 2009-11-03 14:48:56, Olle E Johansson wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviewboard.asterisk.org/r/421/
> -----------------------------------------------------------
> 
> (Updated 2009-11-03 14:48:56)
> 
> 
> Review request for Asterisk Developers.
> 
> 
> Summary
> -------
> 
> Implementation of a dialplan function for checking manager accounts. I've made it extensible so that we can add other parameters when needed. Right now it only checks the number of logged in sessions for a manager account.
> 
> 
> Diffs
> -----
> 
>   /trunk/main/manager.c 227348 
> 
> Diff: https://reviewboard.asterisk.org/r/421/diff
> 
> 
> Testing
> -------
> 
> Tested on my Linux system and it reports manager logins properly - 0, 1 and 2 concurrent sessions...
> 
> 
> Thanks,
> 
> Olle E
> 
>




More information about the asterisk-dev mailing list