summaryrefslogtreecommitdiffstats
path: root/simple/simple-transport/src/main/java/org/simpleframework/transport/SocketTransport.java
blob: b0ba04a5f090d62e1999a3b30c1ae497a91271c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * SocketTransport.java February 2007
 *
 * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package org.simpleframework.transport;

import static org.simpleframework.transport.TransportEvent.READ;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Map;

import javax.net.ssl.SSLEngine;

import org.simpleframework.transport.reactor.Reactor;
import org.simpleframework.transport.trace.Trace;

/**
 * The <code>SocketTransport</code> object offers a transport that can
 * send and receive bytes in a non-blocking manner. The contract of
 * the <code>Transport</code> is that it must either write the data
 * it is asked to write or it must queue that data for delivery. For
 * the vast majority of cases data is written directly to the socket
 * without any need for queuing or selection for write ready events.
 * <p>
 * In the event that the client TCP window is full and writing would
 * block this makes use of a queue of buffers which can be used to 
 * append data to. The buffers are lazily instantiated so the memory
 * required is created only in the rare case that they are needed.
 * Once a buffer is full it is queued to an asynchronous thread where
 * the buffer queue is drained and sent to the client when the TCP
 * window of the client is capable of accepting it.
 * <p>
 * In order to improve the network performance of this transport the
 * default packet size sent to the TCP stack is four kilobytes. This
 * ensures that the fragments of response delivered to the TCP layer
 * are sufficiently large for optimum network performance.
 *
 * @author Niall Gallagher
 */
public class SocketTransport implements Transport {
   
   /**
    * This is the writer that is used to flush the buffer queue.
    */
   private SocketBufferWriter writer;

   /**
    * This is the underlying byte channel used to send the  data.
    */         
   private SocketChannel channel;    
   
   /**
    * This is the socket that this transport is representing.
    */
   private Socket socket;
   
   /**
    * This is the trace used to monitor all transport events.
    */
   private Trace trace;
  
   /**
    * This is used to determine if the transport has been closed.
    */
   private boolean closed;
   
   /**
    * Constructor for the <code>SocketTransport</code> object. This 
    * requires a reactor to perform asynchronous writes and also the
    * pipeline which is used to read and write data. This transport
    * will use a queue of buffers which are lazily initialized so as
    * to only allocate the memory on demand.
    *
    * @param socket this is used to read and write the data
    * @param reactor this is used to perform asynchronous writes
    */
   public SocketTransport(Socket socket, Reactor reactor) throws IOException {
      this(socket, reactor, 4096);
   }

   /**
    * Constructor for the <code>SocketTransport</code> object. This 
    * requires a reactor to perform asynchronous writes and also the
    * pipeline which is used to read and write data. This transport
    * will use a queue of buffers which are lazily initialized so as
    * to only allocate the memory on demand.
    *
    * @param socket this is used to read and write the data
    * @param reactor this is used to perform asynchronous writes
    * @param buffer this is the size of the output buffer to use 
    */
   public SocketTransport(Socket socket, Reactor reactor, int buffer) throws IOException {
      this(socket, reactor, buffer, 20480);
   }

   /**
    * Constructor for the <code>SocketTransport</code> object. This 
    * requires a reactor to perform asynchronous writes and also the
    * pipeline which is used to read and write data. This transport
    * will use a queue of buffers which are lazily initialized so as
    * to only allocate the memory on demand.
    *
    * @param socket this is used to read and write the data
    * @param reactor this is used to perform asynchronous writes
    * @param buffer this is the size of the output buffer to use      
    * @param threshold this is the maximum size of the output buffer
    */
   public SocketTransport(Socket socket, Reactor reactor, int buffer, int threshold) throws IOException {
     this.writer = new SocketBufferWriter(socket, reactor, buffer, threshold);     
     this.channel = socket.getChannel();
     this.trace = socket.getTrace();
     this.socket = socket;
   }
   
   /**
    * This is used to acquire the SSL certificate used when the
    * server is using a HTTPS connection. For plain text connections
    * or connections that use a security mechanism other than SSL
    * this will be null. This is only available when the connection
    * makes specific use of an SSL engine to secure the connection.
    * 
    * @return this returns the associated SSL certificate if any
    */
   public Certificate getCertificate() {
      return null;
   }
   
   /**
    * This is used to acquire the trace object that is associated
    * with the socket. A trace object is used to collection details
    * on what operations are being performed on the socket. For
    * instance it may contain information relating to I/O events
    * or more application specific events such as errors. 
    * 
    * @return this returns the trace associated with this socket
    */
   public Trace getTrace() {
      return trace;
   }
   
   /**
    * This method is used to get the <code>Map</code> of attributes 
    * by this pipeline. The attributes map is used to maintain details
    * about the connection. Information such as security credentials
    * to client details can be placed within the attribute map.
    *
    * @return this returns the map of attributes for this pipeline
    */   
   public Map getAttributes() {     
      return socket.getAttributes();
   }

   /**
    * This is used to acquire the SSL engine used for https. If the
    * pipeline is connected to an SSL transport this returns an SSL
    * engine which can be used to establish the secure connection
    * and send and receive content over that connection. If this is
    * null then the pipeline represents a normal transport. 
    *  
    * @return the SSL engine used to establish a secure transport
    */   
   public SSLEngine getEngine() {     
      return socket.getEngine();
   }

   /**
    * This method is used to acquire the <code>SocketChannel</code>
    * for the connection. This allows the server to acquire the input
    * and output streams with which to communicate. It can also be 
    * used to configure the connection and perform various network 
    * operations that could otherwise not be performed.
    *
    * @return this returns the socket used by this HTTP pipeline
    */      
   public SocketChannel getChannel() {      
      return socket.getChannel();
   }   
   
   /**
    * This is used to perform a non-blocking read on the transport.
    * If there are no bytes available on the input buffers then
    * this method will return zero and the buffer will remain the
    * same. If there is data and the buffer can be filled then this
    * will return the number of bytes read. Finally if the socket
    * is closed this will return a -1 value.
    *
    * @param data this is the buffer to append the bytes to
    *
    * @return this returns the number of bytes that were read 
    */ 
   public int read(ByteBuffer data) throws IOException {
      if(closed) {
         throw new TransportException("Transport is closed");         
      }      
      int count = channel.read(data);
      
      if(trace != null) {
         trace.trace(READ, count);
      }
      return count;
   }
   
   /**
    * This method is used to deliver the provided buffer of bytes to
    * the underlying transport. Depending on the connection type the
    * array may be encoded for SSL transport or send directly. This
    * will buffer the bytes within the internal buffer to ensure 
    * that the response fragments are sufficiently large for the
    * network. Smaller packets result poorer performance.
    *
    * @param data this is the array of bytes to send to the client
    */   
   public  void write(ByteBuffer data) throws IOException{  
      if(closed) {
         throw new TransportException("Transport is closed");
      }    
      writer.write(data);
   }    
   
   /**
    * This is used to flush the internal buffer to the underlying
    * socket. Flushing with this method is always non-blocking, so
    * if the socket is not write ready and the buffer can be queued
    * it will be queued and the calling thread will return.
    */
   public void flush() throws IOException {      
      if(closed) {
         throw new TransportException("Transport is closed");
      }
      writer.flush();
   }
   
   /**
    * This method is used to flush the internal buffer and close
    * the underlying socket. This method will not complete until
    * all buffered data is written and the underlying socket is
    * closed at which point this can be disposed of.
    */
   public void close() throws IOException {
      if(!closed) {              
         writer.flush();
         writer.close();
         closed = true;
      }
   }
}