Imported Upstream version 3.2.2
[debian/gnuradio] / mblock / src / lib / mb_connection.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <mb_connection.h>
27
28 bool
29 mb_conn_table::lookup_conn_by_name(const std::string &component_name,
30                                    const std::string &port_name,
31                                    mb_conn_iter *itp, int *which_ep)
32 {
33   mb_conn_iter end = d_connections.end();
34   for (mb_conn_iter it = d_connections.begin(); it != end; ++it){
35
36     if (it->d_ep[0].component_name() == component_name
37         && it->d_ep[0].port_name() == port_name){
38       *itp = it;
39       *which_ep = 0;
40       return true;
41     }
42
43     if (it->d_ep[1].component_name() == component_name
44         && it->d_ep[1].port_name() == port_name){
45       *itp = it;
46       *which_ep = 1;
47       return true;
48     }
49   }
50
51   return false;
52 }
53
54 bool
55 mb_conn_table::lookup_conn_by_port(const mb_port *port,
56                                    mb_conn_iter *itp, int *which_ep)
57 {
58   mb_conn_iter end = d_connections.end();
59   for (mb_conn_iter it = d_connections.begin(); it != end; ++it){
60     if (it->d_ep[0].port().get() == port){
61       *itp = it;
62       *which_ep = 0;
63       return true;
64     }
65     if (it->d_ep[1].port().get() == port){
66       *itp = it;
67       *which_ep = 1;
68       return true;
69     }
70   }
71
72   return false;
73 }
74
75 void
76 mb_conn_table::create_conn(const mb_endpoint &ep0, const mb_endpoint &ep1)
77 {
78   d_connections.push_back(mb_connection(ep0, ep1));
79 }
80
81 void
82 mb_conn_table::disconnect(const std::string &comp_name1, const std::string &port_name1,
83                           const std::string &comp_name2, const std::string &port_name2)
84 {
85   mb_conn_iter it;
86   int          which_ep;
87
88   // look for comp_name1/port_name1
89   bool found = lookup_conn_by_name(comp_name1, port_name1, &it, &which_ep);
90
91   if (!found)   // no error if not found
92     return;
93
94   // FIXME if/when we do replicated ports, we may have one-to-many,
95   // or many-to-many bindings.  For now, be paranoid
96   assert(it->d_ep[which_ep^1].component_name() == comp_name2);
97   assert(it->d_ep[which_ep^1].port_name() == port_name2);
98
99   d_connections.erase(it);              // Poof!
100 }
101
102 void
103 mb_conn_table::disconnect_component(const std::string component_name)
104 {
105   mb_conn_iter next;
106   mb_conn_iter end = d_connections.end();
107   for (mb_conn_iter it = d_connections.begin(); it != end; it = next){
108     if (it->d_ep[0].component_name() == component_name
109         || it->d_ep[1].component_name() == component_name)
110       next = d_connections.erase(it);   // Poof!
111     else
112       next = ++it;
113   }
114 }
115
116 void
117 mb_conn_table::disconnect_all()
118 {
119   d_connections.clear();                // All gone!
120 }
121
122 int
123 mb_conn_table::nconnections() const
124 {
125   return d_connections.size();
126 }