IoAcceptor.java
01 /*
02  *
03  * Licensed to the Apache Software Foundation (ASF) under one
04  * or more contributor license agreements.  See the NOTICE file
05  * distributed with this work for additional information
06  * regarding copyright ownership.  The ASF licenses this file
07  * to you under the Apache License, Version 2.0 (the
08  * "License"); you may not use this file except in compliance
09  * with 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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  */
21 package org.apache.qpid.transport.network.io;
22 
23 import org.apache.qpid.transport.Binding;
24 import org.apache.qpid.transport.TransportException;
25 
26 import java.io.IOException;
27 
28 import java.net.InetSocketAddress;
29 import java.net.ServerSocket;
30 import java.net.Socket;
31 import java.net.SocketAddress;
32 
33 import java.nio.ByteBuffer;
34 
35 
36 /**
37  * IoAcceptor
38  *
39  */
40 
41 public class IoAcceptor<E> extends Thread
42 {
43 
44 
45     private ServerSocket socket;
46     private Binding<E,ByteBuffer> binding;
47 
48     public IoAcceptor(SocketAddress address, Binding<E,ByteBuffer> binding)
49         throws IOException
50     {
51         socket = new ServerSocket();
52         socket.setReuseAddress(true);
53         socket.bind(address);
54         this.binding = binding;
55 
56         setName(String.format("IoAcceptor - %s", socket.getInetAddress()));
57     }
58 
59     /**
60         Close the underlying ServerSocket if it has not already been closed.
61      */
62     public void close() throws IOException
63     {
64         if (!socket.isClosed())
65         {
66             socket.close();
67         }
68     }
69 
70     public IoAcceptor(String host, int port, Binding<E,ByteBuffer> binding)
71         throws IOException
72     {
73         this(new InetSocketAddress(host, port), binding);
74     }
75 
76     public void run()
77     {
78         while (true)
79         {
80             try
81             {
82                 Socket sock = socket.accept();
83                 IoTransport<E> transport = new IoTransport<E>(sock, binding,false);
84             }
85             catch (IOException e)
86             {
87                 throw new TransportException(e);
88             }
89         }
90     }
91 
92 }