1. /*
  2. * @(#)ReaderThreadImpl.java 1.13 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.transport;
  8. import java.io.IOException;
  9. import com.sun.corba.se.pept.transport.Connection;
  10. import com.sun.corba.se.pept.transport.ReaderThread;
  11. import com.sun.corba.se.pept.transport.Selector;
  12. import com.sun.corba.se.spi.orb.ORB;
  13. import com.sun.corba.se.spi.orbutil.threadpool.Work;
  14. import com.sun.corba.se.impl.orbutil.ORBUtility;
  15. public class ReaderThreadImpl
  16. implements
  17. ReaderThread,
  18. Work
  19. {
  20. private ORB orb;
  21. private Connection connection;
  22. private Selector selector;
  23. private boolean keepRunning;
  24. private long enqueueTime;
  25. public ReaderThreadImpl(ORB orb,
  26. Connection connection, Selector selector)
  27. {
  28. this.orb = orb;
  29. this.connection = connection;
  30. this.selector = selector;
  31. keepRunning = true;
  32. }
  33. ////////////////////////////////////////////////////
  34. //
  35. // ReaderThread methods.
  36. //
  37. public Connection getConnection()
  38. {
  39. return connection;
  40. }
  41. public void close()
  42. {
  43. if (orb.transportDebugFlag) {
  44. dprint(".close: " + connection);
  45. }
  46. keepRunning = false;
  47. }
  48. ////////////////////////////////////////////////////
  49. //
  50. // Work methods.
  51. //
  52. // REVISIT - this needs alot more from previous ReaderThread.
  53. public void doWork()
  54. {
  55. try {
  56. if (orb.transportDebugFlag) {
  57. dprint(".doWork: Start ReaderThread: " + connection);
  58. }
  59. while (keepRunning) {
  60. try {
  61. if (orb.transportDebugFlag) {
  62. dprint(".doWork: Start ReaderThread cycle: "
  63. + connection);
  64. }
  65. if (connection.read()) {
  66. // REVISIT - put in pool;
  67. return;
  68. }
  69. if (orb.transportDebugFlag) {
  70. dprint(".doWork: End ReaderThread cycle: "
  71. + connection);
  72. }
  73. } catch (Throwable t) {
  74. if (orb.transportDebugFlag) {
  75. dprint(".doWork: exception in read: " + connection,t);
  76. }
  77. orb.getTransportManager().getSelector(0)
  78. .unregisterForEvent(getConnection().getEventHandler());
  79. getConnection().close();
  80. }
  81. }
  82. } finally {
  83. if (orb.transportDebugFlag) {
  84. dprint(".doWork: Terminated ReaderThread: " + connection);
  85. }
  86. }
  87. }
  88. public void setEnqueueTime(long timeInMillis)
  89. {
  90. enqueueTime = timeInMillis;
  91. }
  92. public long getEnqueueTime()
  93. {
  94. return enqueueTime;
  95. }
  96. public String getName() { return "ReaderThread"; }
  97. ////////////////////////////////////////////////////
  98. //
  99. // Implementation.
  100. //
  101. private void dprint(String msg)
  102. {
  103. ORBUtility.dprint("ReaderThreadImpl", msg);
  104. }
  105. protected void dprint(String msg, Throwable t)
  106. {
  107. dprint(msg);
  108. t.printStackTrace(System.out);
  109. }
  110. }
  111. // End of file.