00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 00023 #include "PushFlowControl.hxx" 00024 #include "Processing.hxx" 00025 #include "OutPort.hxx" 00026 #include "InPort.hxx" 00027 #include "Network.hxx" 00028 00029 namespace CLAM 00030 { 00031 00032 PushFlowControl::PushFlowControl() 00033 { 00034 } 00035 00036 void PushFlowControl::ProcessingAddedToNetwork( Processing & added ) 00037 { 00038 NetworkTopologyChanged(); 00039 00040 if (added.GetInPorts().Size() == 0) // if it's a generator 00041 mGenerators.push_back( &added ); 00042 } 00043 00044 void PushFlowControl::ProcessingRemovedFromNetwork( Processing & removed ) 00045 { 00046 NetworkTopologyChanged(); 00047 00048 if (removed.GetInPorts().Size() == 0) // if it's a generator 00049 mGenerators.remove( &removed ); 00050 } 00051 00052 void PushFlowControl::Do() 00053 { 00054 std::list< Processing* > toDo(mGenerators); 00055 00056 while (!toDo.empty()) 00057 { 00058 // pop the next processing 00059 Processing * next = *(toDo.begin()); // the first 00060 toDo.pop_front(); 00061 00062 if(next->CanConsumeAndProduce()) 00063 { 00064 next->Do(); 00065 std::cerr << "Consume "<<next->GetClassName() << std::endl; 00066 } 00067 else 00068 { 00069 //std::cerr << "Can't consume "<<next->GetClassName() << std::endl; 00070 } 00071 AddNewPossibleProcessingsToDo(next, toDo); 00072 } 00073 } 00074 00075 void PushFlowControl::AddNewPossibleProcessingsToDo( 00076 Processing * producer, 00077 std::list<Processing*> & toDo ) 00078 { 00079 00080 OutPortRegistry::Iterator itOutPort; 00081 00082 for (itOutPort=producer->GetOutPorts().Begin(); 00083 itOutPort!=producer->GetOutPorts().End(); 00084 itOutPort++) 00085 { 00086 Network::InPortsList consumers; 00087 consumers = mNetwork->GetInPortsConnectedTo( **itOutPort ); 00088 00089 Network::InPortsList::iterator itInPort; 00090 00091 for (itInPort=consumers.begin(); itInPort!=consumers.end(); itInPort++) 00092 { 00093 //ignore orphan inports 00094 if (!(*itInPort)->HasProcessing()) 00095 continue; 00096 00097 Processing * proc = (*itInPort)->GetProcessing(); 00098 00099 if (proc->CanConsumeAndProduce()) 00100 { 00101 //std::cerr <<"\n\t CAN"<<proc->GetClassName(); 00102 toDo.push_back( proc ); 00103 } 00104 } 00105 } 00106 } 00107 00108 } // namespace CLAM 00109 00110