001    /**************************************************************** 
002     * This work is derived from 'jnamed.java' distributed in       *
003     * 'dnsjava-2.0.5'. This original is licensed as follows:       *
004     * Copyright (c) 1999-2005, Brian Wellington                    *
005     * All rights reserved.                                         *
006     *                                                              *
007     * Redistribution and use in source and binary forms, with or   * 
008     * without modification, are permitted provided that the        *  
009     * following conditions are met:                                * 
010     *                                                              * 
011     *  * Redistributions of source code must retain the above      *
012     *    copyright notice, this list of conditions and the         *
013     *    following disclaimer.                                     *
014     *  * Redistributions in binary form must reproduce the above   *
015     *    copyright notice, this list of conditions and the         *
016     *    following disclaimer in the documentation and/or other    *
017     *    materials provided with the distribution.                 *
018     *  * Neither the name of the dnsjava project nor the names     *
019     *    of its contributors may be used to endorse or promote     *
020     *    products derived from this software without specific      *
021     *    prior written permission.                                 *
022     *                                                              *
023     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND       *
024     * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,  *
025     * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF     *
026     * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE     *
027     * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR         *
028     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
029     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,     *
030     * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR       *
031     * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS         *
032     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF            *
033     * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    *
034     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT   *
035     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          *
036     * POSSIBILITY OF SUCH DAMAGE.                                  *
037     *                                                              *
038     * Modifications are                                            * 
039     * Licensed to the Apache Software Foundation (ASF) under one   *
040     * or more contributor license agreements.  See the NOTICE file *
041     * distributed with this work for additional information        *
042     * regarding copyright ownership.  The ASF licenses this file   *
043     * to you under the Apache License, Version 2.0 (the            *
044     * "License"); you may not use this file except in compliance   *
045     * with the License.  You may obtain a copy of the License at   *
046     *                                                              *
047     *   http://www.apache.org/licenses/LICENSE-2.0                 *
048     *                                                              *
049     * Unless required by applicable law or agreed to in writing,   *
050     * software distributed under the License is distributed on an  *
051     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
052     * KIND, either express or implied.  See the License for the    *
053     * specific language governing permissions and limitations      *
054     * under the License.                                           *
055     ****************************************************************/
056    
057    package org.apache.james.jspf.tester;
058    
059    import java.io.DataInputStream;
060    import java.io.DataOutputStream;
061    import java.io.IOException;
062    import java.io.InputStream;
063    import java.net.InetAddress;
064    import java.net.ServerSocket;
065    import java.net.Socket;
066    
067    public final class TCPListener implements Runnable {
068        
069        private final static class TCPServer implements Runnable {
070            private final Socket serverSocket;
071    
072            private ResponseGenerator responseGenerator;
073    
074            private TCPServer(Socket s, ResponseGenerator rg) {
075                this.serverSocket = s;
076                this.responseGenerator = rg;
077            }
078    
079            public void run() {
080                try {
081                    int inLength;
082                    DataInputStream dataIn;
083                    DataOutputStream dataOut;
084                    byte[] in;
085    
086                    InputStream is = serverSocket.getInputStream();
087                    dataIn = new DataInputStream(is);
088                    inLength = dataIn.readUnsignedShort();
089                    in = new byte[inLength];
090                    dataIn.readFully(in);
091    
092                    int length = in.length;
093                    byte[] response = responseGenerator.generateReply(in, length);
094                    if (response == null) return;
095                    dataOut = new DataOutputStream(serverSocket.getOutputStream());
096                    dataOut.writeShort(response.length);
097                    dataOut.write(response);
098                } catch (IOException e) {
099                    System.out.println("TCPclient("
100                            + serverSocket.getLocalAddress().getHostAddress() + "#" + serverSocket.getLocalPort()
101                            + "): " + e);
102                } finally {
103                    try {
104                        serverSocket.close();
105                    } catch (IOException e) {
106                    }
107                }
108            }
109    
110        }
111    
112        private final int port;
113    
114        private final InetAddress addr;
115    
116        private ResponseGenerator responseGenerator;
117    
118        public TCPListener(InetAddress addr, int port, ResponseGenerator rg) {
119            this.port = port;
120            this.addr = addr;
121            this.responseGenerator = rg;
122        }
123    
124        public void run() {
125            try {
126                ServerSocket sock = new ServerSocket(port, 128, addr);
127                while (true) {
128                    new Thread(new TCPServer(sock.accept(), responseGenerator)).start();
129                }
130            } catch (IOException e) {
131                System.out.println("serveTCP(" + addr.getHostAddress() + "#" + port + "): "
132                        + e);
133            }
134        }
135    }