jdk/src/demo/solaris/jni/Poller/SimpleServer.java
author lana
Wed, 23 Nov 2016 16:16:33 +0000
changeset 42234 29a0c1a888f2
parent 25859 3317bb8137f4
permissions -rw-r--r--
Added tag jdk-9+146 for changeset d139f0e91e7e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.Byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Simple Java "server" using a single thread to handle each connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class SimpleServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  private final static int BYTESPEROP= PollingServer.BYTESPEROP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  private final static int PORTNUM   = PollingServer.PORTNUM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  private final static int MAXCONN   = PollingServer.MAXCONN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
   * This synchronization object protects access to certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
   * data (bytesRead,eventsToProcess) by concurrent Consumer threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  private final static Object eventSync = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  private static InputStream[] instr = new InputStream[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  private static int bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  private static int bytesToRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  public SimpleServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    Socket[] sockArr = new Socket[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    long timestart, timestop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    int bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    int totalConn=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    System.out.println ("Serv: Initializing port " + PORTNUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
      ServerSocket skMain = new ServerSocket (PORTNUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
      bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
      Socket ctrlSock = skMain.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
      BufferedReader ctrlReader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        new BufferedReader(new InputStreamReader(ctrlSock.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
      String ctrlString = ctrlReader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
      bytesToRead = Integer.valueOf(ctrlString).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
      ctrlString = ctrlReader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
      totalConn = Integer.valueOf(ctrlString).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      System.out.println("Receiving " + bytesToRead + " bytes from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                         totalConn + " client connections");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      timestart = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
       * Take connections, spawn off connection handling threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      ConnHandler[] connHA = new ConnHandler[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      int conn = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      while ( conn < totalConn ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
          Socket sock = skMain.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
          connHA[conn] = new ConnHandler(sock.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
          connHA[conn].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
          conn++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      while ( bytesRead < bytesToRead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
          java.lang.Thread.sleep(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      timestop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      System.out.println("Time for all reads (" + totalConn +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                         " sockets) : " + (timestop-timestart));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      // Tell the client it can now go away
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
      byte[] buff = new byte[BYTESPEROP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
      ctrlSock.getOutputStream().write(buff,0,BYTESPEROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    } catch (Exception exc) { exc.printStackTrace(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
   * main ... just create invoke the SimpleServer constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
  public static void main (String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    SimpleServer server = new SimpleServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
   * Connection Handler inner class...one of these per client connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
  class ConnHandler extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private InputStream instr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public ConnHandler(InputStream inputStr) { instr = inputStr; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        byte[] buff = new byte[BYTESPEROP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        while ( bytesRead < bytesToRead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
          bytes = instr.read (buff, 0, BYTESPEROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
          if (bytes > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            synchronized(eventSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
              bytesRead += bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             * Any real server would do some synchronized and some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
             * unsynchronized work on behalf of the client, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
             * most likely send some data back...but this is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
             * gross oversimplification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
          else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (bytesRead < bytesToRead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
              System.out.println("instr.read returned : " + bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      catch (Exception e) {e.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}