Merged mblock work-in-progress from eb/mb -r4341:4633 into trunk.
[debian/gnuradio] / mblock / src / lib / mb_port_simple.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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 2, 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 <mb_msg_accepter.h>
28 #include <mb_exception.h>
29 #include <mb_mblock.h>
30 #include <mb_mblock_impl.h>
31 #include <assert.h>
32
33
34 mb_port_simple::mb_port_simple(mb_mblock *mblock,
35                                const std::string &port_name,
36                                const std::string &protocol_class_name,
37                                bool conjugated,
38                                mb_port::port_type_t port_type)
39   : mb_port(mblock, port_name, protocol_class_name, conjugated, port_type)
40 {
41 }
42
43 mb_port_simple::~mb_port_simple()
44 {
45   // nop
46 }
47
48 void
49 mb_port_simple::send(pmt_t signal, pmt_t data, pmt_t metadata, mb_pri_t priority)
50 {
51   if (port_type() == mb_port::RELAY)  // Can't send directly to a RELAY port
52     throw mbe_invalid_port_type(mblock(), mblock()->fullname(), port_name());
53
54   mb_msg_accepter_sptr  accepter = find_accepter(this);
55   if (accepter)
56     (*accepter)(signal, data, metadata, priority);
57 }
58
59
60 mb_msg_accepter_sptr
61 mb_port_simple::find_accepter(mb_port_simple *start)
62 {
63   mb_port_simple        *p = start;
64   mb_port_simple        *pp = 0;
65   mb_mblock             *context = 0;
66   mb_endpoint           peer_ep;
67   mb_msg_accepter_sptr  r;
68
69   // Set up initial context.
70
71   switch(p->port_type()){
72   case mb_port::INTERNAL:       // binding is in our name space
73     context = p->mblock();
74     break;
75
76   case mb_port::EXTERNAL:       // binding is in parent's name space
77     context = p->mblock()->parent();
78     break;
79
80   default:
81     throw std::logic_error("Can't happen: mb_port_simple::find_accepter [1]");
82   }
83
84
85  traverse:
86
87   if (!context->impl()->lookup_other_endpoint(p, &peer_ep))
88     return mb_msg_accepter_sptr();      // not bound
89   
90   pp = dynamic_cast<mb_port_simple *>(peer_ep.port().get());    // peer port
91   assert(pp);
92
93   switch (pp->port_type()){     
94   case mb_port::INTERNAL:       // Terminate here.
95   case mb_port::EXTERNAL:
96     r = pp->make_accepter();
97     // FIXME cache the result
98     return r;
99
100   case mb_port::RELAY:          // Traverse to other side of relay port.
101     if (peer_ep.inside_of_relay_port_p()){
102       // We're on inside of relay port, headed out.
103       p = pp;
104       context = p->mblock()->parent();
105
106       // Corner case: we're attempting to traverse a relay port on the border
107       // of the top block...
108       if (!context)
109         return mb_msg_accepter_sptr();  // not bound
110
111       goto traverse;
112     }
113     else {
114       // We're on the outside of relay port, headed in.
115       p = pp;
116       context = p->mblock();
117       goto traverse;
118     }
119     break;
120
121   default:
122     throw std::logic_error("Can't happen: mb_port_simple::find_accepter [2]");
123   }
124 }
125
126
127 mb_msg_accepter_sptr
128 mb_port_simple::make_accepter()
129 {
130   return d_mblock->impl()->make_accepter(port_name());
131 }