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

Revision 809, 4.5 kB (checked in by ppalmers, 14 years ago)

First round of cleanup:
- make Ports auto-register to a PortManager?
- remove the different 'signal' types, everything is now period-signaled.
- removed obsolete streaming test programs

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