Commit 071661fa authored by Tristan Matthews's avatar Tristan Matthews

* #29736: presence: cleanup

parent df2d411d
......@@ -56,8 +56,8 @@
int modId; // used to extract data structure from event_subscription
void pres_client_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry) {
//(void) th;
void pres_client_timer_cb(pj_timer_heap_t * /*th*/, pj_timer_entry *entry)
{
/* TODO : clean*/
PresSubClient *c = (PresSubClient *) entry->user_data;
DEBUG("timout for %s",c->getURI().c_str());
......
......@@ -39,9 +39,11 @@
#include "pres_sub_server.h"
/* Callback called when *server* subscription state has changed. */
void pres_evsub_on_srv_state(pjsip_evsub *sub, pjsip_event *event) {
void pres_evsub_on_srv_state(pjsip_evsub *sub, pjsip_event *event)
{
pjsip_rx_data *rdata = event->body.rx_msg.rdata;
if(!rdata) {
if (!rdata) {
DEBUG("Presence_subscription_server estate has changed but no rdata.");
return;
}
......@@ -49,7 +51,7 @@ void pres_evsub_on_srv_state(pjsip_evsub *sub, pjsip_event *event) {
PJ_UNUSED_ARG(event);
SIPPresence * pres = Manager::instance().getSipAccount("IP2IP")->getPresence();
pres->lock();
PresSubServer *presSubServer = (PresSubServer *) pjsip_evsub_get_mod_data(sub,pres->getModId());
PresSubServer *presSubServer = (PresSubServer *) pjsip_evsub_get_mod_data(sub, pres->getModId());
DEBUG("Presence_subscription_server to %s is %s", presSubServer->remote, pjsip_evsub_get_state_name(sub));
if (presSubServer) {
......@@ -61,12 +63,15 @@ void pres_evsub_on_srv_state(pjsip_evsub *sub, pjsip_event *event) {
pjsip_evsub_set_mod_data(sub, pres->getModId(), NULL);
pres->removePresSubServer(presSubServer);
}
/* TODO check if other cases should be handled*/
}
pres->unlock();
}
pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata)
{
pjsip_method *method = &rdata->msg_info.msg->line.req.method;
pj_str_t *str = &method->name;
......@@ -93,9 +98,9 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
std::string name(rdata->msg_info.to->name.ptr, rdata->msg_info.to->name.slen);
std::string server(rdata->msg_info.from->name.ptr, rdata->msg_info.from->name.slen);
DEBUG("Incomming pres_on_rx_subscribe_request for %s, name:%s, server:%s."
, request.c_str()
, name.c_str()
, server.c_str());
, request.c_str()
, name.c_str()
, server.c_str());
/* get parents*/
std::string accountId = "IP2IP"; /* this code is only used for IP2IP accounts */
......@@ -108,6 +113,7 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
std::string c(acc->getContactHeader());
const pj_str_t contact = pj_str((char*) c.c_str());
status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, &contact, &dlg);
if (status != PJ_SUCCESS) {
char errmsg[PJ_ERR_MSG_SIZE];
pj_strerror(status, errmsg, sizeof(errmsg));
......@@ -123,6 +129,7 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
/* Create server presence subscription: */
status = pjsip_pres_create_uas(dlg, &pres_cb, rdata, &sub);
if (status != PJ_SUCCESS) {
int code = PJSIP_ERRNO_TO_SIP_STATUS(status);
pjsip_tx_data *tdata;
......@@ -134,6 +141,7 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
}
status = pjsip_dlg_create_response(dlg, rdata, code, NULL, &tdata);
if (status == PJ_SUCCESS) {
status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), tdata);
}
......@@ -145,10 +153,12 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
/* Attach our data to the subscription: */
char* remote = (char*) pj_pool_alloc(dlg->pool, PJSIP_MAX_URL_SIZE);
status = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, dlg->remote.info->uri, remote, PJSIP_MAX_URL_SIZE);
if (status < 1)
pj_ansi_strcpy(remote, "<-- url is too long-->");
else
remote[status] = '\0';
//pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, dlg->local.info->uri, contact.ptr, PJSIP_MAX_URL_SIZE);
/* Create a new PresSubServer server and wait for client approve */
......@@ -159,6 +169,7 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
/* Capture the value of Expires header. */
expires_hdr = (pjsip_expires_hdr*) pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
if (expires_hdr)
presSubServer->setExpires(expires_hdr->ivalue);
else
......@@ -173,6 +184,7 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
/* Create and send 2xx response to the SUBSCRIBE request: */
status = pjsip_pres_accept(sub, rdata, st_code, &msg_data.hdr_list);
if (status != PJ_SUCCESS) {
WARN("Unable to accept presence subscription %d", status);
pjsip_pres_terminate(sub, PJ_FALSE);
......@@ -182,7 +194,8 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
// Unsubscribe case
ev_state = PJSIP_EVSUB_STATE_ACTIVE;
if (presSubServer->getExpires() == 0){
if (presSubServer->getExpires() == 0) {
ev_state = PJSIP_EVSUB_STATE_TERMINATED;
pres->unlock();
return PJ_TRUE;
......@@ -194,23 +207,24 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
// fake temporary status
pjrpid_element rpid = {
PJRPID_ELEMENT_TYPE_PERSON,
pj_str("20"),
PJRPID_ACTIVITY_UNKNOWN,
pj_str("Your subscription was received and waits for approval.")
PJRPID_ELEMENT_TYPE_PERSON,
pj_str("20"),
PJRPID_ACTIVITY_UNKNOWN,
pj_str("Your subscription was received and waits for approval.")
};
pjsip_pres_status fake_status_data;
pj_bzero(&fake_status_data, sizeof(pjsip_pres_status));
fake_status_data.info_cnt = 1;
fake_status_data.info[0].basic_open = false;
fake_status_data.info[0].id = pj_str("0"); /* todo: tuplie_id*/
pj_memcpy(&fake_status_data.info[0].rpid, &rpid,sizeof(pjrpid_element));
pj_memcpy(&fake_status_data.info[0].rpid, &rpid, sizeof(pjrpid_element));
pjsip_pres_set_status(sub, &fake_status_data);
/* Create and send the the first NOTIFY to active subscription: */
pj_str_t stateStr = pj_str("");
tdata = NULL;
status = pjsip_pres_notify(sub, ev_state, &stateStr, &reason, &tdata);
if (status == PJ_SUCCESS) {
pres->fillDoc(tdata, &msg_data);
status = pjsip_pres_send_request(sub, tdata);
......@@ -222,6 +236,7 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
pres->unlock();
return status;
}
pres->unlock();
return PJ_TRUE;
}
......@@ -229,63 +244,64 @@ pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata) {
PresSubServer::PresSubServer(SIPPresence * pres, pjsip_evsub *evsub, char *r, pjsip_dialog *d)
: pres_(pres)
, sub(evsub)
, remote(r)
, dlg(d)
, expires(-1)
, approved(false)
: remote(r)
, pres_(pres)
, sub_(evsub)
, dlg_(d)
, expires_(-1)
, approved_(false)
{}
PresSubServer::~PresSubServer(){
PresSubServer::~PresSubServer()
{
//TODO: check if evsub needs to be forced TERMINATED.
}
void PresSubServer::setExpires(int ms) {
expires = ms;
void PresSubServer::setExpires(int ms)
{
expires_ = ms;
}
int PresSubServer::getExpires(){
return expires;
int PresSubServer::getExpires()
{
return expires_;
}
/*SIPPresence * PresSubServer::getPresence(){
return pres_;
}*/
bool PresSubServer::matches(char *s){
bool PresSubServer::matches(char *s)
{
// servers match if they have the same remote uri and the account ID.
return (!(strcmp(remote,s))) ;
return (!(strcmp(remote, s))) ;
}
void PresSubServer::approve(const bool& flag){
approved = flag;
void PresSubServer::approve(const bool& flag)
{
approved_ = flag;
// attach the real status data
pjsip_pres_set_status(sub, pres_->getStatus());
pjsip_pres_set_status(sub_, pres_->getStatus());
}
void PresSubServer::notify() {
/* Only send NOTIFY once subscription is active. Some subscriptions
* may still be in NULL (when app is adding a new buddy while in the
* on_incoming_subscribe() callback) or PENDING (when user approval is
* being requested) state and we don't send NOTIFY to these subs until
* the user accepted the request.
*/
if ((pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE) && (approved)) {
void PresSubServer::notify()
{
/* Only send NOTIFY once subscription is active. Some subscriptions
* may still be in NULL (when app is adding a new buddy while in the
* on_incoming_subscribe() callback) or PENDING (when user approval is
* being requested) state and we don't send NOTIFY to these subs until
* the user accepted the request.
*/
if ((pjsip_evsub_get_state(sub_) == PJSIP_EVSUB_STATE_ACTIVE) && (approved_)) {
DEBUG("Notifying %s.", remote);
pjsip_tx_data *tdata;
pjsip_pres_set_status(sub, pres_->getStatus());
pjsip_pres_set_status(sub_, pres_->getStatus());
if (pjsip_pres_current_notify(sub, &tdata) == PJ_SUCCESS) {
if (pjsip_pres_current_notify(sub_, &tdata) == PJ_SUCCESS) {
// add msg header and send
pres_->fillDoc(tdata, NULL);
pjsip_pres_send_request(sub, tdata);
}
else{
pjsip_pres_send_request(sub_, tdata);
} else {
WARN("Unable to create/send NOTIFY");
pjsip_pres_terminate(sub, PJ_FALSE);
pjsip_pres_terminate(sub_, PJ_FALSE);
}
}
}
......@@ -31,7 +31,7 @@
#ifndef SERVERPRESENCESUB_H
#define SERVERPRESENCESUB_H
#define SERVERPRESENCESUB_H
#include <pj/string.h>
#include <pjsip/sip_types.h>
......@@ -44,19 +44,19 @@
extern pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata);
static pjsip_module mod_presence_server = {
NULL, NULL, /* prev, next. */
pj_str("mod-presence-server"), /* Name. */
-1, /* Id */
NULL, NULL, /* prev, next. */
pj_str("mod-presence-server"), /* Name. */
-1, /* Id */
PJSIP_MOD_PRIORITY_DIALOG_USAGE,
NULL, /* load() */
NULL, /* start() */
NULL, /* stop() */
NULL, /* unload() */
&pres_on_rx_subscribe_request, /* on_rx_request() */
NULL, /* on_rx_response() */
NULL, /* on_tx_request. */
NULL, /* on_tx_response() */
NULL, /* on_tsx_state() */
NULL, /* load() */
NULL, /* start() */
NULL, /* stop() */
NULL, /* unload() */
&pres_on_rx_subscribe_request, /* on_rx_request() */
NULL, /* on_rx_response() */
NULL, /* on_tx_request. */
NULL, /* on_tx_response() */
NULL, /* on_tsx_state() */
};
......@@ -65,44 +65,44 @@ class SIPpresence;
class PresSubServer {
public:
PresSubServer(SIPPresence * pres, pjsip_evsub *evsub, char *r,pjsip_dialog *d);
~PresSubServer();
/* TODO: add '< >' to URI for consistance*/
char *remote; /**< Remote URI. */
/*
* Acces to the evsub expire variable.
* It was recieved in the SUBSCRIBE request.
*/
void setExpires(int ms);
int getExpires();
/*
* Match method
* s is the URI (remote)
*/
bool matches(char *s);
/*
* Allow the subscriber for being notified.
*/
void approve(const bool& flag);
/*
* Notify subscriber with the pres_status_date of the account
*/
void notify();
friend void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event);
friend pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata);
private:
NON_COPYABLE(PresSubServer);
SIPPresence *pres_;
pjsip_evsub *sub; /**< The evsub. */
pjsip_dialog *dlg; /**< Dialog. */
int expires; /**< "expires" value in the request. */
bool approved; /**< The client approved this subscription*/
public:
PresSubServer(SIPPresence * pres, pjsip_evsub *evsub, char *r, pjsip_dialog *d);
~PresSubServer();
/* TODO: add '< >' to URI for consistance*/
char *remote; /**< Remote URI. */
/*
* Acces to the evsub expire variable.
* It was recieved in the SUBSCRIBE request.
*/
void setExpires(int ms);
int getExpires();
/*
* Match method
* s is the URI (remote)
*/
bool matches(char *s);
/*
* Allow the subscriber for being notified.
*/
void approve(const bool& flag);
/*
* Notify subscriber with the pres_status_date of the account
*/
void notify();
friend void pres_evsub_on_srv_state(pjsip_evsub *sub, pjsip_event *event);
friend pj_bool_t pres_on_rx_subscribe_request(pjsip_rx_data *rdata);
private:
NON_COPYABLE(PresSubServer);
SIPPresence *pres_;
pjsip_evsub *sub_;
pjsip_dialog *dlg_;
int expires_;
bool approved_;
};
#endif /* SERVERPRESENCESUB_H */
#endif /* SERVERPRESENCESUB_H */
......@@ -32,7 +32,6 @@
#ifndef SIPPRESENCE_H
#define SIPPRESENCE_H
#include <vector>
#include <string>
#include <list>
......@@ -214,9 +213,9 @@ public:
pjsip_pres_status pres_status_data; /**< Presence Data.*/
pjsip_publishc *publish_sess; /**< Client publication session.*/
pj_bool_t enabled_; /**< Allow for status publish,*/
private:
pj_bool_t enabled_; /**< Allow for status publish,*/
NON_COPYABLE(SIPPresence);
SIPAccount * acc_; /**< Associated SIP account. */
......
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