<commit_before>/// Copyright 2007-2008 The Apache Software Foundation.
/// 
/// Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
/// this file except in compliance with the License. You may obtain a copy of the 
/// License at 
/// 
///   http://www.apache.org/licenses/LICENSE-2.0 
/// 
/// Unless required by applicable law or agreed to in writing, software distributed 
/// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
/// CONDITIONS OF ANY KIND, either express or implied. See the License for the 
/// specific language governing permissions and limitations under the License.
namespace MassTransit.ServiceBus.Subscriptions
{
    using System.Collections.Generic;
    using Exceptions;
	using Messages;

	public class SubscriptionClient :
		IHostedService,
		Consumes<CacheUpdateResponse>.All,
		Consumes<AddSubscription>.All,
		Consumes<RemoveSubscription>.All
	{
		private readonly ISubscriptionCache _cache;
		private readonly IServiceBus _serviceBus;
		private readonly IEndpoint _subscriptionServiceEndpoint;

		public SubscriptionClient(IServiceBus serviceBus, ISubscriptionCache cache, IEndpoint subscriptionServiceEndpoint)
		{
			_serviceBus = serviceBus;
			_cache = cache;
			_subscriptionServiceEndpoint = subscriptionServiceEndpoint;
		}

		public void Consume(CacheUpdateResponse message)
		{
			foreach (Subscription sub in message.Subscriptions)
			{
				if (!IsOwnedSubscription(sub))
					_cache.Add(sub);
			}
		}

		public void Consume(AddSubscription message)
		{
			if (!IsOwnedSubscription(message.Subscription))
				_cache.Add(message.Subscription);
		}

		public void Consume(RemoveSubscription message)
		{
			if (!IsOwnedSubscription(message.Subscription))
				_cache.Remove(message.Subscription);
		}

		public void Dispose()
		{
		    Stop();

			//the bus owns the client so it shouldn't be disposed
			//the bus owns the cache so it shouldn't be disposed
			_subscriptionServiceEndpoint.Dispose();
		}

		public void Start()
		{
			ValidateThatBusAndClientAreNotOnSameEndpoint(_serviceBus, _subscriptionServiceEndpoint);

			_cache.OnAddSubscription += Cache_OnAddSubscription;
			_cache.OnRemoveSubscription += Cache_OnRemoveSubscription;

			_serviceBus.Subscribe(this);

			_subscriptionServiceEndpoint.Send(new CacheUpdateRequest(_serviceBus.Endpoint.Uri));
		}

		public void Stop()
		{
			_subscriptionServiceEndpoint.Send(new CancelSubscriptionUpdates(_serviceBus.Endpoint.Uri));

            // it makes sense to unsubscribe to everything we are subscribed to, right?

            _serviceBus.Unsubscribe(this);

            _cache.OnAddSubscription -= Cache_OnAddSubscription;
            _cache.OnRemoveSubscription -= Cache_OnRemoveSubscription;

            IList<Subscription> subscriptions = _cache.List();
		    foreach (Subscription subscription in subscriptions)
		    {
                if(IsOwnedSubscription(subscription))
		            _subscriptionServiceEndpoint.Send(new RemoveSubscription(subscription));
		    }
		}

		private bool IsOwnedSubscription(Subscription subscription)
		{
			if (subscription.EndpointUri == _serviceBus.Endpoint.Uri)
				return true;

			return false;
		}


		private static void ValidateThatBusAndClientAreNotOnSameEndpoint(IServiceBus bus, IEndpoint endpoint)
		{
			if (bus.Endpoint.Uri.Equals(endpoint.Uri))
			{
				string message = string.Format("Both the service bus and subscription client are listening on the same endpoint {0}", endpoint.Uri);
				throw new EndpointException(endpoint, message);
			}
		}

		public void Cache_OnAddSubscription(object sender, SubscriptionEventArgs e)
		{
			if (IsOwnedSubscription(e.Subscription))
			{
				AddSubscription message = new AddSubscription(e.Subscription);

				_subscriptionServiceEndpoint.Send(message);
			}
		}


		public void Cache_OnRemoveSubscription(object sender, SubscriptionEventArgs e)
		{
			if (IsOwnedSubscription(e.Subscription))
			{
				RemoveSubscription message = new RemoveSubscription(e.Subscription);

				_subscriptionServiceEndpoint.Send(message);
			}
		}
	}
}<commit_msg>Okay so I screwed up on the sub mgr NRE<commit_after>/// Copyright 2007-2008 The Apache Software Foundation.
/// 
/// Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
/// this file except in compliance with the License. You may obtain a copy of the 
/// License at 
/// 
///   http://www.apache.org/licenses/LICENSE-2.0 
/// 
/// Unless required by applicable law or agreed to in writing, software distributed 
/// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
/// CONDITIONS OF ANY KIND, either express or implied. See the License for the 
/// specific language governing permissions and limitations under the License.
namespace MassTransit.ServiceBus.Subscriptions
{
	using Exceptions;
	using Messages;

	public class SubscriptionClient :
		IHostedService,
		Consumes<CacheUpdateResponse>.All,
		Consumes<AddSubscription>.All,
		Consumes<RemoveSubscription>.All
	{
		private readonly ISubscriptionCache _cache;
		private readonly IServiceBus _serviceBus;
		private readonly IEndpoint _subscriptionServiceEndpoint;

		public SubscriptionClient(IServiceBus serviceBus, ISubscriptionCache cache, IEndpoint subscriptionServiceEndpoint)
		{
			_serviceBus = serviceBus;
			_cache = cache;
			_subscriptionServiceEndpoint = subscriptionServiceEndpoint;
		}

		public void Consume(CacheUpdateResponse message)
		{
			foreach (Subscription sub in message.Subscriptions)
			{
				if (!IsOwnedSubscription(sub))
					_cache.Add(sub);
			}
		}

		public void Consume(AddSubscription message)
		{
			if (!IsOwnedSubscription(message.Subscription))
				_cache.Add(message.Subscription);
		}

		public void Consume(RemoveSubscription message)
		{
			if (!IsOwnedSubscription(message.Subscription))
				_cache.Remove(message.Subscription);
		}

		public void Dispose()
		{
			//the bus owns the client so it shouldn't be disposed
			//the bus owns the cache so it shouldn't be disposed
			_subscriptionServiceEndpoint.Dispose();
		}

		public void Start()
		{
			ValidateThatBusAndClientAreNotOnSameEndpoint(_serviceBus, _subscriptionServiceEndpoint);

			_cache.OnAddSubscription += Cache_OnAddSubscription;
			_cache.OnRemoveSubscription += Cache_OnRemoveSubscription;

			_serviceBus.Subscribe(this);

			_subscriptionServiceEndpoint.Send(new CacheUpdateRequest(_serviceBus.Endpoint.Uri));
		}

		public void Stop()
		{
			_subscriptionServiceEndpoint.Send(new CancelSubscriptionUpdates(_serviceBus.Endpoint.Uri));

			_serviceBus.Unsubscribe(this);

			_cache.OnAddSubscription -= Cache_OnAddSubscription;
			_cache.OnRemoveSubscription -= Cache_OnRemoveSubscription;
		}

		private bool IsOwnedSubscription(Subscription subscription)
		{
			if (subscription.EndpointUri == _serviceBus.Endpoint.Uri)
				return true;

			return false;
		}


		private static void ValidateThatBusAndClientAreNotOnSameEndpoint(IServiceBus bus, IEndpoint endpoint)
		{
			if (bus.Endpoint.Uri.Equals(endpoint.Uri))
			{
				string message = string.Format("Both the service bus and subscription client are listening on the same endpoint {0}", endpoint.Uri);
				throw new EndpointException(endpoint, message);
			}
		}

		public void Cache_OnAddSubscription(object sender, SubscriptionEventArgs e)
		{
			if (IsOwnedSubscription(e.Subscription))
			{
				AddSubscription message = new AddSubscription(e.Subscription);

				_subscriptionServiceEndpoint.Send(message);
			}
		}


		public void Cache_OnRemoveSubscription(object sender, SubscriptionEventArgs e)
		{
			if (IsOwnedSubscription(e.Subscription))
			{
				RemoveSubscription message = new RemoveSubscription(e.Subscription);

				_subscriptionServiceEndpoint.Send(message);
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.07844356041770263, 'fraction_numerical': 0.0032322227747389356, 'mean_word_length': 3.563244469653999, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1789029', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2147, 'n_words': 559}