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

Revision 839, 4.7 kB (checked in by ppalmers, 15 years ago)

fix shutdown segfault

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