Consumer.cpp
Go to the documentation of this file.
00001 #include "Consumer.hpp"
00002 #include <sys/types.h>
00003 #include <sys/stat.h>
00004 #include <fcntl.h>
00005 #include <iostream>
00006 #include <boost/lexical_cast.hpp>
00007 #include <rtt/extras/FileDescriptorActivity.hpp>
00008 
00009 using namespace std;
00010 using namespace fd;
00011 
00012 Consumer::Consumer(std::string const& name)
00013     : ConsumerBase(name)
00014     , m_fd(-1)
00015 {
00016     // The test suite must create a pipe and give us the FD for the read side
00017     // through the FD_DRIVEN_TEST_FILE environment variable.
00018     char const* fd = getenv("FD_DRIVEN_TEST_FILE");
00019     if (fd)
00020         m_fd = boost::lexical_cast<int>(fd);
00021     else
00022         std::cerr << "FD_DRIVEN_TEST_FILE is not set" << std::endl;
00023 }
00024 
00025 Consumer::~Consumer()
00026 {
00027     close(m_fd);
00028 }
00029 
00031 // hooks defined by Orocos::RTT. See Consumer.hpp for more detailed
00032 // documentation about them.
00033 
00034 bool Consumer::configureHook() {
00035     if (m_fd != -1)
00036         getActivity<RTT::extras::FileDescriptorActivity>()->watch(m_fd);
00037     return TaskContext::configureHook();
00038 }
00039 
00040 void Consumer::updateHook()
00041 {
00042     static int idx = 'A';
00043 
00044     // We should have one byte on stdin
00045     char byte;
00046     int ret = read(m_fd, &byte, 1);
00047     if (ret == -1)
00048     {
00049         cerr << "ERROR: error reading input" << endl;
00050         exit(1);
00051     }
00052     else if (ret == 0)
00053     {
00054         return;
00055     }
00056     else if (byte != idx)
00057     {
00058         cerr << "ERROR: expected " << (char)idx << ", got " << byte << endl;
00059         exit(1);
00060     }
00061     ++idx;
00062 }
00063 
00064 // void Consumer::errorHook() {}
00065 // void Consumer::stopHook() {}
00066 // void Consumer::cleanupHook() {}
00067 


orogen
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Sat Jun 8 2019 19:52:17