Ejemplo n.º 1
0
		private static uint KeepListener (
		    GLib.Signal.EmissionHook listener, 
		    string objType, string signalName, string hookData)
		{
			GLib.GType type = GLib.GType.FromName (objType);
			if (type != GLib.GType.Invalid) {
				lock (listenerListMutex)
				{
					ListenerInfo info = new ListenerInfo();
					info.Id = (uint) ListenerList.Count + 1;
					info.SignalName = signalName;
					info.Type = type;
					info.HookId = GLib.Signal.AddEmissionHook (signalName, type, listener);
					ListenerList.Add (info.Id, info);
					return info.Id;
				}
			}
			else
			{
				throw new NotSupportedException ("Invalid object type " + objType);
			}
		}
        /// <summary>
        /// Returns a MessageStreamListener instance based on this instance's configuratino (timeout, bucket name etc.)
        /// 
        /// When multiple listeners are requested with the exact same parameters (usually when multiple clients are instantiated from the same configuration),
        /// the same listener will be returned each time.
        /// </summary>
        /// <returns></returns>
        private MessageStreamListener GetPooledListener()
        {
            // create a unique key based on the parameters
            // to find out if we already have a listener attached to this pool
            var hcc = new HashCodeCombiner();

            hcc.Add(this.Timeout);
            hcc.Add(this.DeadTimeout);
            hcc.Add(this.RetryCount);
            hcc.Add(this.RetryTimeout.GetHashCode());
            hcc.Add(this.bucketName.GetHashCode());

            if (credential != null)
            {
                hcc.Add((this.credential.UserName ?? String.Empty).GetHashCode());
                hcc.Add((this.credential.Password ?? String.Empty).GetHashCode());
                hcc.Add((this.credential.Domain ?? String.Empty).GetHashCode());
            }

            for (var i = 0; i < this.poolUrls.Length; i++)
                hcc.Add(this.poolUrls[i].GetHashCode());

            var hash = hcc.CurrentHash;

            MessageStreamListener retval;

            lock (ListenerSync)
                if (listeners.TryGetValue(hash, out retval))
                {
                    listenerRefs[retval].RefCount++;
                    retval.Subscribe(this.HandleMessage);
                }
                else
                {
                    var name = this.bucketName;

                    // create a new listener for the pool urls
                    retval = new MessageStreamListener(poolUrls, (client, root) => ResolveBucketUri(client, root, name));

                    retval.Timeout = this.Timeout;
                    retval.DeadTimeout = this.DeadTimeout;
                    retval.Credentials = this.credential;
                    retval.RetryCount = this.RetryCount;
                    retval.RetryTimeout = this.RetryTimeout;

                    retval.Subscribe(this.HandleMessage);

                    listeners[hash] = retval;
                    listenerRefs[retval] = new ListenerInfo { RefCount = 1, HashKey = hash };

                    retval.Start();
                }

            return retval;
        }
Ejemplo n.º 3
0
		private static uint KeepListener (
		    GLib.Signal.EmissionHook listener, 
		    string objType, string signalName)
		{
			GLib.GType type = GLib.GType.FromName (objType);
			if (type != GLib.GType.Invalid) {
				
				//FIXME: drop this workaround for bug#386950
				if (signalName.Contains ("property")) return 0;
				
				lock (listenerListSync)
				{
					ListenerInfo info = new ListenerInfo();
					info.Id = (uint) ListenerList.Count + 1;
					info.SignalName = signalName;
					info.Type = type;
					info.HookId = GLib.Signal.AddEmissionHook (signalName, type, listener);
					ListenerList.Add (info.Id, info);
					return info.Id;
				}
			}
			else
			{
				throw new NotSupportedException ("Invalid object type " + objType);
			}
		}
Ejemplo n.º 4
0
        private void Bind(ListenerInfo info, bool rebind)
        {
            if (Unloaded)
            {
                return;
            }

            if (rebind)
            {
                Console.WriteLine("Appear to have lost a binding.  Rebinding.");
            }
            info.Listener.Start(info.Binding.Backlog);
            info.Listener.BeginAcceptTcpClient(OnBeginAcceptTcpClient, info);
        }
Ejemplo n.º 5
0
 private void AcceptServer(TcpClient client, ListenerInfo info)
 {
     // TODO
 }
Ejemplo n.º 6
0
        private void AcceptClient(TcpClient client, ListenerInfo info)
        {
            IPEndPoint ep = (IPEndPoint)client.Client.RemoteEndPoint;
            if (IsKLined(ep.Address))
            {
                Console.WriteLine("Client is K:lined!  Dropping.");
                client.Close();
                return;
            }

            string ip = ep.Address.ToString();
            HostMask mask = HostMask.Parse("*!:" + ep.Port + "@" + ip);
            mask.Account = "/" + ip;
            LocalUser user = new LocalUser(this, client, mask, info.Binding.Protocol == Protocols.Rfc2812 ? false : true);
            user.Start();

            UsersByMask.Add(user.Mask, user);
        }
Ejemplo n.º 7
0
        public override void Restart()
        {
            Stop();

            Remotes.Clear();
            ConnectRemotes(Odd);

            try
            {
                foreach (Binding b in Settings.Bindings)
                {
                    for (int i = 0; i < b.Interfaces.Length; i++)
                    {
                        try
                        {
                            IPAddress ip;
                            if (!IPAddress.TryParse(b.Interfaces[i], out ip))
                            {
                                Console.WriteLine("Skipping invalid interface: {0}", b.Interfaces[i]);
                                continue;
                            }

                            ListenerInfo listener = new ListenerInfo(new TcpListener(ip, b.Port), b);
                            Bind(listener, false);
                            Listeners.Add(listener);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error binding to interface: {0}", ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to read bindings from settings.  Cannot receive incoming connections!  ({0})", ex.Message);
            }
        }
Ejemplo n.º 8
0
        private void UnregisterForEvent(ListenerInfo listenerInfo)
        {
            // Send the event register request to the called device
            Call call = new Call(listenerInfo.driver, UNREGISTER_LISTENER_SERVICE, listenerInfo.instanceId);
            call.AddParameter(REGISTER_EVENT_LISTENER_EVENT_KEY_PARAMETER, listenerInfo.eventKey);

            Response response = CallService(listenerInfo.device, call);
            if (response == null)
                throw new ServiceCallException("No response receive from unregister service call.");
            if (!string.IsNullOrEmpty(response.error))
                throw new ServiceCallException(response.error);
        }
Ejemplo n.º 9
0
        private void SendRegister(UpDevice device, IDictionary<string, object> parameters, ListenerInfo info)
        {
            // Send the event register request to the called device
            Call serviceCall = new Call(info.driver, REGISTER_LISTENER_SERVICE, info.instanceId);
            serviceCall.AddParameter(REGISTER_EVENT_LISTENER_EVENT_KEY_PARAMETER, info.eventKey);
            if (parameters != null)
            {
                foreach (var pair in parameters)
                {
                    if (pair.Key.Equals(REGISTER_EVENT_LISTENER_EVENT_KEY_PARAMETER, System.StringComparison.InvariantCultureIgnoreCase))
                        throw new System.ArgumentException("Can't use reserved keys as parameters for registerForEvent");
                    serviceCall.AddParameter(pair.Key, pair.Value);
                }
            }

            Response response = CallService(device, serviceCall);
            if (response == null)
                throw new System.Exception("No response received during register process.");
            else if (!string.IsNullOrEmpty(response.error))
                throw new System.Exception(response.error);
        }
Ejemplo n.º 10
0
        private void RegisterNewListener(
            UpDevice device,
            IDictionary<string, object> parameters,
            string eventIdentifier,
            ListenerInfo info)
        {
            if (device != null)
                SendRegister(device, parameters, info);

            // If the registry process goes ok, then add the listenner to the listener map
            List<ListenerInfo> listeners = null;
            if (!listenerMap.TryGetValue(eventIdentifier, out listeners))
                listenerMap[eventIdentifier] = listeners = new List<ListenerInfo>();
            listeners.Add(info);
            logger.Log("Registered listener for event :" + eventIdentifier);
        }
Ejemplo n.º 11
0
 private Call BuildRegisterCall(IDictionary<string, object> parameters, ListenerInfo info)
 {
     Call serviceCall = new Call(info.driver, REGISTER_LISTENER_SERVICE, info.instanceId);
     serviceCall.AddParameter(REGISTER_EVENT_LISTENER_EVENT_KEY_PARAMETER, info.eventKey);
     if (parameters != null)
     {
         foreach (var pair in parameters)
         {
             if (pair.Key.Equals(REGISTER_EVENT_LISTENER_EVENT_KEY_PARAMETER, System.StringComparison.InvariantCultureIgnoreCase))
                 throw new System.ArgumentException("Can't use reserved keys as parameters for registerForEvent");
             serviceCall.AddParameter(pair.Key, pair.Value);
         }
     }
     return serviceCall;
 }
Ejemplo n.º 12
0
        public void Register(
            UOSEventListener listener,
            UpDevice device,
            string driver,
            string instanceId = null,
            string eventKey = null,
            IDictionary<string, object> parameters = null)
        {
            // If the listener is already registered it cannot be registered again
            string eventIdentifier = GetEventIdentifier(device, driver, instanceId, eventKey);
            logger.Log("Registering listener for event :" + eventIdentifier);

            List<ListenerInfo> list;
            if (!listenerMap.TryGetValue(eventIdentifier, out list))
                list = null;

            if (FindListener(listener, list) == null)
            {
                ListenerInfo info = new ListenerInfo();

                info.driver = driver;
                info.instanceId = instanceId;
                info.eventKey = eventKey;
                info.listener = listener;
                info.device = device;

                RegisterNewListener(device, parameters, eventIdentifier, info);
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Starts the listening process
        /// </summary>
        /// <remarks>
        /// This method is also called internally after a client connection has been
        /// established, to refresh the count of listeners
        /// </remarks>
        public void Listen()
        {
            lock (_listeners)
            {
                int listenersToStart = Math.Max(0, _maxClients - _listeners.Count);
                if (_alwaysListeningClients != null)
                    listenersToStart = Math.Min(_alwaysListeningClients.Value, listenersToStart);

                _logger.InfoFormat("Starting {0}-Listeners on pipe '{1}'", listenersToStart, _pipeName);

                for (int listenerCounter = 0; listenerCounter < listenersToStart; listenerCounter++)
                {
                    Thread listenerThread = new Thread(new ParameterizedThreadStart(ListenerThreadProc));
                    ListenerInfo currentListenerInfo = new ListenerInfo(DateTime.Now, listenerThread);
                    _listeners.Add(currentListenerInfo);
                    listenerThread.Start(currentListenerInfo);
                }
            }
        }