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
PDF
rtpengine - Media Relaying and Beyond
PDF
SIPREC RTPEngine Media Forking
PDF
Astricon 10 (October 2013) - SIP over WebSocket on Kamailio
PDF
Kamailio - SIP Firewall for Carrier Grade Traffic
PDF
Kamailio on Docker
PDF
rtpengine and kamailio - or how to simulate calls at scale
PDF
Kamailio - Load Balancing Load Balancers
Kamailio - API Based SIP Routing
rtpengine - Media Relaying and Beyond
SIPREC RTPEngine Media Forking
Astricon 10 (October 2013) - SIP over WebSocket on Kamailio
Kamailio - SIP Firewall for Carrier Grade Traffic
Kamailio on Docker
rtpengine and kamailio - or how to simulate calls at scale
Kamailio - Load Balancing Load Balancers

What's hot (20)

PDF
FreeSWITCH as a Microservice
PDF
Kamailio - SIP Routing in Lua
PPTX
FreeSWITCH as a Kickass SBC
PDF
Why is Kamailio so different? An introduction.
PDF
Kamailio :: A Quick Introduction
ODP
Expanding Asterisk with Kamailio
PDF
pg_proctab: Accessing System Stats in PostgreSQL
PDF
Scaling Asterisk with Kamailio
PDF
Using Kamailio for Scalability and Security
PDF
Sipwise rtpengine
PDF
Faster packet processing in Linux: XDP
PDF
Kamailio - Large Unified Communication Platforms
PDF
Scaling FreeSWITCH Performance
PDF
Linux Networking Explained
ODP
Using Asterisk and Kamailio for Reliable, Scalable and Secure Communication S...
PDF
Homer - Workshop at Kamailio World 2017
PDF
SIP Attack Handling (Kamailio World 2021)
PDF
Go Concurrency
PDF
Media Handling in FreeSWITCH
PDF
Job Queue in Golang
FreeSWITCH as a Microservice
Kamailio - SIP Routing in Lua
FreeSWITCH as a Kickass SBC
Why is Kamailio so different? An introduction.
Kamailio :: A Quick Introduction
Expanding Asterisk with Kamailio
pg_proctab: Accessing System Stats in PostgreSQL
Scaling Asterisk with Kamailio
Using Kamailio for Scalability and Security
Sipwise rtpengine
Faster packet processing in Linux: XDP
Kamailio - Large Unified Communication Platforms
Scaling FreeSWITCH Performance
Linux Networking Explained
Using Asterisk and Kamailio for Reliable, Scalable and Secure Communication S...
Homer - Workshop at Kamailio World 2017
SIP Attack Handling (Kamailio World 2021)
Go Concurrency
Media Handling in FreeSWITCH
Job Queue in Golang
Ad

Similar to Aynchronous Processing in Kamailio Configuration File (20)

PDF
Kamailio - SIP Servers Everywhere
PDF
FOSDEM 2017 - RTC Services With Lua and Kamailio
PPTX
SecZone 2011: Scrubbing SAP clean with SOAP
PDF
Monitoring VoIP Systems
PDF
OSMC 2014: Monitoring VoIP Systems | Sebastian Damm
PDF
Kamailio - Surfing Big Waves Of SIP With Style
KEY
MOW2010: 1TB MySQL Database Migration and HA Infrastructure by Alex Gorbachev...
PDF
DSLing your System For Scalability Testing Using Gatling - Dublin Scala User ...
PDF
Saltstack - Orchestration & Application Deployment
PDF
Kamailioworld 2018 - Modular and test driven SIP Routing with Lua
PPTX
Being HAPI! Reverse Proxying on Purpose
PDF
Immutant
PPTX
SAP (in)security: Scrubbing SAP clean with SOAP
PDF
Sensu and Sensibility - Puppetconf 2014
PDF
Designing High Performance RTC Signaling Servers
PDF
myHadoop 0.30
PDF
php & performance
PPTX
Building and Scaling Node.js Applications
PDF
Kamailio with Docker and Kubernetes
PPTX
IOT Firmware: Best Pratices
Kamailio - SIP Servers Everywhere
FOSDEM 2017 - RTC Services With Lua and Kamailio
SecZone 2011: Scrubbing SAP clean with SOAP
Monitoring VoIP Systems
OSMC 2014: Monitoring VoIP Systems | Sebastian Damm
Kamailio - Surfing Big Waves Of SIP With Style
MOW2010: 1TB MySQL Database Migration and HA Infrastructure by Alex Gorbachev...
DSLing your System For Scalability Testing Using Gatling - Dublin Scala User ...
Saltstack - Orchestration & Application Deployment
Kamailioworld 2018 - Modular and test driven SIP Routing with Lua
Being HAPI! Reverse Proxying on Purpose
Immutant
SAP (in)security: Scrubbing SAP clean with SOAP
Sensu and Sensibility - Puppetconf 2014
Designing High Performance RTC Signaling Servers
myHadoop 0.30
php & performance
Building and Scaling Node.js Applications
Kamailio with Docker and Kubernetes
IOT Firmware: Best Pratices
Ad

More from Daniel-Constantin Mierla (13)

PDF
TAD Summit 2016 - The Mobile World Up Side Down
PDF
Snappy Kamailio
PDF
Kamailio Updates - VUC 588
PDF
SIP Server Optimizations for Mobile Networks
PDF
Kamailio and VoIP Wild World
PDF
Kamailio - Secure Communication
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 - Unifying SIP and Web Worlds with Lua
PDF
Kamailio - The Story for Asterisk
TAD Summit 2016 - The Mobile World Up Side Down
Snappy Kamailio
Kamailio Updates - VUC 588
SIP Server Optimizations for Mobile Networks
Kamailio and VoIP Wild World
Kamailio - Secure Communication
10 Years SER - Awards
Sculpturing SIP World
CPDL - Charging Plan Definition Language
SER - SIP Express Router
SIP Router Project
Kamailio - Unifying SIP and Web Worlds with Lua
Kamailio - The Story for Asterisk

Recently uploaded (20)

PPTX
Modernising the Digital Integration Hub
PPT
Module 1.ppt Iot fundamentals and Architecture
PPTX
TEXTILE technology diploma scope and career opportunities
PDF
STKI Israel Market Study 2025 version august
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
Architecture types and enterprise applications.pdf
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Five Habits of High-Impact Board Members
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
DOCX
search engine optimization ppt fir known well about this
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
UiPath Agentic Automation session 1: RPA to Agents
Modernising the Digital Integration Hub
Module 1.ppt Iot fundamentals and Architecture
TEXTILE technology diploma scope and career opportunities
STKI Israel Market Study 2025 version august
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Microsoft Excel 365/2024 Beginner's training
Architecture types and enterprise applications.pdf
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Taming the Chaos: How to Turn Unstructured Data into Decisions
The influence of sentiment analysis in enhancing early warning system model f...
Developing a website for English-speaking practice to English as a foreign la...
sustainability-14-14877-v2.pddhzftheheeeee
Five Habits of High-Impact Board Members
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Custom Battery Pack Design Considerations for Performance and Safety
A proposed approach for plagiarism detection in Myanmar Unicode text
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
search engine optimization ppt fir known well about this
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
UiPath Agentic Automation session 1: RPA to Agents

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:[email protected];transport=sctp"; $uac_req(furi)="sip:[email protected]"; $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 !