add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / store / LockVerifyServer.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.net.ServerSocket;
21 import java.net.Socket;
22 import java.io.OutputStream;
23 import java.io.InputStream;
24 import java.io.IOException;
25
26 /**
27  * Simple standalone server that must be running when you
28  * use {@link VerifyingLockFactory}.  This server simply
29  * verifies at most one process holds the lock at a time.
30  * Run without any args to see usage.
31  *
32  * @see VerifyingLockFactory
33  * @see LockStressTest
34  */
35
36 public class LockVerifyServer {
37
38   private static String getTime(long startTime) {
39     return "[" + ((System.currentTimeMillis()-startTime)/1000) + "s] ";
40   }
41
42   public static void main(String[] args) throws IOException {
43
44     if (args.length != 1) {
45       System.out.println("\nUsage: java org.apache.lucene.store.LockVerifyServer port\n");
46       System.exit(1);
47     }
48
49     final int port = Integer.parseInt(args[0]);
50
51     ServerSocket s = new ServerSocket(port);
52     s.setReuseAddress(true);
53     System.out.println("\nReady on port " + port + "...");
54
55     int lockedID = 0;
56     long startTime = System.currentTimeMillis();
57
58     while(true) {
59       Socket cs = s.accept();
60       OutputStream out = cs.getOutputStream();
61       InputStream in = cs.getInputStream();
62
63       int id = in.read();
64       int command = in.read();
65
66       boolean err = false;
67
68       if (command == 1) {
69         // Locked
70         if (lockedID != 0) {
71           err = true;
72           System.out.println(getTime(startTime) + " ERROR: id " + id + " got lock, but " + lockedID + " already holds the lock");
73         }
74         lockedID = id;
75       } else if (command == 0) {
76         if (lockedID != id) {
77           err = true;
78           System.out.println(getTime(startTime) + " ERROR: id " + id + " released the lock, but " + lockedID + " is the one holding the lock");
79         }
80         lockedID = 0;
81       } else
82         throw new RuntimeException("unrecognized command " + command);
83
84       System.out.print(".");
85
86       if (err)
87         out.write(1);
88       else
89         out.write(0);
90
91       out.close();
92       in.close();
93       cs.close();
94     }
95   }
96 }