/// <summary>
 /// Writes this instances properties to the item using it's public properties (Only data related fields are assigned)
 /// </summary>
 /// <param name="item">The item to write properties to</param>
 public void WriteProperties(AddressBookItem item)
 {
     item.Name        = _name;
     item.Port        = _port;
     item.Description = _description;
     item.Address     = _address;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a AddressBookItem to the list
        /// </summary>
        /// <param name="item"></param>
        public void Add(AddressBookItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("AddressBookItem", "A null reference to a AddressBookItem cannot be added to the list.");
            }

            if (this.Contains(item))
            {
                throw new NameNotUniqueException(item.Name);
            }

            // bind to the end point's events
            item.Changed           += new AddressingEventHandler(this.OnChanged);
            item.BeforeNameChanged += new NameChangeEventHandler(this.OnBeforeAddressNameAddressChanged);

            // add the item
            base.InnerList.Add(item);

            // set the item's parent
            item.Parent = _parent;

            /*
             * raise the event
             *
             * starting it at the object level, which will trickle all the way up through the object heirarchies
             * normally, the "this" pointer would be the context of the call, but because we want the object
             * to get the add and remove events as well, we start the call with it
             * */
            AddressBookItemEventArgs e = new AddressBookItemEventArgs(item, AddressingActions.Added);

            item.OnChanged(this, e);
            //this.OnChanged(this, e);
        }
Exemplo n.º 3
0
        public void Add(AddressBookItem item, bool overwrite)
        {
            // if we're not overwriting, just add it like normal
            if (!overwrite)
            {
                this.Add(item);
                return;
            }

            // again we can't add null references
            if (item == null)
            {
                throw new ArgumentNullException("AddressBookItem", "A null reference to an AddressBookItem cannot be added to the list.");
            }

            // if there is a collision, go ahead and overwrite the existing one
            if (this.Contains(item))
            {
                // find the existing one
                AddressBookItem existingItem = this[item.Name];

                // write the properties from the one coming in, to the existing one
                item.WriteProperties(existingItem);
                return;
            }
            else
            {
                // try and find it by id
                AddressBookItem existingItem = this.FindById(item.Id);

                // if we have a hit, that means this is the same object, just renamed
                if (existingItem != null)
                {
                    // so give it a new id
                    item.GetNewId();
                }
            }

            // bind to the end point's events
            item.Changed           += new AddressingEventHandler(this.OnChanged);
            item.BeforeNameChanged += new NameChangeEventHandler(this.OnBeforeAddressNameAddressChanged);

            // add the item
            base.InnerList.Add(item);

            // set the item's parent
            item.Parent = _parent;

            /*
             * raise the event
             *
             * starting it at the object level, which will trickle all the way up through the object heirarchies
             * normally, the "this" pointer would be the context of the call, but because we want the object
             * to get the add and remove events as well, we start the call with it
             * */
            AddressBookItemEventArgs e = new AddressBookItemEventArgs(item, AddressingActions.Added);

            item.OnChanged(this, e);
//			this.OnChanged(this, e);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Looks up the connection manager responsible for the address book item and disconnects it from the remote host
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDisconnectSessionForAddressBookItem(object sender, BackgroundThreadStartEventArgs e)
        {
            // retrieve the address book item that is requiring attention
            AddressBookItem item = (AddressBookItem)e.Args[0];

            try
            {
                lock (_sessionManagers)
                {
                    AddressBookItemConnectionManager manager = _sessionManagers[item];
                    if (manager != null)
                    {
                        manager.Disconnect();
                    }
                }
            }
            catch (ThreadAbortException)
            {
                // ignore thread abort exceptions
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
		/// <summary>
		/// Adds a AddressBookItem to the list
		/// </summary>
		/// <param name="item"></param>
		public void Add(AddressBookItem item)
		{
			if (item == null)
				throw new ArgumentNullException("AddressBookItem", "A null reference to a AddressBookItem cannot be added to the list.");
			
			if (this.Contains(item))
				throw new NameNotUniqueException(item.Name);

			// bind to the end point's events
			item.Changed += new AddressingEventHandler(this.OnChanged);
            item.BeforeNameChanged += new NameChangeEventHandler(this.OnBeforeAddressNameAddressChanged);

			// add the item
			base.InnerList.Add(item);			
			
			// set the item's parent
			item.Parent = _parent;

			/*
			 * raise the event
			 * 
			 * starting it at the object level, which will trickle all the way up through the object heirarchies
			 * normally, the "this" pointer would be the context of the call, but because we want the object
			 * to get the add and remove events as well, we start the call with it
			 * */
			AddressBookItemEventArgs e = new AddressBookItemEventArgs(item, AddressingActions.Added);
			item.OnChanged(this, e); 
			//this.OnChanged(this, e);
		}
Exemplo n.º 6
0
        /// <summary>
        /// Removes a AddressBookItem from the list
        /// </summary>
        /// <param name="item"></param>
        public void Remove(AddressBookItem item)
        {
            if (this.Contains(item))
            {
                /*
                 * raise the event
                 *
                 * starting it at the object level, which will trickle all the way up through the object heirarchies
                 * normally, the "this" pointer would be the context of the call, but because we want the object
                 * to get the add and remove events as well, we start the call with it
                 * */
                AddressBookItemEventArgs e = new AddressBookItemEventArgs(item, AddressingActions.Removed);
                item.OnChanged(this, e);
//				this.OnChanged(this, e);

                item.Changed           -= new AddressingEventHandler(this.OnChanged);
                item.BeforeNameChanged -= new NameChangeEventHandler(this.OnBeforeAddressNameAddressChanged);

                // remove the item
                base.InnerList.Remove(item);

                // orphan the item
                item.Parent = null;
            }
        }
 /// <summary>
 /// Initializes a new instance of the AddressBookItem class
 /// </summary>
 /// <param name="address">Another AddressBookItem instance to copy properties from (a cloning constructor)</param>
 public AddressBookItem(AddressBookItem item)
 {
     _id          = item.Id;
     _address     = item.Address;
     _port        = item.Port;
     _name        = item.Name;
     _description = item.Description;
 }
Exemplo n.º 8
0
        /// <summary>
        /// Determines if the list contains a AddressBookItem with the same id
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(AddressBookItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("AddressBookItem", "A null reference to a AddressBookItem cannot be compared to items in the list.");
            }

            return(this.Contains(item.Name));
        }
		/// <summary>
		/// Returns the connection manager responsible for the address book item
		/// </summary>
		public AddressBookItemConnectionManager this[AddressBookItem item]
		{
			get
			{
				foreach(AddressBookItemConnectionManager manager in base.InnerList)
					if (manager.AddressBookItem == item)
						return manager;
				return null;
			}
		}
Exemplo n.º 10
0
 /// <summary>
 /// Looks up the context currently bound to an item
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public virtual IAddressBookItemContext LookupContextForItem(AddressBookItem item)
 {
     lock (_contextLookupTable)
     {
         if (_contextLookupTable.ContainsKey(item))
         {
             return(_contextLookupTable[item] as IAddressBookItemContext);
         }
     }
     return(null);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Creates a queued thread pool job that will connect a connection for the address book item
        /// </summary>
        /// <param name="item"></param>
        /// <param name="disconnectFirst"></param>
        public virtual void Connect(AddressBookItem item, bool disconnectFirst, bool verboseSession, bool autoRecv)
        {
            BackgroundThreadPoolJob job = new BackgroundThreadPoolJob(
                item.Id,
                true,
                this,
                new object[] { item, disconnectFirst, verboseSession, autoRecv },
                new BackgroundThreadStartEventHandler(OnConnectSessionForAddressBookItem),
                null);

            _threadPool.QueueJob(job);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Creates a queued thread pool job that will disconnect a connection for the address book item
        /// </summary>
        /// <param name="item"></param>
        public virtual void Disconnect(AddressBookItem item)
        {
            BackgroundThreadPoolJob job = new BackgroundThreadPoolJob(
                item.Id,
                true,
                this,
                new object[] { item },
                new BackgroundThreadStartEventHandler(OnDisconnectSessionForAddressBookItem),
                null);

            _threadPool.QueueJob(job);
        }
 /// <summary>
 /// Returns the connection manager responsible for the address book item
 /// </summary>
 public AddressBookItemConnectionManager this[AddressBookItem item]
 {
     get
     {
         foreach (AddressBookItemConnectionManager manager in base.InnerList)
         {
             if (manager.AddressBookItem == item)
             {
                 return(manager);
             }
         }
         return(null);
     }
 }
		/// <summary>
		/// Initializes a new instance of the AddressBookItemConnectionManager class
		/// </summary>
		/// <param name="item">The address book item that defines the remote host to connect to</param>
		/// <param name="connection">The Tcp connection that will be used throughout the lifetime of the connection</param>
		public AddressBookItemConnectionManager(AddressBookItem item, HttpConnection connection)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			if (connection == null)
				throw new ArgumentNullException("connection");

			_propertyBag = new Hashtable();
			_item = item;
			_connection = connection;
			_connection.Opened += new HttpConnectionEventHandler(OnInternalConnectionOpened);
			_connection.Closed += new HttpConnectionEventHandler(OnInternalConnectionClosed);
			_connection.Exception += new ExceptionEventHandler(OnInternalConnectionException);
		}      
Exemplo n.º 15
0
        /// <summary>
        /// Unbinds a context from an item
        /// </summary>
        /// <param name="item"></param>
        public virtual void UnbindContextFromItem(AddressBookItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            lock (_contextLookupTable)
            {
                if (_contextLookupTable.ContainsKey(item))
                {
                    _contextLookupTable.Remove(item);
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Initializes a new instance of the AddressBookItemConnectionManager class
        /// </summary>
        /// <param name="item">The address book item that defines the remote host to connect to</param>
        /// <param name="connection">The Tcp connection that will be used throughout the lifetime of the connection</param>
        public AddressBookItemConnectionManager(AddressBookItem item, HttpConnection connection)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            _propertyBag           = new Hashtable();
            _item                  = item;
            _connection            = connection;
            _connection.Opened    += new HttpConnectionEventHandler(OnInternalConnectionOpened);
            _connection.Closed    += new HttpConnectionEventHandler(OnInternalConnectionClosed);
            _connection.Exception += new ExceptionEventHandler(OnInternalConnectionException);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Allows a context to be pre-bound to an item so that the estabilish context event will already contain the context
        /// </summary>
        /// <param name="item"></param>
        /// <param name="context"></param>
        public virtual void BindContextToItem(AddressBookItem item, IAddressBookItemContext context)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            lock (_contextLookupTable)
            {
                if (_contextLookupTable.ContainsKey(item))
                {
                    _contextLookupTable[item] = context;
                }
                else
                {
                    _contextLookupTable.Add(item, context);
                }
            }
        }
		/// <summary>
		/// Determines if the list contains a AddressBookItem with the same id
		/// </summary>
		/// <param name="item"></param>
		/// <returns></returns>
		public bool Contains(AddressBookItem item)
		{
			if (item == null)
				throw new ArgumentNullException("AddressBookItem", "A null reference to a AddressBookItem cannot be compared to items in the list.");

			return this.Contains(item.Name);
		}
		/// <summary>
		/// Removes a AddressBookItem from the list
		/// </summary>
		/// <param name="item"></param>
		public void Remove(AddressBookItem item)
		{
			if (this.Contains(item))
			{				
				/*
				 * raise the event
				 * 
				 * starting it at the object level, which will trickle all the way up through the object heirarchies
				 * normally, the "this" pointer would be the context of the call, but because we want the object
				 * to get the add and remove events as well, we start the call with it
				 * */
				AddressBookItemEventArgs e = new AddressBookItemEventArgs(item, AddressingActions.Removed);				
				item.OnChanged(this, e);
//				this.OnChanged(this, e);

				item.Changed -= new AddressingEventHandler(this.OnChanged);
				item.BeforeNameChanged -= new NameChangeEventHandler(this.OnBeforeAddressNameAddressChanged);

				// remove the item 
				base.InnerList.Remove(item);			

				// orphan the item
				item.Parent = null;
			}
		}
		public void Add(AddressBookItem item, bool overwrite)
		{
			// if we're not overwriting, just add it like normal
			if (!overwrite)
			{
				this.Add(item);
				return;
			}

			// again we can't add null references
			if (item == null)
				throw new ArgumentNullException("AddressBookItem", "A null reference to an AddressBookItem cannot be added to the list.");

			// if there is a collision, go ahead and overwrite the existing one
			if (this.Contains(item))
			{
				// find the existing one
				AddressBookItem existingItem = this[item.Name];				

				// write the properties from the one coming in, to the existing one
				item.WriteProperties(existingItem);
				return;
			}
			else
			{
				// try and find it by id
				AddressBookItem existingItem = this.FindById(item.Id);

				// if we have a hit, that means this is the same object, just renamed
				if (existingItem != null)
					// so give it a new id
					item.GetNewId();	
			}

			// bind to the end point's events
			item.Changed += new AddressingEventHandler(this.OnChanged);
			item.BeforeNameChanged += new NameChangeEventHandler(this.OnBeforeAddressNameAddressChanged);

			// add the item
			base.InnerList.Add(item);			
			
			// set the item's parent
			item.Parent = _parent;

			/*
			 * raise the event
			 * 
			 * starting it at the object level, which will trickle all the way up through the object heirarchies
			 * normally, the "this" pointer would be the context of the call, but because we want the object
			 * to get the add and remove events as well, we start the call with it
			 * */
			AddressBookItemEventArgs e = new AddressBookItemEventArgs(item, AddressingActions.Added);
			item.OnChanged(this, e);
//			this.OnChanged(this, e);
		}
		/// <summary>
		/// Creates a queued thread pool job that will disconnect a connection for the address book item
		/// </summary>
		/// <param name="item"></param>
		public virtual void Disconnect(AddressBookItem item)
		{
			BackgroundThreadPoolJob job = new BackgroundThreadPoolJob(
				item.Id, 
				true, 
				this, 
				new object[] {item}, 
				new BackgroundThreadStartEventHandler(OnDisconnectSessionForAddressBookItem), 
				null);

			_threadPool.QueueJob(job);
		}
Exemplo n.º 22
0
        /// <summary>
        /// Creates a connection manager for the address book item, and connects the connection to the remote host specified by the address book item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnConnectSessionForAddressBookItem(object sender, BackgroundThreadStartEventArgs e)
        {
            // retrieve the address book item that is requiring attention
            AddressBookItem item            = (AddressBookItem)e.Args[0];
            bool            disconnectFirst = (bool)e.Args[1];
            bool            verboseSession  = (bool)e.Args[2];
            bool            autoRecv        = (bool)e.Args[3];

            try
            {
                /*
                 * if we are supposed to disconnect the current connection first
                 * */
                if (disconnectFirst)
                {
                    lock (_sessionManagers)
                    {
                        // look up the item to see if an existing connection is already in use
                        AddressBookItemConnectionManager prevManager = _sessionManagers[item];

                        // if a connection manager is found for the item then disconnect it
                        if (prevManager != null)
                        {
                            prevManager.Disconnect();
                        }
                    }
                }

                // create a new tcp connection in verbose mode
                HttpConnection connection = new HttpConnection(verboseSession);

                // create a new connection manager to manage the connection
                AddressBookItemConnectionManager manager = new AddressBookItemConnectionManager(item, connection);

                // wire up to the manager's events
                manager.ConnectionEstablishContext += new AddressBookItemConnectionManagerContextEventHandler(OnConnectionEstablishContext);
                manager.ConnectionResolvingAddress += new AddressBookItemConnectionManagerResolvingAddressEventHandler(OnConnectionResolvingAddress);
                manager.BeforeConnectionOpened     += new AddressBookItemConnectionManagerCancelEventHandler(OnBeforeConnectionOpened);
                manager.BeforeConnectionClosed     += new AddressBookItemConnectionManagerCancelEventHandler(OnBeforeConnectionClosed);
                manager.ConnectionOpened           += new AddressBookItemConnectionManagerEventHandler(OnConnectionOpened);
                manager.ConnectionClosed           += new AddressBookItemConnectionManagerEventHandler(OnConnectionClosed);
                manager.ConnectionException        += new AddressBookItemConnectionManagerExceptionEventHandler(OnConnectionException);

                // create the thread context that will enable us to determine the background thread upon which this connection manager is operating
                manager.ThreadContext = new AddressBookItemBackgroundThreadContext((BackgroundThread)sender, item);

                // instruct the manager to connect the connection to the remote host
                // pass it instructions on whether it should begin receiving automatically or not
                // NOTE: In almost every case from a client created connection this will be false.
                //		 Only server created sessions will be set to automatically receive.
                manager.Connect(autoRecv, manager.ThreadContext);

                // add the connection manager to our list of connection managers
                lock (_sessionManagers)
                    _sessionManagers.Add(manager);
            }
            catch (ThreadAbortException)
            {
                // ignore thread abort exceptions
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
		/// <summary>
		/// Initializes a new instance of the AddressBookItemBackgroundThreadContext class
		/// </summary>
		/// <param name="thread"></param>
		/// <param name="item"></param>
		public AddressBookItemBackgroundThreadContext(BackgroundThread thread, AddressBookItem item)
		{
			_thread = thread;
			_item = item;
		}
Exemplo n.º 24
0
		/// <summary>
		/// Writes this instances properties to the item using it's public properties (Only data related fields are assigned)
		/// </summary>
		/// <param name="item">The item to write properties to</param>
		public void WriteProperties(AddressBookItem item)
		{
			item.Name = _name;
			item.Port = _port;
			item.Description = _description;
			item.Address = _address;
		}
 /// <summary>
 /// Initializes a new instance of the AddressBookItemEventArgs class
 /// </summary>
 /// <param name="addressBook">The context of the event</param>
 /// <param name="action">The action taken on the context</param>
 public AddressBookItemEventArgs(AddressBookItem item, AddressingActions action) : base(item, action)
 {
 }
		/// <summary>
		/// Allows a context to be pre-bound to an item so that the estabilish context event will already contain the context
		/// </summary>
		/// <param name="item"></param>
		/// <param name="context"></param>
		public virtual void BindContextToItem(AddressBookItem item, IAddressBookItemContext context)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			if (context == null)
				throw new ArgumentNullException("context");

			lock(_contextLookupTable)
			{
				if (_contextLookupTable.ContainsKey(item))
					_contextLookupTable[item] = context;
				else
					_contextLookupTable.Add(item, context);
			}
		}
		/// <summary>
		/// Unbinds a context from an item
		/// </summary>
		/// <param name="item"></param>
		public virtual void UnbindContextFromItem(AddressBookItem item)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			lock(_contextLookupTable)
			{
				if (_contextLookupTable.ContainsKey(item))
					_contextLookupTable.Remove(item);
			}
		}
		/// <summary>
		/// Initializes a new instance of the AddressBookItemEventArgs class
		/// </summary>
		/// <param name="addressBook">The context of the event</param>
		/// <param name="action">The action taken on the context</param>
		public AddressBookItemEventArgs(AddressBookItem item, AddressingActions action) : base(item, action)
		{

		}
		/// <summary>
		/// Looks up the context currently bound to an item
		/// </summary>
		/// <param name="item"></param>
		/// <returns></returns>
		public virtual IAddressBookItemContext LookupContextForItem(AddressBookItem item)
		{
			lock(_contextLookupTable)
			{
				if (_contextLookupTable.ContainsKey(item))
					return _contextLookupTable[item] as IAddressBookItemContext;
			}
			return null;
		}
		/// <summary>
		/// Creates a queued thread pool job that will connect a connection for the address book item
		/// </summary>
		/// <param name="item"></param>
		/// <param name="disconnectFirst"></param>
		public virtual void Connect(AddressBookItem item, bool disconnectFirst, bool verboseSession, bool autoRecv)
		{
			BackgroundThreadPoolJob job = new BackgroundThreadPoolJob(
				item.Id, 
				true, 
				this, 
				new object[] {item, disconnectFirst, verboseSession, autoRecv}, 
				new BackgroundThreadStartEventHandler(OnConnectSessionForAddressBookItem), 
				null);

			_threadPool.QueueJob(job);
		}
Exemplo n.º 31
0
		/// <summary>
		/// Initializes a new instance of the AddressBookItem class
		/// </summary>
		/// <param name="address">Another AddressBookItem instance to copy properties from (a cloning constructor)</param>
		public AddressBookItem(AddressBookItem item)
		{
			_id = item.Id;
			_address = item.Address;
			_port = item.Port;
			_name = item.Name;
			_description = item.Description;
		}		
			public AddressBookItemTreeNode(AddressBookItem item) : base(item.Name, 2 /* image index */, 2 /* selected image index */)
			{
				_item = item;
			}
Exemplo n.º 33
0
 public AddressBookItemTreeNode(AddressBookItem item) : base(item.Name, 2 /* image index */, 2 /* selected image index */)
 {
     _item = item;
 }
 /// <summary>
 /// Initializes a new instance of the AddressBookItemBackgroundThreadContext class
 /// </summary>
 /// <param name="thread"></param>
 /// <param name="item"></param>
 public AddressBookItemBackgroundThreadContext(BackgroundThread thread, AddressBookItem item)
 {
     _thread = thread;
     _item   = item;
 }