WidcommBluetoothFactory.cs :  » Business-Application » 32feet.NET » InTheHand » Net » Bluetooth » Widcomm » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » 32feet.NET 
32feet.NET » InTheHand » Net » Bluetooth » Widcomm » WidcommBluetoothFactory.cs
// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Net.Bluetooth.Widcomm.WidcommBluetoothFactoryBase
// 
// Copyright (c) 2008-2010 In The Hand Ltd, All rights reserved.
// This source code is licensed under the In The Hand Community License - see License.txt

using System;
using System.Collections.Generic;
using System.Text;
using InTheHand.Net.Sockets;
using System.Diagnostics;
using InTheHand.Net.Bluetooth.Factory;

namespace InTheHand.Net.Bluetooth.Widcomm{
    abstract class WidcommBluetoothFactoryBase : BluetoothFactory
    {
        internal abstract WidcommBtInterface GetWidcommBtInterface();
        internal abstract WidcommRfcommStream GetWidcommRfcommStream();
        internal abstract WidcommRfcommStream GetWidcommRfcommStreamWithoutRfcommIf();
        internal abstract IRfcommPort GetWidcommRfcommPort();
        internal abstract IRfCommIf GetWidcommRfCommIf();
        internal abstract ISdpService GetWidcommSdpService();
        //
#if WIDCOMM_SINGLE_THREADING || WinXP
        internal abstract WidcommPortSingleThreader GetSingleThreader();
#endif
    }

    sealed class WidcommBluetoothFactory : WidcommBluetoothFactoryBase
    {
        static IBtIf s_btIf;
        static WidcommBtInterface s_btInterface;
        static WidcommBluetoothSecurity m_btSecurity;
#if WIDCOMM_SINGLE_THREADING || WinXP
        static WidcommPortSingleThreader _st;
#endif

        public WidcommBluetoothFactory()
        {
            lock (typeof(WidcommBluetoothFactory)) {
                // CBtIf: MUST ONLY be ONE of these, and must be FIRST created!
                //   "Only one object of this class should be instantiated by the application."
                //   "This class must be instantiated before any other DK classes are used"
                if (s_btIf == null) {
                    IBtIf btIf = new WidcommBtIf();
#if WIDCOMM_SINGLE_THREADING || WinXP
                    Debug.Assert(_st == null);
                    if (_st == null) _st = new WidcommPortSingleThreader();
                    Debug.Assert(GetSingleThreader() != null);
                    if (GetSingleThreader() != null) {
                        btIf = new WidcommStBtIf(this, btIf);
                        WidcommUtils.Trace_WriteLine("IBtIf using WidcommStBtIf.");
                    }
#endif
                    Debug.Assert(s_btInterface == null);
                    WidcommBtInterface btInterface = new WidcommBtInterface(btIf, this);
                    // Don't set these until we're sure that initialisation has 
                    // all completed successfully.
                    s_btIf = btIf;
                    s_btInterface = btInterface;
                } else {
                    Debug.Assert(s_btInterface != null, "One set but not the other!");
                }
            }
            // Check radio is connected.
            this.GetPrimaryRadio();
        }

        //-----
        protected override void Dispose(bool disposing)
        {
            IDisposable iface;
            IDisposable st;
            lock (typeof(WidcommBluetoothFactory)) {
                iface = (IDisposable)s_btInterface;
                s_btIf = null;
                s_btInterface = null;
                m_btSecurity = null;
#if WIDCOMM_SINGLE_THREADING || WinXP
                st = _st;
                _st = null;
#else
                st = null;
#endif
            }//lock
            if (iface != null)
                iface.Dispose();
            if (st != null)  // Must NOT dispose this first!
                st.Dispose();
        }

        //-----
#if WIDCOMM_SINGLE_THREADING || WinXP
        internal override WidcommPortSingleThreader GetSingleThreader()
        {
            return _st;
        }
#endif

        //-----
        protected override IBluetoothClient GetBluetoothClient()
        {
            return new WidcommBluetoothClient(this);
        }

        protected override IBluetoothClient GetBluetoothClient(System.Net.Sockets.Socket acceptedSocket)
        {
            throw new NotSupportedException("Cannot create a BluetoothClient from a Socket on the Widcomm stack.");
        }

        protected override IBluetoothClient GetBluetoothClient(BluetoothEndPoint localEP)
        {
            return new WidcommBluetoothClient(localEP, this);
        }

        //----------------
        protected override IBluetoothListener GetBluetoothListener()
        {
            return new WidcommBluetoothListener(this);
        }

        //----------------
        internal override WidcommBtInterface GetWidcommBtInterface()
        {
            return s_btInterface;
        }

        internal override WidcommRfcommStream GetWidcommRfcommStream()
        {
            return new WidcommRfcommStream(GetWidcommRfcommPort(), GetWidcommRfCommIf(), this);
        }

        internal override WidcommRfcommStream GetWidcommRfcommStreamWithoutRfcommIf()
        {
            return new WidcommRfcommStream(GetWidcommRfcommPort(), null, this);
        }

        internal override IRfcommPort GetWidcommRfcommPort()
        {
            // Handling of single threadedness is done within WidcommRfcommStream.
            return new WidcommRfcommPort();
        }

        internal override IRfCommIf GetWidcommRfCommIf()
        {
            IRfCommIf inst = new WidcommRfCommIf();
#if WIDCOMM_SINGLE_THREADING || WinXP
            if (GetSingleThreader() != null) {
                inst = new WidcommStRfCommIf(this, inst);
                WidcommUtils.Trace_WriteLine("IRfCommIf using WidcommStRfCommIf.");
            }
#endif
            return inst;
        }

        protected override IBluetoothDeviceInfo GetBluetoothDeviceInfo(BluetoothAddress address)
        {
            return WidcommBluetoothDeviceInfo.CreateFromGivenAddress(address, this);
        }

        //----------------
        protected override IBluetoothRadio GetPrimaryRadio()
        {
            return new WidcommBluetoothRadio(this);
        }

        protected override IBluetoothRadio[] GetAllRadios()
        {
            // Widcomm supports only one radio.
            return new IBluetoothRadio[] { GetPrimaryRadio() };
        }

        //----------------
        internal override ISdpService GetWidcommSdpService()
        {
            return new SdpService();
        }

        //----------------
        protected override IBluetoothSecurity GetBluetoothSecurity()
        {
            if (m_btSecurity == null) {
                m_btSecurity = new WidcommBluetoothSecurity(this);
            }
            return m_btSecurity;
        }

    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.