Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / CORBA / Asynchron.java
blob275b57091204b5014d408f0395a01e2be9e32709
1 /* Asynchron.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package gnu.CORBA;
41 import org.omg.CORBA.Request;
42 import org.omg.CORBA.WrongTransaction;
44 import java.util.Iterator;
45 import java.util.LinkedList;
47 /**
48 * Handles the asynchronous dynamic invocations.
50 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
52 public class Asynchron
54 LinkedList sent = new LinkedList();
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.
61 * @param requests the prepared array of requests.
63 * @see Request#send_oneway()
65 public void send_multiple_requests_oneway(Request[] requests)
67 for (int i = 0; i < requests.length; i++)
69 requests [ i ].send_oneway();
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.
81 * @param requests the prepared array of requests.
83 * @see #poll_next_response()
84 * @see #get_next_response()
85 * @see Request#send_deferred()
87 public void send_multiple_requests_deferred(Request[] requests)
89 synchronized (sent)
91 for (int i = 0; i < requests.length; i++)
93 sent.add(requests [ i ]);
95 // TODO Reuse threads that are instantiated in the method below,
96 // one thread per call.
97 requests [ i ].send_deferred();
103 * Find if any of the requests that have been previously sent with
104 * {@link #send_multiple_requests_deferred}, have a response yet.
106 * @return true if there is at least one response to the previously
107 * sent request, false otherwise.
109 public boolean poll_next_response()
111 synchronized (sent)
113 Iterator iter = sent.iterator();
114 Request r;
115 while (iter.hasNext())
117 r = (Request) iter.next();
118 if (r.poll_response())
119 return true;
122 return false;
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.
133 * The returned response is removed from the list of the currently
134 * submitted responses and is never returned again.
136 * @return the previously sent request that now contains the received
137 * response.
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.
144 public Request get_next_response()
145 throws WrongTransaction
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;
153 // Double the waiting time
154 int INC = 2;
156 // Do not increase if the waiting time is already over 500 ms.
157 int MAX = 500;
158 while (true)
160 synchronized (sent)
162 Iterator iter = sent.iterator();
163 Request r;
164 while (iter.hasNext())
166 r = (Request) iter.next();
167 if (r.poll_response())
169 sent.remove(r);
170 return r;
176 Thread.sleep(wait);
177 if (wait < MAX)
178 wait = wait * INC;
180 catch (InterruptedException ex)