jdk/src/sample/share/nio/server/README.txt
author emc
Wed, 05 Nov 2014 08:37:04 -0500
changeset 27386 784414cffd9a
parent 25859 3317bb8137f4
child 39878 11c674ba1512
permissions -rw-r--r--
8035259: javac, incorrect shadowing of classes vs type parameters Summary: Cause javac to look at type variables first when resolving names in an extends/implements list Reviewed-by: mcimadamore, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
        A Simple NIO-based HTTP/HTTPS Server Example
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
INTRODUCTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
============
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
This directory contains a simple HTTP/HTTPS server.  HTTP/HTTPS are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
common network protocols that provide for data transfer, and are more
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
fully described in RFC 2616 and RFC 2818 (Available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
http://www.ietf.org ). HTTPS is essentially HTTP after the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
has been secured with SSL/TLS.  TLS is the successor to SSL, and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
described in RFC 2246.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
This server was written to demonstrate some of the functionality new to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
the Java 2 platform.  The demo is not meant to be a full tutorial, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
assumes the reader has some familiarity with the subject matter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
In particular, it shows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
    New I/O (java.nio, java.nio.channels, java.util.regex, java.nio.charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
        Introduced in version 1.4 of the platform, NIO was designed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
        overcome some of the scalability limitations found in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
        existing blocking java.net.* API's, and to address other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
        concepts such as Regular Expression parsing and Character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
        Sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
        This server demonstrates:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
            ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
            Blocking and Non-Blocking I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
            SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
            ServerSocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
            Selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
            CharacterSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
            Pattern matching using Regular Expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    JSSE (javax.net.ssl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
	Introduced in version 1.4 of the platform, JSSE provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
	network security using SSL/TLS for java.net.Socket-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
	traffic.  In version 1.5, the SSLEngine API was introduced
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
	which separates the SSL/TLS functionality from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
	I/O model.  By making this separation, applications can adapt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
	I/O and compute strategies to best fit their circumstances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        This server demonstrates:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            Using SSLEngine to create a HTTPS server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
	    Creating simple key material for use with HTTPS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    Concurrency Library (java.util.concurrent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Introduced in version 1.5 of the platform, the concurrency
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        library provides a mechanism which decouples task submission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        from the mechanics of how each task will be run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        This server demonstrates:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            A ThreadPool with a fixed number of threads, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            based on the number of available processors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
SETUP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
=====
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
The server must be built on version 1.5 (or later) of the platform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
Invoking the following should be sufficient:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    % mkdir build
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    % javac -source 1.5 -target 1.5 -d build *.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
The following creates the document root:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    % mkdir root
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
All documents should be placed in this directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
For HTTPS, the server authenticates itself to clients by using simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
Public Key Infrastructure (PKI) credentials in the form of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
X509Certificates.  You must create the server's credentials before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
attempting to run the server in "-secure" mode.  The server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
currently hardcoded to look for its credentials in a file called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
"testkeys".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
In this example, we'll create credentials for a fictional widget web
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
site owned by the ubiquitous "Xyzzy, Inc.".  When you run this in your
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
own environment, replace "widgets.xyzzy.com" with the hostname of your
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
The easiest way to create the SSL/TLS credentials is to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
java keytool, by doing the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        (<CR> represents your end-of-line key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    % keytool -genkey -keyalg rsa -keystore testkeys -alias widgets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    Enter keystore password:  passphrase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    What is your first and last name?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    [Unknown]:  widgets.xyzzy.com<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    What is the name of your organizational unit?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    [Unknown]:  Consumer Widgets Group<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    What is the name of your organization?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    [Unknown]:  Xyzzy, Inc.<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    What is the name of your City or Locality?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    [Unknown]:  Arcata<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    What is the name of your State or Province?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    [Unknown]:  CA<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    What is the two-letter country code for this unit?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    [Unknown]:  US<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    Is CN=widgets.xyzzy.com, OU=Consumer Widgets Group, O="Xyzzy, Inc.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    L=Arcata, ST=CA, C=US correct?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    [no]:  yes<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    Enter key password for <mykey>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    (RETURN if same as keystore password):  <CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
This directory also contain a very simple URL reader (URLDumper), which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
connects to a specified URL and places all output into a specified file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
SERVER EXECUTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    % java -classpath build Server N1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    Usage:  Server <type> [options]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                B1      Blocking/Single-threaded Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                BN      Blocking/Multi-threaded Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                BP      Blocking/Pooled-thread Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                N1      Nonblocking/Single-threaded Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                N2      Nonblocking/Dual-threaded Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        options:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                -port port                port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    default:  8000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                -backlog backlog          backlog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    default:  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                -secure                   encrypt with SSL/TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
		    default is insecure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
"http://" URLs should be used with insecure mode, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
"https://" for secure mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
The "B*" servers use classic blocking I/O:  in other words, calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
read()/write() will not return until the I/O operation has completed.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
"N*" servers use non-blocking mode and Selectors to determine which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
Channels are ready to perform I/O.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
B1:	A single-threaded server which completely services each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
	connection before moving to the next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
B2:	A multi-threaded server which creates a new thread for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
	connection.  This is not efficient for large numbers of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
	connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
BP:	A multi-threaded server which creates a pool of threads for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
	by the server.  The Thread pool decides how to schedule those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
	threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
N1:	A single-threaded server.  All accept() and read()/write()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
	operations are performed by a single thread, but only after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
	being selected for those operations by a Selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
N2:	A dual-threaded server which performs accept()s in one thread, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
	services requests in a second.  Both threads use select().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
CLIENT EXECUTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
You can test the server using any standard browser such as Internet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
Explorer or Mozilla, but since the browser will not trust the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
credentials you just created, you may need to accept the credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
via the browser's pop-up dialog box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
Alternatively, to use the certificates using the simple included JSSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
client URLDumper, export the server certificate into a new truststore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
and then run the application using the new truststore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    % keytool -export -keystore testkeys -alias widgets -file widgets.cer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    Enter keystore password:  passphrase<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    Certificate stored in file <widgets.cer>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    % keytool -import -keystore trustCerts -alias widgetServer \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            -file widgets.cer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    Enter keystore password:  passphrase<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    Owner: CN=widgets.xyzzy.com, OU=Consumer, O="xyzzy, inc.", L=Arcata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    ST=CA, C=US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    Issuer: CN=widgets.xyzzy.com, OU=Consumer, O="xyzzy, inc.",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    L=Arcata, ST=CA, C=US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    Serial number: 4086cc7a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    Valid from: Wed Apr 21 12:33:14 PDT 2004 until: Tue Jul 20 12:33:14
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    PDT 2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    Certificate fingerprints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        MD5:  39:71:42:CD:BF:0D:A9:8C:FB:8B:4A:CD:F8:6D:19:1F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        SHA1: 69:5D:38:E9:F4:6C:E5:A7:4C:EA:45:8E:FB:3E:F3:9A:84:01:6F:22
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    Trust this certificate? [no]:  yes<CR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    Certificate was added to keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    % java -classpath build -Djavax.net.ssl.trustStore=trustCerts \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        -Djavax.net.ssl.TrustStorePassword=passphrase \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        URLDumper https://widgets.xyzzy.com:8000/ outputFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
NOTE:  The server must be run with "-secure" in order to receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
"https://" URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
WARNING:  This is just a simple example for code exposition, you should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
spend more time understanding PKI security concerns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
SOURCE CODE OVERVIEW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
====================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
The main class is Server, which handles program startup, and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
subclassed by the "B*" and "N*" server classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
Following a successful accept(), the "B*" variants each create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
RequestServicer object to perform the actual request/reply operations.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
primary differences between the different "B*" servers is how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
RequestServicer is actually run:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    B1:	RequestServicer.run() is directly called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    BN:	A new thread is started, and the thread calls RequestServicer.run().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    BP:	A ThreadPool is created, and the pool framework is given Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
	tasks to complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
In the "N*" variations, a Dispatcher object is created, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
responsible for performing the select, and then issuing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
corresponding handler:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    N1:	A single thread is used for all accept()/read()/write() operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    N2:	Similar to N1, but a separate thread is used for the accept()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
	operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
In all cases, once the connection has been accepted, a ChannelIO object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
is created to handle all I/O.  In the insecure case, the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
SocketChannel methods are directly called.  However in the secure case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
more manipulations are needed to first secure the channel, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
encrypt/decrypt the data, and finally properly send any shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
messages.  ChannelIOSecure extends ChannelIO, and provides the secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
variants of the corresponding ChannelIO calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
RequestServicer and RequestHandler are the main drivers for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
blocking and non-blocking variants, respectively.  They are responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    Performing any initial handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    Reading the request data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        All data is stored in a local buffer in the ChannelIO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    Parsing the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        The request data is obtained from the ChannelIO object, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        is processed by Request class, which represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        parsed URI address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    Locating/preparing/sending the data or reporting error conditions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        A Reply object is created which represents the entire object to send,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        including the HTTP/HTTPS headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    Shutdown/closing the channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
CLOSING THOUGHTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
This example represents a simple server: it is not production quality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
It was primarily meant to demonstrate the new APIs in versions 1.4 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
1.5 of the platform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
This example could certainly be expanded to address other areas of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
concern: for example, assigning multiple threads to handle the selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
Channels, or delegating SSLEngine tasks to multiple threads.  There are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
so many ways to implement compute and I/O strategies, we encourage you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
to experiment and find what works best for your situation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
To steal a phrase from many textbooks:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    "It is left as an exercise for the reader..."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279