Orocos Real-Time Toolkit  2.8.3
ChannelInterface.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:07 CEST 2009 ChannelInterface.cpp
3 
4  ChannelInterface.cpp - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Sylvain Joyeux
8  email : sylvain.joyeux@m4x.org
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #include "../internal/Channels.hpp"
40 #include "../os/Atomic.hpp"
41 #include "../os/MutexLock.hpp"
42 #include <boost/lexical_cast.hpp>
43 
44 using namespace RTT;
45 using namespace RTT::detail;
46 
48  : input(0)
49 
50 {
51  ORO_ATOMIC_SETUP(&refcount,0);
52 }
53 
55 {
56  ORO_ATOMIC_CLEANUP(&refcount);
57 }
58 
60 { RTT::os::MutexLock lock(inout_lock);
61  return ChannelElementBase::shared_ptr(input);
62 }
63 
65 { RTT::os::MutexLock lock(inout_lock);
66  return ChannelElementBase::shared_ptr(output);
67 }
68 
70 {
71  this->output = output;
72  if (output)
73  output->input = this;
74 }
75 
77 {
78  if (forward)
79  {
80  shared_ptr output = getOutput();
81  if (output)
82  output->disconnect(true);
83  }
84  else
85  {
86  shared_ptr input = getInput();
87  if (input)
88  input->disconnect(false);
89  }
90 
91  { RTT::os::MutexLock lock(inout_lock);
92  this->input = 0;
93  this->output = 0;
94  }
95 }
96 
98 {
99  shared_ptr input = getInput();
100  return input ? input->getInputEndPoint() : this;
101 }
103 {
104  shared_ptr output = getOutput();
105  return output ? output->getOutputEndPoint() : this;
106 }
107 
109 {
110  // we go against the data stream
111  shared_ptr input = getInput();
112  if (input)
113  return input->inputReady();
114  return false;
115 }
116 
118 {
119  shared_ptr input = getInput();
120  if (input)
121  input->clear();
122 }
123 
125 {
126  shared_ptr output = getOutput();
127  if (output)
128  return output->signal();
129  return true;
130 }
131 
133  return 0;
134 }
135 
137  return 0;
138 }
139 
141  return false;
142 }
143 
144 std::string ChannelElementBase::getRemoteURI() const {
145  if(!output)
146  {
147  return std::string();
148  }
149  return output->getLocalURI();
150 }
151 
152 std::string ChannelElementBase::getLocalURI() const {
153  return std::string(boost::lexical_cast<std::string>(this));
154 }
155 
157  return std::string("ChannelElementBase");
158 }
159 
161 {
162  oro_atomic_inc(&refcount);
163 }
164 
166 {
167  if ( oro_atomic_dec_and_test(&refcount) ) delete this;
168 }
169 
171 { p->ref(); }
172 
174 { p->deref(); }
175 
virtual PortInterface * getPort() const
Gets the port this channel element is connected to.
virtual void clear()
Clears any data stored by the channel.
void RTT_API intrusive_ptr_add_ref(ChannelElementBase *e)
virtual internal::ConnID * getConnID() const
Gets the Connection ID of this channel.
virtual bool isRemoteElement() const
This function may be used to identify, if the current element uses a network transport, to send the data to the next Element in the logical chain.
ChannelElementBase::shared_ptr getInputEndPoint()
Returns the first input channel element of this connection.
ChannelElementBase()
A default constructed ChannelElementBase has no input nor output configured.
virtual std::string getRemoteURI() const
This function returns the URI of the next channel element in the logical chain.
virtual bool signal()
Signals that there is new data available on this channel By default, the channel element forwards the...
ChannelElementBase::shared_ptr getInput()
Returns the current input channel element.
Convenient short notation for every sub-namespace of RTT.
virtual void disconnect(bool forward)
Performs a disconnection of this channel&#39;s endpoints.
void oro_atomic_inc(oro_atomic_t *a)
Increment a atomically.
void ORO_ATOMIC_SETUP(oro_atomic_t *a, int n)
Initializes the uninitialized atomic structure a with a counter value of &#39;n&#39;.
virtual std::string getLocalURI() const
This function return the URI of this element.
void ORO_ATOMIC_CLEANUP(oro_atomic_t *a)
Cleans up all resources allocated durint the setup of atomic structure a.
void setOutput(shared_ptr output)
Sets the output of this channel element to output and sets the input of output to this...
boost::intrusive_ptr< ChannelElementBase > shared_ptr
int oro_atomic_dec_and_test(oro_atomic_t *a)
Decrement a atomically and test for zero.
void deref()
Decreases the reference count, and deletes the object if it is zero.
void RTT_API intrusive_ptr_release(ChannelElementBase *e)
virtual bool inputReady()
This is called by an input port when it is ready to receive data.
virtual std::string getElementName() const
Returns the class name of this element.
This class is used in places where a permanent representation of a reference to a connection is neede...
Definition: ConnID.hpp:58
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
In the data flow implementation, a channel is created by chaining ChannelElementBase objects...
The base class of every data flow port.
ChannelElementBase::shared_ptr getOutputEndPoint()
Returns the last output channel element of this connection.
ChannelElementBase::shared_ptr getOutput()
Returns the next channel element in the channel&#39;s propagation direction.
void ref()
Increases the reference count.
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51