Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Store / LockVerifyServer.cs
1 /* 
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
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  */
17
18 using System;
19
20 namespace Mono.Lucene.Net.Store
21 {
22         
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.
27         /// 
28         /// </summary>
29         /// <seealso cref="VerifyingLockFactory">
30         /// </seealso>
31         /// <seealso cref="LockStressTest">
32         /// </seealso>
33         
34         public class LockVerifyServer
35         {
36                 
37                 private static System.String GetTime(long startTime)
38                 {
39                         return "[" + (((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - startTime) / 1000) + "s] ";
40                 }
41                 
42                 [STAThread]
43                 public static void  Main(System.String[] args)
44                 {
45                         
46                         if (args.Length != 1)
47                         {
48                                 System.Console.Out.WriteLine("\nUsage: java Mono.Lucene.Net.Store.LockVerifyServer port\n");
49                                 System.Environment.Exit(1);
50                         }
51                         
52                         int port = System.Int32.Parse(args[0]);
53                         
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 + "...");
60                         
61                         int lockedID = 0;
62                         long startTime = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
63                         
64                         while (true)
65                         {
66                                 System.Net.Sockets.TcpClient cs = s.AcceptTcpClient();
67                                 System.IO.Stream out_Renamed = cs.GetStream();
68                                 System.IO.Stream in_Renamed = cs.GetStream();
69                                 
70                                 int id = in_Renamed.ReadByte();
71                                 int command = in_Renamed.ReadByte();
72                                 
73                                 bool err = false;
74                                 
75                                 if (command == 1)
76                                 {
77                                         // Locked
78                                         if (lockedID != 0)
79                                         {
80                                                 err = true;
81                                                 System.Console.Out.WriteLine(GetTime(startTime) + " ERROR: id " + id + " got lock, but " + lockedID + " already holds the lock");
82                                         }
83                                         lockedID = id;
84                                 }
85                                 else if (command == 0)
86                                 {
87                                         if (lockedID != id)
88                                         {
89                                                 err = true;
90                                                 System.Console.Out.WriteLine(GetTime(startTime) + " ERROR: id " + id + " released the lock, but " + lockedID + " is the one holding the lock");
91                                         }
92                                         lockedID = 0;
93                                 }
94                                 else
95                                         throw new System.SystemException("unrecognized command " + command);
96                                 
97                                 System.Console.Out.Write(".");
98                                 
99                                 if (err)
100                                         out_Renamed.WriteByte((System.Byte) 1);
101                                 else
102                                         out_Renamed.WriteByte((System.Byte) 0);
103                                 
104                                 out_Renamed.Close();
105                                 in_Renamed.Close();
106                                 cs.Close();
107                         }
108                 }
109         }
110 }