00001 """
00002 Copyright 2008 Free Software Foundation, Inc.
00003 This file is part of GNU Radio
00004
00005 GNU Radio Companion is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009
00010 GNU Radio Companion is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 GNU General Public License for more details.
00014
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00018 """
00019
00020
00021
00022
00023
00024
00025
00026 from grc.elements.Element import Element
00027 from grc.Utils import odict
00028
00029 class Connection(Element):
00030
00031 def __init__(self, flow_graph, porta, portb):
00032 """!
00033 Make a new connection given the parent and 2 ports.
00034 @param flow_graph the parent of this element
00035 @param porta a port (any direction)
00036 @param portb a port (any direction)
00037 @throws Error cannot make connection
00038 @return a new connection
00039 """
00040 Element.__init__(self, flow_graph)
00041 source = sink = None
00042
00043 for port in (porta, portb):
00044 if port.is_source(): source = port
00045 if port.is_sink(): sink = port
00046
00047 assert(source and sink)
00048 assert(not source.is_full())
00049 assert(not sink.is_full())
00050 self._source = source
00051 self._sink = sink
00052
00053 def __str__(self): return 'Connection (%s -> %s)'%(self.get_source(), self.get_sink())
00054
00055 def is_connection(self): return True
00056
00057 def validate(self):
00058 """
00059 Validate the connections.
00060 The ports must match in type.
00061 """
00062 source_type = self.get_source().get_type()
00063 sink_type = self.get_sink().get_type()
00064 try: assert source_type == sink_type
00065 except AssertionError: self._add_error_message('Source type "%s" does not match sink type "%s".'%(source_type, sink_type))
00066
00067 def get_enabled(self):
00068 """!
00069 Get the enabled state of this connection.
00070 @return true if source and sink blocks are enabled
00071 """
00072 return self.get_source().get_parent().get_enabled() and \
00073 self.get_sink().get_parent().get_enabled()
00074
00075
00076
00077
00078 def get_sink(self): return self._sink
00079 def get_source(self): return self._source
00080
00081
00082
00083
00084 def export_data(self):
00085 """
00086 Export this connection's info.
00087 @return a nested data odict
00088 """
00089 n = odict()
00090 n['source_block_id'] = self.get_source().get_parent().get_id()
00091 n['sink_block_id'] = self.get_sink().get_parent().get_id()
00092 n['source_key'] = self.get_source().get_key()
00093 n['sink_key'] = self.get_sink().get_key()
00094 return n