/// <summary>
 /// Creates an instance of the event source class.
 /// </summary>
 /// <param name="prefix">The event source namespace prefix.</param>
 /// <param name="namespaceURI">The event source namespace Uri.</param>
 /// <param name="name">The event source type name.</param>
 public DpwsWseEventSource(string prefix, string namespaceURI, string name)
 {
     m_name         = name;
     m_prefix       = prefix;
     m_namespaceURI = namespaceURI[namespaceURI.Length - 1] == '/' ? namespaceURI.Substring(0, namespaceURI.Length - 1) : namespaceURI;
     m_Operation    = m_namespaceURI + "/" + m_name;
     m_eventSinks   = new DpwsWseEventSinks();
 }
Example #2
0
 /// <summary>
 /// Creates an instance of the event source class.
 /// </summary>
 /// <param name="prefix">The event source namespace prefix.</param>
 /// <param name="namespaceURI">The event source namespace Uri.</param>
 /// <param name="name">The event source type name.</param>
 public DpwsWseEventSource(string prefix, string namespaceURI, string name)
 {
     m_name = name;
     m_prefix = prefix;
     m_namespaceURI = namespaceURI[namespaceURI.Length - 1] == '/' ? namespaceURI.Substring(0, namespaceURI.Length - 1) : namespaceURI;
     m_Operation = m_namespaceURI + "/" + m_name;
     m_eventSinks = new DpwsWseEventSinks();
 }
        /// <summary>
        /// Use this method to raise an event.
        /// </summary>
        /// <param name="hostedService">The hosted service that contains the event source.</param>
        /// <param name="eventSource">The event source that define the event.</param>
        /// <param name="eventMessage">The event message buffer.</param>
        /// <remarks>
        /// A device developer is responsible for building the event message buffer sent
        /// to clients that have an active event subscription with an event source. The subscription manager
        /// uses the hosted service parameter to access various properties of the service. The event source
        /// parameter is used to access the event sinks collection of the event source. Note: This method
        /// requires special provisions in order to properly build event message headers for each event sink.
        /// In order to send an event to a listening client the soap.header.To property must be changed for
        /// each listening client. In the future custom attribute support will solve this problem. For now
        /// however a search and replace mechanism is used to modifiy the header.To property. When a device
        /// developer builds the event message buffer they must use the search string WSDNOTIFYTOADDRESS for
        /// the To header property.
        /// </remarks>
        public void FireEvent(DpwsHostedService hostedService, DpwsWseEventSource eventSource, WsMessage msgEvt)
        {
            // Find the specified event source
            if (eventSource == null)
            {
                throw new ArgumentNullException();
            }

            DpwsWseEventSinks eventSinks = eventSource.EventSinks;

            // if there are event sources display message
            int count = eventSinks.Count;

            if (count > 0)
            {
                System.Ext.Console.Write("");
                System.Ext.Console.Write("Firing " + eventSource.Name);
                System.Ext.Console.Write("");
            }

            // Loop through event sinks and send the event message
            for (int i = count - 1; i >= 0; i--)
            {
                DpwsWseEventSink eventSink = eventSinks[i];

                // Try to send event. If attempt fails delete the subscription/eventSink
                try
                {
                    msgEvt.Header = new WsWsaHeader(msgEvt.Header.Action, msgEvt.Header.RelatesTo, eventSink.NotifyTo.Address.AbsoluteUri,
                                                    null, null, eventSink.NotifyTo.RefParameters);

                    SendEvent(msgEvt, eventSink.NotifyTo.Address);
                }
                catch (Exception e)
                {
                    System.Ext.Console.Write("");
                    System.Ext.Console.Write("FireEvent failed. Deleting EventSink! NotifyToAddress = " + eventSink.NotifyTo.Address + " Exception: " + e.Message);
                    System.Ext.Console.Write("");

                    if (!(e is WebException))
                    {
                        try
                        {
                            // Send oneway subscription end message
                            SendSubscriptionEnd(eventSink, "DeliveryFailure", hostedService.ServiceID);
                        }
                        catch { }
                    }

                    // Remove event sink from event source list
                    eventSinks.RemoveAt(i);
                }
            }
        }
        /// <summary>
        /// Use this method to raise an event.
        /// </summary>
        /// <param name="hostedService">The hosted service that contains the event source.</param>
        /// <param name="eventSource">The event source that define the event.</param>
        /// <param name="eventMessage">The event message buffer.</param>
        /// <remarks>
        /// A device developer is responsible for building the event message buffer sent
        /// to clients that have an active event subscription with an event source. The subscription manager
        /// uses the hosted service parameter to access various properties of the service. The event source
        /// parameter is used to access the event sinks collection of the event source. Note: This method
        /// requires special provisions in order to properly build event message headers for each event sink.
        /// In order to send an event to a listening client the soap.header.To property must be changed for
        /// each listening client. In the future custom attribute support will solve this problem. For now
        /// however a search and replace mechanism is used to modifiy the header.To property. When a device
        /// developer builds the event message buffer they must use the search string WSDNOTIFYTOADDRESS for
        /// the To header property.
        /// </remarks>
        public void FireEvent(DpwsHostedService hostedService, DpwsWseEventSource eventSource, WsWsaHeader eventHeader, String eventMessageBody)
        {
            // Find the specified event source
            if (eventSource == null)
            {
                throw new ArgumentNullException("FireEvent could not locate specified hosted service");
            }
            DpwsWseEventSinks eventSinks = eventSource.EventSinks;

            // if there are event sources display message
            int count = eventSinks.Count;

            if (count > 0)
            {
                System.Ext.Console.Write("");
                System.Ext.Console.Write("Firing " + eventSource.Name);
                System.Ext.Console.Write("");
            }

            // Loop through event sinks and send the event message
            for (int i = 0; i < count; i++)
            {
                DpwsWseEventSink eventSink = eventSinks[i];

                // Try to send event. If attempt fails delete the subscription/eventSink
                try
                {
                    MemoryStream soapStream = new MemoryStream();
                    XmlWriter    xmlWriter  = XmlWriter.Create(soapStream);

                    WsWsaHeader header = new WsWsaHeader(eventHeader.Action, eventHeader.RelatesTo, eventSink.NotifyTo.Address.AbsoluteUri,
                                                         null, null, eventSink.NotifyTo.RefParameters);

                    WsSoapMessageWriter.WriteSoapMessageStart(xmlWriter, WsSoapMessageWriter.Prefixes.Wse, null, header, null);

                    if (eventMessageBody != null)
                    {
                        xmlWriter.WriteRaw(eventMessageBody);
                    }

                    WsSoapMessageWriter.WriteSoapMessageEnd(xmlWriter);

                    xmlWriter.Flush();
                    xmlWriter.Close();

                    SendEvent(soapStream.ToArray(), eventSink.NotifyTo.Address.AbsoluteUri);
                }
                catch (Exception e)
                {
                    System.Ext.Console.Write("");
                    System.Ext.Console.Write("FireEvent failed. Deleting EventSink! NotifyToAddress = " + eventSink.NotifyTo.Address + " Exception: " + e.Message);
                    System.Ext.Console.Write("");

                    // Send oneway subscription end message
                    try
                    {
                        SendSubscriptionEnd(eventSink, "DeliveryFailure", hostedService.ServiceID);
                    }
                    catch { }

                    // Remove event sink from event source list
                    eventSinks.Remove(eventSink);
                }
            }
        }