View Javadoc

1   /***
2    *
3    * Copyright 2004 Hiram Chirino
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.activeio.adapter;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.InetAddress;
23  import java.net.Socket;
24  import java.net.SocketAddress;
25  import java.net.SocketException;
26  import java.nio.channels.SocketChannel;
27  
28  import org.activeio.Packet;
29  import org.activeio.SynchChannel;
30  import org.activeio.net.SocketMetadata;
31  import org.activeio.packet.ByteArrayPacket;
32  
33  /***
34   * Provides a {@see java.net.Socket} interface to a {@see org.activeio.SynchChannel}.
35   * 
36   * If the {@see org.activeio.SynchChannel} being adapted can not be 
37   * {@see org.activeio.Channel#narrow(Class)}ed to a {@see org.activeio.net.SocketMetadata} 
38   * then all methods accessing socket metadata will throw a {@see java.net.SocketException}.
39   *  
40   */
41  public class SynchChannelToSocketAdapter extends Socket {
42      
43      private final SynchChannel channel;
44      private final SynchChannelInputStream inputStream;
45      private final SynchChannelOutputStream outputStream;
46      private final SocketMetadata socketMetadata;
47      private final Packet urgentPackget = new ByteArrayPacket(new byte[1]);
48      boolean closed;
49  
50      public SynchChannelToSocketAdapter(SynchChannel channel) {
51          this(channel, (SocketMetadata)channel.narrow(SocketMetadata.class));
52      }
53      
54      public SynchChannelToSocketAdapter(SynchChannel channel, SocketMetadata socketMetadata) {
55          this.channel = channel;
56          this.socketMetadata = socketMetadata;
57          this.inputStream = new SynchChannelInputStream(channel);
58          this.outputStream = new SynchChannelOutputStream(channel);
59      }
60  
61      public boolean isConnected() {
62          return true;
63      }
64      
65      public boolean isBound() {
66          return true;
67      }
68      
69      public boolean isClosed() {
70          return closed;
71      }
72  
73      public void bind(SocketAddress bindpoint) throws IOException {
74          throw new IOException("Not supported");
75      }
76  
77      public synchronized void close() throws IOException {
78          if( closed )
79              return;
80          closed = true;
81          inputStream.close();
82          outputStream.close();
83          channel.dispose();
84      }
85      
86      public void connect(SocketAddress endpoint) throws IOException {
87          throw new IOException("Not supported");
88      }
89      
90      public void connect(SocketAddress endpoint, int timeout) throws IOException {
91          throw new IOException("Not supported");
92      }
93  
94      public SocketChannel getChannel() {
95          return null;
96      }
97      
98      public InputStream getInputStream() throws IOException {
99          return inputStream;
100     }
101 
102     public OutputStream getOutputStream() throws IOException {
103         return outputStream;
104     }
105     
106     public boolean isInputShutdown() {
107         return inputStream.isClosed();
108     }
109 
110     public boolean isOutputShutdown() {
111         return outputStream.isClosed();
112     }
113     
114     public void sendUrgentData(int data) throws IOException {
115         urgentPackget.clear();
116         urgentPackget.write(data);
117         urgentPackget.flip();
118         channel.write(urgentPackget);
119     }
120 
121     public int getSoTimeout() throws SocketException {
122         return (int) inputStream.getTimeout();
123     }
124     
125     public synchronized void setSoTimeout(int timeout) throws SocketException {
126         inputStream.setTimeout(timeout);
127     }    
128     
129     public void shutdownOutput() throws IOException {
130         outputStream.close();
131     }
132     
133     public void shutdownInput() throws IOException {
134         inputStream.close();
135     }
136     
137     protected SocketMetadata getSocketMetadata() throws SocketException {
138         if( socketMetadata == null )
139             throw new SocketException("No socket metadata available.");
140         return socketMetadata;
141     }
142     
143     public InetAddress getInetAddress() {
144         if( socketMetadata ==null )
145             return null;
146         return socketMetadata.getInetAddress();
147     }
148     public boolean getKeepAlive() throws SocketException {
149         return getSocketMetadata().getKeepAlive();
150     }
151     public InetAddress getLocalAddress() {
152         if( socketMetadata ==null )
153             return null;
154         return socketMetadata.getLocalAddress();
155     }
156     public int getLocalPort() {
157         if( socketMetadata ==null )
158             return -1;
159         return socketMetadata.getLocalPort();
160     }
161     public SocketAddress getLocalSocketAddress() {
162         if( socketMetadata ==null )
163             return null;
164         return socketMetadata.getLocalSocketAddress();
165     }
166     public boolean getOOBInline() throws SocketException {
167         return getSocketMetadata().getOOBInline();
168     }
169     public int getPort() {
170         if( socketMetadata ==null )
171             return -1;
172         return socketMetadata.getPort();
173     }
174     public int getReceiveBufferSize() throws SocketException {
175         return getSocketMetadata().getReceiveBufferSize();
176     }
177     public SocketAddress getRemoteSocketAddress() {
178         if( socketMetadata ==null )
179             return null;
180         return socketMetadata.getRemoteSocketAddress();
181     }
182     public boolean getReuseAddress() throws SocketException {
183         return getSocketMetadata().getReuseAddress();
184     }
185     public int getSendBufferSize() throws SocketException {
186         return getSocketMetadata().getSendBufferSize();
187     }
188     public int getSoLinger() throws SocketException {
189         return getSocketMetadata().getSoLinger();
190     }
191     public boolean getTcpNoDelay() throws SocketException {
192         return getSocketMetadata().getTcpNoDelay();
193     }
194     public int getTrafficClass() throws SocketException {
195         return getSocketMetadata().getTrafficClass();
196     }
197     public void setKeepAlive(boolean on) throws SocketException {
198         getSocketMetadata().setKeepAlive(on);
199     }
200     public void setOOBInline(boolean on) throws SocketException {
201         getSocketMetadata().setOOBInline(on);
202     }
203     public void setReceiveBufferSize(int size) throws SocketException {
204         getSocketMetadata().setReceiveBufferSize(size);
205     }
206     public void setReuseAddress(boolean on) throws SocketException {
207         getSocketMetadata().setReuseAddress(on);
208     }
209     public void setSendBufferSize(int size) throws SocketException {
210         getSocketMetadata().setSendBufferSize(size);
211     }
212     public void setSoLinger(boolean on, int linger) throws SocketException {
213         getSocketMetadata().setSoLinger(on, linger);
214     }
215     public void setTcpNoDelay(boolean on) throws SocketException {
216         getSocketMetadata().setTcpNoDelay(on);
217     }
218     public void setTrafficClass(int tc) throws SocketException {
219         getSocketMetadata().setTrafficClass(tc);
220     }
221 }