2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 namespace Mono.Lucene.Net.Store
23 /// <summary> Simple standalone server that must be running when you
24 /// use {@link VerifyingLockFactory}. This server simply
25 /// verifies at most one process holds the lock at a time.
26 /// Run without any args to see usage.
29 /// <seealso cref="VerifyingLockFactory">
31 /// <seealso cref="LockStressTest">
34 public class LockVerifyServer
37 private static System.String GetTime(long startTime)
39 return "[" + (((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - startTime) / 1000) + "s] ";
43 public static void Main(System.String[] args)
48 System.Console.Out.WriteLine("\nUsage: java Mono.Lucene.Net.Store.LockVerifyServer port\n");
49 System.Environment.Exit(1);
52 int port = System.Int32.Parse(args[0]);
54 System.Net.Sockets.TcpListener temp_tcpListener;
55 temp_tcpListener = new System.Net.Sockets.TcpListener(System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0], port);
56 temp_tcpListener.Server.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReuseAddress, 1);
57 temp_tcpListener.Start();
58 System.Net.Sockets.TcpListener s = temp_tcpListener;
59 System.Console.Out.WriteLine("\nReady on port " + port + "...");
62 long startTime = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
66 System.Net.Sockets.TcpClient cs = s.AcceptTcpClient();
67 System.IO.Stream out_Renamed = cs.GetStream();
68 System.IO.Stream in_Renamed = cs.GetStream();
70 int id = in_Renamed.ReadByte();
71 int command = in_Renamed.ReadByte();
81 System.Console.Out.WriteLine(GetTime(startTime) + " ERROR: id " + id + " got lock, but " + lockedID + " already holds the lock");
85 else if (command == 0)
90 System.Console.Out.WriteLine(GetTime(startTime) + " ERROR: id " + id + " released the lock, but " + lockedID + " is the one holding the lock");
95 throw new System.SystemException("unrecognized command " + command);
97 System.Console.Out.Write(".");
100 out_Renamed.WriteByte((System.Byte) 1);
102 out_Renamed.WriteByte((System.Byte) 0);