Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

RtcOutPort.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00021 #ifndef __RtcOutPort_h__
00022 #define __RtcOutPort_h__
00023 
00024 #define ACE_HAS_CLOCK_GETTIME
00025 
00026 #include "rtm/RtcOutPortBase.h"
00027 #include "rtm/idl/RTCDataTypeSkel.h"
00028 #include <ace/OS.h>
00029 #include <ace/Time_Value.h>
00030 #include <iostream>
00031 
00032 
00033 
00034 namespace RTM {
00062   template <class T> class OutPortAny
00063         : public OutPortBase
00064   {
00065   public:
00091         OutPortAny(const char* name, T& value, int bufsize=64)
00092           : m_Buffer(bufsize), m_Value(value)
00093         {
00094           // Set PortProfile
00095           m_Profile.name = CORBA::string_dup(name);
00096           CORBA::Any var;
00097           var <<= m_Value;
00098           m_Profile.port_type = var.type();
00099           
00100           // Initializing ring buffer
00101           for (int i = 0 ; i < m_Buffer.buff_length() + 1; i++)
00102                 {
00103                   m_Buffer.put(m_Value);
00104                 }
00105         }
00106         
00130         OutPortAny(const char* name, int bufsize=64)
00131           : m_Buffer(bufsize), m_Value(m_Dummy)
00132         {
00133           // Set PortProfile
00134           m_Profile.name = CORBA::string_dup(name);
00135           CORBA::Any var;
00136           var <<= m_Value;
00137           m_Profile.port_type = var.type();
00138           
00139           // Initializing ring buffer
00140           for (int i = 0 ; i < m_Buffer.buff_length() + 1; i++)
00141                 {
00142                   m_Buffer.put(m_Value);
00143                 }
00144         }
00145         
00146         
00160         virtual ~OutPortAny(){};
00161         
00175         CORBA::Any* get()
00176           throw (CORBA::SystemException)
00177         {
00178           ACE_TRACE("OutPortBase::get()");
00179           CORBA::Any_var tmp = new CORBA::Any();
00180           tmp <<= m_Buffer.get_new();
00181           return tmp._retn();
00182         }
00183         
00197         //      RtmRes push(const InPort_ptr& inport, SubscriptionID subsid)
00198         RtmRes push(const InPort_ptr& inport, std::string subsid)
00199         {
00200           ACE_TRACE("OutPortBase::push()")
00201                 
00202                 if (CORBA::is_nil(inport)) 
00203                   {
00204                         ACE_DEBUG((LM_DEBUG, "Invalid object reference: inport"));
00205                         return RTM_ERR;
00206                   }
00207           try
00208                 {
00209                   CORBA::Any val;
00210                   val <<= m_Buffer.get_new();
00211                   inport->put(val);
00212                 }
00213           catch(CORBA::SystemException& e)
00214                 {
00215                   ACE_DEBUG((LM_DEBUG, "CORBA System Exception."));
00216                   unsubscribe(subsid.c_str());
00217                   return RTM_ERR;
00218                 }
00219           catch(RTM::InPort::Disconnected& e)
00220                 {
00221                   ACE_DEBUG((LM_DEBUG, "Disconnected."));
00222                   unsubscribe(subsid.c_str());
00223                   return RTM_ERR;
00224                 }
00225           return RTM_OK;
00226         }
00227         
00228         
00229         
00243         virtual void initBuffer(T& value)
00244         {
00245           for (int i = 0 ; i <= m_Buffer.buff_length(); i++)
00246                 {
00247                   m_Buffer.put(value);
00248                 }
00249         }
00250         
00251         
00267         virtual void write()
00268         {
00269           struct timespec ts;
00270           ACE_OS::clock_gettime(CLOCK_REALTIME, &ts);
00271           m_Value.tm.sec = ts.tv_sec;
00272           m_Value.tm.nsec = ts.tv_nsec;
00273           m_Buffer.put(m_Value);
00274           updateall();
00275         };
00276         
00277         
00297         virtual void write(T value)
00298         {
00299           struct timespec ts;
00300           ACE_OS::clock_gettime(CLOCK_REALTIME, &ts);
00301           value.tm.sec = ts.tv_sec;
00302           value.tm.nsec = ts.tv_nsec;
00303           m_Buffer.put(value);
00304           updateall();
00305         };
00306         
00307         
00327         virtual void operator<<(T& value)
00328         {
00329           struct timespec ts;
00330           ACE_OS::clock_gettime(CLOCK_REALTIME, &ts);
00331           value.tm.sec = ts.tv_sec;
00332           value.tm.nsec = ts.tv_nsec;
00333           m_Buffer.put(value);
00334           updateall();
00335         }
00336         
00337         
00338   private:
00346         T& m_Value;
00347         
00355         T m_Dummy;
00356         
00364         RingBuffer<T> m_Buffer;
00365   };
00366   
00367 }; // End of namesepace RTM
00368 
00369 #endif // __RtcOutputChannel_h__

Generated on Thu Dec 16 15:29:18 2004 for OpenRTM by doxygen 1.3.6