org/objectweb/proactive/calcium/Facade.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.calcium;
00032 
00033 import java.io.Serializable;
00034 import java.util.Hashtable;
00035 import java.util.Vector;
00036 
00037 import org.objectweb.proactive.ProActive;
00038 import org.objectweb.proactive.calcium.Skernel;
00039 import org.objectweb.proactive.calcium.exceptions.PanicException;
00040 
00050 public class Facade {
00051 
00052         private Skernel skernel;
00053         private TaskStreamQueue results;
00054         
00055         public Facade(Skernel skernel){
00056                 this.skernel=skernel;
00057                 this.results = new TaskStreamQueue();
00058         }
00059         
00060         public synchronized void putTask(Task<?> task){ 
00061                 skernel.putTask(task);
00062         }
00063         
00064         public void boot(Skernel skernel){
00065                 this.skernel=skernel;
00066         }
00067         
00068         @SuppressWarnings("unchecked")
00069         public synchronized <T> Task<T> getResult(int streamId) throws PanicException{
00070                         
00071                 //Get new tasks from the skernel
00072                 try {
00073                         while(results.isEmpty(streamId)){
00074                                 wait(1000);
00075                                 loadResultsFromSkernel();
00076                         }
00077                 } catch (InterruptedException e) {               
00078                         e.printStackTrace();
00079                         return null;
00080                 }
00081                 
00082                 return (Task<T>)results.get(streamId);
00083         }
00084         
00085         private synchronized void loadResultsFromSkernel() throws PanicException{
00086                 while(skernel.hasResults()){
00087                         Task<?> taskResult =  (Task<?>) skernel.getResult();
00088 
00089                         //TODO Temporary ProActive generics bug workaround 
00090                         //This is the supelec trick
00091                         taskResult=(Task<?>)ProActive.getFutureValue(taskResult);
00092                         results.put(taskResult);
00093                 }
00094         }
00095 
00103         class TaskStreamQueue implements Serializable{
00104                 
00105                 Hashtable<Integer, Vector<Task<?>>> results;
00106                 int size;
00107                 
00108                 public TaskStreamQueue(){
00109                         results = new Hashtable<Integer, Vector<Task<?>>>();
00110                         size=0;
00111                 }
00112                 
00118                 public synchronized void put(Task<?> task){
00119                         int streamId=task.getStreamId();
00120 
00121                         if(!results.containsKey(streamId)){
00122                                 results.put(streamId, new Vector<Task<?>>());
00123                         }
00124                         
00125                         Vector<Task<?>> vector=results.get(streamId);
00126                         
00127                         vector.add(task);
00128                         size++;
00129                 }
00130                 
00138                 public synchronized Task<?> get(int streamId){
00139                         if(isEmpty(streamId)) return null;
00140                          
00141                         Vector<Task<?>> vector=results.get(streamId);
00142                         if(vector.size()==0) return null;
00143                         if(vector.size()==1) results.remove(streamId);
00144                         size--;
00145                         return vector.remove(0);
00146                 }
00147                 
00154                 public synchronized boolean isEmpty(int streamId){
00155                         return !results.containsKey(streamId);
00156                 }
00157                 
00161                 public synchronized int size(){
00162                         return size;
00163                 }
00164                 
00170                 public synchronized int size(int streamId){
00171                         if(isEmpty(streamId)) return 0;
00172                         Vector<Task<?>> vector=results.get(streamId);
00173                         return vector.size();
00174                 }
00175         }
00176 }

Generated on Mon Jan 22 15:16:05 2007 for ProActive by  doxygen 1.5.1