00001
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef RtcOutPort_h
00041 #define RtcOutPort_h
00042
00043 #define ACE_HAS_CLOCK_GETTIME
00044
00045 #include "rtm/RtcOutPortBase.h"
00046 #include "rtm/idl/RTCDataTypeSkel.h"
00047 #include <ace/OS.h>
00048 #include <ace/Time_Value.h>
00049 #include <iostream>
00050
00051 #define DEFAULT_BUFFER_SIZE 64
00052 #ifdef WIN32
00053 #include <atltime.h>
00054 #endif //WIN32
00055
00056 namespace RTM {
00084 template <class T> class OutPortAny
00085 : public OutPortBase
00086 {
00087 public:
00113 OutPortAny(const char* name, T& value, int bufsize=DEFAULT_BUFFER_SIZE)
00114 : m_Buffer(bufsize, value), m_Value(value)
00115 {
00116
00117 m_Profile.name = CORBA::string_dup(name);
00118 CORBA::Any var;
00119 var <<= m_Value;
00120 m_Profile.port_type = var.type();
00121
00122
00123 for (int i = 0 ; i < m_Buffer.buff_length(); i++)
00124 {
00125 m_Buffer.put(m_Value);
00126 }
00127 }
00128
00152 OutPortAny(const char* name, int bufsize=DEFAULT_BUFFER_SIZE)
00153 : m_Buffer(bufsize, m_Dummy), m_Value(m_Dummy)
00154 {
00155
00156 m_Profile.name = CORBA::string_dup(name);
00157 CORBA::Any var;
00158 var <<= m_Value;
00159 m_Profile.port_type = var.type();
00160
00161
00162 for (int i = 0 ; i < m_Buffer.buff_length(); i++)
00163 {
00164 m_Buffer.put(m_Value);
00165 }
00166 }
00167
00168
00182 virtual ~OutPortAny(){};
00183
00197 CORBA::Any* get()
00198 throw (CORBA::SystemException)
00199 {
00200 ACE_TRACE("OutPortBase::get()");
00201 CORBA::Any_var tmp = new CORBA::Any();
00202 tmp <<= m_Buffer.get_new();
00203 return tmp._retn();
00204 }
00205
00219
00220 RtmRes push(InPort_ptr inport, std::string subsid)
00221 {
00222 ACE_TRACE("OutPortBase::push()");
00223
00224 if (CORBA::is_nil(inport))
00225 {
00226 ACE_DEBUG((LM_DEBUG, "Invalid object reference: inport"));
00227 return RTM_ERR;
00228 }
00229
00230 try
00231 {
00232 CORBA::Any val;
00233 val <<= m_Buffer.get_new();
00234
00235 if (inport != NULL)
00236 {
00237 inport->put(val);
00238 }
00239 }
00240 catch(CORBA::SystemException& se)
00241 {
00242
00243 se;
00244
00245 unsubscribeNoLocked(subsid.c_str());
00246 return RTM_ERR;
00247 }
00248 catch(RTM::PortBase::Disconnected& dce)
00249 {
00250
00251 dce;
00252
00253 unsubscribeNoLocked(subsid.c_str());
00254 return RTM_ERR;
00255 }
00256 return RTM_OK;
00257 }
00258
00259
00260
00274 virtual void initBuffer(T& value)
00275 {
00276 for (int i = 0 ; i <= m_Buffer.buff_length(); i++)
00277 {
00278 m_Buffer.put(value);
00279 }
00280 }
00281
00282
00283 #ifdef WIN32
00284 void getFileTime(struct timespec &ts) {
00285 ULARGE_INTEGER tmpbuf;
00286 CFileTime myFT;
00287 ULONGLONG s1;
00288 ULONGLONG s2;
00289 ULONGLONG s3;
00290 SYSTEMTIME time1970;
00291 FILETIME file1970;
00292
00293 time1970.wYear = 1970;
00294 time1970.wMonth = 1;
00295 time1970.wDayOfWeek = 0;
00296 time1970.wDay = 1;
00297 time1970.wHour = 0;
00298 time1970.wMinute = 0;
00299 time1970.wSecond = 0;
00300 time1970.wMilliseconds = 0;
00301 SystemTimeToFileTime(&time1970,&file1970);
00302 CFileTime oldFT(file1970);
00303
00304 myFT = CFileTime::GetCurrentTime();
00305 tmpbuf.LowPart = myFT.dwLowDateTime;
00306 tmpbuf.HighPart = myFT.dwHighDateTime;
00307 s1 = s2 = tmpbuf.QuadPart;
00308 tmpbuf.LowPart = oldFT.dwLowDateTime;
00309 tmpbuf.HighPart = oldFT.dwHighDateTime;
00310 s3 = tmpbuf.QuadPart;
00311 s1 = s2 = s1 - s3;
00312 s1 = (s1 / CFileTime::Second )*CFileTime::Second;
00313 s2 = (s2 - s1)*100;
00314 s1 = (s1 / CFileTime::Second);
00315
00316 ts.tv_sec = (long) s1;
00317 ts.tv_nsec = (long) s2;
00318 };
00319 #endif
00320
00335 virtual void write()
00336 {
00337 struct timespec ts;
00338 #ifdef WIN32
00339 getFileTime(ts);
00340 #else // WIN32
00341 ACE_OS::clock_gettime(CLOCK_REALTIME, &ts);
00342 #endif // WIN32
00343 m_Value.tm.sec = ts.tv_sec;
00344 m_Value.tm.nsec = ts.tv_nsec;
00345 m_Buffer.put(m_Value);
00346 updateall();
00347 };
00348
00349 virtual void write(long sec, long nsec)
00350 {
00351 m_Value.tm.sec = sec;
00352 m_Value.tm.nsec = nsec;
00353 m_Buffer.put(m_Value);
00354 updateall();
00355 };
00356
00357
00374 virtual void write_pm()
00375 {
00376 write();
00377 };
00378
00379
00399 virtual void write(T value)
00400 {
00401 struct timespec ts;
00402 #ifdef WIN32
00403 getFileTime(ts);
00404 #else // WIN32
00405 ACE_OS::clock_gettime(CLOCK_REALTIME, &ts);
00406 #endif // WIN32
00407 value.tm.sec = ts.tv_sec;
00408 value.tm.nsec = ts.tv_nsec;
00409 m_Buffer.put(value);
00410 updateall();
00411 };
00412
00413
00433 virtual void operator<<(T& value)
00434 {
00435 struct timespec ts;
00436 #ifdef WIN32
00437 getFileTime(ts);
00438 #else // WIN32
00439 ACE_OS::clock_gettime(CLOCK_REALTIME, &ts);
00440 #endif // WIN32
00441 value.tm.sec = ts.tv_sec;
00442 value.tm.nsec = ts.tv_nsec;
00443 m_Buffer.put(value);
00444 updateall();
00445 }
00446
00447
00448 private:
00456 T& m_Value;
00457
00465 T m_Dummy;
00466
00474 RingBuffer<T> m_Buffer;
00475 };
00476
00477 };
00478
00479 #endif // RtcOutputChannel_h