<commit_before>using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using CookComputing.XmlRpc;
using Palaso.Services.Dictionary;
using Palaso.Services.ForServers;

namespace Palaso.Services
{
	/// <summary>
	/// Wraps the inter-process communication mechanism so that this code isn't
	/// spread throughout the implementations.  The details here can be changed, with
	/// less impact on the servers and clients if we ever leave the (.net 2)
	/// remoting and move back to the (.net 3) WPF approach.
	/// </summary>
	public class IpcSystem
	{
		/// <summary>
		/// Because .net remoting (.net 2) requires each application to have its own port,
		/// when providing services, we need to first find an open port, and when we go
		/// looking for providers, we may need to query on serveral ports.
		/// </summary>
		public static int StartingPort=5678;

		public static readonly int NumberOfPortsToTry = 3;

		public static string URLPrefix
		{
			get
			{
				//return "net.pipe://localhost/";
				return "http://localhost";
			}
		}


		public static TServiceInterface GetExistingService<TServiceInterface>(string unescapedServiceName)
			where TServiceInterface : class, IXmlRpcProxy, IPingable
		{
			System.Diagnostics.Debug.Assert(!unescapedServiceName.Contains("%"),"please leave it to this method to figure out the correct escaping to do.");
			System.Diagnostics.Debug.WriteLine("Trying to get service: " + unescapedServiceName);
			System.Diagnostics.Debug.Assert(!IsWellFormedUriStringMonoSafe(unescapedServiceName), "This method needs a service name, not a whole uri.");


			TServiceInterface serviceProxy = XmlRpcProxyGen.Create<TServiceInterface>();
			for (int port = StartingPort; port < StartingPort + NumberOfPortsToTry; port++)
			{
				//nb: this timeout does work, but reducing it to the point where it
				//actually returns earlier just makes it unreliable (consisten test failures to find
				//existing services on my fast machine). 100,000 is default timeout, but
				//you get a "nope, not there" in a second or two

				//serviceProxy.Timeout = 1000;//2000 is safe on my fast machine
				serviceProxy.Url = GetUrlForService(FixupServiceName(unescapedServiceName), port);

				try
				{
					Debug.WriteLine("Looking for service at " + serviceProxy.Url);
					//hack: just need some way to see if it's alive, there should be a lower-level way to do that
					((IPingable)serviceProxy).Ping();
					return serviceProxy;//found one
				}
				catch (Exception e) //swallow
				{
					Debug.WriteLine("This is not necessarily a problem: "+e.Message);
				}
			}
			return null;
		}



		private static string GetUrlForService(string name, int port)
		{
			return URLPrefix + ":" + port + "/" + FixupServiceName(name);
		}

		private static string FixupServiceName(string name)
		{
			return name.Replace(@"\", "/");
		}

		public static string GetRemotingNameForService(string name)
		{
			return name;
		}

		private static int GetAChannelWeCanUse()
		{
			//nb: only one channel per port+protocol<commit_msg>Tweaked logging of IPC stuff.<commit_after>using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using CookComputing.XmlRpc;
using Palaso.Services.Dictionary;
using Palaso.Services.ForServers;

namespace Palaso.Services
{
	/// <summary>
	/// Wraps the inter-process communication mechanism so that this code isn't
	/// spread throughout the implementations.  The details here can be changed, with
	/// less impact on the servers and clients if we ever leave the (.net 2)
	/// remoting and move back to the (.net 3) WPF approach.
	/// </summary>
	public class IpcSystem
	{
		/// <summary>
		/// Because .net remoting (.net 2) requires each application to have its own port,
		/// when providing services, we need to first find an open port, and when we go
		/// looking for providers, we may need to query on serveral ports.
		/// </summary>
		public static int StartingPort=5678;

		public static readonly int NumberOfPortsToTry = 3;

		public static string URLPrefix
		{
			get
			{
				//return "net.pipe://localhost/";
				return "http://localhost";
			}
		}


		public static TServiceInterface GetExistingService<TServiceInterface>(string unescapedServiceName)
			where TServiceInterface : class, IXmlRpcProxy, IPingable
		{
			System.Diagnostics.Debug.Assert(!unescapedServiceName.Contains("%"),"please leave it to this method to figure out the correct escaping to do.");
			System.Diagnostics.Debug.Assert(!IsWellFormedUriStringMonoSafe(unescapedServiceName), "This method needs a service name, not a whole uri.");
		  //  System.Diagnostics.Debug.WriteLine("("+unescapedServiceName+") trying to get service: " + unescapedServiceName);


			TServiceInterface serviceProxy = XmlRpcProxyGen.Create<TServiceInterface>();
			for (int port = StartingPort; port < StartingPort + NumberOfPortsToTry; port++)
			{
				//nb: this timeout does work, but reducing it to the point where it
				//actually returns earlier just makes it unreliable (consisten test failures to find
				//existing services on my fast machine). 100,000 is default timeout, but
				//you get a "nope, not there" in a second or two

				//serviceProxy.Timeout = 1000;//2000 is safe on my fast machine
				serviceProxy.Url = GetUrlForService(FixupServiceName(unescapedServiceName), port);

				try
				{
					Debug.WriteLine("Checking for service at " + serviceProxy.Url);
					//hack: just need some way to see if it's alive, there should be a lower-level way to do that
					((IPingable)serviceProxy).Ping();
					Debug.WriteLine("   Found & pinged.");
					return serviceProxy;//found one
				}
				catch (Exception e) //swallow
				{
					Debug.WriteLine("   " + e.Message + " (not necessarily a problem)");
				}
			}
			return null;
		}



		private static string GetUrlForService(string name, int port)
		{
			return URLPrefix + ":" + port + "/" + FixupServiceName(name);
		}

		private static string FixupServiceName(string name)
		{
			return name.Replace(@"\", "/");
		}

		public static string GetRemotingNameForService(string name)
		{
			return name;
		}

		private static int GetAChannelWeCanUse()
		{
			//nb: only one channel per port+protocol
{'fraction_non_alphanumeric': 0.09954128440366973, 'fraction_numerical': 0.00672782874617737, 'mean_word_length': 3.6324362606232294, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6483565', 'n_tokens_mistral': 2173, 'n_tokens_neox': 1851, 'n_words': 650}