CLAM-Development  1.4.0
Network.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2007 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 #ifndef _Network_hxx_
22 #define _Network_hxx_
23 
24 #include "Processing.hxx"
25 #include "InPort.hxx"
26 #include "OutPort.hxx"
27 #include "InControl.hxx"
28 #include "OutControl.hxx"
29 #include "Component.hxx"
30 #include "Text.hxx"
31 #include <string>
32 #include <list>
33 #include <map>
34 #include <set>
35 //#include <utility>
36 
37 namespace CLAM
38 {
39 
40 class NetworkPlayer;
41 class FlowControl;
42 class ControlSink;
43 class ControlSource;
44 
46 {
47 public:
48  int x;
49  int y;
51 };
52 
53 
54 class Network : public Component
55 {
56 
57 public:
58  // used types
59  typedef std::list<std::string> NamesList;
60  typedef std::map <std::string, Processing* > ProcessingsMap;
61  typedef std::list<InPortBase *> InPortsList;
62 
63  typedef std::vector<Processing*> Processings;
64  typedef std::vector<ControlSource*> ControlSources;
65  typedef std::vector<ControlSink*> ControlSinks;
66 
67  typedef std::vector<InformationText*> InformationTexts;
68 
69  typedef struct { int x, y, width, height; } Geometry;
70  typedef struct { Processing* processing; Geometry geometry; } ProcessingAndGeometry;
71  typedef std::map <std::string, Geometry> ProcessingsGeometriesMap;
72  typedef struct { std::string sourceName, sinkName; } Connection;
73  typedef std::list<Connection> ConnectionsList;
74  // attributes for canvas copy & paste
75  typedef std::set<std::string> NamesSet;
76 public:
77  // constructor / destructor
78  Network();
79  virtual ~Network();
80 
81  // Methods related to network itself
82  const std::string& GetName() const { return _name; }
83  void SetName( const std::string& name ) { _name=name; }
84 
85  const Text& GetDescription() const {return _description;};
86  void SetDescription( const Text& description ) {_description=description;};
87 
88  virtual const char * GetClassName() const
89  {
90  return "Network";
91  }
92 
93  // Methods related with player and flow
94  bool IsStopped() const;
95  bool IsPlaying() const;
96  bool IsPaused() const;
97  bool IsRealTime() const;
98  void Start();
99  void Stop();
100  void Pause();
101  void Do();
103  void AddFlowControl( FlowControl* );
104  void Clear();
106  void SetPlayer( NetworkPlayer* player);
107  unsigned BackendBufferSize();
108  unsigned BackendSampleRate();
109 
111  virtual void StoreOn( Storage & storage) const;
112  virtual void LoadFrom( Storage & storage);
113 
115  bool UpdateSelections (const NamesList & processingsNamesList);
116  void setPasteMode() { _setPasteMode=true; }
117 
119  bool SetProcessingsGeometries (const ProcessingsGeometriesMap & processingsGeometries);
121 
123  const Geometry findProcessingGeometry(Processing* ) const;
124  const Processings getOrderedProcessings(const std::string & type, bool horizontalOrder) const;
125  const Processings getOrderedProcessingsByAttribute(const std::string & attribute, bool horizontalOrder=false) const;
126  const ControlSinks getOrderedControlSinks() const;
128  static const bool compareGeometriesUpperYThan(ProcessingAndGeometry &, ProcessingAndGeometry &);
129  static const bool compareGeometriesUpperXThan(ProcessingAndGeometry &, ProcessingAndGeometry &);
130 
131 /*// TODO: make individual geometries loadings/storings??
132  const Geometry GetAndEraseGeometry(std::string name);*/
134  bool ConnectPorts( const std::string &, const std::string & );
135  bool ConnectControls( const std::string &, const std::string & );
136  bool DisconnectPorts( const std::string &, const std::string & );
137  bool DisconnectControls( const std::string &, const std::string & );
138 
140  Processing& GetProcessing( const std::string & name ) const;
142  void AddProcessing( const std::string & name, Processing* processing, const ProcessingConfig * config=0 );
144  Processing & AddProcessing( const std::string & name, const std::string & key );
145  std::string AddProcessing( const std::string& key );
146  std::string GetUnusedName( const std::string& prefix, const bool cutOnLastSeparator=false, const std::string separator="_") const;
147  void RemoveProcessing ( const std::string & );
148  bool HasProcessing( const std::string & name ) const;
152  bool ConfigureProcessing( const std::string &, const ProcessingConfig & );
155  bool RenameProcessing( const std::string & oldName, const std::string & newName );
156 
157  const std::string & GetNetworkId(const Processing * proc) const;
158 
159  // accessors to nodes and processing
160  ProcessingsMap::iterator BeginProcessings();
161  ProcessingsMap::iterator EndProcessings();
162  ProcessingsMap::const_iterator BeginProcessings() const;
163  ProcessingsMap::const_iterator EndProcessings() const;
164 
165  // accessors to txt boxes
166  void addInformationText(InformationText * informationText);
167  void removeInformationText(InformationText * informationText);
168 
169  InformationTexts::iterator BeginInformationTexts();
170  InformationTexts::iterator EndInformationTexts();
171  InformationTexts::const_iterator BeginInformationTexts() const;
172  InformationTexts::const_iterator EndInformationTexts() const;
173 
174  InPortBase & GetInPortByCompleteName( const std::string& ) const;
175  OutPortBase & GetOutPortByCompleteName( const std::string& ) const;
176  InControlBase & GetInControlByCompleteName( const std::string& ) const;
177  OutControlBase & GetOutControlByCompleteName( const std::string& ) const;
178 
179  NamesList GetInPortsConnectedTo( const std::string & ) const;
180  NamesList GetInControlsConnectedTo( const std::string & ) const;
182 
183  std::string GetConnectorIdentifier( const std::string& ) const;
184  std::string GetProcessingIdentifier( const std::string& ) const;
185 
191  bool IsReady() const;
192 
194  bool IsEmpty() const;
195 
197  bool HasMisconfiguredProcessings() const;
198 
200  bool HasUnconnectedInPorts() const;
201 
206  bool HasSyncSource() const;
207 
209  bool SupportsVariableAudioSize() const;
210 
215  std::string GetConfigurationErrors() const;
220  std::string GetUnconnectedInPorts() const;
221 
226  typedef std::pair<bool, std::string> ConnectionState;
228  void ResetConnectionReport();
229 
230 private:
231  // fields
232  std::string _name;
233  Text _description;
234  ProcessingsMap _processings;
235  FlowControl* _flowControl;
236  NetworkPlayer* _player;
237  InformationTexts _informationTexts;
238 
239  // attributes for canvas copy & paste
240  mutable NamesSet _selectedProcessings;
241  bool _setPasteMode;
242 
243  bool HasSelectionAndContains(const std::string & name) const;
244 
245  // attributes for canvas processing geometries
246  mutable ProcessingsGeometriesMap _processingsGeometries;
247  void StringPairToInts(const std::string & geometryInString, int & a, int & b);
248  const std::string IntsToString (const int & a, const int & b) const;
249 
250 protected:
251  static std::size_t PositionOfLastIdentifier( const std::string& str)
252  {
253  std::size_t result = str.find_last_of( NamesIdentifiersSeparator() );
254  CLAM_ASSERT( result!=std::string::npos, "Malformed port/control name. It should be ProcessingName.[Port/Control]Name");
255  return result;
256  }
257  static std::size_t PositionOfProcessingIdentifier( const std::string& str)
258  {
259  std::size_t endPos = PositionOfLastIdentifier(str)-1;
260  std::size_t last_ofResult = str.find_last_of( NamesIdentifiersSeparator(), endPos );
261  return last_ofResult == std::string::npos ? 0 : last_ofResult+1;
262  }
264  {
265  return '.';
266  }
267 private:
268  bool BrokenConnection( const std::string & producer, const std::string & consumer );
269  std::vector<std::string> _brokenConnections;
270 };
271 
272 }// namespace
273 #endif
274