2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 This file is part of GNU Radio
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 from Element import Element
23 class Connection(Element):
25 def __init__(self, flow_graph, porta, portb):
27 Make a new connection given the parent and 2 ports.
28 @param flow_graph the parent of this element
29 @param porta a port (any direction)
30 @param portb a port (any direction)
31 @throws Error cannot make connection
32 @return a new connection
34 Element.__init__(self, flow_graph)
36 #separate the source and sink
37 for port in (porta, portb):
38 if port.is_source(): source = port
39 if port.is_sink(): sink = port
40 assert(source and sink)
41 #ensure that this connection (source -> sink) is unique
42 for connection in self.get_parent().get_connections():
43 assert not (connection.get_source() is source and connection.get_sink() is sink)
48 return 'Connection (\n\t%s\n\t\t%s\n\t%s\n\t\t%s\n)'%(
49 self.get_source().get_parent(),
51 self.get_sink().get_parent(),
55 def is_connection(self): return True
59 Validate the connections.
60 The ports must match in type.
62 Element.validate(self)
63 source_type = self.get_source().get_type()
64 sink_type = self.get_sink().get_type()
65 try: assert source_type == sink_type
66 except AssertionError: self.add_error_message('Source type "%s" does not match sink type "%s".'%(source_type, sink_type))
68 def get_enabled(self):
70 Get the enabled state of this connection.
71 @return true if source and sink blocks are enabled
73 return self.get_source().get_parent().get_enabled() and \
74 self.get_sink().get_parent().get_enabled()
76 #############################
78 #############################
79 def get_sink(self): return self._sink
80 def get_source(self): return self._source
82 ##############################################
83 ## Import/Export Methods
84 ##############################################
85 def export_data(self):
87 Export this connection's info.
88 @return a nested data odict
91 n['source_block_id'] = self.get_source().get_parent().get_id()
92 n['sink_block_id'] = self.get_sink().get_parent().get_id()
93 n['source_key'] = self.get_source().get_key()
94 n['sink_key'] = self.get_sink().get_key()