Convert gr-audio-portaudio to Boost via gruel
[debian/gnuradio] / mblock / src / lib / mb_port_simple.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2009 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_port_simple.h>
27 #include <mblock/msg_accepter.h>
28 #include <mblock/exception.h>
29 #include <mblock/mblock.h>
30 #include <mb_mblock_impl.h>
31 #include <assert.h>
32 #include <mbi_runtime_lock.h>
33
34 using namespace pmt;
35
36 mb_port_simple::mb_port_simple(mb_mblock *mblock,
37                                const std::string &port_name,
38                                const std::string &protocol_class_name,
39                                bool conjugated,
40                                mb_port::port_type_t port_type)
41   : mb_port(mblock, port_name, protocol_class_name, conjugated, port_type),
42     d_cache_valid(false)
43 {
44 }
45
46 mb_port_simple::~mb_port_simple()
47 {
48   // nop
49 }
50
51 void
52 mb_port_simple::send(pmt_t signal, pmt_t data, pmt_t metadata, mb_pri_t priority)
53 {
54   if (port_type() == mb_port::RELAY)  // Can't send directly to a RELAY port
55     throw mbe_invalid_port_type(mblock(), mblock()->instance_name(), port_name());
56
57   mb_msg_accepter_sptr  accepter = find_accepter(this);
58   if (accepter)
59     (*accepter)(signal, data, metadata, priority);
60 }
61
62
63 mb_msg_accepter_sptr
64 mb_port_simple::find_accepter(mb_port_simple *start)
65 {
66   mb_port_simple        *p = start;
67   mb_port_simple        *pp = 0;
68   mb_mblock             *context = 0;
69   mb_endpoint           peer_ep;
70   mb_msg_accepter_sptr  r;
71
72   if (start->d_cache_valid)
73     return start->d_cached_accepter;
74
75   mbi_runtime_lock      l(p->mblock());
76
77   // Set up initial context.
78
79   switch(p->port_type()){
80   case mb_port::INTERNAL:       // binding is in our name space
81     context = p->mblock();
82     break;
83
84   case mb_port::EXTERNAL:       // binding is in parent's name space
85     context = p->mblock()->parent();
86     if (!context)                       // can't be bound if there's no parent
87       return mb_msg_accepter_sptr();    // not bound
88     break;
89
90   default:
91     throw std::logic_error("Can't happen: mb_port_simple::find_accepter [1]");
92   }
93
94
95  traverse:
96
97   if (!context->impl()->lookup_other_endpoint(p, &peer_ep))
98     return mb_msg_accepter_sptr();      // not bound
99   
100   pp = dynamic_cast<mb_port_simple *>(peer_ep.port().get());    // peer port
101   assert(pp);
102
103   switch (pp->port_type()){     
104   case mb_port::INTERNAL:       // Terminate here.
105   case mb_port::EXTERNAL:
106     r = pp->make_accepter();
107
108     // cache the result
109
110     start->d_cached_accepter = r;
111     start->d_cache_valid = true;
112     return r;
113
114   case mb_port::RELAY:          // Traverse to other side of relay port.
115     if (peer_ep.inside_of_relay_port_p()){
116       // We're on inside of relay port, headed out.
117       p = pp;
118       context = p->mblock()->parent();
119
120       // Corner case: we're attempting to traverse a relay port on the border
121       // of the top block...
122       if (!context)
123         return mb_msg_accepter_sptr();  // not bound
124
125       goto traverse;
126     }
127     else {
128       // We're on the outside of relay port, headed in.
129       p = pp;
130       context = p->mblock();
131       goto traverse;
132     }
133     break;
134
135   default:
136     throw std::logic_error("Can't happen: mb_port_simple::find_accepter [2]");
137   }
138 }
139
140
141 mb_msg_accepter_sptr
142 mb_port_simple::make_accepter()
143 {
144   return d_mblock->impl()->make_accepter(port_symbol());
145 }
146
147 void
148 mb_port_simple::invalidate_cache()
149 {
150   d_cache_valid = false;
151   d_cached_accepter.reset();
152 }