root/branches/api-cleanup/src/libstreaming/generic/PortManager.cpp

Revision 811, 4.7 kB (checked in by ppalmers, 14 years ago)

fixes for introduced bugs

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     flushDebugOutput();
43     // delete all ports that are still registered to the manager
44     for ( PortVectorIterator it = m_Ports.begin();
45     it != m_Ports.end();
46     ++it )
47     {
48         debugOutput( DEBUG_LEVEL_VERBOSE, "deleting port %s at %p\n", (*it)->getName().c_str(), *it);
49         flushDebugOutput();
50         delete *it; //FIXME
51     }
52 }
53
54 bool PortManager::makeNameUnique(Port *port)
55 {
56     bool done=false;
57     int idx=0;
58     std::string portname_orig=port->getName();
59    
60     while(!done && idx<10000) {
61         bool is_unique=true;
62        
63         for ( PortVectorIterator it = m_Ports.begin();
64         it != m_Ports.end();
65         ++it )
66         {
67             is_unique &= !((*it)->getName() == port->getName());
68         }
69        
70         if (is_unique) {
71             done=true;
72         } else {
73             std::ostringstream portname;
74             portname << portname_orig << idx++;
75            
76             port->setName(portname.str());
77         }
78     }
79    
80     if(idx<10000) return true;
81     else return false;
82 }
83
84 /**
85  *
86  * @param port
87  * @return
88  */
89 bool PortManager::registerPort(Port *port)
90 {
91     assert(port);
92
93     debugOutput( DEBUG_LEVEL_VERBOSE, "Adding port %s, type: %d, dir: %d, dtype: %d\n",
94         port->getName().c_str(), port->getPortType(), port->getDirection(), port->getDataType());
95
96     port->setVerboseLevel(getDebugLevel());
97
98     if (makeNameUnique(port)) {
99         m_Ports.push_back(port);
100         return true;
101     } else {
102         return false;
103     }
104 }
105
106 bool PortManager::unregisterPort(Port *port)
107 {
108     assert(port);
109     debugOutput( DEBUG_LEVEL_VERBOSE, "unregistering port %s\n",port->getName().c_str());
110
111     for ( PortVectorIterator it = m_Ports.begin();
112       it != m_Ports.end();
113       ++it )
114     {
115         if(*it == port) {
116             m_Ports.erase(it);
117             return true;
118         }
119     }
120
121     debugOutput( DEBUG_LEVEL_VERBOSE, "port %s not found \n",port->getName().c_str());
122
123     return false; //not found
124
125 }
126
127 int PortManager::getPortCount(enum Port::E_PortType type) {
128     int count=0;
129
130     for ( PortVectorIterator it = m_Ports.begin();
131       it != m_Ports.end();
132       ++it )
133     {
134         if ( (*it)->getPortType() == type ) {
135             count++;
136         }
137     }
138     return count;
139 }
140
141 int PortManager::getPortCount() {
142     int count=0;
143
144     count+=m_Ports.size();
145
146     return count;
147 }
148
149 Port * PortManager::getPortAtIdx(unsigned int index) {
150
151     return m_Ports.at(index);
152
153 }
154
155 void PortManager::setVerboseLevel(int i) {
156     setDebugLevel(i);
157     for ( PortVectorIterator it = m_Ports.begin();
158       it != m_Ports.end();
159       ++it )
160     {
161         (*it)->setVerboseLevel(i);
162     }
163 }
164
165
166 bool PortManager::resetPorts() {
167     debugOutput( DEBUG_LEVEL_VERBOSE, "reset ports\n");
168
169     for ( PortVectorIterator it = m_Ports.begin();
170       it != m_Ports.end();
171       ++it )
172     {
173         if(!(*it)->reset()) {
174             debugFatal("Could not reset port %s",(*it)->getName().c_str());
175             return false;
176         }
177     }
178     return true;
179 }
180
181 bool PortManager::initPorts() {
182     debugOutput( DEBUG_LEVEL_VERBOSE, "init ports\n");
183
184     for ( PortVectorIterator it = m_Ports.begin();
185       it != m_Ports.end();
186       ++it )
187     {
188         if(!(*it)->init()) {
189             debugFatal("Could not init port %s\n", (*it)->getName().c_str());
190             return false;
191         }
192     }
193     return true;
194 }
195
196 bool PortManager::preparePorts() {
197     debugOutput( DEBUG_LEVEL_VERBOSE, "preparing ports\n");
198
199     for ( PortVectorIterator it = m_Ports.begin();
200       it != m_Ports.end();
201       ++it )
202     {
203         if(!(*it)->prepare()) {
204             debugFatal("Could not prepare port %s",(*it)->getName().c_str());
205             return false;
206         }
207
208     }
209     return true;
210 }
211
212 }
Note: See TracBrowser for help on using the browser.