OpenWalnut  1.5.0dev
WModuleConnector.h
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 #ifndef WMODULECONNECTOR_H
26 #define WMODULECONNECTOR_H
27 
28 #include <memory>
29 #include <set>
30 #include <shared_mutex>
31 #include <string>
32 
33 #include <boost/bind/bind.hpp>
34 #include <boost/signals2/connection.hpp>
35 #include <boost/signals2/signal.hpp>
36 #include <boost/thread.hpp>
37 
38 #include "WModule.h"
39 #include "WModuleCombinerTypes.h"
40 #include "WModuleConnectorSignals.h"
41 
42 
43 
46 
47 /**
48  * Base class for modelling connections between kernel modules. It contains several pure virtual member functions and can
49  * therefore not instantiated directly.
50  */
51 class WModuleConnector: public std::enable_shared_from_this<WModuleConnector>
52 {
53  friend class WModuleConnectorTest; //!< Access for test class.
54  friend class WModuleProjectFileCombiner; //!< Access for creating a module graph automatically.
55 
56 public:
57  /**
58  * Shared pointer to this class.
59  */
60  typedef std::shared_ptr< WModuleConnector > SPtr;
61 
62  /**
63  * Const shared pointer to this class.
64  */
65  typedef std::shared_ptr< const WModuleConnector > ConstSPtr;
66 
67  /**
68  * Constructor.
69  *
70  * \param module the module which is owner of this connector.
71  * \param name The name of this connector.
72  * \param description Short description of this connector.
73  */
74  WModuleConnector( std::shared_ptr< WModule > module, std::string name="", std::string description="" );
75 
76  /**
77  * Destructor.
78  */
79  virtual ~WModuleConnector();
80 
81  /**
82  * Returns the module which owns this connector.
83  *
84  * \return the module owning the connector.
85  */
86  std::shared_ptr< WModule > getModule() const;
87 
88  /**
89  * Disconnects this connector if connected. If it is not connected: nothing happens.
90  *
91  * \param con the connector to disconnect.
92  * \param removeFromOwnList if true the specified connection is also removed from the own connection list. If false it won't.
93  */
94  virtual void disconnect( std::shared_ptr<WModuleConnector> con, bool removeFromOwnList = true );
95 
96  /**
97  * Disconnects ALL connected connectors.
98  */
99  virtual void disconnectAll();
100 
101  /**
102  * Connects this Module Connector with another one. During connection process, just the connectibility flag from
103  * WModuleConnector::connectable is used to determine whether the connection is possible or not.
104  *
105  * \param con the connector to connect.
106  * \param force force connection even if incompatible. Used primarily for project file loader. If true, only a \ref lazyConnectable is used.
107  *
108  * \exception WModuleConnectionFailed if connection can not be established.
109  */
110  virtual void connect( std::shared_ptr<WModuleConnector> con, bool force = false );
111 
112  /**
113  * Checks whether this connector is connected to the given one. If there is the strange case where one connector is connected
114  * with the other one but not vice versa it will throw an exception.
115  *
116  * \param con the connector to check connection with.
117  *
118  * \return true if connected
119  *
120  * \throw WModuleConnectionInvalid thrown if one connector thinks it is connected but the other one not.
121  */
122  bool isConnectedTo( std::shared_ptr<WModuleConnector> con );
123 
124  /**
125  * Gets the count of connections currently established.
126  *
127  * \return the number of connections.
128  */
129  unsigned int isConnected();
130 
131  /**
132  * Connects a specified notify function with a signal this module instance is offering.
133  *
134  * \exception WSignalSubscriptionFailed thrown if the signal can't be connected.
135  *
136  * \param signal the signal to connect to.
137  * \param notifier the notifier function to bind.
138  *
139  * \return connection descriptor.
140  */
141  virtual boost::signals2::connection subscribeSignal( MODULE_CONNECTOR_SIGNAL signal, t_GenericSignalHandlerType notifier );
142 
143  /**
144  * Gives information about this connection.
145  *
146  * \return The connection's description.
147  */
148  const std::string getDescription() const;
149 
150  /**
151  * Sets the connector's description. This is not thread-safe! Do not use it outside the WModule thread.
152  *
153  * \param desc the new description.
154  */
155  void setDescription( std::string desc );
156 
157  /**
158  * Gives name of connection.
159  *
160  * \return The name of this connection
161  */
162  const std::string getName() const;
163 
164  /**
165  * Gives canonical name of connection. The canonical name is a descriptor including module name. The description is
166  * ModuleName:ConnectorName.
167  *
168  * \return The name of this connection
169  */
170  const std::string getCanonicalName() const;
171 
172  /**
173  * Sets the connector's name. This is not thread-safe! Do not use it outside the WModule thread.
174  *
175  * \param name the new name.
176  */
177  void setName( std::string name );
178 
179  /**
180  * Checks whether the specified connector is connectable to this one.
181  *
182  * \param con the connector to check against.
183  *
184  * \return true if compatible.
185  */
186  virtual bool connectable( std::shared_ptr<WModuleConnector> con ) = 0;
187 
188  /**
189  * Checks whether the specified connector is connectable to this one, but ignores compatibility the type to be transferred. If you implement your
190  * own connectors, please override and check for compatibility with your matching connectors, but ignore the transfer type.
191  *
192  * \param con the connector to check against.
193  *
194  * \return true if compatible.
195  */
196  virtual bool lazyConnectable( std::shared_ptr<WModuleConnector> con ) = 0;
197 
198  /**
199  * Returns a list of possible disconnections for this connector. Please be aware that the connections might change during the life-time of
200  * the returned DisconnectCombiner instances.
201  *
202  * \return the possible disconnections.
203  */
204  WCombinerTypes::WOneToOneCombiners getPossibleDisconnections();
205 
206  /**
207  * Tries to convert this instance to an input connector.
208  *
209  * \return this as input connector
210  */
211  std::shared_ptr< WModuleInputConnector > toInputConnector();
212 
213  /**
214  * Tries to convert this instance to an output connector.
215  *
216  * \return this as output connector
217  */
218  std::shared_ptr< WModuleOutputConnector > toOutputConnector();
219 
220  /**
221  * Returns true if this instance is an WModuleInputConnector.
222  *
223  * \return true if castable to WModuleInputConnector.
224  */
225  virtual bool isInputConnector() const = 0;
226 
227  /**
228  * Returns true if this instance is an WModuleOutputConnector.
229  *
230  * \return true if castable to WModuleOutputConnector.
231  */
232  virtual bool isOutputConnector() const = 0;
233 
234  /**
235  * Gets the condition variable that gets fired whenever new data has been sent.
236  *
237  * \return the condition
238  */
239  std::shared_ptr< WCondition > getDataChangedCondition();
240 
241 protected:
242  /**
243  * List of connectors connected to this connector.
244  */
245  std::set<std::shared_ptr<WModuleConnector> > m_connected;
246 
247  /**
248  * Lock for avoiding concurrent write to m_Connected (multiple reader, single writer lock). The read lock can be acquired using
249  * the boost::shared_lock<std::shared_mutex> lock( m_ConnectionListLock );.
250  */
251  std::shared_mutex m_connectionListLock;
252 
253  /**
254  * Connect additional signals.
255  *
256  * \param con the connector that requests connection.
257  *
258  */
259  virtual void connectSignals( std::shared_ptr<WModuleConnector> con );
260 
261  /**
262  * Disconnect all signals subscribed by this connector from "con".
263  *
264  * \param con the connector that gets disconnected.
265  */
266  virtual void disconnectSignals( std::shared_ptr<WModuleConnector> con );
267 
268  /**
269  * Gives the signal handler function responsible for a given signal. Modules defining own signal handlers should overwrite
270  * this function. This function is protected since boost::functions are callable, which is what is not wanted here. Just
271  * signals should call them.
272  *
273  * \param signal the signal to get the handler for.
274  *
275  * \return the signal handler for "signal".
276  */
277  virtual const t_GenericSignalHandlerType getSignalHandler( MODULE_CONNECTOR_SIGNAL signal );
278 
279  /**
280  * The Module this connector belongs to
281  */
282  std::weak_ptr< WModule > m_module;
283 
284  /**
285  * The name of the module owning this connector.
286  */
287  std::string m_moduleName;
288 
289  /**
290  * Gets called whenever a connector gets connected to the specified input.
291  *
292  * \param here the connector of THIS module that got connected to "there"
293  * \param there the connector that has been connected with the connector "here" of this module.
294  */
295  virtual void notifyConnectionEstablished( std::shared_ptr<WModuleConnector> here, std::shared_ptr<WModuleConnector> there );
296 
297  /**
298  * Gets called whenever a connection between a remote and local connector gets closed.
299  *
300  * \param here the connector of THIS module getting disconnected.
301  * \param there the connector of the other module getting disconnected.
302  */
303  virtual void notifyConnectionClosed( std::shared_ptr<WModuleConnector> here, std::shared_ptr<WModuleConnector> there );
304 
305  /**
306  * Signal emitted whenever connection has been established.
307  */
308  t_GenericSignalType signal_ConnectionEstablished;
309 
310  /**
311  * Signal emitted whenever connection has been closed.
312  */
313  t_GenericSignalType signal_ConnectionClosed;
314 
315  /**
316  * Condition fired whenever data changes. Proper calling is made in subclasses.
317  */
318  std::shared_ptr< WCondition > m_dataChangedCondition;
319 
320 private:
321  /**
322  * The connections name.
323  */
324  std::string m_name;
325 
326  /**
327  * The connections description.
328  */
329  std::string m_description;
330 };
331 
332 #endif // WMODULECONNECTOR_H
Tests the WModuleConnector class.
Base class for modelling connections between kernel modules.
const std::string getDescription() const
Gives information about this connection.
virtual bool isInputConnector() const =0
Returns true if this instance is an WModuleInputConnector.
virtual void disconnect(std::shared_ptr< WModuleConnector > con, bool removeFromOwnList=true)
Disconnects this connector if connected.
unsigned int isConnected()
Gets the count of connections currently established.
bool isConnectedTo(std::shared_ptr< WModuleConnector > con)
Checks whether this connector is connected to the given one.
std::string m_description
The connections description.
std::string m_moduleName
The name of the module owning this connector.
virtual const t_GenericSignalHandlerType getSignalHandler(MODULE_CONNECTOR_SIGNAL signal)
Gives the signal handler function responsible for a given signal.
std::shared_ptr< WModuleOutputConnector > toOutputConnector()
Tries to convert this instance to an output connector.
const std::string getCanonicalName() const
Gives canonical name of connection.
std::shared_ptr< const WModuleConnector > ConstSPtr
Const shared pointer to this class.
virtual void connectSignals(std::shared_ptr< WModuleConnector > con)
Connect additional signals.
std::shared_ptr< WCondition > getDataChangedCondition()
Gets the condition variable that gets fired whenever new data has been sent.
std::string m_name
The connections name.
t_GenericSignalType signal_ConnectionClosed
Signal emitted whenever connection has been closed.
virtual void disconnectAll()
Disconnects ALL connected connectors.
virtual void disconnectSignals(std::shared_ptr< WModuleConnector > con)
Disconnect all signals subscribed by this connector from "con".
t_GenericSignalType signal_ConnectionEstablished
Signal emitted whenever connection has been established.
std::shared_ptr< WModuleInputConnector > toInputConnector()
Tries to convert this instance to an input connector.
virtual bool connectable(std::shared_ptr< WModuleConnector > con)=0
Checks whether the specified connector is connectable to this one.
WCombinerTypes::WOneToOneCombiners getPossibleDisconnections()
Returns a list of possible disconnections for this connector.
virtual bool isOutputConnector() const =0
Returns true if this instance is an WModuleOutputConnector.
std::shared_ptr< WModuleConnector > SPtr
Shared pointer to this class.
virtual boost::signals2::connection subscribeSignal(MODULE_CONNECTOR_SIGNAL signal, t_GenericSignalHandlerType notifier)
Connects a specified notify function with a signal this module instance is offering.
virtual void notifyConnectionEstablished(std::shared_ptr< WModuleConnector > here, std::shared_ptr< WModuleConnector > there)
Gets called whenever a connector gets connected to the specified input.
std::shared_ptr< WCondition > m_dataChangedCondition
Condition fired whenever data changes.
const std::string getName() const
Gives name of connection.
std::set< std::shared_ptr< WModuleConnector > > m_connected
List of connectors connected to this connector.
std::shared_mutex m_connectionListLock
Lock for avoiding concurrent write to m_Connected (multiple reader, single writer lock).
void setName(std::string name)
Sets the connector's name.
void setDescription(std::string desc)
Sets the connector's description.
std::shared_ptr< WModule > getModule() const
Returns the module which owns this connector.
virtual void connect(std::shared_ptr< WModuleConnector > con, bool force=false)
Connects this Module Connector with another one.
virtual void notifyConnectionClosed(std::shared_ptr< WModuleConnector > here, std::shared_ptr< WModuleConnector > there)
Gets called whenever a connection between a remote and local connector gets closed.
virtual ~WModuleConnector()
Destructor.
WModuleConnector(std::shared_ptr< WModule > module, std::string name="", std::string description="")
Constructor.
virtual bool lazyConnectable(std::shared_ptr< WModuleConnector > con)=0
Checks whether the specified connector is connectable to this one, but ignores compatibility the type...
std::weak_ptr< WModule > m_module
The Module this connector belongs to.
Class implementing input connection functionality between modules.
Class implementing output connection functionality between modules.
This class is able to parse project files and create the appropriate module graph inside a specified ...