root/trunk/libffado/src/libstreaming/generic/PortManager.cpp

Revision 750, 5.7 kB (checked in by ppalmers, 15 years ago)

Code refactoring. Tries to simplify things and tries to put all code where it belongs.

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "PortManager.h"
25 #include "Port.h"
26
27 #include <assert.h>
28
29 #include <iostream>
30 #include <sstream>
31
32
33 namespace Streaming {
34
35 IMPL_DEBUG_MODULE( PortManager, PortManager, DEBUG_LEVEL_NORMAL );
36
37 PortManager::PortManager() {
38
39 }
40
41 PortManager::~PortManager() {
42 //     deleteAllPorts();
43 }
44
45 // bool PortManager::setPortBuffersize(unsigned int newsize) {
46 //     debugOutput( DEBUG_LEVEL_VERBOSE, "setting port buffer size to %d\n",newsize);
47 //
48 //
49 //     for ( PortVectorIterator it = m_Ports.begin();
50 //       it != m_Ports.end();
51 //       ++it )
52 //     {
53 //         if(!(*it)->setBufferSize(newsize)) {
54 //             debugFatal("Could not set buffer size for port %s\n",(*it)->getName().c_str());
55 //             return false;
56 //         }
57 //     }
58 //
59 //     return true; //not found
60 //
61 // }
62
63 bool PortManager::makeNameUnique(Port *port)
64 {
65     bool done=false;
66     int idx=0;
67     std::string portname_orig=port->getName();
68    
69     while(!done && idx<10000) {
70         bool is_unique=true;
71        
72         for ( PortVectorIterator it = m_Ports.begin();
73         it != m_Ports.end();
74         ++it )
75         {
76             is_unique &= !((*it)->getName() == port->getName());
77         }
78        
79         if (is_unique) {
80             done=true;
81         } else {
82             std::ostringstream portname;
83             portname << portname_orig << idx++;
84            
85             port->setName(portname.str());
86         }
87     }
88    
89     if(idx<10000) return true;
90     else return false;
91 }
92
93 /**
94  *
95  * @param port
96  * @return
97  */
98 bool PortManager::addPort(Port *port)
99 {
100     assert(port);
101
102     debugOutput( DEBUG_LEVEL_VERBOSE, "Adding port %s, type: %d, dir: %d, dtype: %d\n",
103         port->getName().c_str(), port->getPortType(), port->getDirection(), port->getDataType());
104
105     port->setVerboseLevel(getDebugLevel());
106
107     if (makeNameUnique(port)) {
108         m_Ports.push_back(port);
109         return true;
110     } else {
111         return false;
112     }
113 }
114
115 bool PortManager::deletePort(Port *port)
116 {
117     assert(port);
118     debugOutput( DEBUG_LEVEL_VERBOSE, "deleting port %s\n",port->getName().c_str());
119
120     for ( PortVectorIterator it = m_Ports.begin();
121       it != m_Ports.end();
122       ++it )
123     {
124         if(*it == port) {
125             m_Ports.erase(it);
126 //             delete *it;
127             return true;
128         }
129     }
130
131     debugOutput( DEBUG_LEVEL_VERBOSE, "port %s not found \n",port->getName().c_str());
132
133     return false; //not found
134
135 }
136
137 void PortManager::deleteAllPorts()
138 {
139     debugOutput( DEBUG_LEVEL_VERBOSE, "deleting all ports\n");
140
141     for ( PortVectorIterator it = m_Ports.begin();
142       it != m_Ports.end();
143       ++it )
144     {
145         m_Ports.erase(it);
146 //         delete *it;
147     }
148
149     return;
150
151 }
152
153 int PortManager::getPortCount(enum Port::E_PortType type) {
154     int count=0;
155
156     for ( PortVectorIterator it = m_Ports.begin();
157       it != m_Ports.end();
158       ++it )
159     {
160         if ( (*it)->getPortType() == type ) {
161             count++;
162         }
163     }
164     return count;
165 }
166
167 int PortManager::getPortCount() {
168     int count=0;
169
170     count+=m_Ports.size();
171
172     return count;
173 }
174
175 Port * PortManager::getPortAtIdx(unsigned int index) {
176
177     return m_Ports.at(index);
178
179 }
180
181 void PortManager::setVerboseLevel(int i) {
182     setDebugLevel(i);
183     for ( PortVectorIterator it = m_Ports.begin();
184       it != m_Ports.end();
185       ++it )
186     {
187         (*it)->setVerboseLevel(i);
188     }
189 }
190
191
192 bool PortManager::resetPorts() {
193     debugOutput( DEBUG_LEVEL_VERBOSE, "reset ports\n");
194
195     for ( PortVectorIterator it = m_Ports.begin();
196       it != m_Ports.end();
197       ++it )
198     {
199         if(!(*it)->reset()) {
200             debugFatal("Could not reset port %s",(*it)->getName().c_str());
201             return false;
202         }
203     }
204     return true;
205 }
206
207 bool PortManager::initPorts() {
208     debugOutput( DEBUG_LEVEL_VERBOSE, "init ports\n");
209
210     for ( PortVectorIterator it = m_Ports.begin();
211       it != m_Ports.end();
212       ++it )
213     {
214         if(!(*it)->init()) {
215             debugFatal("Could not init port %s",(*it)->getName().c_str());
216             return false;
217         }
218     }
219     return true;
220 }
221
222 bool PortManager::preparePorts() {
223     debugOutput( DEBUG_LEVEL_VERBOSE, "preparing ports\n");
224
225     // clear the cache lists
226     m_PeriodPorts.clear();
227     m_PacketPorts.clear();
228
229     for ( PortVectorIterator it = m_Ports.begin();
230       it != m_Ports.end();
231       ++it )
232     {
233         if(!(*it)->prepare()) {
234             debugFatal("Could not prepare port %s",(*it)->getName().c_str());
235             return false;
236         }
237
238         // now prepare the cache lists
239         switch((*it)->getSignalType()) {
240             case Port::E_PacketSignalled:
241                 m_PacketPorts.push_back(*it);
242                 break;
243             case Port::E_PeriodSignalled:
244                 m_PeriodPorts.push_back(*it);
245                 break;
246             default:
247                 debugWarning("%s has unsupported port type\n",
248                              (*it)->getName().c_str());
249             break;
250         }
251     }
252     return true;
253 }
254
255 }
Note: See TracBrowser for help on using the browser.