<commit_before>// Copyright 2006 <NAME> <<EMAIL>>
// This software is made available under the MIT License
// See COPYING for details

using System;
using System.Collections.Generic;
using org.freedesktop.DBus;

namespace DBus
{
	public sealed class Bus : Connection
	{
		static readonly string DBusName = "org.freedesktop.DBus";
		static readonly ObjectPath DBusPath = new ObjectPath ("/org/freedesktop/DBus");

		static Dictionary<string,Bus> buses = new Dictionary<string,Bus> ();

		static Bus starterBus = null;
		static Bus systemBus = Address.StarterBusType == "system" ? Starter : Bus.Open (Address.System);
		static Bus sessionBus = Address.StarterBusType == "session" ? Starter : Bus.Open (Address.Session);

		IBus bus;
		string address;
		string uniqueName;

		public static Bus System
		{
			get {
				return systemBus;
			}
		}

		public static Bus Session
		{
			get {
				return sessionBus;
			}
		}

		public static Bus Starter
		{
			get {
				if (starterBus == null) {
					try {
						starterBus = Bus.Open (Address.Starter);
					} catch (Exception e) {
						throw new Exception ("Unable to open the starter message bus.", e);
					}
				}

				return starterBus;
			}
		}

		public static new Bus Open (string address)
		{
			if (address == null)
				throw new ArgumentNullException ("address");

			Bus bus;
			if (buses.TryGetValue (address, out bus))
				return bus;

			bus = new Bus (address);
			buses[address] = bus;

			return bus;
		}

		public Bus (string address) : base (address)
		{
			this.bus = GetObject<IBus> (DBusName, DBusPath);
			this.address = address;
			Register ();
		}

		//should this be public?
		//as long as Bus subclasses Connection, having a Register with a completely different meaning is bad
		void Register ()
		{
			if (uniqueName != null)
				throw new Exception ("Bus already has a unique name");

			uniqueName = bus.Hello ();
		}

		protected override void CloseInternal ()
		{
			/* In case the bus was opened with static method
			 * Open, clear it from buses dictionary
			 */
			if (buses.ContainsKey (address))
				buses.Remove (address);
		}

		protected override bool CheckBusNameExists (string busName)
		{
			return NameHasOwner (busName);
		}

		public ulong GetUnixUser (string name)
		{
			return bus.GetConnectionUnixUser (name);
		}

		public RequestNameReply RequestName (string name)
		{
			return RequestName (name, NameFlag.None);
		}

		public RequestNameReply RequestName (string name, NameFlag flags)
		{
			return bus.RequestName (name, flags);
		}

		public ReleaseNameReply ReleaseName (string name)
		{
			return bus.ReleaseName (name);
		}<commit_msg>Don't check name of DBus bus<commit_after>// Copyright 2006 <NAME> <<EMAIL>>
// This software is made available under the MIT License
// See COPYING for details

using System;
using System.Collections.Generic;
using org.freedesktop.DBus;

namespace DBus
{
	public sealed class Bus : Connection
	{
		static readonly string DBusName = "org.freedesktop.DBus";
		static readonly ObjectPath DBusPath = new ObjectPath ("/org/freedesktop/DBus");

		static Dictionary<string,Bus> buses = new Dictionary<string,Bus> ();

		static Bus starterBus = null;
		static Bus systemBus = Address.StarterBusType == "system" ? Starter : Bus.Open (Address.System);
		static Bus sessionBus = Address.StarterBusType == "session" ? Starter : Bus.Open (Address.Session);

		IBus bus;
		string address;
		string uniqueName;

		public static Bus System
		{
			get {
				return systemBus;
			}
		}

		public static Bus Session
		{
			get {
				return sessionBus;
			}
		}

		public static Bus Starter
		{
			get {
				if (starterBus == null) {
					try {
						starterBus = Bus.Open (Address.Starter);
					} catch (Exception e) {
						throw new Exception ("Unable to open the starter message bus.", e);
					}
				}

				return starterBus;
			}
		}

		public static new Bus Open (string address)
		{
			if (address == null)
				throw new ArgumentNullException ("address");

			Bus bus;
			if (buses.TryGetValue (address, out bus))
				return bus;

			bus = new Bus (address);
			buses[address] = bus;

			return bus;
		}

		public Bus (string address) : base (address)
		{
			this.bus = GetObject<IBus> (DBusName, DBusPath);
			this.address = address;
			Register ();
		}

		//should this be public?
		//as long as Bus subclasses Connection, having a Register with a completely different meaning is bad
		void Register ()
		{
			if (uniqueName != null)
				throw new Exception ("Bus already has a unique name");

			uniqueName = bus.Hello ();
		}

		protected override void CloseInternal ()
		{
			/* In case the bus was opened with static method
			 * Open, clear it from buses dictionary
			 */
			if (buses.ContainsKey (address))
				buses.Remove (address);
		}

		protected override bool CheckBusNameExists (string busName)
		{
			if (busName == DBusName)
				return true;
			return NameHasOwner (busName);
		}

		public ulong GetUnixUser (string name)
		{
			return bus.GetConnectionUnixUser (name);
		}

		public RequestNameReply RequestName (string name)
		{
			return RequestName (name, NameFlag.None);
		}

		public RequestNameReply RequestName (string name, NameFlag flags)
		{
			return bus.RequestName (name, flags);
		}

		public ReleaseNameReply ReleaseName (string name)
		{
			return bus.ReleaseName (name);
		}
{'fraction_non_alphanumeric': 0.09737827715355805, 'fraction_numerical': 0.00149812734082397, 'mean_word_length': 3.13710302091402, 'pattern_counts': {'":': 0, '<': 15, '<?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': '7073375', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1582, 'n_words': 540}