<commit_before>﻿/***************************************************************************************

	Copyright 2012 <NAME>

	   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.
 
	File Name:		RestRequestProcessor.cs
	Namespace:		Manatee.Trello.Internal
	Class Name:		RestRequestProcessor
	Purpose:		Processes REST requests as they appear on the queue.

***************************************************************************************/

using System;
using System.Threading;
using Manatee.Trello.Rest;

namespace Manatee.Trello.Internal.RequestProcessing
{
	internal class RestRequestProcessor : IRestRequestProcessor
	{
		private const string BaseUrl = "https://api.trello.com/1";

		private readonly IRequestQueue _queue;
		private readonly IRestClientProvider _clientProvider;
		private readonly string _appKey;
		private readonly object _lock;
		private readonly Thread _workerThread;
		private bool _shutdown;
		private bool _isActive;

		public bool IsActive
		{
			get { return _isActive; }
			set
			{
				_isActive = value;
				Pulse();
			}
		}
		public string AppKey { get { return _appKey; } }
		public string UserToken { get; set; }

		public RestRequestProcessor(IRequestQueue queue, IRestClientProvider clientProvider, TrelloAuthorization auth)
		{
			_queue = queue;
			_clientProvider = clientProvider;
			_appKey = auth.AppKey;
			UserToken = auth.UserToken;
			_lock = new object();
			_shutdown = false;
			_isActive = true;
			_workerThread = new Thread(Process);
			_workerThread.Start();
		}

		public void AddRequest<T>(IRestRequest request)
			where T : class
		{
			PrepRequest(request);
			_queue.Enqueue<T>(request);
			Pulse();
		}
		public void ShutDown()
		{
			_shutdown = true;
			Pulse();
			_workerThread.Join();
		}
		public void NetworkStatusChanged(object sender, EventArgs e)
		{
			Pulse();
		}

		private void Process()
		{
			lock (_lock)<commit_msg>BUG FIX:  Request processor worker thread prevents client app from closing<commit_after>﻿/***************************************************************************************

	Copyright 2012 <NAME>

	   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.
 
	File Name:		RestRequestProcessor.cs
	Namespace:		Manatee.Trello.Internal
	Class Name:		RestRequestProcessor
	Purpose:		Processes REST requests as they appear on the queue.

***************************************************************************************/

using System;
using System.Threading;
using Manatee.Trello.Rest;

namespace Manatee.Trello.Internal.RequestProcessing
{
	internal class RestRequestProcessor : IRestRequestProcessor
	{
		private const string BaseUrl = "https://api.trello.com/1";

		private readonly IRequestQueue _queue;
		private readonly IRestClientProvider _clientProvider;
		private readonly string _appKey;
		private readonly object _lock;
		private readonly Thread _workerThread;
		private bool _shutdown;
		private bool _isActive;

		public bool IsActive
		{
			get { return _isActive; }
			set
			{
				_isActive = value;
				Pulse();
			}
		}
		public string AppKey { get { return _appKey; } }
		public string UserToken { get; set; }

		public RestRequestProcessor(IRequestQueue queue, IRestClientProvider clientProvider, TrelloAuthorization auth)
		{
			_queue = queue;
			_clientProvider = clientProvider;
			_appKey = auth.AppKey;
			UserToken = auth.UserToken;
			_lock = new object();
			_shutdown = false;
			_isActive = true;
			_workerThread = new Thread(Process) {IsBackground = true};
			_workerThread.Start();
		}

		public void AddRequest<T>(IRestRequest request)
			where T : class
		{
			PrepRequest(request);
			_queue.Enqueue<T>(request);
			Pulse();
		}
		public void ShutDown()
		{
			_shutdown = true;
			Pulse();
			_workerThread.Join();
		}
		public void NetworkStatusChanged(object sender, EventArgs e)
		{
			Pulse();
		}

		private void Process()
		{
			lock (_lock)
{'fraction_non_alphanumeric': 0.14014300306435137, 'fraction_numerical': 0.0036772216547497445, 'mean_word_length': 4.052631578947368, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '3315405', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1329, 'n_words': 449}