private bool PutInstance(MemcachedInstance instance, string name)
        {
            lock (_InstanceListLock)
            {
                if (IsInstanceExists(name))
                {
                    return(false);
                }

                _MemcachedInstances.Add(name.ToLower(), instance);
            }

            return(true);
        }
Exemple #2
0
        public static Memcached AttachExist(string name, ushort port, int pid)
        {
            Memcached   memcached = new Memcached();
            MemcachedIo mio;

            Log.Info("Attaching exist memcached instance.");

            if (!MemcachedInstance.WaitUntilInstanceDestroyed(name, new TimeSpan(0, 0, 5)))
            {
                Log.Warning("{0} could not be deregistered", name);
                return(null);
            }

            try
            {
                memcached.inst = MemcachedInstance.CreateInstanceFromExist(pid, port, name);
            }
            catch (Exception e)
            {
                //probably still exist
                Log.Warning(e.Message);
                return(null);
            }

            Log.Info("instance \"{0}\" created at {1} port", name, port);


            Log.Info("attaching to the memcached IO interface.");

            mio = new MemcachedIo();

            if (!mio.AttachMemcachedInstance(memcached.inst))
            {
                Log.Error("Memcached instance could not be attached to Memcached Io object");
                memcached.inst.Kill();
                memcached.inst = null;
                memcached      = null;

                return(null);
            }

            memcached.Instance = mio;

            Log.Info("Connection success...");

            return(memcached);
        }
        public bool AttachMemcachedInstance(MemcachedInstance instance)
        {
            if (!instance.IsRunning)
            {
                return(false);
            }

            MemcachedClientConfiguration conf = new MemcachedClientConfiguration();

            this.serverIp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), instance.Port);

            conf.Protocol = MemcachedProtocol.Binary;
            conf.Servers.Add(this.serverIp);

            this.mc = new MemcachedClient(conf);

            this.instance = instance;

            return(true);
        }
        public static MemcachedInstance CreateInstanceFromExist(int pid, ushort port, string name)
        {
            MemcachedInstance inst = new MemcachedInstance(0, name, port);

            if (!inst.PutInstance(inst, inst.name))
            {
                throw new Exception(string.Format("{0} is already registered!", inst.name));
            }

            inst.process = MemcachedProcess.Attach(pid);

            if (inst.process == null)
            {
                inst.RemoveInstance(name);
                inst = null;
                return(null);
            }

            inst.process.Tag     = name;
            inst.process.Exited += inst.Process_Exited;

            return(inst);
        }