Obtain() public abstract method

Attempts to obtain exclusive access and immediately return upon success or failure. Use #close to release the lock.
public abstract Obtain ( ) : bool
return bool
 public override bool Obtain(long lockWaitTimeout)
 {
     lock (this)
     {
         bool obtained = lock_Renamed.Obtain(lockWaitTimeout);
         if (obtained)
         {
             Verify((sbyte)1);
         }
         return(obtained);
     }
 }
 public override bool Obtain()
 {
     lock (this)
     {
         bool obtained = @lock.Obtain();
         if (obtained)
         {
             Verify((byte)1);
         }
         return(obtained);
     }
 }
Example #3
0
        // Simple test to verify locking system is "working".  On
        // NFS, if it's misconfigured, you can hit long (35
        // second) timeouts which cause Lock.obtain to take far
        // too long (it assumes the obtain() call takes zero
        // time).
        private void  AcquireTestLock()
        {
            lock (this)
            {
                if (tested)
                {
                    return;
                }
                tested = true;

                // Ensure that lockDir exists and is a directory.
                bool tmpBool;
                if (System.IO.File.Exists(lockDir.FullName))
                {
                    tmpBool = true;
                }
                else
                {
                    tmpBool = System.IO.Directory.Exists(lockDir.FullName);
                }
                if (!tmpBool)
                {
                    try
                    {
                        System.IO.Directory.CreateDirectory(lockDir.FullName);
                    }
                    catch
                    {
                        throw new System.SystemException("Cannot create directory: " + lockDir.FullName);
                    }
                }
                else if (!System.IO.Directory.Exists(lockDir.FullName))
                {
                    throw new System.SystemException("Found regular file where directory expected: " + lockDir.FullName);
                }

                System.String randomLockName = "lucene-" + System.Convert.ToString(new System.Random().Next(), 16) + "-test.lock";

                Lock l = MakeLock(randomLockName);
                try
                {
                    l.Obtain();
                    l.Release();
                }
                catch (System.IO.IOException e)
                {
                    System.SystemException e2 = new System.SystemException("Failed to acquire random test lock; please verify filesystem for lock directory '" + lockDir + "' supports locking", e);
                    throw e2;
                }
            }
        }
Example #4
0
        public virtual void  TestNativeFSLockFactory()
        {
            NativeFSLockFactory f = new NativeFSLockFactory(SupportClass.AppSettings.Get("tempDir", System.IO.Path.GetTempPath()));

            f.SetLockPrefix("test");
            Lock l  = f.MakeLock("commit");
            Lock l2 = f.MakeLock("commit");

            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(!l2.Obtain(), "succeeded in obtaining lock twice");
            l.Release();

            Assert.IsTrue(l2.Obtain(), "failed to obtain 2nd lock after first one was freed");
            l2.Release();

            // Make sure we can obtain first one again, test isLocked():
            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(l.IsLocked());
            Assert.IsTrue(l2.IsLocked());
            l.Release();
            Assert.IsFalse(l.IsLocked());
            Assert.IsFalse(l2.IsLocked());
        }
Example #5
0
        public virtual void TestNativeFSLockFactory()
        {
            //NativeFSLockFactory f = new NativeFSLockFactory(CreateTempDir(LuceneTestCase.TestClass.SimpleName));
            NativeFSLockFactory f = new NativeFSLockFactory(AppSettings.Get("tempDir", Path.GetTempPath()));

            f.LockPrefix = "test";
            Lock l  = f.MakeLock("commit");
            Lock l2 = f.MakeLock("commit");

            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(!l2.Obtain(), "succeeded in obtaining lock twice");
            l.Dispose();

            Assert.IsTrue(l2.Obtain(), "failed to obtain 2nd lock after first one was freed");
            l2.Dispose();

            // Make sure we can obtain first one again, test isLocked():
            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(l.Locked);
            Assert.IsTrue(l2.Locked);
            l.Dispose();
            Assert.IsFalse(l.Locked);
            Assert.IsFalse(l2.Locked);
        }
Example #6
0
            /// <summary>
            /// Calls <see cref="DoBody"/> while <i>lock</i> is obtained.  Blocks if lock
            /// cannot be obtained immediately.  Retries to obtain lock once per second
            /// until it is obtained, or until it has tried ten times. Lock is released when
            /// <see cref="DoBody"/> exits. </summary>
            /// <exception cref="LockObtainFailedException"> if lock could not
            /// be obtained </exception>
            /// <exception cref="IOException"> if <see cref="Lock.Obtain()"/> throws <see cref="IOException"/> </exception>
            public virtual T Run()
            {
                bool locked = false;

                try
                {
                    locked = @lock.Obtain(lockWaitTimeout);
                    return(DoBody());
                }
                finally
                {
                    if (locked)
                    {
                        @lock.Dispose();
                    }
                }
            }
Example #7
0
            /// <summary>
            /// Calls <seealso cref="#doBody"/> while <i>lock</i> is obtained.  Blocks if lock
            /// cannot be obtained immediately.  Retries to obtain lock once per second
            /// until it is obtained, or until it has tried ten times. Lock is released when
            /// <seealso cref="#doBody"/> exits. </summary>
            /// <exception cref="LockObtainFailedException"> if lock could not
            /// be obtained </exception>
            /// <exception cref="System.IO.IOException"> if <seealso cref="Lock#obtain"/> throws System.IO.IOException </exception>
            public virtual object Run()
            {
                bool locked = false;

                try
                {
                    locked = @lock.Obtain(LockWaitTimeout);
                    return(DoBody());
                }
                finally
                {
                    if (locked)
                    {
                        @lock.Release();
                    }
                }
            }
Example #8
0
            /// <summary>Calls <see cref="DoBody" /> while <i>lock</i> is obtained.  Blocks if lock
            /// cannot be obtained immediately.  Retries to obtain lock once per second
            /// until it is obtained, or until it has tried ten times. Lock is released when
            /// <see cref="DoBody" /> exits.
            /// </summary>
            /// <throws>  LockObtainFailedException if lock could not </throws>
            /// <summary> be obtained
            /// </summary>
            /// <throws>  IOException if <see cref="Lock.Obtain(long)" /> throws IOException </throws>
            public virtual object run()
            {
                bool locked = false;

                try
                {
                    locked = lock_Renamed.Obtain(lockWaitTimeout);
                    return(DoBody());
                }
                finally
                {
                    if (locked)
                    {
                        lock_Renamed.Release();
                    }
                }
            }
Example #9
0
        public virtual void TestNativeFSLockFactoryLockExists()
        {
            DirectoryInfo tempDir  = new DirectoryInfo(AppSettings.Get("tempDir", Path.GetTempPath()));
            FileInfo      lockFile = new FileInfo(/*tempDir, */ "test.lock");

            lockFile.Create();

            Lock l = (new NativeFSLockFactory(tempDir)).MakeLock("test.lock");

            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            l.Dispose();
            Assert.IsFalse(l.Locked, "failed to release lock");
            if (lockFile.Exists)
            {
                lockFile.Delete();
            }
        }
Example #10
0
 public override bool Obtain()
 {
     UninterruptableMonitor.Enter(this);
     try
     {
         bool obtained = @lock.Obtain();
         if (obtained)
         {
             Verify((byte)1);
         }
         return(obtained);
     }
     finally
     {
         UninterruptableMonitor.Exit(this);
     }
 }
        // Simple test to verify locking system is "working".  On
        // NFS, if it's misconfigured, you can hit long (35
        // second) timeouts which cause Lock.obtain to take far
        // too long (it assumes the obtain() call takes zero
        // time).  Since it's a configuration problem, we test up
        // front once on creating the LockFactory:
        private void  AcquireTestLock()
        {
            System.String randomLockName = "lucene-" + System.Convert.ToString(new System.Random().Next(), 16) + "-test.lock";

            Lock l = MakeLock(randomLockName);

            try
            {
                l.Obtain();
            }
            catch (System.IO.IOException e)
            {
                System.IO.IOException e2 = new System.IO.IOException("Failed to acquire random test lock; please verify filesystem for lock directory '" + lockDir + "' supports locking", e);
                throw e2;
            }

            l.Release();
        }
Example #12
0
        public virtual void TestDirectInstantiation()
        {
            DirectoryInfo path = CreateTempDir("testDirectInstantiation");

            byte[] largeBuffer = new byte[Random().Next(256 * 1024)], largeReadBuffer = new byte[largeBuffer.Length];
            for (int i = 0; i < largeBuffer.Length; i++)
            {
                largeBuffer[i] = (byte)i; // automatically loops with modulo
            }

            var dirs = new FSDirectory[] { new SimpleFSDirectory(path, null), new NIOFSDirectory(path, null), new MMapDirectory(path, null) };

            for (int i = 0; i < dirs.Length; i++)
            {
                FSDirectory dir = dirs[i];
                dir.EnsureOpen();
                string      fname    = "foo." + i;
                string      lockname = "foo" + i + ".lck";
                IndexOutput @out     = dir.CreateOutput(fname, NewIOContext(Random()));
                @out.WriteByte((byte)(sbyte)i);
                @out.WriteBytes(largeBuffer, largeBuffer.Length);
                @out.Dispose();

                for (int j = 0; j < dirs.Length; j++)
                {
                    FSDirectory d2 = dirs[j];
                    d2.EnsureOpen();
                    Assert.IsTrue(SlowFileExists(d2, fname));
                    Assert.AreEqual(1 + largeBuffer.Length, d2.FileLength(fname));

                    // don't do read tests if unmapping is not supported!
                    if (d2 is MMapDirectory && !((MMapDirectory)d2).UseUnmap)
                    {
                        continue;
                    }

                    IndexInput input = d2.OpenInput(fname, NewIOContext(Random()));
                    Assert.AreEqual((byte)i, input.ReadByte());
                    // read array with buffering enabled
                    Arrays.Fill(largeReadBuffer, (byte)0);
                    input.ReadBytes(largeReadBuffer, 0, largeReadBuffer.Length, true);
                    Assert.AreEqual(largeBuffer, largeReadBuffer);
                    // read again without using buffer
                    input.Seek(1L);
                    Arrays.Fill(largeReadBuffer, (byte)0);
                    input.ReadBytes(largeReadBuffer, 0, largeReadBuffer.Length, false);
                    Assert.AreEqual(largeBuffer, largeReadBuffer);
                    input.Dispose();
                }

                // delete with a different dir
                dirs[(i + 1) % dirs.Length].DeleteFile(fname);

                for (int j = 0; j < dirs.Length; j++)
                {
                    FSDirectory d2 = dirs[j];
                    Assert.IsFalse(SlowFileExists(d2, fname));
                }

                Lock @lock = dir.MakeLock(lockname);
                Assert.IsTrue(@lock.Obtain());

                for (int j = 0; j < dirs.Length; j++)
                {
                    FSDirectory d2    = dirs[j];
                    Lock        lock2 = d2.MakeLock(lockname);
                    try
                    {
                        Assert.IsFalse(lock2.Obtain(1));
                    }
                    catch (LockObtainFailedException e)
                    {
                        // OK
                    }
                }

                @lock.Dispose();

                // now lock with different dir
                @lock = dirs[(i + 1) % dirs.Length].MakeLock(lockname);
                Assert.IsTrue(@lock.Obtain());
                @lock.Dispose();
            }

            for (int i = 0; i < dirs.Length; i++)
            {
                FSDirectory dir = dirs[i];
                dir.EnsureOpen();
                dir.Dispose();
                Assert.IsFalse(dir.IsOpen);
            }
        }
Example #13
0
        public static void Main(string[] args)
        {
            if (args.Length != 7)
            {
                // LUCENENET specific - our wrapper console shows the correct usage
                throw new ArgumentException();
                //Console.WriteLine("Usage: java Lucene.Net.Store.LockStressTest myID verifierHost verifierPort lockFactoryClassName lockDirName sleepTimeMS count\n" +
                //    "\n" +
                //    "  myID = int from 0 .. 255 (should be unique for test process)\n" +
                //    "  verifierHost = hostname that LockVerifyServer is listening on\n" +
                //    "  verifierPort = port that LockVerifyServer is listening on\n" +
                //    "  lockFactoryClassName = primary LockFactory class that we will use\n" +
                //    "  lockDirName = path to the lock directory (only set for Simple/NativeFSLockFactory\n" +
                //    "  sleepTimeMS = milliseconds to pause betweeen each lock obtain/release\n" +
                //    "  count = number of locking tries\n" +
                //    "\n" +
                //    "You should run multiple instances of this process, each with its own\n" +
                //    "unique ID, and each pointing to the same lock directory, to verify\n" +
                //    "that locking is working correctly.\n" +
                //    "\n" +
                //    "Make sure you are first running LockVerifyServer.");
                //Environment.FailFast("1");
            }

            int arg  = 0;
            int myID = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture);

            if (myID < 0 || myID > 255)
            {
                throw new ArgumentOutOfRangeException("ID", "ID must be a unique int 0..255"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention)
                //Console.WriteLine("myID must be a unique int 0..255");
                //Environment.Exit(1);
            }

            string verifierHost         = args[arg++];
            int    verifierPort         = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture);
            string lockFactoryClassName = args[arg++];
            string lockDirName          = args[arg++];
            int    sleepTimeMS          = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture);
            int    count = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture);

            IPAddress[] addresses = Dns.GetHostAddressesAsync(verifierHost).Result;
            IPAddress   addr      = addresses.Length > 0 ? addresses[0] : null;

            Type c;

            try
            {
                c = Type.GetType(lockFactoryClassName);
                if (c is null)
                {
                    // LUCENENET: try again, this time with the Store namespace
                    c = Type.GetType("Lucene.Net.Store." + lockFactoryClassName);
                }
            }
            catch (Exception e)
            {
                throw new IOException("unable to find LockClass " + lockFactoryClassName, e);
            }

            LockFactory lockFactory;

            try
            {
                lockFactory = (LockFactory)Activator.CreateInstance(c);
            }
            catch (Exception e) when(e.IsIllegalAccessException() || e.IsInstantiationException() || e.IsClassNotFoundException())
            {
                throw new IOException("Cannot instantiate lock factory " + lockFactoryClassName, e);
            }
            // LUCENENET specific - added more explicit exception message in this case
            catch (Exception e) when(e.IsClassCastException())
            {
                throw new IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory", e);
            }

            DirectoryInfo lockDir = new DirectoryInfo(lockDirName);

            if (lockFactory is FSLockFactory fsLockFactory)
            {
                fsLockFactory.SetLockDir(lockDir);
            }

            Console.WriteLine("Connecting to server " + addr + " and registering as client " + myID + "...");
            using Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
            socket.Connect(verifierHost, verifierPort);

            using Stream stream = new NetworkStream(socket);
            BinaryReader intReader = new BinaryReader(stream);
            BinaryWriter intWriter = new BinaryWriter(stream);

            intWriter.Write(myID);
            stream.Flush();

            lockFactory.LockPrefix = "test";
            LockFactory verifyLF = new VerifyingLockFactory(lockFactory, stream);
            Lock        l        = verifyLF.MakeLock("test.lock");
            Random      rnd      = new Random();

            // wait for starting gun
            if (intReader.ReadInt32() != 43)
            {
                throw new IOException("Protocol violation");
            }

            for (int i = 0; i < count; i++)
            {
                bool obtained = false;

                try
                {
                    obtained = l.Obtain(rnd.Next(100) + 10);
                }
#pragma warning disable 168
                catch (LockObtainFailedException e)
#pragma warning restore 168
                {
                }

                if (obtained)
                {
                    Thread.Sleep(sleepTimeMS);
                    l.Dispose();
                }

                if (i % 500 == 0)
                {
                    Console.WriteLine((i * 100.0 / count) + "% done.");
                }

                Thread.Sleep(sleepTimeMS);
            }
            Console.WriteLine("Finished " + count + " tries.");
        }
        public static void MainTMP(System.String[] args)
        {
            if (args.Length != 6)
            {
                System.Console.Out.WriteLine("\nUsage: java Lucene.Net.Store.LockStressTest myID verifierHostOrIP verifierPort lockFactoryClassName lockDirName sleepTime\n" + "\n" + "  myID = int from 0 .. 255 (should be unique for test process)\n" + "  verifierHostOrIP = host name or IP address where LockVerifyServer is running\n" + "  verifierPort = port that LockVerifyServer is listening on\n" + "  lockFactoryClassName = primary LockFactory class that we will use\n" + "  lockDirName = path to the lock directory (only set for Simple/NativeFSLockFactory\n" + "  sleepTimeMS = milliseconds to pause betweeen each lock obtain/release\n" + "\n" + "You should run multiple instances of this process, each with its own\n" + "unique ID, and each pointing to the same lock directory, to verify\n" + "that locking is working correctly.\n" + "\n" + "Make sure you are first running LockVerifyServer.\n" + "\n");
                System.Environment.Exit(1);
            }

            int myID = System.Int32.Parse(args[0]);

            if (myID < 0 || myID > 255)
            {
                System.Console.Out.WriteLine("myID must be a unique int 0..255");
                System.Environment.Exit(1);
            }

            System.String verifierHost = args[1];
            int           verifierPort = System.Int32.Parse(args[2]);

            System.String lockFactoryClassName = args[3];
            System.String lockDirName          = args[4];
            int           sleepTimeMS          = System.Int32.Parse(args[5]);

            System.Type c;
            try
            {
                c = System.Type.GetType(lockFactoryClassName);
            }
            catch (System.Exception)
            {
                throw new System.IO.IOException("unable to find LockClass " + lockFactoryClassName);
            }

            LockFactory lockFactory;

            try
            {
                lockFactory = (LockFactory)System.Activator.CreateInstance(c);
            }
            catch (System.UnauthorizedAccessException)
            {
                throw new System.IO.IOException("IllegalAccessException when instantiating LockClass " + lockFactoryClassName);
            }
            catch (System.InvalidCastException)
            {
                throw new System.IO.IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory");
            }
            catch (System.Exception)
            {
                throw new System.IO.IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName);
            }

            System.IO.DirectoryInfo lockDir = new System.IO.DirectoryInfo(lockDirName);

            if (lockFactory is NativeFSLockFactory)
            {
                ((NativeFSLockFactory)lockFactory).LockDir = lockDir;
            }
            else if (lockFactory is SimpleFSLockFactory)
            {
                ((SimpleFSLockFactory)lockFactory).LockDir = lockDir;
            }

            lockFactory.LockPrefix = "test";

            LockFactory verifyLF = new VerifyingLockFactory((sbyte)myID, lockFactory, verifierHost, verifierPort);

            Lock l = verifyLF.MakeLock("test.lock");

            while (true)
            {
                bool obtained = false;

                try
                {
                    obtained = l.Obtain(10);
                }
                catch (LockObtainFailedException)
                {
                    System.Console.Out.Write("x");
                }

                if (obtained)
                {
                    System.Console.Out.Write("l");
                    l.Release();
                }
                System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * sleepTimeMS));
            }
        }
Example #15
0
        public virtual void  TestDirectInstantiation()
        {
            System.IO.DirectoryInfo path = new System.IO.DirectoryInfo(AppSettings.Get("tempDir", System.IO.Path.GetTempPath()));

            int sz = 2;

            Directory[] dirs = new Directory[sz];

            dirs[0] = new SimpleFSDirectory(path, null);
            // dirs[1] = new NIOFSDirectory(path, null);
            System.Console.WriteLine("Skipping NIOFSDirectory() test under Lucene.Net");
            dirs[1] = new MMapDirectory(path, null);

            for (int i = 0; i < sz; i++)
            {
                Directory dir = dirs[i];
                dir.EnsureOpen();
                System.String fname       = "foo." + i;
                System.String lockname    = "foo" + i + ".lck";
                IndexOutput   out_Renamed = dir.CreateOutput(fname, null);
                out_Renamed.WriteByte((byte)i);
                out_Renamed.Close();

                for (int j = 0; j < sz; j++)
                {
                    Directory d2 = dirs[j];
                    d2.EnsureOpen();
                    Assert.IsTrue(d2.FileExists(fname, null));
                    Assert.AreEqual(1, d2.FileLength(fname, null));

                    // don't test read on MMapDirectory, since it can't really be
                    // closed and will cause a failure to delete the file.
                    if (d2 is MMapDirectory)
                    {
                        continue;
                    }

                    IndexInput input = d2.OpenInput(fname, null);
                    Assert.AreEqual((byte)i, input.ReadByte(null));
                    input.Close();
                }

                // delete with a different dir
                dirs[(i + 1) % sz].DeleteFile(fname, null);

                for (int j = 0; j < sz; j++)
                {
                    Directory d2 = dirs[j];
                    Assert.IsFalse(d2.FileExists(fname, null));
                }

                Lock lock_Renamed = dir.MakeLock(lockname);
                Assert.IsTrue(lock_Renamed.Obtain());

                for (int j = 0; j < sz; j++)
                {
                    Directory d2    = dirs[j];
                    Lock      lock2 = d2.MakeLock(lockname);
                    try
                    {
                        Assert.IsFalse(lock2.Obtain(1));
                    }
                    catch (LockObtainFailedException)
                    {
                        // OK
                    }
                }

                lock_Renamed.Release();

                // now lock with different dir
                lock_Renamed = dirs[(i + 1) % sz].MakeLock(lockname);
                Assert.IsTrue(lock_Renamed.Obtain());
                lock_Renamed.Release();
            }

            for (int i = 0; i < sz; i++)
            {
                Directory dir = dirs[i];
                dir.EnsureOpen();
                dir.Close();
                Assert.IsFalse(dir.isOpen_ForNUnit);
            }
        }
Example #16
0
        public static void Main(string[] args)
        {
            if (args.Length != 7)
            {
                Console.WriteLine("Usage: java Lucene.Net.Store.LockStressTest myID verifierHost verifierPort lockFactoryClassName lockDirName sleepTimeMS count\n" + "\n" + "  myID = int from 0 .. 255 (should be unique for test process)\n" + "  verifierHost = hostname that LockVerifyServer is listening on\n" + "  verifierPort = port that LockVerifyServer is listening on\n" + "  lockFactoryClassName = primary LockFactory class that we will use\n" + "  lockDirName = path to the lock directory (only set for Simple/NativeFSLockFactory\n" + "  sleepTimeMS = milliseconds to pause betweeen each lock obtain/release\n" + "  count = number of locking tries\n" + "\n" + "You should run multiple instances of this process, each with its own\n" + "unique ID, and each pointing to the same lock directory, to verify\n" + "that locking is working correctly.\n" + "\n" + "Make sure you are first running LockVerifyServer.");
                Environment.Exit(1);
            }

            int arg  = 0;
            int myID = Convert.ToInt32(args[arg++]);

            if (myID < 0 || myID > 255)
            {
                Console.WriteLine("myID must be a unique int 0..255");
                Environment.Exit(1);
            }

            IPHostEntry verifierHost         = Dns.GetHostEntry(args[arg++]);
            int         verifierPort         = Convert.ToInt32(args[arg++]);
            IPAddress   verifierIp           = verifierHost.AddressList[0];
            IPEndPoint  addr                 = new IPEndPoint(verifierIp, verifierPort);
            string      lockFactoryClassName = args[arg++];
            string      lockDirName          = args[arg++];
            int         sleepTimeMS          = Convert.ToInt32(args[arg++]);
            int         count                = Convert.ToInt32(args[arg++]);

            Type c;

            try
            {
                c = Type.GetType(lockFactoryClassName);
            }
            catch (Exception)
            {
                throw new IOException("unable to find LockClass " + lockFactoryClassName);
            }

            LockFactory lockFactory;

            try
            {
                lockFactory = (LockFactory)Activator.CreateInstance(c);
            }
            catch (UnauthorizedAccessException)
            {
                throw new System.IO.IOException("Cannot instantiate lock factory " + lockFactoryClassName);
            }
            catch (InvalidCastException)
            {
                throw new System.IO.IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory");
            }
            catch (Exception)
            {
                throw new System.IO.IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName);
            }

            DirectoryInfo lockDir = new DirectoryInfo(lockDirName);

            if (lockFactory is FSLockFactory)
            {
                ((FSLockFactory)lockFactory).LockDir = lockDir;
            }

            Console.WriteLine("Connecting to server " + addr + " and registering as client " + myID + "...");
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                using (Stream @out = new NetworkStream(socket, FileAccess.ReadWrite), @in = new NetworkStream(socket, FileAccess.Read))
                {
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                    socket.Connect(verifierIp, 500);

                    BinaryReader intReader = new BinaryReader(@in);
                    BinaryWriter intWriter = new BinaryWriter(@out);

                    intWriter.Write(myID);
                    @out.Flush();

                    lockFactory.LockPrefix = "test";
                    LockFactory verifyLF = new VerifyingLockFactory(lockFactory, @in, @out);
                    Lock        l        = verifyLF.MakeLock("test.lock");
                    Random      rnd      = new Random();

                    // wait for starting gun
                    if (intReader.ReadInt32() != 43)
                    {
                        throw new System.IO.IOException("Protocol violation");
                    }

                    for (int i = 0; i < count; i++)
                    {
                        bool obtained = false;

                        try
                        {
                            obtained = l.Obtain(rnd.Next(100) + 10);
                        }
                        catch (LockObtainFailedException e)
                        {
                        }

                        if (obtained)
                        {
                            Thread.Sleep(sleepTimeMS);
                            l.Release();
                        }

                        if (i % 500 == 0)
                        {
                            Console.WriteLine((i * 100.0 / count) + "% done.");
                        }

                        Thread.Sleep(sleepTimeMS);
                    }
                }
            }

            Console.WriteLine("Finished " + count + " tries.");
        }