1. /*
  2. * @(#)ListenerThread.java 1.40 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.iiop;
  8. import java.net.InetAddress;
  9. import java.net.Socket;
  10. import java.net.SocketException;
  11. import java.net.ServerSocket;
  12. import java.io.IOException;
  13. public class ListenerThread extends Thread
  14. {
  15. protected static final int MAX_CLEANUP_RETRIES=5;
  16. protected ServerSocket serverSocket;
  17. protected ConnectionTable connectionTable;
  18. protected String socketType;
  19. private boolean keepRunning = true;
  20. ListenerThread(ConnectionTable connectionTable,
  21. ThreadGroup g,
  22. ServerSocket serverSocket,
  23. String socketType)
  24. {
  25. super(g, "JavaIDL Listener");
  26. this.serverSocket = serverSocket;
  27. this.connectionTable = connectionTable;
  28. this.socketType = socketType;
  29. }
  30. public ListenerThread(ConnectionTable connectionTable,
  31. ServerSocket serverSocket,
  32. String socketType)
  33. {
  34. super("JavaIDL Listener");
  35. this.serverSocket = serverSocket;
  36. this.connectionTable = connectionTable;
  37. this.socketType = socketType;
  38. }
  39. public ServerSocket getSocket()
  40. {
  41. return serverSocket;
  42. }
  43. public void run()
  44. {
  45. int tries = 0;
  46. // Loop forever listening for incoming connections.
  47. while (keepRunning) {
  48. try {
  49. // Accept an incoming connection request
  50. Socket socket = serverSocket.accept();
  51. // set socket to disable Nagle's algorithm (always send immediately)
  52. try {
  53. socket.setTcpNoDelay(true);
  54. } catch (Exception e) {
  55. }
  56. // Create a Connection instance and process incoming request
  57. Connection conn = connectionTable.getConnection(socket,
  58. socketType);
  59. tries = 0;
  60. // Check if #connections > HighWaterMark, if so cleanup.
  61. // This prevents incoming connections from being rejected
  62. // because of TCP/IP or OS network connection limits.
  63. connectionTable.checkConnectionTable();
  64. }
  65. catch (SocketException ex) {
  66. // An exception was thrown by the accept() call.
  67. // This probably means that we have run out of network
  68. // connections.
  69. if (tries == MAX_CLEANUP_RETRIES) {
  70. // We tried closing existing connections but that didnt help
  71. // So it must be some unknown networking problem.
  72. continue;
  73. }
  74. // Clean up some existing connections.
  75. if (!connectionTable.cleanUp()) {
  76. // Could not close any connections :-(
  77. continue;
  78. }
  79. tries++;
  80. }
  81. catch (Exception ex) { }
  82. }
  83. }
  84. synchronized void shutdown() {
  85. keepRunning = false;
  86. try {
  87. serverSocket.close();
  88. } catch (IOException ioex) {}
  89. }
  90. }