Source code: alice/infrastructure/Tucson.java
1 /*
2 * TuCSoN coordination infrastructure - Copyright (C) 2001 deis.unibo.it
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package alice.infrastructure;
19 import alice.logictuple.*;
20 import alice.tucson.*;
21 import alice.tucson.runtime.Node;
22 import alice.tucson.User;
23 import java.util.*;
24
25 /**
26 * Defines the static service providing directly
27 * access to TuCSoN alice.infrastructure.
28 * <p>
29 * It provides the basic TuCSoN interface, which reflects
30 * the coordination language of the model.
31 *
32 * @see alice.logictuple.LogicTuple
33 *
34 * @author <a href="mailto:aricci@deis.unibo.it">Alessandro Ricci</a>
35 * @version 1.0
36 */
37 public class Tucson {
38
39 /** TuCSoN infrastructure default port */
40 public static final int DEFAULT_PORT = 20504;
41
42 /** TuCSoN infrastructure version */
43 public static final String VERSION = "1.0.0";
44
45 /**
46 * Places (synchronously) a tuples on the specified tuple centre
47 *
48 * @param tid the identifier of the target tuple centre
49 * @param t the logic tuple to be placed
50 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
51 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
52 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
53 */
54 public static void out(TupleCentreId id, LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
55 try {
56 User user=resolveUser();
57 user.out(id,t);
58 } catch (TucsonIOException ex){
59 users=new HashMap();
60 throw new TucsonIOException();
61 }
62 }
63
64 /**
65 * Places (asynchronously) a tuples on the specified tuple centre
66 *
67 * @param tid the identifier of the target tuple centre
68 * @param t the logic tuple to be placed
69 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
70 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
71 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
72 */
73 public static void outAsync(TupleCentreId id, LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
74 try {
75 User user=resolveUser();
76 user.outAsync(id,t);
77 } catch (TucsonIOException ex){
78 users=new HashMap();
79 throw new TucsonIOException();
80 }
81 }
82
83 /**
84 * Gets (blocking) a matching logic tuples from the specified tuple centre
85 *
86 * @param tid the identifier of the target tuple centre
87 * @param t the logic tuple specifying the template used for the matching
88 * @return the logic tuple retrieved from the tuple centre
89 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
90 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
91 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
92 */
93 public static LogicTuple in(TupleCentreId id, LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
94 try {
95 User user=resolveUser();
96 return user.in(id,t);
97 } catch (TucsonIOException ex){
98 users=new HashMap();
99 throw new TucsonIOException();
100 }
101 }
102
103 /**
104 * Reads (blocking) a matching logic tuples from the specified tuple centre
105 *
106 * @param tid the identifier of the target tuple centre
107 * @param t the logic tuple specifying the template used for the matching
108 * @return the logic tuple matching the request
109 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
110 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
111 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
112 */
113 public static LogicTuple rd(TupleCentreId id, LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
114 try {
115 User user=resolveUser();
116 return user.rd(id,t);
117 } catch (TucsonIOException ex){
118 users=new HashMap();
119 throw new TucsonIOException();
120 }
121 }
122
123 /**
124 * Gets if present (without blocking) a matching logic tuples from the specified tuple centre
125 *
126 * @param tid the identifier of the target tuple centre
127 * @param t the logic tuple specifying the template used for the matching
128 * @return the logic tuple retrieved from the tuple centre
129 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
130 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
131 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
132 */
133 public static LogicTuple inp(TupleCentreId id, LogicTuple t)throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
134 try {
135 User user=resolveUser();
136 return user.inp(id,t);
137 } catch (TucsonIOException ex){
138 users=new HashMap();
139 throw new TucsonIOException();
140 }
141 }
142
143 /**
144 * Reads if present (without blocking) a matching logic tuples from the specified tuple centre
145 *
146 * @param tid the identifier of the target tuple centre
147 * @param t the logic tuple specifying the template used for the matching
148 * @return the logic tuple matching the request
149 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
150 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
151 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
152 */
153 public static LogicTuple rdp(TupleCentreId id, LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
154 try {
155 User user=resolveUser();
156 return user.rdp(id,t);
157 } catch (TucsonIOException ex){
158 users=new HashMap();
159 throw new TucsonIOException();
160 }
161 }
162
163 /**
164 * Sets the behaviour specification of a specified tuple centre
165 *
166 * @param tid the identifier of the target tuple centre
167 * @param t the logic tuple specifying the behaviour
168 * @return the logic tuple reflecting the behaviour just set
169 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
170 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
171 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
172 */
173 public static LogicTuple setSpec(TupleCentreId id, LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException{
174 try {
175 User user=resolveUser();
176 return user.setSpec(id,t);
177 } catch (TucsonIOException ex){
178 users=new HashMap();
179 throw new TucsonIOException();
180 }
181 }
182
183 /**
184 * Gets the behaviour specification of a specified tuple centre
185 *
186 * @param tid the identifier of the target tuple centre
187 * @return the logic tuple representing the behaviour of the tuple centre
188 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
189 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
190 */
191 public static LogicTuple getSpec(TupleCentreId id) throws NoTucsonNodeException, TucsonIOException {
192 try {
193 User user=resolveUser();
194 return user.getSpec(id);
195 } catch (TucsonIOException ex){
196 users=new HashMap();
197 throw new TucsonIOException();
198 }
199 }
200
201
202 /**
203 * Places (synchronously) a tuples on the default tuple centre
204 *
205 * @param t the logic tuple to be placed
206 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
207 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
208 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
209 */
210 public static void out(LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
211 try {
212 User user=resolveUser();
213 user.out(t);
214 } catch (TucsonIOException ex){
215 users=new HashMap();
216 throw new TucsonIOException();
217 }
218 }
219
220 /**
221 * Places (asynchronously) a tuples on the default tuple centre
222 *
223 * @param t the logic tuple to be placed
224 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
225 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
226 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
227 */
228 public static void outAsync(LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
229 try {
230 User user=resolveUser();
231 user.outAsync(t);
232 } catch (TucsonIOException ex){
233 users=new HashMap();
234 throw new TucsonIOException();
235 }
236 }
237
238 /**
239 * Gets (blocking) a matching logic tuples from the default tuple centre
240 *
241 * @param t the logic tuple specifying the template used for the matching
242 * @return the logic tuple retrieved from the tuple centre
243 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
244 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
245 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
246 */
247 public static LogicTuple in(LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
248 try {
249 User user=resolveUser();
250 return user.in(t);
251 } catch (TucsonIOException ex){
252 users=new HashMap();
253 throw new TucsonIOException();
254 }
255 }
256
257 /**
258 * Reads (blocking) a matching logic tuples from the default tuple centre
259 *
260 * @param t the logic tuple specifying the template used for the matching
261 * @return the logic tuple matching the request
262 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
263 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
264 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
265 */
266 public static LogicTuple rd(LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
267 try {
268 User user=resolveUser();
269 return user.rd(t);
270 } catch (TucsonIOException ex){
271 users=new HashMap();
272 throw new TucsonIOException();
273 }
274 }
275
276 /**
277 * Gets if present (without blocking) a matching logic tuples from the default tuple centre
278 *
279 * @param t the logic tuple specifying the template used for the matching
280 * @return the logic tuple retrieved from the tuple centre
281 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
282 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
283 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
284 */
285 public static LogicTuple inp(LogicTuple t)throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
286 try {
287 User user=resolveUser();
288 return user.inp(t);
289 } catch (TucsonIOException ex){
290 users=new HashMap();
291 throw new TucsonIOException();
292 }
293 }
294
295 /**
296 * Reads if present (without blocking) a matching logic tuples from the default tuple centre
297 *
298 * @param t the logic tuple specifying the template used for the matching
299 * @return the logic tuple matching the request
300 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
301 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
302 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
303 */
304 public static LogicTuple rdp(LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException {
305 try {
306 User user=resolveUser();
307 return user.rdp(t);
308 } catch (TucsonIOException ex){
309 users=new HashMap();
310 throw new TucsonIOException();
311 }
312 }
313
314 /**
315 * Reads if present (without blocking) a matching logic tuples from the default tuple centre
316 *
317 * @param t the logic tuple specifying the template used for the matching
318 * @return the logic tuple matching the request
319 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
320 * @exception alice.tucson.InvalidLogicTupleException if the specified logic tuple is not valid
321 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
322 */
323 public static LogicTuple setSpec(LogicTuple t) throws NoTucsonNodeException, TucsonIOException, InvalidLogicTupleException{
324 try {
325 User user=resolveUser();
326 return user.setSpec(t);
327 } catch (TucsonIOException ex){
328 users=new HashMap();
329 throw new TucsonIOException();
330 }
331 }
332
333 /**
334 * Gets the behaviour specification of the default tuple centre
335 *
336 * @param tid the identifier of the target tuple centre
337 * @return the logic tuple representing the behaviour of the tuple centre
338 * @exception alice.tucson.NoTucsonNodeException if the target tuple centre is not reachable
339 * @exception alice.tucson.TucsonIOException if some I/O generic errors happened
340 */
341 public static LogicTuple getSpec() throws NoTucsonNodeException, TucsonIOException {
342 try {
343 User user=resolveUser();
344 return user.getSpec();
345 } catch (TucsonIOException ex){
346 users=new HashMap();
347 throw new TucsonIOException();
348 }
349 }
350
351 /**
352 * Gets user info invoking the service.
353 * <p>
354 * if the user is not present, then create a new user.
355 *
356 * @return the user object
357 */
358 private static synchronized User resolveUser(){
359 User user=(User)users.get(Thread.currentThread());
360 if (user==null){
361 long timeId=System.currentTimeMillis();
362 String name=Thread.currentThread().getName();
363 user=new User(new AgentId(name+"_"+timeId));
364 users.put(Thread.currentThread(),user);
365 }
366 return user;
367 }
368
369 /** keeps track of users using the infrastructure */
370 private static HashMap users=new HashMap();
371 }
372