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