001: package org.apache.lucene.store;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.net.ServerSocket;
021: import java.net.Socket;
022: import java.io.OutputStream;
023: import java.io.InputStream;
024: import java.io.IOException;
025:
026: /**
027: * Simple standalone server that must be running when you
028: * use {@link VerifyingLockFactory}. This server simply
029: * verifies at most one process holds the lock at a time.
030: * Run without any args to see usage.
031: *
032: * @see VerifyingLockFactory
033: * @see LockStressTest
034: */
035:
036: public class LockVerifyServer {
037:
038: private static String getTime(long startTime) {
039: return "[" + ((System.currentTimeMillis() - startTime) / 1000)
040: + "s] ";
041: }
042:
043: public static void main(String[] args) throws IOException {
044:
045: if (args.length != 1) {
046: System.out
047: .println("\nUsage: java org.apache.lucene.store.LockVerifyServer port\n");
048: System.exit(1);
049: }
050:
051: final int port = Integer.parseInt(args[0]);
052:
053: ServerSocket s = new ServerSocket(port);
054: s.setReuseAddress(true);
055: System.out.println("\nReady on port " + port + "...");
056:
057: int lockedID = 0;
058: long startTime = System.currentTimeMillis();
059:
060: while (true) {
061: Socket cs = s.accept();
062: OutputStream out = cs.getOutputStream();
063: InputStream in = cs.getInputStream();
064:
065: int id = in.read();
066: int command = in.read();
067:
068: boolean err = false;
069:
070: if (command == 1) {
071: // Locked
072: if (lockedID != 0) {
073: err = true;
074: System.out.println(getTime(startTime)
075: + " ERROR: id " + id + " got lock, but "
076: + lockedID + " already holds the lock");
077: }
078: lockedID = id;
079: } else if (command == 0) {
080: if (lockedID != id) {
081: err = true;
082: System.out.println(getTime(startTime)
083: + " ERROR: id " + id
084: + " released the lock, but " + lockedID
085: + " is the one holding the lock");
086: }
087: lockedID = 0;
088: } else
089: throw new RuntimeException("unrecognized command "
090: + command);
091:
092: System.out.print(".");
093:
094: if (err)
095: out.write(1);
096: else
097: out.write(0);
098:
099: out.close();
100: in.close();
101: cs.close();
102: }
103: }
104: }
|