noxxi / p5-net-sip

Net::SIP Perl Module
Other
15 stars 22 forks source link

Question: How to list all active calls? #38

Closed pali closed 3 years ago

pali commented 3 years ago

I'm using Net::SIP::Simple (with listen method) and I need to list all active calls, meaning to retrieve list of all Net::SIP::Simple::Call objects which belongs to the main Net::SIP::Simple. I'm looking at documentation and also source code of Net::SIP::Simple but I cannot find a simple way how to do it.

Why I need it? I want to implement safe Perl's $SIG{INT} action which safely hangup all active calls at RTP data layer and then quit my whole application which is using Net::SIP::Simple's loop method.

In every active Net::SIP::Simple call at RTP PCMA layer I'm using V.23 modem for transferring binary data inside voice SIP call. And when application receive SIGINT signal I want to safely close every binary data connection at V.23 modem layer, then close RTP PCMA layer and then quit (via BYE packet) SIP layer of call. And after that stop Net::SIP::Simple's loop and exit whole application.

So is there any simple way how to list all Net::SIP::Simple::Call objects in Perl's $SIG{INT} callback, so I can initiate slightly complicated connection closing of every Net::SIP::Simple::Call call?

noxxi commented 3 years ago

Interesting use case. Unfortunately Net::SIP does not internally track these objects and there is no need inside Net::SIP for this. But on the listener side there are cb_create and cb_cleanup which allow tracking of the calls. I would not recommend to put the "real" objects into a list though since this might affect self-destruction. Instead only weak references should better be used (i.e. Scalar::Util::weaken).

pali commented 3 years ago

Ok, thank you for a quick reply!

So it is enough to put weak reference of $call object in cb_create callback into some my global list and then remove it in cb_cleanup? Just want to be sure that cb_create callback is always called for created Net::SIP::Simple::Call object and then cb_cleanup is always called when destroying call, so there would not be any ghost references in my global list.

In case you are interested, it is for transfering SMS to/from fixed lines and I make my application public: https://github.com/pali/sip-fsms

noxxi commented 3 years ago

cb_create is always called from the listener whenever a new Net::SIP::Call gets created on INVITE (see Net::SIP::Simple::listen). cb_cleanup is called from Net::SIP::Call::cleanup which itself gets automatically called on orderly shutdown using BYE or CANCEL. The property of weaken is that there can be ghost references: if the object is already destroyed the reference will be undef.

noxxi commented 3 years ago

In case you are interested, it is for transfering SMS to/from fixed lines and I make my application public: https://github.com/pali/sip-fsms

I don't understand all this V.23 but this looks like cool stuff.

pali commented 3 years ago

Perfect! Thank you for a nice Net::SIP module.