Network.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2007 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 #ifndef _Network_hxx_
00022 #define _Network_hxx_
00023 
00024 #include "Processing.hxx"
00025 #include "InPort.hxx"
00026 #include "OutPort.hxx"
00027 #include "InControl.hxx"
00028 #include "OutControl.hxx"
00029 #include "Component.hxx"
00030 #include "Text.hxx"
00031 #include <string>
00032 #include <list>
00033 #include <map>
00034 #include <set>
00035 //#include <utility>
00036 
00037 namespace CLAM
00038 {
00039 
00040 class NetworkPlayer;
00041 class FlowControl;
00042 class ControlSink;
00043 class ControlSource;
00044 
00045 class InformationText
00046 {
00047 public:
00048         int x;
00049         int y;
00050         Text text;
00051 };
00052 
00053 
00054 class Network : public Component
00055 {
00056 
00057 public:
00058         // used types
00059         typedef std::list<std::string> NamesList;
00060         typedef std::map <std::string, Processing* > ProcessingsMap;
00061         typedef std::list<InPortBase *> InPortsList;
00062 
00063         typedef std::vector<Processing*> Processings;
00064         typedef std::vector<ControlSource*> ControlSources;
00065         typedef std::vector<ControlSink*> ControlSinks;
00066 
00067         typedef std::vector<InformationText*> InformationTexts;
00068 
00069         typedef struct { int x, y, width, height; } Geometry;
00070         typedef struct { Processing* processing; Geometry geometry; } ProcessingAndGeometry;
00071         typedef std::map <std::string, Geometry> ProcessingsGeometriesMap;
00072         typedef struct { std::string sourceName, sinkName; } Connection;
00073         typedef std::list<Connection> ConnectionsList;
00074         // attributes for canvas copy & paste
00075         typedef std::set<std::string> NamesSet;
00076 public:
00077         // constructor / destructor
00078         Network();
00079         virtual ~Network();
00080 
00081         // Methods related to network itself
00082         const std::string& GetName() const { return _name; }
00083         void SetName( const std::string& name ) { _name=name; }
00084 
00085         const Text& GetDescription() const {return _description;};
00086         void SetDescription( const Text& description ) {_description=description;};
00087 
00088         virtual const char * GetClassName() const
00089         {
00090                 return "Network";
00091         }
00092 
00093         // Methods related with player and flow
00094         bool IsStopped() const;
00095         bool IsPlaying() const;
00096         bool IsPaused() const;
00097         bool IsRealTime() const;
00098         void Start();
00099         void Stop();
00100         void Pause();
00101         void Do();
00103         void AddFlowControl( FlowControl* );
00104         void Clear();
00106         void SetPlayer( NetworkPlayer* player);
00107         unsigned BackendBufferSize();
00108         unsigned BackendSampleRate();
00109 
00111         virtual void StoreOn( Storage & storage) const;
00112         virtual void LoadFrom( Storage & storage);
00113 
00115         bool UpdateSelections (const NamesList & processingsNamesList);
00116         void setPasteMode() { _setPasteMode=true; }
00117 
00119         bool SetProcessingsGeometries (const ProcessingsGeometriesMap & processingsGeometries);
00120         const ProcessingsGeometriesMap GetAndClearGeometries();
00121 
00123         const Geometry findProcessingGeometry(Processing* ) const;
00124         const Processings getOrderedProcessings(const std::string & type, bool horizontalOrder) const;
00125         const Processings getOrderedProcessingsByAttribute(const std::string & attribute, bool horizontalOrder=false) const;
00126         const ControlSinks getOrderedControlSinks() const;
00127         const ControlSources getOrderedControlSources() const;
00128         static const bool compareGeometriesUpperYThan(ProcessingAndGeometry &, ProcessingAndGeometry &);
00129         static const bool compareGeometriesUpperXThan(ProcessingAndGeometry &, ProcessingAndGeometry &);
00130 
00131 /*// TODO: make individual geometries loadings/storings??
00132         const Geometry GetAndEraseGeometry(std::string name);*/
00134         bool ConnectPorts( const std::string &, const std::string & );
00135         bool ConnectControls( const std::string &, const std::string & );
00136         bool DisconnectPorts( const std::string &, const std::string & );
00137         bool DisconnectControls( const std::string &, const std::string & );
00138 
00140         Processing& GetProcessing( const std::string & name ) const;
00142         void AddProcessing( const std::string & name, Processing* processing, const ProcessingConfig * config=0 );
00144         Processing & AddProcessing( const std::string & name, const std::string & key );
00145         std::string AddProcessing( const std::string& key );
00146         std::string GetUnusedName( const std::string& prefix, const bool cutOnLastSeparator=false, const std::string separator="_") const;
00147         void RemoveProcessing ( const std::string & );
00148         bool HasProcessing( const std::string & name ) const;
00152         bool ConfigureProcessing( const std::string &, const ProcessingConfig & );
00154         void ReconfigureAllProcessings();
00155         bool RenameProcessing( const std::string & oldName, const std::string & newName );      
00156         
00157         const std::string & GetNetworkId(const Processing * proc) const;
00158         
00159         // accessors to nodes and processing
00160         ProcessingsMap::iterator BeginProcessings();
00161         ProcessingsMap::iterator EndProcessings();
00162         ProcessingsMap::const_iterator BeginProcessings() const;
00163         ProcessingsMap::const_iterator EndProcessings() const;
00164 
00165         // accessors to txt boxes
00166         void addInformationText(InformationText * informationText);
00167         void removeInformationText(InformationText * informationText);
00168 
00169         InformationTexts::iterator BeginInformationTexts();
00170         InformationTexts::iterator EndInformationTexts();
00171         InformationTexts::const_iterator BeginInformationTexts() const;
00172         InformationTexts::const_iterator EndInformationTexts() const;
00173 
00174         InPortBase & GetInPortByCompleteName( const std::string& ) const;
00175         OutPortBase & GetOutPortByCompleteName( const std::string& ) const;
00176         InControlBase & GetInControlByCompleteName( const std::string& ) const;
00177         OutControlBase & GetOutControlByCompleteName( const std::string& ) const;
00178 
00179         NamesList GetInPortsConnectedTo( const std::string & ) const;
00180         NamesList GetInControlsConnectedTo( const std::string & ) const;
00181         InPortsList GetInPortsConnectedTo( OutPortBase & ) const;
00182 
00183         std::string GetConnectorIdentifier( const std::string& ) const;
00184         std::string GetProcessingIdentifier( const std::string& ) const;
00185 
00191         bool IsReady() const;
00192 
00194         bool IsEmpty() const;
00195 
00197         bool HasMisconfiguredProcessings() const;
00198 
00200         bool HasUnconnectedInPorts() const;
00201 
00206         bool HasSyncSource() const;
00207 
00209         bool SupportsVariableAudioSize() const;
00210 
00215         std::string GetConfigurationErrors() const;
00220         std::string GetUnconnectedInPorts() const;
00221 
00226         typedef std::pair<bool, std::string> ConnectionState; 
00227         ConnectionState GetConnectionReport() const;
00228         void ResetConnectionReport();
00229 
00230 private:
00231         // fields
00232         std::string _name;
00233         Text _description;
00234         ProcessingsMap _processings;
00235         FlowControl* _flowControl;
00236         NetworkPlayer* _player;
00237         InformationTexts _informationTexts;
00238 
00239         // attributes for canvas copy & paste
00240         mutable NamesSet _selectedProcessings;
00241         bool _setPasteMode;
00242         
00243         bool HasSelectionAndContains(const std::string & name) const;
00244 
00245         // attributes for canvas processing geometries
00246         mutable ProcessingsGeometriesMap _processingsGeometries;
00247         void StringPairToInts(const std::string & geometryInString, int & a, int & b);
00248         const std::string IntsToString (const int & a, const int & b) const;
00249 
00250 protected:
00251         static std::size_t PositionOfLastIdentifier( const std::string& str)
00252         {
00253                 std::size_t result = str.find_last_of( NamesIdentifiersSeparator() );
00254                 CLAM_ASSERT( result!=std::string::npos, "Malformed port/control name. It should be ProcessingName.[Port/Control]Name");
00255                 return result;
00256         }
00257         static std::size_t PositionOfProcessingIdentifier( const std::string& str)
00258         {
00259                 std::size_t endPos = PositionOfLastIdentifier(str)-1;
00260                 std::size_t     last_ofResult = str.find_last_of( NamesIdentifiersSeparator(), endPos );
00261                 return last_ofResult == std::string::npos ? 0 : last_ofResult+1;
00262         }
00263         static char NamesIdentifiersSeparator()
00264         {
00265                 return '.';
00266         }
00267 private:
00268         bool BrokenConnection( const std::string & producer, const std::string & consumer );
00269         std::vector<std::string> _brokenConnections;
00270 };
00271 
00272 }// namespace
00273 #endif
00274 
Generated by  doxygen 1.6.3