SlideShare a Scribd company logo
Kamailio SIP Server
async processing in config
!
!
www.kamailio.org
Daniel-Constantin Mierla
Co-Founder Kamailio
@miconda
!
www.asipto.com
!2
What’s the issue?
avoid blocking operations
!
!
!3
The benefit?
!
!
increase the performance
!4
Performance?
!
not an issue for kamailio
!
!
!
thank you!
!
Questions?
!5
But then …
• Interaction with database	

• sometimes you must wait	

• sometimes is not that real-time critical	

!
• DNS	

• dns is down, internet is down	

!
• Interaction with other external systems	

• rating engine - billing systems
Kamailio
application architecture
!7
Application design
! multi-process application
! not multi-threading
! dedicated processes for
" main attendant
" tcp attendant
" sip tcp receivers
" sip udp receivers
" sip sctp receivers
" control interface
" FIFO receiver
" DATAGRAM/UDP receiver
" XMLRPC receiver
" timers
" main timers (sip retransmissions)
" dedicated timers
" custom timers
" special handling
" custom processing
!8
Blocking task delegation to another process
INVITE X REQUEST IN
INVITE X
PROCESSING
FORWARD
INVITE X
READ SIP
MESSAGE
READ SIP
MESSAGE
read from message queue
KAMAILIOPROCESSA
KAMAILIOPROCESSB
blocking task
process task
read from message queue
process task
message queue
!9
USERS
USERS
sip
routing
node
Blocking task delegation to another instance
dedicated
processing
node
sip
sip sip
Basic Kamailio Components
!11
Cases
!
!
Impacting SIP routing
!
• need to wait for async task to finish	

• suspend sip message processing	

• handle other SIP messages	

• when async task done, resume processing of suspended message	

!
No impact on SIP routing
!
• unrelated event	

• can be pushed out of SIP handling process and that’s a;;	

!
!12
• TMX	

• suspend-resume transaction	

• UAC	

• send custom messages from kamailio config	

• ASYNC	

• asynchronous execution of routing blocks	

• asynchronous sleep	

• MQUEUE	

• internal message queue	

• RTIMER	

• create custom processes that can react on timer	

• JSONRPC-C	

• send json-rpc commands and process responses asynchronously	

• EVAPI	

• publishing event messages via tcp and process responses asynchronously	

evapi_relay(“{ "event": "test",n "data": { "fU": "$fU" }n}");
Send push notifications
!
- suspend invite - resume on registration -
Push notifications
!14
Config
# ----- htable params -----	

modparam("htable", "db_url", DBURL)	

modparam("htable", "htable", "vtp=>size=10;autoexpire=120;dbtable=htable;dbmode=1")	

modparam("htable", "htable",“a=>size=6;")	

!
request_route {	

…	

	

 # send the push	

	

 route(PUSHAYASYNC);	

!
	

 # user location service	

	

 route(LOCATION);	

}	

!
!
Push notifications
!15
Config
# Handle SIP registrations	

route[REGISTRAR] {	

	

 if (!is_method("REGISTER"))	

	

 	

 return;	

!
	

 if(isflagset(FLT_NATS))	

	

 {	

	

 	

 setbflag(FLB_NATB);	

	

 	

 # uncomment next line to do SIP NAT pinging 	

	

 	

 ## setbflag(FLB_NATSIPPING);	

	

 }	

	

 if (!save("location"))	

	

 	

 sl_reply_error();	

!
	

 route(PUSHJOIN);	

	

 exit;	

}	

!
!
!
Push notifications
!16
Config
# do the PUSH notification	

route[SENDPUSH] {	

	

 $var(luaret) = 0;	

!
	

 if(lua_runstring(“do_push([[$hdr(X-VxTo)]], [[$tU]], [[$hdr(X-VxFrom)]], [[$fU]], [[$ci]])")<0)	

	

 {	

	

 	

 send_reply("501", "No link to destination");	

	

 	

 exit;	

	

 }	

	

 if($var(luaret)!=1)	

	

 {	

	

 	

 send_reply("501", "Unknown destination");	

	

 	

 exit;	

	

 }	

	

 send_reply("110", "Push sent");	

}	

!
!
!
Push notifications
!17
Config
# do push in async mode	

route[PUSHASYNC] {	

	

 if (!is_method("INVITE"))	

	

 	

 return;	

!
	

 if(registered("location"))	

	

 	

 return;	

!
	

 route(SENDPUSH);	

!
	

 if(!t_suspend())	

	

 {	

	

 xlog("failed suspending trasaction [$T(id_index):$T(id_label)]n");	

	

 	

 send_reply("501", "Unknown destination");	

	

 	

 exit;	

	

 }	

	

 xdbg("suspended transaction [$T(id_index):$T(id_label)] $fU => $rUn");	

	

 $sht(vtp=>join::$rU) = "" + $T(id_index) + ":" + $T(id_label);	

	

 xdbg("htale key value [$sht(vtp=>join::$rU)]n");	

	

 exit;	

}	

!
!
Push notifications
!18
Config
# join pending INVITE with the incoming REGISTER	

route[PUSHJOIN] {	

	

 if (!is_method("REGISTER"))	

	

 	

 return;	

	

 $var(hjoin) = 0;	

	

 lock("$tU");	

	

 $var(hjoin) = $sht(vtp=>join::$tU);	

	

 $sht(vtp=>join::$tU) = $null;	

	

 unlock("$tU");	

	

 if($var(hjoin)==0)	

	

 	

 return;	

	

 $var(id_index) = $(var(hjoin){s.select,0,:}{s.int});	

	

 $var(id_label) = $(var(hjoin){s.select,1,:}{s.int});	

	

 xdbg("resuming trasaction [$var(id_index):$var(id_label)] $tU ($var(hjoin))n");	

	

 t_continue("$var(id_index)", "$var(id_label)", "LOCATION");	

}	

!
Central Accounting System
!20
Accounting Server
ACC
!21
Proxy Server Config
onreply_route[ACC] {
if(status!="200")
return;
$uac_req(method)="ACCOUNTING";
$uac_req(ruri)="sip:store@accounting.kamailio.org;transport=sctp";
$uac_req(furi)="sip:server@server1.kamailio.org";
$uac_req(hdrs)="Content-Type: text/accounting-csvrn";
pv_printf(“$uac_req(body)”, "$TS,$ci,$ft,$tt,$T_req($fu),$T_req($ru)");
uac_send_req();
}
!22
Acc Server Config
request_route {
if(method=="ACCOUNTING" && $rU="store")
{
sql_query("ca",
"insert into accounting
(timeval,callid,ftag,ttag,src,dst)
values ('$(rb{s.select,0,,})',
'$(rb{s.select,1,,})',
'$(rb{s.select,2,,})',
'$(rb{s.select,3,,})',
'$(rb{s.select,4,,})',
'$(rb{s.select,5,,})' )",
"ra");
send_reply("200", "Stored");
}
}
!23
SIPWEET
!24
SIPWEET
Design
!
• Lua twitter library	

• Twitter operation is an HTTP request	

• can take some time to be processed	

• we cannot afford that when processing SIP signaling	

• solution: use asynchronous processing	

• config file message queue	

• dedicated process for twitter operations	

• Kamailio modules	

• app_lua	

• mqueue	

• rtimer	

• sqlops	

Sample implementation
• notification of a missed call
• use of Twitter direct message
!25
SIPWEET
Config
!
loadmodule "app_lua.so"	

loadmodule "rtimer.so"	

loadmodule "sqlops.so"	

loadmodule “mqueue.so”	

!
# ----- app_lua -----	

modparam("app_lua", "load", 	

"/usr/local/etc/kamailio/lua/sipweet.lua")	

!
# ----- rtimer -----	

modparam("rtimer", "timer", 	

"name=sipweet;interval=10;mode=1;")	

modparam("rtimer", "exec", 	

"timer=sipweet;route=SIPWEET;")	

!
# ----- sqlops -----	

modparam("sqlops","sqlcon",	

"ca=>mysql://openser:openserrw@localhost/openser")	

!
# ----- mqueue -----	

modparam("mqueue", "mqueue", "name=sipweet")
!26
SIPWEET
Config
!
# Twitter routing	

route[SIPWEET] {	

	

 # consume tweeties	

	

 while(mq_fetch("sipweet"))	

	

 {	

	

 	

 xlog("Tweeting to $mqk(sipweet) [[$mqv(sipweet)]]n");	

!
	

 	

 # get twitter user	

	

 	

 sql_query("ca",	

	

 	

 "select twuser from sipweetusers where sipuser='$mqk(sipweet)'",	

	

 	

 "ra");	

	

 	

 if($dbr(ra=>rows)>0)	

	

 	

 {	

	

 	

 	

 $var(twuser) = $dbr(ra=>[0,0]);	

	

 	

 	

 $var(twmsg) = $mqv(sipweet);	

	

 	

 	

 if(!lua_runstring("sipweetdm([[$var(twuser)]], [[$var(twmsg)]])"))	

	

 	

 	

 {	

	

 	

 	

 	

 xdbg("failed to send dm to: $mqk(sipweet) - $var(twuser)!n");	

	

 	

 	

 }	

	

 	

 }	

	

 }	

}
!27
SIPWEET
Config
!
# Twitees queuing	

route[TWQUEUE] {	

	

 if(!is_method("INVITE"))	

	

 	

 return;	

	

 mq_add("sipweet", "$rU", "Missed call from $fU ($Tf)");	

 	

}	

!
route {	

	

 ...	

	

 if(!lookup(“location”)) {	

	

 	

 route(TWQUEUE);	

t_newtran();	

t_reply("404", "Not Found");	

exit;	

	

 }	

	

 ...	

}
!28
SIPWEET
Database table
!
CREATE TABLE `sipweetusers` (	

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,	

`twuser` varchar(64) NOT NULL,	

`sipuser` varchar(64) NOT NULL,	

PRIMARY KEY (`id`),	

UNIQUE KEY (`twuser`),	

UNIQUE KEY (`sipuser`)	

);
mysql> select * from sipweetusers;
+----+---------+---------+
| id | twuser | sipuser |
+----+---------+---------+
| 1 | miconda | 101 |
+----+---------+---------+
!29
SIPWEET
Lua script
!
-- SIPweet	

!
-- loading module	

require("twitter")	

!
local initialized = 0	

local mytw	

!
function sipweetinit()	

if initialized == 0 then	

	

 	

 mytw = twitter.client("Consumer Key",	

	

 	

 	

 	

 "Consumer Secret",	

	

 	

 	

 	

 "OAuth Token",	

	

 	

 	

 	

 "OAuth Token Secret");	

initialized = 1	

end	

end	

!
function sipweetdm(userid, message)	

sipweetinit()	

	

 mytw:sendMessage{ user = userid, text = message }	

end
!30
SIPWEET
!
!
And the messages goes ...
!
• nice and quick to the twitter client	

!
sponsors and exhibitors
??? questions ???
www.asipto.com

More Related Content

PDF
Kamailio - API Based SIP Routing
ODP
Expanding Asterisk with Kamailio
PDF
Linux Kernel vs DPDK: HTTP Performance Showdown
PDF
rtpengine and kamailio - or how to simulate calls at scale
PDF
Kamailio :: A Quick Introduction
ODP
Using Asterisk and Kamailio for Reliable, Scalable and Secure Communication S...
PDF
SIPREC RTPEngine Media Forking
PDF
Scaling Asterisk with Kamailio
Kamailio - API Based SIP Routing
Expanding Asterisk with Kamailio
Linux Kernel vs DPDK: HTTP Performance Showdown
rtpengine and kamailio - or how to simulate calls at scale
Kamailio :: A Quick Introduction
Using Asterisk and Kamailio for Reliable, Scalable and Secure Communication S...
SIPREC RTPEngine Media Forking
Scaling Asterisk with Kamailio

What's hot (20)

PDF
Linux Networking Explained
PDF
Introduction to FreeSWITCH
PPTX
Exactly-Once Financial Data Processing at Scale with Flink and Pinot
PPTX
HCL Domino V12 Key Security Features Overview
PPTX
DNS Presentation
PDF
rtpengine - Media Relaying and Beyond
PDF
Unrevealed Story Behind Viettel Network Cloud Hotpot | Đặng Văn Đại, Hà Mạnh ...
DOCX
Linux admin interview questions
PDF
From DTrace to Linux
PDF
Velocity 2015 linux perf tools
PDF
MyRocks Deep Dive
PDF
Deploying Flink on Kubernetes - David Anderson
PDF
Disaster Recovery Plans for Apache Kafka
DOCX
Type of DDoS attacks with hping3 example
PDF
RocksDB Performance and Reliability Practices
PDF
Flink 2.0: Navigating the Future of Unified Stream and Batch Processing
PDF
Apache Spark Overview
PDF
Astricon 10 (October 2013) - SIP over WebSocket on Kamailio
PPTX
Evening out the uneven: dealing with skew in Flink
PPT
Ipc in linux
Linux Networking Explained
Introduction to FreeSWITCH
Exactly-Once Financial Data Processing at Scale with Flink and Pinot
HCL Domino V12 Key Security Features Overview
DNS Presentation
rtpengine - Media Relaying and Beyond
Unrevealed Story Behind Viettel Network Cloud Hotpot | Đặng Văn Đại, Hà Mạnh ...
Linux admin interview questions
From DTrace to Linux
Velocity 2015 linux perf tools
MyRocks Deep Dive
Deploying Flink on Kubernetes - David Anderson
Disaster Recovery Plans for Apache Kafka
Type of DDoS attacks with hping3 example
RocksDB Performance and Reliability Practices
Flink 2.0: Navigating the Future of Unified Stream and Batch Processing
Apache Spark Overview
Astricon 10 (October 2013) - SIP over WebSocket on Kamailio
Evening out the uneven: dealing with skew in Flink
Ipc in linux
Ad

Similar to Aynchronous Processing in Kamailio Configuration File (20)

PDF
SIP Server Optimizations for Mobile Networks
PDF
Kamailio - SIP Routing in Lua
PDF
Kamailio Updates - VUC 588
PDF
Kamailio - SIP Servers Everywhere
PDF
Kamailio - Large Unified Communication Platforms
PPTX
Introduction to Kamailio (TADSummit 2020 Asia)
PDF
Using Kamailio for Scalability and Security
PDF
FOSS Sthlm: Realtime Communication Update
PDF
Kamailio - Secure Communication
PDF
Kamailio - Surfing Big Waves Of SIP With Style
PDF
Kamailioworld 2018 - Modular and test driven SIP Routing with Lua
PPT
Astricon 2010: Scaling Asterisk installations
PDF
Kamailio - SIP Firewall for Carrier Grade Traffic
PDF
Kamailio - Unifying SIP and Web Worlds with Lua
PDF
Why is Kamailio so different? An introduction.
PDF
Kamailio and VoIP Wild World
PDF
Kamailio and VoIP Wild World
PDF
FOSDEM 2017 - RTC Services With Lua and Kamailio
PDF
KazooCon 2014 - Playing Kazoo Dudka Style
PPTX
CI/CD and TDD in deploying kamailio
SIP Server Optimizations for Mobile Networks
Kamailio - SIP Routing in Lua
Kamailio Updates - VUC 588
Kamailio - SIP Servers Everywhere
Kamailio - Large Unified Communication Platforms
Introduction to Kamailio (TADSummit 2020 Asia)
Using Kamailio for Scalability and Security
FOSS Sthlm: Realtime Communication Update
Kamailio - Secure Communication
Kamailio - Surfing Big Waves Of SIP With Style
Kamailioworld 2018 - Modular and test driven SIP Routing with Lua
Astricon 2010: Scaling Asterisk installations
Kamailio - SIP Firewall for Carrier Grade Traffic
Kamailio - Unifying SIP and Web Worlds with Lua
Why is Kamailio so different? An introduction.
Kamailio and VoIP Wild World
Kamailio and VoIP Wild World
FOSDEM 2017 - RTC Services With Lua and Kamailio
KazooCon 2014 - Playing Kazoo Dudka Style
CI/CD and TDD in deploying kamailio
Ad

More from Daniel-Constantin Mierla (10)

PDF
TAD Summit 2016 - The Mobile World Up Side Down
PDF
Snappy Kamailio
PDF
Designing High Performance RTC Signaling Servers
PDF
10 Years SER - Awards
PDF
Sculpturing SIP World
PDF
CPDL - Charging Plan Definition Language
PDF
SER - SIP Express Router
PDF
SIP Router Project
PDF
Kamailio - The Story for Asterisk
PDF
Kamailio - Load Balancing Load Balancers
TAD Summit 2016 - The Mobile World Up Side Down
Snappy Kamailio
Designing High Performance RTC Signaling Servers
10 Years SER - Awards
Sculpturing SIP World
CPDL - Charging Plan Definition Language
SER - SIP Express Router
SIP Router Project
Kamailio - The Story for Asterisk
Kamailio - Load Balancing Load Balancers

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
KodekX | Application Modernization Development
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Electronic commerce courselecture one. Pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Machine learning based COVID-19 study performance prediction
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
“AI and Expert System Decision Support & Business Intelligence Systems”
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
Review of recent advances in non-invasive hemoglobin estimation
20250228 LYD VKU AI Blended-Learning.pptx
KodekX | Application Modernization Development
Reach Out and Touch Someone: Haptics and Empathic Computing
Per capita expenditure prediction using model stacking based on satellite ima...
Electronic commerce courselecture one. Pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Machine learning based COVID-19 study performance prediction
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Aynchronous Processing in Kamailio Configuration File

  • 1. Kamailio SIP Server async processing in config ! ! www.kamailio.org Daniel-Constantin Mierla Co-Founder Kamailio @miconda ! www.asipto.com
  • 2. !2 What’s the issue? avoid blocking operations ! !
  • 4. !4 Performance? ! not an issue for kamailio ! ! ! thank you! ! Questions?
  • 5. !5 But then … • Interaction with database • sometimes you must wait • sometimes is not that real-time critical ! • DNS • dns is down, internet is down ! • Interaction with other external systems • rating engine - billing systems
  • 7. !7 Application design ! multi-process application ! not multi-threading ! dedicated processes for " main attendant " tcp attendant " sip tcp receivers " sip udp receivers " sip sctp receivers " control interface " FIFO receiver " DATAGRAM/UDP receiver " XMLRPC receiver " timers " main timers (sip retransmissions) " dedicated timers " custom timers " special handling " custom processing
  • 8. !8 Blocking task delegation to another process INVITE X REQUEST IN INVITE X PROCESSING FORWARD INVITE X READ SIP MESSAGE READ SIP MESSAGE read from message queue KAMAILIOPROCESSA KAMAILIOPROCESSB blocking task process task read from message queue process task message queue
  • 9. !9 USERS USERS sip routing node Blocking task delegation to another instance dedicated processing node sip sip sip
  • 11. !11 Cases ! ! Impacting SIP routing ! • need to wait for async task to finish • suspend sip message processing • handle other SIP messages • when async task done, resume processing of suspended message ! No impact on SIP routing ! • unrelated event • can be pushed out of SIP handling process and that’s a;; !
  • 12. !12 • TMX • suspend-resume transaction • UAC • send custom messages from kamailio config • ASYNC • asynchronous execution of routing blocks • asynchronous sleep • MQUEUE • internal message queue • RTIMER • create custom processes that can react on timer • JSONRPC-C • send json-rpc commands and process responses asynchronously • EVAPI • publishing event messages via tcp and process responses asynchronously evapi_relay(“{ "event": "test",n "data": { "fU": "$fU" }n}");
  • 13. Send push notifications ! - suspend invite - resume on registration -
  • 14. Push notifications !14 Config # ----- htable params ----- modparam("htable", "db_url", DBURL) modparam("htable", "htable", "vtp=>size=10;autoexpire=120;dbtable=htable;dbmode=1") modparam("htable", "htable",“a=>size=6;") ! request_route { … # send the push route(PUSHAYASYNC); ! # user location service route(LOCATION); } ! !
  • 15. Push notifications !15 Config # Handle SIP registrations route[REGISTRAR] { if (!is_method("REGISTER")) return; ! if(isflagset(FLT_NATS)) { setbflag(FLB_NATB); # uncomment next line to do SIP NAT pinging ## setbflag(FLB_NATSIPPING); } if (!save("location")) sl_reply_error(); ! route(PUSHJOIN); exit; } ! ! !
  • 16. Push notifications !16 Config # do the PUSH notification route[SENDPUSH] { $var(luaret) = 0; ! if(lua_runstring(“do_push([[$hdr(X-VxTo)]], [[$tU]], [[$hdr(X-VxFrom)]], [[$fU]], [[$ci]])")<0) { send_reply("501", "No link to destination"); exit; } if($var(luaret)!=1) { send_reply("501", "Unknown destination"); exit; } send_reply("110", "Push sent"); } ! ! !
  • 17. Push notifications !17 Config # do push in async mode route[PUSHASYNC] { if (!is_method("INVITE")) return; ! if(registered("location")) return; ! route(SENDPUSH); ! if(!t_suspend()) { xlog("failed suspending trasaction [$T(id_index):$T(id_label)]n"); send_reply("501", "Unknown destination"); exit; } xdbg("suspended transaction [$T(id_index):$T(id_label)] $fU => $rUn"); $sht(vtp=>join::$rU) = "" + $T(id_index) + ":" + $T(id_label); xdbg("htale key value [$sht(vtp=>join::$rU)]n"); exit; } ! !
  • 18. Push notifications !18 Config # join pending INVITE with the incoming REGISTER route[PUSHJOIN] { if (!is_method("REGISTER")) return; $var(hjoin) = 0; lock("$tU"); $var(hjoin) = $sht(vtp=>join::$tU); $sht(vtp=>join::$tU) = $null; unlock("$tU"); if($var(hjoin)==0) return; $var(id_index) = $(var(hjoin){s.select,0,:}{s.int}); $var(id_label) = $(var(hjoin){s.select,1,:}{s.int}); xdbg("resuming trasaction [$var(id_index):$var(id_label)] $tU ($var(hjoin))n"); t_continue("$var(id_index)", "$var(id_label)", "LOCATION"); } !
  • 21. !21 Proxy Server Config onreply_route[ACC] { if(status!="200") return; $uac_req(method)="ACCOUNTING"; $uac_req(ruri)="sip:store@accounting.kamailio.org;transport=sctp"; $uac_req(furi)="sip:server@server1.kamailio.org"; $uac_req(hdrs)="Content-Type: text/accounting-csvrn"; pv_printf(“$uac_req(body)”, "$TS,$ci,$ft,$tt,$T_req($fu),$T_req($ru)"); uac_send_req(); }
  • 22. !22 Acc Server Config request_route { if(method=="ACCOUNTING" && $rU="store") { sql_query("ca", "insert into accounting (timeval,callid,ftag,ttag,src,dst) values ('$(rb{s.select,0,,})', '$(rb{s.select,1,,})', '$(rb{s.select,2,,})', '$(rb{s.select,3,,})', '$(rb{s.select,4,,})', '$(rb{s.select,5,,})' )", "ra"); send_reply("200", "Stored"); } }
  • 24. !24 SIPWEET Design ! • Lua twitter library • Twitter operation is an HTTP request • can take some time to be processed • we cannot afford that when processing SIP signaling • solution: use asynchronous processing • config file message queue • dedicated process for twitter operations • Kamailio modules • app_lua • mqueue • rtimer • sqlops Sample implementation • notification of a missed call • use of Twitter direct message
  • 25. !25 SIPWEET Config ! loadmodule "app_lua.so" loadmodule "rtimer.so" loadmodule "sqlops.so" loadmodule “mqueue.so” ! # ----- app_lua ----- modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/sipweet.lua") ! # ----- rtimer ----- modparam("rtimer", "timer", "name=sipweet;interval=10;mode=1;") modparam("rtimer", "exec", "timer=sipweet;route=SIPWEET;") ! # ----- sqlops ----- modparam("sqlops","sqlcon", "ca=>mysql://openser:openserrw@localhost/openser") ! # ----- mqueue ----- modparam("mqueue", "mqueue", "name=sipweet")
  • 26. !26 SIPWEET Config ! # Twitter routing route[SIPWEET] { # consume tweeties while(mq_fetch("sipweet")) { xlog("Tweeting to $mqk(sipweet) [[$mqv(sipweet)]]n"); ! # get twitter user sql_query("ca", "select twuser from sipweetusers where sipuser='$mqk(sipweet)'", "ra"); if($dbr(ra=>rows)>0) { $var(twuser) = $dbr(ra=>[0,0]); $var(twmsg) = $mqv(sipweet); if(!lua_runstring("sipweetdm([[$var(twuser)]], [[$var(twmsg)]])")) { xdbg("failed to send dm to: $mqk(sipweet) - $var(twuser)!n"); } } } }
  • 27. !27 SIPWEET Config ! # Twitees queuing route[TWQUEUE] { if(!is_method("INVITE")) return; mq_add("sipweet", "$rU", "Missed call from $fU ($Tf)"); } ! route { ... if(!lookup(“location”)) { route(TWQUEUE); t_newtran(); t_reply("404", "Not Found"); exit; } ... }
  • 28. !28 SIPWEET Database table ! CREATE TABLE `sipweetusers` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `twuser` varchar(64) NOT NULL, `sipuser` varchar(64) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY (`twuser`), UNIQUE KEY (`sipuser`) ); mysql> select * from sipweetusers; +----+---------+---------+ | id | twuser | sipuser | +----+---------+---------+ | 1 | miconda | 101 | +----+---------+---------+
  • 29. !29 SIPWEET Lua script ! -- SIPweet ! -- loading module require("twitter") ! local initialized = 0 local mytw ! function sipweetinit() if initialized == 0 then mytw = twitter.client("Consumer Key", "Consumer Secret", "OAuth Token", "OAuth Token Secret"); initialized = 1 end end ! function sipweetdm(userid, message) sipweetinit() mytw:sendMessage{ user = userid, text = message } end
  • 30. !30 SIPWEET ! ! And the messages goes ... ! • nice and quick to the twitter client !