Kamailio Advanced Training
Click here for more details!

Learn how to build RTC services with Kamailio!

Monday, April 19, 2010

Kamailio 3.0.x - DYK-03 - while

Break out of while loop

See also: Kamailio 3.0.x - DYK - Table of Content

In 1.5.x or lower, the only way to end the while cycling was through loop condition.
while(loop condition) { actions; }
Starting with 3.0.0, you can use break statement to jump out of while loop.

For example, checking if the source IP is in a white list stored in AVPs (e.g., loaded from DB):
$var(i) = 0;
while($(avp(wlist)[$var(i)])) {
if($si == $(avp(wlist)[$var(i)])) {
$var(i) = -1;
break;
}
$var(i) = $var(i) + 1;
}
if($var(i)>=0) { # not found
sl_send_reply("403", "Forbidden");
exit;
}
In 1.5.x or older, break cannot be used and the loop condition would have to be ($var(i)>=0 && $(avp(wlist)[$var(i)])). At runtime, for each loop the evaluation of condition is now faster with 3.0.0+ since it is simpler. Of course, the advantages are many more than this one, once you get a more complex logic inside the while, like being able to break from many places inside the loop, simplify the structure by avoiding many if-then-else to wrap action not to be executed when the condition is met inside the while, a.s.o.

No comments:

Post a Comment