Commit 6eb9a6f6 authored by Tristan Matthews's avatar Tristan Matthews

* #29736: presence: cleanup

parent 23d95cfe
......@@ -48,10 +48,10 @@
SIPPresence::SIPPresence(SIPAccount *acc)
: pres_status_data()
, publish_sess()
, enabled(true)
, enabled_(true)
, acc_(acc)
, pres_sub_server_list_ () //IP2IP context
, pres_sub_client_list_ ()
, pres_sub_server_list_() //IP2IP context
, pres_sub_client_list_()
, mutex_()
, mutex_nesting_level_()
, mutex_owner_()
......@@ -59,59 +59,68 @@ SIPPresence::SIPPresence(SIPAccount *acc)
, pool_()
{
/* init default status */
updateStatus(true,"Available");
updateStatus(true, "Available");
/* init pool */
pj_caching_pool_init(&cp_, &pj_pool_factory_default_policy, 0);
pool_ = pj_pool_create(&cp_.factory, "pres", 1000, 1000, NULL);
/* Create mutex */
if(pj_mutex_create_recursive(pool_, "pres",&mutex_) != PJ_SUCCESS)
ERROR("Unable to create mutex");
if (pj_mutex_create_recursive(pool_, "pres", &mutex_) != PJ_SUCCESS)
ERROR("Unable to create mutex");
}
SIPPresence::~SIPPresence(){
SIPPresence::~SIPPresence()
{
/* Flush the lists */
for (auto c : pres_sub_client_list_)
removePresSubClient(c) ;
for (auto s : pres_sub_server_list_)
removePresSubServer(s);
}
SIPAccount * SIPPresence::getAccount() const {
SIPAccount * SIPPresence::getAccount() const
{
return acc_;
}
pjsip_pres_status * SIPPresence::getStatus() {
pjsip_pres_status * SIPPresence::getStatus()
{
return &pres_status_data;
}
int SIPPresence::getModId() const {
return ((SIPVoIPLink*) (acc_->getVoIPLink()))->getModId();
int SIPPresence::getModId() const
{
return ((SIPVoIPLink*)(acc_->getVoIPLink()))->getModId();
}
pj_pool_t* SIPPresence::getPool() const {
pj_pool_t* SIPPresence::getPool() const
{
return pool_;
}
void SIPPresence::enable(const bool& flag){
enabled = flag;
void SIPPresence::enable(const bool& flag)
{
enabled_ = flag;
}
void SIPPresence::updateStatus(const bool& status, const std::string &note){
void SIPPresence::updateStatus(const bool& status, const std::string &note)
{
//char* pj_note = (char*) pj_pool_alloc(pool_, "50");
pjrpid_element rpid = {
PJRPID_ELEMENT_TYPE_PERSON,
pj_str("20"),
PJRPID_ACTIVITY_UNKNOWN,
pj_str((char *) note.c_str())};
PJRPID_ELEMENT_TYPE_PERSON,
pj_str("20"),
PJRPID_ACTIVITY_UNKNOWN,
pj_str((char *) note.c_str())
};
/* fill activity if user not available. */
if(note=="away")
if (note == "away")
rpid.activity = PJRPID_ACTIVITY_AWAY;
else if (note=="busy")
else if (note == "busy")
rpid.activity = PJRPID_ACTIVITY_BUSY;
else // TODO: is there any other possibilities
DEBUG("Presence : no activity");
......@@ -120,104 +129,117 @@ void SIPPresence::updateStatus(const bool& status, const std::string &note){
pres_status_data.info_cnt = 1;
pres_status_data.info[0].basic_open = status;
pres_status_data.info[0].id = pj_str("0"); /* todo: tuplie_id*/
pj_memcpy(&pres_status_data.info[0].rpid, &rpid,sizeof(pjrpid_element));
pj_memcpy(&pres_status_data.info[0].rpid, &rpid, sizeof(pjrpid_element));
/* "contact" field is optionnal */
}
void SIPPresence::sendPresence(const bool& status, const std::string &note){
updateStatus(status,note);
if(enabled){
if (acc_->isIP2IP())
notifyPresSubServer(); // to each subscribers
else
pres_publish(this); // to the PBX server
}
void SIPPresence::sendPresence(const bool& status, const std::string &note)
{
updateStatus(status, note);
if (not enabled_)
return;
if (acc_->isIP2IP())
notifyPresSubServer(); // to each subscribers
else
pres_publish(this); // to the PBX server
}
void SIPPresence::reportPresSubClientNotification(const std::string& uri, pjsip_pres_status * status){
void SIPPresence::reportPresSubClientNotification(const std::string& uri, pjsip_pres_status * status)
{
/* Update our info. See pjsua_buddy_get_info() for additionnal ideas*/
const std::string basic(status->info[0].basic_open ? "open" : "closed");
const std::string note(status->info[0].rpid.note.ptr,status->info[0].rpid.note.slen);
DEBUG(" Received status of PresSubClient %s: status=%s note=%s",uri.c_str(),(status->info[0].basic_open?"open":"closed"),note.c_str());
const std::string note(status->info[0].rpid.note.ptr, status->info[0].rpid.note.slen);
DEBUG(" Received status of PresSubClient %s: status=%s note=%s", uri.c_str(), (status->info[0].basic_open ? "open" : "closed"), note.c_str());
/* report status to client signal */
Manager::instance().getClient()->getPresenceManager()->newBuddySubscription(uri, status->info[0].basic_open, note);
}
void SIPPresence::subscribeClient(const std::string& uri, const bool& flag){
void SIPPresence::subscribeClient(const std::string& uri, const bool& flag)
{
/* Check if the buddy was already subscribed */
for(auto c : pres_sub_client_list_)
if(c->getURI()==uri){
DEBUG("-PresSubClient:%s exists in the list. Replace it.",uri.c_str());
for (auto c : pres_sub_client_list_)
if (c->getURI() == uri) {
DEBUG("-PresSubClient:%s exists in the list. Replace it.", uri.c_str());
delete c;
removePresSubClient(c);
break;
}
if(pres_sub_client_list_.size() >= MAX_N_PRES_SUB_CLIENT){
if (pres_sub_client_list_.size() >= MAX_N_PRES_SUB_CLIENT) {
WARN("Can't add PresSubClient, max number reached.");
return;
}
if(flag){
PresSubClient *c = new PresSubClient(uri,this);
if(!(c->subscribe())){
if (flag) {
PresSubClient *c = new PresSubClient(uri, this);
if (!(c->subscribe())) {
WARN("Failed send subscribe.");
delete c;
}
// the buddy has to be accepted before being added in the list
}
}
void SIPPresence::addPresSubClient(PresSubClient *c){
if(pres_sub_client_list_.size() < MAX_N_PRES_SUB_CLIENT){
void SIPPresence::addPresSubClient(PresSubClient *c)
{
if (pres_sub_client_list_.size() < MAX_N_PRES_SUB_CLIENT) {
pres_sub_client_list_.push_back(c);
DEBUG("New Presence_subscription_client client added in the list[l=%i].",pres_sub_client_list_.size());
}
else{
DEBUG("New Presence_subscription_client client added in the list[l=%i].", pres_sub_client_list_.size());
} else {
WARN("Max Presence_subscription_client is reach.");
// let the client alive //delete c;
}
}
void SIPPresence::removePresSubClient(PresSubClient *c){
void SIPPresence::removePresSubClient(PresSubClient *c)
{
DEBUG("Presence_subscription_client removed from the buddy list.");
pres_sub_client_list_.remove(c);
}
void SIPPresence::reportnewServerSubscriptionRequest(PresSubServer *s){
void SIPPresence::reportnewServerSubscriptionRequest(PresSubServer *s)
{
Manager::instance().getClient()->getPresenceManager()->newServerSubscriptionRequest(s->remote);
}
void SIPPresence::approvePresSubServer(const std::string& uri, const bool& flag){
void SIPPresence::approvePresSubServer(const std::string& uri, const bool& flag)
{
for (auto s : pres_sub_server_list_)
if(s->matches((char *) uri.c_str())){
DEBUG("Approve Presence_subscription_server for %s: %s.",s->remote,flag? "true":"false");
s->approve(flag);
// return; // 'return' would prevent multiple-time subscribers from spam
}
if (s->matches((char *) uri.c_str())) {
DEBUG("Approve Presence_subscription_server for %s: %s.", s->remote, flag ? "true" : "false");
s->approve(flag);
// return; // 'return' would prevent multiple-time subscribers from spam
}
}
void SIPPresence::addPresSubServer(PresSubServer *s) {
if(pres_sub_server_list_.size() < MAX_N_PRES_SUB_SERVER){
DEBUG("Presence_subscription_server added: %s.",s->remote);
void SIPPresence::addPresSubServer(PresSubServer *s)
{
if (pres_sub_server_list_.size() < MAX_N_PRES_SUB_SERVER) {
DEBUG("Presence_subscription_server added: %s.", s->remote);
pres_sub_server_list_.push_back(s);
}
else{
} else {
WARN("Max Presence_subscription_server is reach.");
// let de server alive // delete s;
}
}
void SIPPresence::removePresSubServer(PresSubServer *s) {
void SIPPresence::removePresSubServer(PresSubServer *s)
{
pres_sub_server_list_.remove(s);
DEBUG("Presence_subscription_server removed");
}
void SIPPresence::notifyPresSubServer() {
void SIPPresence::notifyPresSubServer()
{
DEBUG("Iterating through Presence_subscription_server:");
for (auto s : pres_sub_server_list_)
s->notify();
}
......@@ -232,7 +254,8 @@ void SIPPresence::lock()
void SIPPresence::unlock()
{
if (--mutex_nesting_level_ == 0)
mutex_owner_ = NULL;
mutex_owner_ = NULL;
pj_mutex_unlock(mutex_);
}
......@@ -247,11 +270,12 @@ void SIPPresence::fillDoc(pjsip_tx_data *tdata, const pres_msg_data *msg_data)
pjsip_msg_add_hdr(tdata->msg, h);
}
if(msg_data == NULL)
if (msg_data == NULL)
return;
const pjsip_hdr *hdr;
hdr = msg_data->hdr_list.next;
while (hdr && hdr != &msg_data->hdr_list) {
pjsip_hdr *new_hdr;
new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
......
......@@ -202,7 +202,7 @@ public:
void notifyPresSubServer();
bool isEnabled() const {
return enabled;
return enabled_;
}
const std::list< PresSubClient *> getClientSubscriptions() {
......@@ -214,7 +214,7 @@ public:
pjsip_pres_status pres_status_data; /**< Presence Data.*/
pjsip_publishc *publish_sess; /**< Client publication session.*/
pj_bool_t enabled; /**< Allow for status publish,*/
pj_bool_t enabled_; /**< Allow for status publish,*/
private:
NON_COPYABLE(SIPPresence);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment