summaryrefslogtreecommitdiff
path: root/mblock/src/lib/mb_connection.cc
blob: 7e3bb8977fa101299a8fc258079024cdf3dbfdc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* -*- c++ -*- */
/*
 * Copyright 2006 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <mb_connection.h>

bool
mb_conn_table::lookup_conn_by_name(const std::string &component_name,
				   const std::string &port_name,
				   mb_conn_iter *itp, int *which_ep)
{
  mb_conn_iter end = d_connections.end();
  for (mb_conn_iter it = d_connections.begin(); it != end; ++it){

    if (it->d_ep[0].component_name() == component_name
	&& it->d_ep[0].port_name() == port_name){
      *itp = it;
      *which_ep = 0;
      return true;
    }

    if (it->d_ep[1].component_name() == component_name
	&& it->d_ep[1].port_name() == port_name){
      *itp = it;
      *which_ep = 1;
      return true;
    }
  }

  return false;
}

bool
mb_conn_table::lookup_conn_by_port(const mb_port *port,
				   mb_conn_iter *itp, int *which_ep)
{
  mb_conn_iter end = d_connections.end();
  for (mb_conn_iter it = d_connections.begin(); it != end; ++it){
    if (it->d_ep[0].port().get() == port){
      *itp = it;
      *which_ep = 0;
      return true;
    }
    if (it->d_ep[1].port().get() == port){
      *itp = it;
      *which_ep = 1;
      return true;
    }
  }

  return false;
}

void
mb_conn_table::create_conn(const mb_endpoint &ep0, const mb_endpoint &ep1)
{
  d_connections.push_back(mb_connection(ep0, ep1));
}

void
mb_conn_table::disconnect(const std::string &comp_name1, const std::string &port_name1,
			  const std::string &comp_name2, const std::string &port_name2)
{
  mb_conn_iter it;
  int	       which_ep;

  // look for comp_name1/port_name1
  bool found = lookup_conn_by_name(comp_name1, port_name1, &it, &which_ep);

  if (!found)	// no error if not found
    return;

  // FIXME if/when we do replicated ports, we may have one-to-many,
  // or many-to-many bindings.  For now, be paranoid
  assert(it->d_ep[which_ep^1].component_name() == comp_name2);
  assert(it->d_ep[which_ep^1].port_name() == port_name2);

  d_connections.erase(it);		// Poof!
}

void
mb_conn_table::disconnect_component(const std::string component_name)
{
  mb_conn_iter next;
  mb_conn_iter end = d_connections.end();
  for (mb_conn_iter it = d_connections.begin(); it != end; it = next){
    if (it->d_ep[0].component_name() == component_name
	|| it->d_ep[1].component_name() == component_name)
      next = d_connections.erase(it);	// Poof!
    else
      next = ++it;
  }
}

void
mb_conn_table::disconnect_all()
{
  d_connections.clear();		// All gone!
}

int
mb_conn_table::nconnections() const
{
  return d_connections.size();
}