Asynchron.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Asynchron.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA;
  32. import org.omg.CORBA.Request;
  33. import org.omg.CORBA.WrongTransaction;
  34. import java.util.Iterator;
  35. import java.util.LinkedList;
  36. /**
  37. * Handles the asynchronous dynamic invocations.
  38. *
  39. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  40. */
  41. public class Asynchron
  42. {
  43. LinkedList sent = new LinkedList();
  44. /**
  45. * Send multiple prepared requests one way, do not caring about the answer.
  46. * The messages, containing requests, will be marked, indicating that
  47. * the sender is not expecting to get a reply.
  48. *
  49. * @param requests the prepared array of requests.
  50. *
  51. * @see Request#send_oneway()
  52. */
  53. public void send_multiple_requests_oneway(Request[] requests)
  54. {
  55. for (int i = 0; i < requests.length; i++)
  56. {
  57. requests [ i ].send_oneway();
  58. }
  59. }
  60. /**
  61. * Send multiple prepared requests expecting to get a reply. All requests
  62. * are send in parallel, each in its own separate thread. When the
  63. * reply arrives, it is stored in the agreed fields of the corresponing
  64. * request data structure. If this method is called repeatedly,
  65. * the new requests are added to the set of the currently sent requests,
  66. * but the old set is not discarded.
  67. *
  68. * @param requests the prepared array of requests.
  69. *
  70. * @see #poll_next_response()
  71. * @see #get_next_response()
  72. * @see Request#send_deferred()
  73. */
  74. public void send_multiple_requests_deferred(Request[] requests)
  75. {
  76. synchronized (sent)
  77. {
  78. for (int i = 0; i < requests.length; i++)
  79. {
  80. sent.add(requests [ i ]);
  81. // TODO Reuse threads that are instantiated in the method below,
  82. // one thread per call.
  83. requests [ i ].send_deferred();
  84. }
  85. }
  86. }
  87. /**
  88. * Find if any of the requests that have been previously sent with
  89. * {@link #send_multiple_requests_deferred}, have a response yet.
  90. *
  91. * @return true if there is at least one response to the previously
  92. * sent request, false otherwise.
  93. */
  94. public boolean poll_next_response()
  95. {
  96. synchronized (sent)
  97. {
  98. Iterator iter = sent.iterator();
  99. Request r;
  100. while (iter.hasNext())
  101. {
  102. r = (Request) iter.next();
  103. if (r.poll_response())
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. /**
  110. * Get the next instance with a response being received. If all currently
  111. * sent responses not yet processed, this method pauses till at least one of
  112. * them is complete. If there are no requests currently sent, the method
  113. * pauses till some request is submitted and the response is received.
  114. * This strategy is identical to the one accepted by Suns 1.4 ORB
  115. * implementation.
  116. *
  117. * The returned response is removed from the list of the currently
  118. * submitted responses and is never returned again.
  119. *
  120. * @return the previously sent request that now contains the received
  121. * response.
  122. *
  123. * @throws WrongTransaction If the method was called from the transaction
  124. * scope different than the one, used to send the request. The exception
  125. * can be raised only if the request is implicitly associated with some
  126. * particular transaction.
  127. */
  128. public Request get_next_response()
  129. throws WrongTransaction
  130. {
  131. // The hard-coded waiting times for the incremental waiter.
  132. // TODO it is possible to write more tricky system where the
  133. // requests notify the Asynchron when they are complete.
  134. // Wait for 5 ms intially.
  135. int wait = 8;
  136. // Double the waiting time
  137. int INC = 2;
  138. // Do not increase if the waiting time is already over 500 ms.
  139. int MAX = 500;
  140. while (true)
  141. {
  142. synchronized (sent)
  143. {
  144. Iterator iter = sent.iterator();
  145. Request r;
  146. while (iter.hasNext())
  147. {
  148. r = (Request) iter.next();
  149. if (r.poll_response())
  150. {
  151. sent.remove(r);
  152. return r;
  153. }
  154. }
  155. }
  156. try
  157. {
  158. Thread.sleep(wait);
  159. if (wait < MAX)
  160. wait = wait * INC;
  161. }
  162. catch (InterruptedException ex)
  163. {
  164. }
  165. }
  166. }
  167. }