Wednesday, June 4, 2008

Subscriptions using UCMA part 5: Enhanced Presence

This one is the real interesting one, how can you subscribe to enhanced presence. See also Subscriptions in UCMA part 3: Presence

The sample code here shows how to subscribe to one user. Actually this is a batch subscribe and is easy extensible for multiple users.

This one is much more complex then subscribing to presence using PIDF. The real thing here is enhanced presence. You can subscribe to multiple categories.

First let's create ISipSubscriptionProcessor again. We have to send a message and we have to set our signaling headers correct. This sample doesn't show how to handle the notification message.

public class PresenceSubscriptionEnhanced : ISipSubscriptionProcessor
{
private string _uri = "";
#region ISipSubscriptionProcessor Members

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

List<SignalingHeader> list = new List<SignalingHeader>();
list.Add(new SignalingHeader("Accept", "application/msrtc-event-categories+xml"));
list.Add(new SignalingHeader("Accept", "application/rlmi+xml"));
list.Add(new SignalingHeader("Accept", "multipart/related"));
list.Add(new SignalingHeader("Content-Type", "application/msrtc-adrl-categorylist+xml"));
list.Add(new SignalingHeader("supported", "eventlist"));
list.Add(new SignalingHeader("supported", "ms-piggyback-first-notify"));
list.Add(new SignalingHeader("Require", "adhoclist"));
list.Add(new SignalingHeader("Require", "categoryList"));
extensionHeaders = list;

}

void ISipSubscriptionProcessor.GetMessageBody(SipSubscription.RequestType requestType, out ContentType contentType, out byte[] messageBody)
{
contentType = new ContentType("application/msrtc-adrl-categorylist+xml");
string message = "<batchSub xmlns=\"http://schemas.microsoft.com/2006/01/sip/batch-subscribe\" uri=\"sip:Marc.Wetters@e-office.com\" name=\"\"> <action name=\"subscribe\" id=\"63792024\"> <adhocList> <resource uri=\""+_uri+"\"/> </adhocList> <categoryList xmlns=\"http://schemas.microsoft.com/2006/09/sip/categorylist\"> <category name=\"calendarData\"/> <category name=\"contactCard\"/> <category name=\"note\"/> <category name=\"services\"/> <category name=\"state\"/> </categoryList> </action> </batchSub>";
messageBody = System.Text.Encoding.UTF8.GetBytes(message);

}

void ISipSubscriptionProcessor.ProcessErrorResponse(SipResponseData message)
{
//TODO;
}

public void ProcessNotification(SipMessageData message)
{
string s = message.GetMessageBodyString();
//TODO

}

void ISipSubscriptionProcessor.SubscriptionStateChanged(SubscriptionStateChangedEventArgs eventArg)
{
//TODO
}
#endregion
public string Uri
{
get { return _uri;}
set { _uri = value; }

}
}



Let's subscribe.




public void SubscribePresence(string sip)
{
PresenceSubscriptionPIDF presenceSubscription = new PresenceSubscriptionPIDF();
presenceSubscription.Uri = sip;
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



 



1 comment:

Ernő said...

Very interesting article.

What should be the RealTimeAddress in case trying to subscribe to multiple users? Is it possible to add/remove users from and existing subscription using the SendUpdateMessage?

working at e-office