TOBI SignalServer
0.1
|
00001 /* 00002 This file is part of the TOBI SignalServer. 00003 00004 Commercial Usage 00005 Licensees holding valid Graz University of Technology Commercial 00006 licenses may use this file in accordance with the Graz University 00007 of Technology Commercial License Agreement provided with the 00008 Software or, alternatively, in accordance with the terms contained in 00009 a written agreement between you and Graz University of Technology. 00010 00011 -------------------------------------------------- 00012 00013 GNU General Public License Usage 00014 Alternatively, this file may be used under the terms of the GNU 00015 General Public License version 3.0 as published by the Free Software 00016 Foundation and appearing in the file gpl.txt included in the 00017 packaging of this file. Please review the following information to 00018 ensure the GNU General Public License version 3.0 requirements will be 00019 met: http://www.gnu.org/copyleft/gpl.html. 00020 00021 In case of GNU General Public License Usage ,the TOBI SignalServer 00022 is distributed in the hope that it will be useful, 00023 but WITHOUT ANY WARRANTY; without even the implied warranty of 00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00025 GNU General Public License for more details. 00026 00027 You should have received a copy of the GNU General Public License 00028 along with the TOBI SignalServer. If not, see <http://www.gnu.org/licenses/>. 00029 00030 Copyright 2010 Graz University of Technology 00031 Contact: SignalServer@tobi-project.org 00032 */ 00033 00038 #include "hardware/event_listener.h" 00039 00040 #include <boost/bind.hpp> 00041 #include <boost/lexical_cast.hpp> 00042 00043 namespace tobiss 00044 { 00045 using boost::lexical_cast; 00046 using boost::bad_lexical_cast; 00047 00048 using boost::uint32_t; 00049 using std::vector; 00050 using std::cout; 00051 using std::endl; 00052 00053 //----------------------------------------------------------------------------- 00054 00055 EventListener::EventListener(boost::asio::io_service& io) : 00056 // HWThread(parser), 00057 initialized_(0), 00058 event_socket_udp_(io), 00059 buffer_(EVENT_BUFFER_SIZE) 00060 { 00061 00062 setType("EventListener"); 00063 00064 nr_ch_ = 0; 00065 blocks_ = 0; 00066 homogenous_signal_type_ = 1; 00067 // for(unsigned int n = 0; n< nr_ch_; n++) 00068 // { 00069 // channel_info_[n] = make_pair("marker",SIG_EVENT); 00070 // channel_types_.push_back(SIG_USER_2); 00071 // } 00072 00073 boost::system::error_code ec; 00074 boost::asio::ip::udp::endpoint udp_endpoint(boost::asio::ip::udp::v4(), 12344); 00075 event_socket_udp_.open(boost::asio::ip::udp::v4(), ec); 00076 if (!ec) 00077 { 00078 event_socket_udp_.bind(udp_endpoint, ec); 00079 boost::asio::socket_base::broadcast bcast(true); 00080 event_socket_udp_.set_option(bcast); 00081 } 00082 initialized_ = 1; 00083 cout << " * EventListener sucessfully initialized" << endl; 00084 } 00085 //----------------------------------------------------------------------------- 00086 EventListener::~EventListener() 00087 { 00088 boost::system::error_code ec; 00089 event_socket_udp_.close(ec); 00090 } 00091 00092 //----------------------------------------------------------------------------- 00093 00094 SampleBlock<double> EventListener::getAsyncData() 00095 { 00096 boost::shared_lock<boost::shared_mutex> lock(rw_); 00097 // for(unsigned int n = 0; n < 16-events_.size(); n++) 00098 // events_.push_back(0); 00099 data_.init(1, events_.size() , vector<uint32_t>(events_.size(), SIG_EVENT) ); 00100 data_.setSamples(events_); 00101 00102 events_.clear(); 00103 samples_available_ = false; 00104 lock.unlock(); 00105 return(data_); 00106 } 00107 00108 //----------------------------------------------------------------------------- 00109 00110 void EventListener::run() 00111 { 00112 event_socket_udp_.async_receive(boost::asio::buffer(buffer_), 00113 boost::bind(&EventListener::listen4Events, this, 00114 boost::asio::placeholders::error, 00115 boost::asio::placeholders::bytes_transferred)); 00116 } 00117 00118 //----------------------------------------------------------------------------- 00119 00120 void EventListener::stop() 00121 { 00122 00123 } 00124 00125 //----------------------------------------------------------------------------- 00126 00127 void EventListener::listen4Events(const boost::system::error_code& error, 00128 std::size_t bytes_transferred) 00129 { 00130 if(error) 00131 throw std::runtime_error(error.message()); 00132 std::string event; 00133 00134 // boost::unique_lock<boost::shared_mutex> lock(rw_); 00135 // boost::unique_lock<boost::mutex> syn(sync_mut_); 00136 // samples_available_ = true; 00137 for(uint32_t n = 0; n < bytes_transferred; n++) 00138 { 00139 if(buffer_[n] == '\n') 00140 { 00141 try 00142 { 00143 events_.push_back(lexical_cast<double>(event)); 00144 } 00145 catch(bad_lexical_cast &) 00146 { 00147 } 00148 event = ""; 00149 } 00150 else 00151 { 00152 event += buffer_[n]; 00153 } 00154 } 00155 // events_.push_back(lexical_cast<double>(event)); 00156 // lock.unlock(); 00157 // cond_.notify_all(); 00158 // syn.unlock(); 00159 00160 cout << " Event Codes: " << endl; 00161 for(uint32_t n = 0; n < events_.size(); n++) 00162 cout << " " << events_[n] << endl; 00163 00164 cout << endl; 00165 run(); 00166 } 00167 00168 //----------------------------------------------------------------------------- 00169 00170 } //Namespace tobiss