Time-Zone Processing with Asterisk, Part II
Every registration event triggers a “call” event to happen after a delay. The delay is used to collect information used to determine whether to initiate a call. The setup telephone call should be triggered only if the time-zone setup has expired or the SIP device has changed its IP address and the record is no longer valid.
Because the call state handler is placed in the queue for execution by the registration handler, it does have one argument, the extension number of the call in question. The extension can be retrieved as $_[ARG0]. All the data we have added to the heap by processing the database responses and SIP data request is also readily available:
sub call_state_handler {
# Get extension out of arguments to function
my $exten = $_[ARG0];
my $hp = $_[HEAP];
# Variables we use to determine if the call is required
my $skew = $hp->{$exten.'-TIMESKEW'};
my $skew_addr = $hp->{$exten.'-TIMESKEW_ADDR'};
my $skew_start = $hp->{$exten.'-TIMESKEW_START'};
my $skew_end = $hp->{$exten.'-TIMESKEW_END'};
my $sip_peer_ip = $hp->{'SIP-Peer-IP'};
my $now = time();
To determine whether the call is required, the handler compares the current time with the expiration of the time-zone offset record and the IP address of the SIP device against the IP address stored in the time-zone offset record. If the IP addresses match and the offset has not expired, no call is required. Otherwise, a call is needed and made with the makeTZSetupCall function:
if ($now > $skew_end) {
debug_print("Make call - offset has expired.\n");
makeTZSetupCall($_[HEAP], $exten);
} elsif (!($skew_addr eq $sip_peer_ip)) {
debug_print("Make call - SIP IP addr changed\n");
makeTZSetupCall($_[HEAP], $exten);
} else {
debug_print("No call -- record OK & same IP\n");
}
As a final step, the handler needs to remove the variables placed on the heap. The heap is used only to pass variables between state handlers, and the variables are not needed once that function is complete. Each of the variables can be undefined with the undef function:
# Need to clean up heap
undef $_[HEAP]->{$exten.'-TIMESKEW'};
undef $_[HEAP]->{$exten.'-TIMESKEW_ADDR'};
undef $_[HEAP]->{$exten.'-TIMESKEW_START'};
undef $_[HEAP]->{$exten.'-TIMESKEW_END'};
undef $_[HEAP]->{'SIP-Peer-IP'};
} # call_state_handler
Making the setup call uses the Asterisk Manager's Originate command, but it is protected by one final check. I've defined a set of extensions as the remote channel list. Only extensions on the remote channel list will have time-zone setup calls made to them. Initially, the list consists of my softphone and an analog telephone adapter, but I may need to add more in the future. Before originating the call, I ensure that the number is on a remote channel list, which is defined in the global array REMOTE_CHANNEL_LIST. The Originate command can take several arguments as well. The extension, priority and context must refer to where the setup menu is defined. In my case, these values are extension *89 (for *-T-Z), priority 1 and the context from-internal. I also can supply the caller-ID text of “Time Zone Setup” to the phone I am calling:
sub makeTZSetupCall {
my $heap = $_[0];
my $exten = $_[1];
my $callOK = 0;
# Check that extension to call is a remote channel
foreach $number (@REMOTE_CHANNEL_LIST) {
if ($number == $exten) {
$callOK = 1;
}
}
if ($callOK) {
$heap->{server}->put({
'Action' => 'Originate',
'Channel' => 'SIP/'.$exten,
'Context' => $TZ_CONTEXT,
'Exten' => $TZ_EXTEN,
'Priority' => $TZ_PRIORITY,
'Callerid' => $CALLERID,
});
}
} # makeTZSetupCall
If the Originate command is triggered, the newly registered telephone rings, and I go through the voice menu described in last month's article.
Resources
Full Source Listing of Perl Script: ftp.linuxjournal.com/pub/lj/listings/issue156/9284.tgz
Information on the Asterisk Manager API: www.voip-info.org/wiki-Asterisk+manager+API
Perl POE Framework: poe.perl.org
Perl Asterisk Manager Component: search.cpan.org/~xantus/POE-Component-Client-Asterisk-Manager-0.06/Manager.pm
Matthew Gast is the author of the leading technical book on wireless LANs, 802.11 Wireless Networks: The Definitive Guide (O'Reilly Media). He can be reached at matthew.gast@gmail.com, but only when he is close to sea level.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- RSS Feeds
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Introduction to MapReduce with Hadoop on Linux
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Help with Designing or Debugging CORBA Applications
- Linux Systems Administrator
- Returning Values from Bash Functions
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




1 min 30 sec ago
25 min 12 sec ago
2 hours 2 min ago
2 hours 3 min ago
3 hours 56 min ago
6 hours 46 min ago
11 hours 59 min ago
12 hours 1 min ago
12 hours 2 min ago
12 hours 5 min ago