org/objectweb/proactive/calcium/Stream.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.util.Hashtable;
00034 import java.util.Vector;
00035 
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.calcium.exceptions.MuscleException;
00038 import org.objectweb.proactive.calcium.exceptions.PanicException;
00039 import org.objectweb.proactive.calcium.interfaces.Instruction;
00040 import org.objectweb.proactive.calcium.interfaces.Skeleton;
00041 import org.objectweb.proactive.calcium.statistics.Stats;
00042 import org.objectweb.proactive.core.util.log.Loggers;
00043 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00044 
00045 
00046 public class Stream<T>{
00047         static Logger logger = ProActiveLogger.getLogger(Loggers.SKELETONS_KERNEL);
00048         
00049         private int streamId;
00050         private Facade facade;
00051         private Skeleton<T> skeleton;
00052         private int pendingTasks;
00053         
00054         private Hashtable<T, Stats> taskStats;
00055         protected Stream(Facade facade,  Skeleton<T> skeleton){
00056                 
00057                 this.streamId=(int)(Math.random()*Integer.MAX_VALUE);
00058                 this.skeleton=skeleton;
00059                 this.facade=facade;
00060                 this.taskStats = new Hashtable<T, Stats>();
00061                 this.pendingTasks=0;
00062         }
00063 
00068         public void input(T param){
00069                 Vector<T> paramV= new Vector<T>(1);
00070                 paramV.add(param);
00071                 input(paramV);
00072         }
00073         
00078         public void input(Vector<T> paramV){
00079                 
00080                 Vector<Instruction<T>> v = (Vector<Instruction<T>>) skeleton.getInstructionStack();
00081 
00082                 //Put the parameters in a Task container
00083                 for(T param:paramV){
00084                         Task<T> task = new Task<T>(param);
00085                         task.setStack(v);
00086                         task.setStreamId(streamId);
00087                         facade.putTask(task); //add them to the ready queue in the kernel
00088                         pendingTasks++;
00089                 }
00090         }
00091         
00103         @SuppressWarnings("unchecked")
00104         public T getResult() throws PanicException, MuscleException{
00105                 if(pendingTasks==0) return null; //waiting for no results
00106         
00107                 Task<T> task =  (Task<T>) facade.getResult(streamId);
00108                 T res= task.getObject();
00109                 
00110                 taskStats.put(res,task.getStats());
00111                 pendingTasks--;
00112                 
00113                 return res;
00114         }
00115 
00122         public Stats getStats(T res) {
00123                 return this.taskStats.get(res);
00124         }
00125 }

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