OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WModuleInputConnector.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 
27 #include "WModule.h"
28 #include "WModuleOutputConnector.h"
29 #include "WModuleConnectorSignals.h"
30 
31 #include "WModuleInputConnector.h"
32 
33 WModuleInputConnector::WModuleInputConnector( boost::shared_ptr< WModule > module, std::string name, std::string description ):
34  WModuleConnector( module, name, description ),
35  m_updated( false )
36 {
37  // initialize members
38 
39  // connect some signals
40  // This signal is some kind of "forwarder" for the data_changed signal of an output connector.
41  signal_DataChanged.connect( getSignalHandler( DATA_CHANGED ) );
42 
43  // setup conditions
44  m_dataChangedCondition = boost::shared_ptr< WCondition >( new WCondition() );
45 
46  // if connection is closed, also fire "data change"
47  signal_ConnectionClosed.connect( boost::bind( &WModuleInputConnector::setUpdated, this ) );
49 }
50 
52 {
53  // cleanup
54  m_DataChangedConnection.disconnect();
55  signal_ConnectionClosed.disconnect_all_slots();
56 }
57 
58 bool WModuleInputConnector::connectable( boost::shared_ptr<WModuleConnector> con )
59 {
60  // output connectors are just allowed to get connected with input connectors
61  if( dynamic_cast<WModuleOutputConnector*>( con.get() ) ) // NOLINT - since we really need them here
62  {
63  return true;
64  }
65  return false;
66 }
67 
68 void WModuleInputConnector::connectSignals( boost::shared_ptr<WModuleConnector> con )
69 {
71 
72  // connect dataChange signal with an internal handler to ensure we can add the "input" connector pointer, since the output
73  // connector does not set this information.
74  // NOTE: con will be a WModuleOutputConnector
75  m_DataChangedConnection = con->subscribeSignal( DATA_CHANGED,
76  boost::bind( &WModuleInputConnector::notifyDataChange, this, _1, _2 )
77  );
78 }
79 
80 void WModuleInputConnector::disconnectSignals( boost::shared_ptr<WModuleConnector> con )
81 {
82  m_DataChangedConnection.disconnect();
83 
85 }
86 
87 boost::signals2::connection WModuleInputConnector::subscribeSignal( MODULE_CONNECTOR_SIGNAL signal,
88  t_GenericSignalHandlerType notifier )
89 {
90  // connect DataChanged signal
91  switch ( signal )
92  {
93  case DATA_CHANGED:
94  return signal_DataChanged.connect( notifier );
95  default: // we do not know this signal: maybe the base class knows it
96  return WModuleConnector::subscribeSignal( signal, notifier );
97  }
98 }
99 
100 void WModuleInputConnector::notifyDataChange( boost::shared_ptr<WModuleConnector> /*input*/,
101  boost::shared_ptr<WModuleConnector> output )
102 {
103  setUpdated();
104 
105  // since the output connector is not able to fill the parameter "input" we need to forward this message and fill it with the
106  // proper information
107  signal_DataChanged( shared_from_this(), output );
108  m_dataChangedCondition->notify();
109 }
110 
111 boost::shared_ptr< WCondition > WModuleInputConnector::getDataChangedCondition()
112 {
113  return m_dataChangedCondition;
114 }
115 
116 void WModuleInputConnector::notifyConnectionEstablished( boost::shared_ptr<WModuleConnector> here, boost::shared_ptr<WModuleConnector> there )
117 {
118  // since the output connector is not able to fill the parameter "input" we need to forward this message and fill it with the
119  // proper information
120  // NOTE: connection established also emits a data changed signal since the data available at the connector has changed.
121  notifyDataChange( here, there );
122 
123  // forward
125 }
126 
128 {
129  return true;
130 }
131 
133 {
134  return false;
135 }
136 
138 {
139  boost::lock_guard<boost::shared_mutex> lock( m_updatedLock );
140  return m_updated;
141 }
142 
144 {
145  boost::lock_guard<boost::shared_mutex> lock( m_updatedLock );
146  m_updated = true;
147 }
148 
150 {
151  boost::lock_guard<boost::shared_mutex> lock( m_updatedLock );
152  bool old = m_updated;
153  m_updated = false;
154  return old;
155 }
156