Example #1
0
        /// <summary>
        /// Terminates the service application by discarding the alljoyn object
        /// </summary>
        private async void TearDown()
        {
            try
            {
                await busAtt.DisconnectAsync(BasicServiceGlobals.ConnectSpec);
                await busAtt.StopAsync();

                this.OutputLine("Basic Service has been exited.");
                this.OutputLine(string.Empty);
            }
            catch (Exception ex)
            {
                this.OutputLine("An error was produced while closing the service: " + ex.Message.ToString());
            }

            busObject = null;
            busAtt = null;
        }
Example #2
0
        /// <summary>
        /// Established the service functionality of the application by creating and registering
        /// </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 (busObject == null && busAtt == null)
            {
                Task task = new Task(async () =>
                {
                    try
                    {
                        busAtt = new BusAttachment("ServiceApp", true, 4);

                        busObject = new BasicServiceBusObject(busAtt);

                        BasicServiceBusListener busListener = new BasicServiceBusListener(busAtt);
                        OutputLine("BusAttachment, BusListener and BusObject Created.");
                        busAtt.RegisterBusListener(busListener);
                        OutputLine("BusListener Registered.");

                        busAtt.Start();
                        await busAtt.ConnectAsync(BasicServiceGlobals.ConnectSpec);
                        OutputLine("Bundled Daemon Registered.");
                        OutputLine("BusAttachment Connected to " + BasicServiceGlobals.ConnectSpec + ".");
                       
                        SessionOpts sessionOpts = new SessionOpts(
                                BasicServiceGlobals.SessionProps.TrType,
                                BasicServiceGlobals.SessionProps.IsMultiPoint,
                                BasicServiceGlobals.SessionProps.PrType,
                                BasicServiceGlobals.SessionProps.TmType);
                        try
                        {
                            ushort[] portOut = new ushort[1];
                            busAtt.BindSessionPort(BasicServiceGlobals.SessionProps.SessionPort, portOut, sessionOpts, busListener);

                            busAtt.RequestName(BasicServiceGlobals.WellKnownServiceName, (int)RequestNameType.DBUS_NAME_DO_NOT_QUEUE);

                            busAtt.AdvertiseName(BasicServiceGlobals.WellKnownServiceName, TransportMaskType.TRANSPORT_ANY);
                            OutputLine("Name is Being Advertised as: " + BasicServiceGlobals.WellKnownServiceName);
                        }
                        catch (COMException ce)
                        {
                            QStatus s = AllJoynException.GetErrorCode(ce.HResult);
                            OutputLine("Error occurred while trying to establish the service over the bus.");
                            TearDown();
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine("Errors were produced while trying to establish the service.");
                        QStatus status = AllJoynException.GetErrorCode(ex.HResult);
                        busObject = null;
                        busAtt = null;
                    }
                });
                task.Start();
            }
        }