/// <summary> /// Stops the signal service through tearing down the bus /// </summary> /// <param name="sender">UI control which signaled the click event</param> /// <param name="e">arguments associated with the click event</param> private async void Button_StopClick(object sender, RoutedEventArgs e) { if (busAtt != null) { try { await busAtt.DisconnectAsync(SignalConsumerGlobals.ConnectSpec); await busAtt.StopAsync(); this.OutputLine("Signal Consumer has Exited."); } catch (Exception ex) { string exceptionString = ex.ToString(); this.OutputLine("Exiting Basic Service Produced Errors!"); System.Diagnostics.Debug.WriteLine("Exception: " + exceptionString); } busAtt = null; busListener = null; } }
/// <summary> /// Connects to the bus and registers a signal handler for when the 'name' property changes. /// </summary> /// <param name="sender">UI control which signaled the click event.</param> /// <param name="e">arguments associated with the click event.</param> private void Button_RunClick(object sender, RoutedEventArgs e) { if (busAtt == null) { Task task = new Task(async() => { try { busAtt = new BusAttachment("SignalConsumerApp", true, 4); // create and activate the interface InterfaceDescription[] interfaceDescription = new InterfaceDescription[1]; busAtt.CreateInterface(SignalConsumerGlobals.InterfaceName, interfaceDescription, false); interfaceDescription[0].AddSignal("nameChanged", "s", "newName", (byte)0, string.Empty); interfaceDescription[0].AddProperty("name", "s", (byte)PropAccessType.PROP_ACCESS_RW); interfaceDescription[0].Activate(); busListener = new SignalConsumerBusListener(busAtt, foundNameEvent); OutputLine("BusAttachment and BusListener Created."); busAtt.RegisterBusListener(busListener); OutputLine("BusListener Registered."); busAtt.Start(); busAtt.ConnectAsync(SignalConsumerGlobals.ConnectSpec).AsTask().Wait(); OutputLine("Bundled Daemon Registered."); OutputLine("BusAttachment Connected to " + SignalConsumerGlobals.ConnectSpec + "."); busAtt.FindAdvertisedName(SignalConsumerGlobals.WellKnownServiceName); foundNameEvent.WaitOne(); /* Configure session properties and request a session with device with wellKnownName */ SessionOpts sessionOpts = new SessionOpts( SignalConsumerGlobals.SessionProps.TrType, SignalConsumerGlobals.SessionProps.IsMultiPoint, SignalConsumerGlobals.SessionProps.PrType, SignalConsumerGlobals.SessionProps.TmType); SessionOpts[] sessionOptsOut = new SessionOpts[1]; OutputLine("Requesting a session with the well known service name."); JoinSessionResult joinResult = await busAtt.JoinSessionAsync( SignalConsumerGlobals.WellKnownServiceName, SignalConsumerGlobals.SessionProps.SessionPort, busListener, sessionOpts, sessionOptsOut, null); if (QStatus.ER_OK == joinResult.Status) { OutputLine("Join Session was successful (sessionId=" + joinResult.SessionId + ")."); busAtt.AddMatch("type='signal',interface='org.alljoyn.Bus.signal_sample',member='nameChanged'"); OutputLine("Subscribed to the 'nameChanged' signal."); } else { OutputLine("Join Session was unsuccessful."); } } catch (Exception ex) { OutputLine("Errors were produced while establishing the application."); QStatus status = AllJoynException.GetErrorCode(ex.HResult); busAtt = null; } }); task.Start(); } }