summaryrefslogtreecommitdiff
path: root/mblock/src/lib/mb_port_simple.cc
blob: 235e59de2247391d72d70c98a7eebccfd66028aa (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* -*- c++ -*- */
/*
 * Copyright 2007,2008,2009 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_port_simple.h>
#include <mblock/msg_accepter.h>
#include <mblock/exception.h>
#include <mblock/mblock.h>
#include <mb_mblock_impl.h>
#include <assert.h>
#include <mbi_runtime_lock.h>

using namespace pmt;

mb_port_simple::mb_port_simple(mb_mblock *mblock,
			       const std::string &port_name,
			       const std::string &protocol_class_name,
			       bool conjugated,
			       mb_port::port_type_t port_type)
  : mb_port(mblock, port_name, protocol_class_name, conjugated, port_type),
    d_cache_valid(false)
{
}

mb_port_simple::~mb_port_simple()
{
  // nop
}

void
mb_port_simple::send(pmt_t signal, pmt_t data, pmt_t metadata, mb_pri_t priority)
{
  if (port_type() == mb_port::RELAY)  // Can't send directly to a RELAY port
    throw mbe_invalid_port_type(mblock(), mblock()->instance_name(), port_name());

  mb_msg_accepter_sptr  accepter = find_accepter(this);
  if (accepter)
    (*accepter)(signal, data, metadata, priority);
}


mb_msg_accepter_sptr
mb_port_simple::find_accepter(mb_port_simple *start)
{
  mb_port_simple	*p = start;
  mb_port_simple	*pp = 0;
  mb_mblock 		*context = 0;
  mb_endpoint 		peer_ep;
  mb_msg_accepter_sptr	r;

  if (start->d_cache_valid)
    return start->d_cached_accepter;

  mbi_runtime_lock	l(p->mblock());

  // Set up initial context.

  switch(p->port_type()){
  case mb_port::INTERNAL:	// binding is in our name space
    context = p->mblock();
    break;

  case mb_port::EXTERNAL:	// binding is in parent's name space
    context = p->mblock()->parent();
    if (!context)			// can't be bound if there's no parent
      return mb_msg_accepter_sptr();	// not bound
    break;

  default:
    throw std::logic_error("Can't happen: mb_port_simple::find_accepter [1]");
  }


 traverse:

  if (!context->impl()->lookup_other_endpoint(p, &peer_ep))
    return mb_msg_accepter_sptr();	// not bound
  
  pp = dynamic_cast<mb_port_simple *>(peer_ep.port().get());	// peer port
  assert(pp);

  switch (pp->port_type()){	
  case mb_port::INTERNAL:	// Terminate here.
  case mb_port::EXTERNAL:
    r = pp->make_accepter();

    // cache the result

    start->d_cached_accepter = r;
    start->d_cache_valid = true;
    return r;

  case mb_port::RELAY:		// Traverse to other side of relay port.
    if (peer_ep.inside_of_relay_port_p()){
      // We're on inside of relay port, headed out.
      p = pp;
      context = p->mblock()->parent();

      // Corner case: we're attempting to traverse a relay port on the border
      // of the top block...
      if (!context)
	return mb_msg_accepter_sptr();	// not bound

      goto traverse;
    }
    else {
      // We're on the outside of relay port, headed in.
      p = pp;
      context = p->mblock();
      goto traverse;
    }
    break;

  default:
    throw std::logic_error("Can't happen: mb_port_simple::find_accepter [2]");
  }
}


mb_msg_accepter_sptr
mb_port_simple::make_accepter()
{
  return d_mblock->impl()->make_accepter(port_symbol());
}

void
mb_port_simple::invalidate_cache()
{
  d_cache_valid = false;
  d_cached_accepter.reset();
}