Wednesday, June 4, 2008

Subscriptions in UCMA part 4: Presence (PIDF)

In this part I'll show you how to implement subscriptions using PIDF. See also Subscriptions in UCMA part 3: Presence.

First we need as with all subscriptions the ISipSubscriptionProcessor.

public class PresenceSubscriptionPIDF : ISipSubscriptionProcessor
{

#region ISipSubscriptionProcessor Members

void ISipSubscriptionProcessor.GetExtensionHeaders(SipSubscription.RequestType requestType, out IEnumerable<SignalingHeader> extensionHeaders)
{

extensionHeaders = null;
}

void ISipSubscriptionProcessor.GetMessageBody(SipSubscription.RequestType requestType, out ContentType contentType, out byte[] messageBody)
{
contentType = null;
messageBody = new Byte[0];

}

void ISipSubscriptionProcessor.ProcessErrorResponse(SipResponseData message)
{
Console.WriteLine("error = " + message.ResponseCode);
}

public void ProcessNotification(SipMessageData message)
{
string s = message.GetMessageBodyString();
string status = "online";
string sip = "";
try{
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
XmlNamespaceManager nsmanager = new XmlNamespaceManager(doc.NameTable);
nsmanager.AddNamespace("p", "urn:ietf:params:xml:ns:pidf");
nsmanager.AddNamespace("ep", "urn:ietf:params:xml:ns:pidf:status:rpid-status");
nsmanager.AddNamespace("ci", "urn:ietf:params:xml:ns:pidf:cipid");
XmlNode node = null;
node = doc.SelectSingleNode("//p:presence", nsmanager);
sip = node.Attributes["entity"].Value;
node = doc.SelectSingleNode("//p:presence/p:tuple/p:status/p:basic", nsmanager);
if (node.InnerText == "closed")
{
status = "offline";
}
else{
node = doc.SelectSingleNode("//ep:activities/ep:activity", nsmanager);
status = node.InnerText;
}


}catch{
}
//TODO
}

void ISipSubscriptionProcessor.SubscriptionStateChanged(SubscriptionStateChangedEventArgs eventArg)
{
//TODO
}

#endregion
}



Let's subscribe:




public void SubscribePresence(string sip)
{
PresenceSubscriptionPIDF presenceSubscription = new PresenceSubscriptionPIDF();
SipEndpoint myEndpoint = _endPoint;
RealTimeAddress cgAddress = new RealTimeAddress(sip);
SipSubscription mySubScription = new SipSubscription(myEndpoint, cgAddress, "presence", presenceSubscription);
mySubScription.BeginSubscribe(BeginSubscribeCallback, mySubScription);
}
public void BeginSubscribeCallback(IAsyncResult asyncResult)
{

SipSubscription mySubscription = asyncResult.AsyncState as SipSubscription;
try
{
mySubscription.EndSubscribe(asyncResult);

}
catch (PublishSubscribeException)
{
//Error("An exception occurred when unregistering the session.\n{0}", endpoint.ToString());
}

catch (RealTimeException)
{
//Error("An exception occurred when unregistering the session.\n{0}", endpoint.ToString());
}
}



For more information/questions/remarks please contact me sip:marc.wetters@e-office.com



No comments:

working at e-office