1 /****************************************************************
2 * This work is derived from 'jnamed.java' distributed in *
3 * 'dnsjava-2.0.5'. This original is licensed as follows: *
4 * Copyright (c) 1999-2005, Brian Wellington *
5 * All rights reserved. *
6 * *
7 * Redistribution and use in source and binary forms, with or *
8 * without modification, are permitted provided that the *
9 * following conditions are met: *
10 * *
11 * * Redistributions of source code must retain the above *
12 * copyright notice, this list of conditions and the *
13 * following disclaimer. *
14 * * Redistributions in binary form must reproduce the above *
15 * copyright notice, this list of conditions and the *
16 * following disclaimer in the documentation and/or other *
17 * materials provided with the distribution. *
18 * * Neither the name of the dnsjava project nor the names *
19 * of its contributors may be used to endorse or promote *
20 * products derived from this software without specific *
21 * prior written permission. *
22 * *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR *
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT *
35 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
36 * POSSIBILITY OF SUCH DAMAGE. *
37 * *
38 * Modifications are *
39 * Licensed to the Apache Software Foundation (ASF) under one *
40 * or more contributor license agreements. See the NOTICE file *
41 * distributed with this work for additional information *
42 * regarding copyright ownership. The ASF licenses this file *
43 * to you under the Apache License, Version 2.0 (the *
44 * "License"); you may not use this file except in compliance *
45 * with the License. You may obtain a copy of the License at *
46 * *
47 * http://www.apache.org/licenses/LICENSE-2.0 *
48 * *
49 * Unless required by applicable law or agreed to in writing, *
50 * software distributed under the License is distributed on an *
51 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
52 * KIND, either express or implied. See the License for the *
53 * specific language governing permissions and limitations *
54 * under the License. *
55 ****************************************************************/
56
57 package org.apache.james.jspf.tester;
58
59 import java.io.DataInputStream;
60 import java.io.DataOutputStream;
61 import java.io.IOException;
62 import java.io.InputStream;
63 import java.net.InetAddress;
64 import java.net.ServerSocket;
65 import java.net.Socket;
66
67 public final class TCPListener implements Runnable {
68
69 private final static class TCPServer implements Runnable {
70 private final Socket serverSocket;
71
72 private ResponseGenerator responseGenerator;
73
74 private TCPServer(Socket s, ResponseGenerator rg) {
75 this.serverSocket = s;
76 this.responseGenerator = rg;
77 }
78
79 public void run() {
80 try {
81 int inLength;
82 DataInputStream dataIn;
83 DataOutputStream dataOut;
84 byte[] in;
85
86 InputStream is = serverSocket.getInputStream();
87 dataIn = new DataInputStream(is);
88 inLength = dataIn.readUnsignedShort();
89 in = new byte[inLength];
90 dataIn.readFully(in);
91
92 int length = in.length;
93 byte[] response = responseGenerator.generateReply(in, length);
94 if (response == null) return;
95 dataOut = new DataOutputStream(serverSocket.getOutputStream());
96 dataOut.writeShort(response.length);
97 dataOut.write(response);
98 } catch (IOException e) {
99 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 }