PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-tomcat-6.0.35-src/java/org/apache/catalina/tribes/transport/bio/BioReplicationTask.java

#
Java | 166 lines | 89 code | 20 blank | 57 comment | 13 complexity | 7e06661b38dd1ca708d95b80f157972a MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.catalina.tribes.transport.bio;
  18. import org.apache.catalina.tribes.io.ObjectReader;
  19. import org.apache.catalina.tribes.transport.Constants;
  20. import org.apache.catalina.tribes.transport.AbstractRxTask;
  21. import java.net.Socket;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import org.apache.catalina.tribes.io.ListenCallback;
  25. import org.apache.catalina.tribes.ChannelMessage;
  26. import org.apache.catalina.tribes.io.ChannelData;
  27. import org.apache.catalina.tribes.io.BufferPool;
  28. /**
  29. * A worker thread class which can drain channels and echo-back the input. Each
  30. * instance is constructed with a reference to the owning thread pool object.
  31. * When started, the thread loops forever waiting to be awakened to service the
  32. * channel associated with a SelectionKey object. The worker is tasked by
  33. * calling its serviceChannel() method with a SelectionKey object. The
  34. * serviceChannel() method stores the key reference in the thread object then
  35. * calls notify() to wake it up. When the channel has been drained, the worker
  36. * thread returns itself to its parent pool.
  37. *
  38. * @author Filip Hanik
  39. *
  40. * @version $Id: BioReplicationTask.java 939515 2010-04-29 23:59:49Z kkolinko $
  41. */
  42. public class BioReplicationTask extends AbstractRxTask {
  43. protected static org.apache.juli.logging.Log log = org.apache.juli.logging.LogFactory.getLog( BioReplicationTask.class );
  44. protected Socket socket;
  45. protected ObjectReader reader;
  46. public BioReplicationTask (ListenCallback callback) {
  47. super(callback);
  48. }
  49. // loop forever waiting for work to do
  50. public synchronized void run()
  51. {
  52. if ( socket == null ) return;
  53. try {
  54. drainSocket();
  55. } catch ( Exception x ) {
  56. log.error("Unable to service bio socket");
  57. }finally {
  58. try {socket.close();}catch ( Exception ignore){}
  59. try {reader.close();}catch ( Exception ignore){}
  60. reader = null;
  61. socket = null;
  62. }
  63. // done, ready for more, return to pool
  64. if ( getTaskPool() != null ) getTaskPool().returnWorker (this);
  65. }
  66. public synchronized void serviceSocket(Socket socket, ObjectReader reader) {
  67. this.socket = socket;
  68. this.reader = reader;
  69. this.notify(); // awaken the thread
  70. }
  71. protected void execute(ObjectReader reader) throws Exception{
  72. int pkgcnt = reader.count();
  73. if ( pkgcnt > 0 ) {
  74. ChannelMessage[] msgs = reader.execute();
  75. for ( int i=0; i<msgs.length; i++ ) {
  76. /**
  77. * Use send ack here if you want to ack the request to the remote
  78. * server before completing the request
  79. * This is considered an asynchronized request
  80. */
  81. if (ChannelData.sendAckAsync(msgs[i].getOptions())) sendAck(Constants.ACK_COMMAND);
  82. try {
  83. //process the message
  84. getCallback().messageDataReceived(msgs[i]);
  85. /**
  86. * Use send ack here if you want the request to complete on this
  87. * server before sending the ack to the remote server
  88. * This is considered a synchronized request
  89. */
  90. if (ChannelData.sendAckSync(msgs[i].getOptions())) sendAck(Constants.ACK_COMMAND);
  91. }catch ( Exception x ) {
  92. if (ChannelData.sendAckSync(msgs[i].getOptions())) sendAck(Constants.FAIL_ACK_COMMAND);
  93. log.error("Error thrown from messageDataReceived.",x);
  94. }
  95. if ( getUseBufferPool() ) {
  96. BufferPool.getBufferPool().returnBuffer(msgs[i].getMessage());
  97. msgs[i].setMessage(null);
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * The actual code which drains the channel associated with
  104. * the given key. This method assumes the key has been
  105. * modified prior to invocation to turn off selection
  106. * interest in OP_READ. When this method completes it
  107. * re-enables OP_READ and calls wakeup() on the selector
  108. * so the selector will resume watching this channel.
  109. */
  110. protected void drainSocket () throws Exception {
  111. InputStream in = socket.getInputStream();
  112. // loop while data available, channel is non-blocking
  113. byte[] buf = new byte[1024];
  114. int length = in.read(buf);
  115. while ( length >= 0 ) {
  116. int count = reader.append(buf,0,length,true);
  117. if ( count > 0 ) execute(reader);
  118. length = in.read(buf);
  119. }
  120. }
  121. /**
  122. * send a reply-acknowledgement (6,2,3)
  123. * @param key
  124. * @param channel
  125. */
  126. protected void sendAck(byte[] command) {
  127. try {
  128. OutputStream out = socket.getOutputStream();
  129. out.write(command);
  130. out.flush();
  131. if (log.isTraceEnabled()) {
  132. log.trace("ACK sent to " + socket.getPort());
  133. }
  134. } catch ( java.io.IOException x ) {
  135. log.warn("Unable to send ACK back through channel, channel disconnected?: "+x.getMessage());
  136. }
  137. }
  138. public void close() {
  139. setDoRun(false);
  140. try {socket.close();}catch ( Exception ignore){}
  141. try {reader.close();}catch ( Exception ignore){}
  142. reader = null;
  143. socket = null;
  144. super.close();
  145. }
  146. }