Friday, May 30, 2008

Subscriptions in UCMA part 2: Self

In Part 1 we have seen how we can subcribe to Roaming Contacts in this part we will take a look at Self subscription.

You may ask why do we want to subscribe to our self? actually Self subscription returns some important information. Which categories do we have set up. Which containers do we have and the membership of those containers. And who is subscribed to me. The last one can be very handy. For instance we have created a bot, and we want to tell everybody who is using it that it will be down for maintenance or that new functionality has been added.

This one is a little more difficult then the one we have seen in part 1. We actually have to send a message during the subscription.

The class implementing the ISipSubscriptionProcessor interface:

public class RoamingSelfSubscription : ISipSubscriptionProcessor
{
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];
if (requestType == SipSubscription.RequestType.Subscribe ||
requestType == SipSubscription.RequestType.Refresh)
{
contentType = new ContentType("application/vnd-microsoft-roaming-self+xml");
StringWriter sw = new StringWriter(new StringBuilder(128), CultureInfo.InvariantCulture);
XmlTextWriter writer = new XmlTextWriter(sw);
writer.WriteStartElement("roamingList");
writer.WriteAttributeString("xmlns", "http://schemas.microsoft.com/2006/09/sip/roaming-self");
this.AddRoamingType(writer, "categories");
this.AddRoamingType(writer, "containers");
this.AddRoamingType(writer, "subscribers");
writer.WriteEndElement();
sw.Close();
writer.Close();
messageBody = System.Text.Encoding.UTF8.GetBytes(sw.ToString());
}
}

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

void ISipSubscriptionProcessor.SubscriptionStateChanged(SubscriptionStateChangedEventArgs e)
{
//TODO
}
private void AddRoamingType(XmlTextWriter writer, string roamingType)
{
writer.WriteStartElement("roaming");
writer.WriteAttributeString("type", roamingType);
writer.WriteEndElement();
}
public void ProcessNotification(SipMessageData message)
{
String s = message.GetMessageBodyString();
//TODO
}
}



Let's Subscribe, the even package name is "vnd-microsoft-roaming-self".



public void SubscribeRoamingSelf()
{
RoamingSelfSubscription mycgSubscription = new RoamingSelfSubscription();
SipEndpoint myEndpoint = _endPoint;
RealTimeAddress cgAddress = new RealTimeAddress(_endPoint.Uri);
SipSubscription mySubScription = new SipSubscription(myEndpoint, cgAddress, "vnd-microsoft-roaming-self", mycgSubscription);
mySubScription.BeginSubscribe(SubscribeRoamingSelfCallback, mySubScription);

}
private void SubscribeRoamingSelfCallback(IAsyncResult asyncResult)
{
SipSubscription mySubscription = asyncResult.AsyncState as SipSubscription;
try
{
mySubscription.EndSubscribe(asyncResult);
}
catch (PublishSubscribeException)
{
//TODO
}

catch (RealTimeException)
{
//TODO
}
}



 





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

No comments:

working at e-office