TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/src/hardware/eeg_simulator.cpp
Go to the documentation of this file.
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/eeg_simulator.h"
00039 
00040 #include <cmath>
00041 
00042 #include <boost/lexical_cast.hpp>
00043 #include <boost/cstdint.hpp>
00044 
00045 namespace tobiss
00046 {
00047 
00048 using boost::uint16_t;
00049 using std::vector;
00050 using std::string;
00051 using std::cout;
00052 using std::cerr;
00053 using std::endl;
00054 using std::make_pair;
00055 
00056 using boost::lexical_cast;
00057 using boost::bad_lexical_cast;
00058 
00059 const HWThreadBuilderTemplateRegistrator<EEGSimulator> EEGSimulator::factory_registrator_ ("eegsim", "eegsimulator");
00060 
00061 const std::string EEGSimulator::xml_eeg_sim_port_("port");
00062 const std::string EEGSimulator::xml_eeg_config_("eeg_config");
00063 const std::string EEGSimulator::xml_sine_config_("sine_config");
00064 const std::string EEGSimulator::xml_scaling_("scaling");
00065 const std::string EEGSimulator::xml_offset_("offset");
00066 const std::string EEGSimulator::xml_frequ_("frequ");
00067 const std::string EEGSimulator::xml_amplitude_("amplitude");
00068 const std::string EEGSimulator::xml_phase_("phase");
00069 
00070 static const float EEG_DIST_MEAN = 0;
00071 static const float EEG_DIST_STD  = 11;  // lt. Martin Billinger, BCI Comp4, Graz Dataset A, 4.11.2010
00072 
00073 
00074 //static const std::string EEGSIM_MSG_STRING("eegsimconfig");
00075 //static const std::string EEGSIM_EEG_STRING("eeg");
00076 //static const std::string EEGSIM_SINE_STRING("sine");
00077 
00078 //static const std::string EEGSIM_MSG_CMD_DELIMITER(":");
00079 //static const std::string EEGSIM_MSG_PARAM_DELIMITER("/");
00080 //static const std::string EEGSIM_MSG_CHANNEL_DELIMITER(",");
00081 
00082 //static const std::string EEGSIM_MSG_GETCONFIG("getconfig");
00083 //static const std::string EEGSIM_MSG_CONFIG("config");
00084 //static const std::string EEGSIM_MSG_OK("ok");
00085 //static const std::string EEGSIM_MSG_ERROR("error");
00086 
00087 
00088 //static const unsigned int MESSAGE_BUFFER_SIZE_BYTE = 4096;
00089 
00090 //-----------------------------------------------------------------------------
00091 
00092 EEGSimulator::EEGSimulator(boost::asio::io_service& io,
00093                            ticpp::Iterator<ticpp::Element> hw)
00094  : ArtificialSignalSource(io, hw), twister_(static_cast<unsigned int>(std::time(0))),
00095    eeg_dist_(EEG_DIST_MEAN,EEG_DIST_STD), eeg_gen_(twister_,eeg_dist_),
00096    acceptor_(io), socket_(io), port_(0), connected_(0), parser_(str_buffer_)
00097 {
00098 
00099 //  msg_types_map_.insert(std::make_pair( EEGSIM_MSG_GETCONFIG, GetConfig ) );
00100 //  msg_types_map_.insert(std::make_pair( EEGSIM_MSG_CONFIG, Config ) );
00101 
00102   setType("EEG Simulator");
00103 //  message_buffer_.resize(MESSAGE_BUFFER_SIZE_BYTE);
00104 
00105   setHardware(hw);
00106 
00107   boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port_);
00108   acceptor_.open(endpoint.protocol());
00109   acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(false));
00110   acceptor_.bind(endpoint);
00111 
00112   acceptor_.listen();
00113   acceptor_.async_accept(socket_, boost::bind(&EEGSimulator::acceptHandler, this,
00114                                               boost::asio::placeholders::error));
00115   init();
00116   cout << " * EEGSimulator sucessfully initialized" << endl;
00117   cout << "    fs: " << fs_ << "Hz, nr of channels: " << nr_ch_  << ", blocksize: " << blocks_  << endl;
00118 }
00119 
00120 //-----------------------------------------------------------------------------
00121 
00122 EEGSimulator::~EEGSimulator()
00123 {
00124 
00125 }
00126 
00127 //-----------------------------------------------------------------------------
00128 
00129 void EEGSimulator::generateSignal()
00130 {
00131   #ifdef DEBUG
00132     cout << "EEGSimulator: generateSignal" << endl;
00133   #endif
00134 
00135   boost::unique_lock<boost::shared_mutex> lock(rw_);
00136 
00137   // create EEG
00138   std::map<boost::uint16_t, EEGSimMsgParser::EEGConfig>::iterator eeg_it;
00139   for(boost::uint16_t n = 0; n < nr_ch_ ; n++)
00140   {
00141     if( (eeg_it = eeg_config_.find(n)) != eeg_config_.end() )
00142       samples_[n] = eeg_gen_() * eeg_it->second.scaling_ + eeg_it->second.offset_;
00143     else
00144       samples_[n] = eeg_gen_();
00145   }
00146 
00147   // add sine waves
00148   std::multimap<boost::uint16_t, EEGSimMsgParser::SineConfig>::iterator sine_it;
00149 
00150   for(boost::uint16_t n = 0; n < nr_ch_ ; n++)
00151   {
00152     for (sine_it = sine_configs_.equal_range(n).first;
00153          sine_it != sine_configs_.equal_range(n).second;
00154          ++sine_it)
00155     {
00156       samples_[n] += sine_it->second.amplitude_ *
00157                      sin(step_ * 2 * M_PI * sine_it->second.freq_ + sine_it->second.phase_);
00158     }
00159   }
00160 
00161   (step_ < 1-cycle_dur_ ? step_ += cycle_dur_ : step_ = 0);
00162   t_->expires_at(t_->expires_at() + td_);
00163 
00164   if(blocks_  == 1)
00165   {
00166 
00167     boost::unique_lock<boost::mutex> syn(sync_mut_);
00168     samples_available_ = true;
00169     data_.setSamples(samples_);
00170     lock.unlock();
00171     cond_.notify_all();
00172     if(isMaster() && acquiring_)
00173     {
00174       cond_.wait(sync_mut_);
00175       // if( !cond_.timed_wait(sync_mut_, td_))
00176       //   cerr << "Warning: New data was not fetched fast enough!" << endl;
00177       //   throw std::runtime_error("SineGenerator::genSine() -- Timeout; New data was not fetched fast enough!");
00178     }
00179     syn.unlock();
00180   }
00181   else
00182   {
00183     buffer_.appendBlock(samples_, 1);
00184     current_block_++;
00185 
00186     if(current_block_ == blocks_ )
00187     {
00188       boost::unique_lock<boost::mutex> syn(sync_mut_);
00189       samples_available_ = true;
00190       data_ = buffer_;
00191       lock.unlock();
00192       cond_.notify_all();
00193       buffer_.reset();
00194       current_block_ = 0;
00195       if(isMaster() && acquiring_)
00196       {
00197         cond_.wait(sync_mut_);
00198         // if( !cond_.timed_wait(sync_mut_, td_))
00199         //   cerr << "Warning: New data was not fetched fast enough!" << endl;
00200         //   throw std::runtime_error("SineGenerator::genSine() -- Timeout; New data was not fetched fast enough!");
00201       }
00202       syn.unlock();
00203     }
00204   }
00205 
00206   if(running_)
00207     t_->async_wait(boost::bind(&EEGSimulator::generateSignal, this));
00208 }
00209 
00210 //-----------------------------------------------------------------------------
00211 
00212 void EEGSimulator::setHardware(ticpp::Iterator<ticpp::Element>const &hw)
00213 {
00214   #ifdef DEBUG
00215     cout << "EEGSimulator: setHardware" << endl;
00216   #endif
00217 
00218   checkMandatoryHardwareTags(hw);
00219   ticpp::Iterator<ticpp::Element> ds(hw->FirstChildElement(hw_devset_, true));
00220 
00221   setDeviceSettings(ds);
00222 
00223   ticpp::Iterator<ticpp::Element> cs(hw->FirstChildElement(hw_chset_, false));
00224   if (cs != cs.end())
00225   {
00226     for(ticpp::Iterator<ticpp::Element> it(cs); ++it != it.end(); )
00227       if(it->Value() == hw_chset_)
00228       {
00229       string ex_str(type_ + " -- ");
00230         ex_str += "Multiple channel_settings found!";
00231         throw(std::invalid_argument(ex_str));
00232       }
00233       setChannelSettings(cs);
00234   }
00235 }
00236 
00237 //-----------------------------------------------------------------------------
00238 
00239 void EEGSimulator::setDeviceSettings(ticpp::Iterator<ticpp::Element>const &father)
00240 {
00241   #ifdef DEBUG
00242     cout << "EEGSimulator: setDeviceSettings" << endl;
00243   #endif
00244 
00245   ticpp::Iterator<ticpp::Element> elem(father->FirstChildElement(hw_fs_,true));
00246   setSamplingRate(elem);
00247 
00248   elem = father->FirstChildElement(hw_channels_,false);
00249   if(elem != elem.end())
00250     setDeviceChannels(elem);
00251 
00252   elem = father->FirstChildElement(hw_blocksize_,false);
00253   if(elem != elem.end())
00254     setBlocks(elem);
00255 
00256   elem = father->FirstChildElement(xml_eeg_sim_port_,true);
00257   if(elem != elem.end())
00258     setPort(elem);
00259 
00260   elem = father->FirstChildElement(xml_eeg_config_,false);
00261   if(elem != elem.end())
00262     setDeviceEEGConfig(elem);
00263 
00264   elem = father->FirstChildElement(xml_sine_config_,false);
00265   if(elem != elem.end())
00266     setDeviceSineConfig(elem);
00267 }
00268 
00269 //---------------------------------------------------------------------------------------
00270 
00271 void EEGSimulator::setChannelSettings(ticpp::Iterator<ticpp::Element>const &father)
00272 {
00273   #ifdef DEBUG
00274     cout << "EEGSimulator: setChannelSettings" << endl;
00275   #endif
00276 
00277   ticpp::Iterator<ticpp::Element> elem(father->FirstChildElement(hw_chset_sel_,false));
00278   if (elem != elem.end())
00279     setChannelSelection(elem);
00280 
00281   elem = father->FirstChildElement(xml_eeg_config_,false);
00282   if(elem != elem.end())
00283     setChannelEEGConfig(elem);
00284 
00285   elem = father->FirstChildElement(xml_sine_config_,false);
00286   if(elem != elem.end())
00287     setChannelSineConfig(elem);
00288 }
00289 
00290 //-----------------------------------------------------------------------------
00291 
00292 void EEGSimulator::setPort(ticpp::Iterator<ticpp::Element>const &elem)
00293 {
00294   boost::int16_t port = 0;
00295   try
00296   {
00297     port = lexical_cast<boost::int16_t>(elem->GetText(true));
00298   }
00299   catch(bad_lexical_cast &)
00300   {
00301     string ex_str(type_ + " -- Port: value is not a number!");
00302     throw(std::invalid_argument(ex_str));
00303   }
00304   if(port <= 0)
00305   {
00306     string ex_str(type_ + " -- Port: value is <= 0!");
00307     throw(std::invalid_argument(ex_str));
00308   }
00309   port_ = port;
00310 }
00311 
00312 //-----------------------------------------------------------------------------
00313 
00314 void EEGSimulator::setDeviceEEGConfig(ticpp::Iterator<ticpp::Element>const &elem)
00315 {
00316   EEGSimMsgParser::EEGConfig eeg_cfg = getEEGConfig(elem);
00317 
00318   for(unsigned int n = 0; n < channel_info_.size(); n++)
00319     eeg_config_.insert(  make_pair(n, eeg_cfg) );
00320 }
00321 
00322 //-----------------------------------------------------------------------------
00323 
00324 void EEGSimulator::setDeviceSineConfig(ticpp::Iterator<ticpp::Element>const &elem)
00325 {
00326   EEGSimMsgParser::SineConfig sine_cfg = getSineConfig(elem);
00327 
00328   for(unsigned int n = 0; n < channel_info_.size(); n++)
00329     sine_configs_.insert(  make_pair(n, sine_cfg) );
00330 }
00331 
00332 //-----------------------------------------------------------------------------
00333 
00334 void EEGSimulator::setChannelEEGConfig(ticpp::Iterator<ticpp::Element>const &father)
00335 {
00336   ticpp::Iterator<ticpp::Element> elem;
00337   elem = father->FirstChildElement(hw_chset_ch_,false);
00338   for(  ; elem != elem.end(); elem++)
00339     if(elem->Value() == hw_chset_ch_)
00340     {
00341       if(!elem.Get()->HasAttribute(hw_ch_nr_))
00342       {
00343         string ex_str(type_ + " -- ");
00344         ex_str += "Tag <"+xml_eeg_config_+"> given, but channel number ("+hw_ch_nr_+") not given!";
00345         throw(std::invalid_argument(ex_str));
00346       }
00347       checkEEGConfigAttributes(elem);
00348 
00349       uint16_t ch = 0;
00350       try{
00351         ch = lexical_cast<uint16_t>( elem.Get()->GetAttribute(hw_ch_nr_) );
00352       }
00353       catch(bad_lexical_cast &)
00354       {
00355         string ex_str(type_ + " -- ");
00356         ex_str += "Tag <"+ xml_eeg_config_ + "> : Channel is not a number!";
00357         throw(std::invalid_argument(ex_str));
00358       }
00359       if( channel_info_.find(ch) ==  channel_info_.end() )
00360       {
00361         string ex_str(type_ + " -- ");
00362         ex_str += "Tag <"+ xml_eeg_config_ + "> - Channel "+ lexical_cast<string>(ch) +" not set!";
00363         throw(std::invalid_argument(ex_str));
00364       }
00365 
00366       eeg_config_[ch-1] =  getEEGConfig(elem) ;
00367     }
00368     else
00369       throw(std::invalid_argument("EEGSimulator::setChannelEEGConfig -- Tag not equal to \""+hw_chset_ch_+"\"!"));
00370 }
00371 
00372 //-----------------------------------------------------------------------------
00373 
00374 void EEGSimulator::setChannelSineConfig(ticpp::Iterator<ticpp::Element>const &father)
00375 {
00376   ticpp::Iterator<ticpp::Element> elem;
00377   elem = father->FirstChildElement(hw_chset_ch_,false);
00378   for(  ; elem != elem.end(); elem++)
00379     if(elem->Value() == hw_chset_ch_)
00380     {
00381       if(!elem.Get()->HasAttribute(hw_ch_nr_))
00382       {
00383         string ex_str(type_ + " -- ");
00384         ex_str += "Tag <"+xml_sine_config_+"> given, but channel number ("+hw_ch_nr_+") not given!";
00385         throw(std::invalid_argument(ex_str));
00386       }
00387       checkSineConfigAttributes(elem);
00388 
00389       uint16_t ch = 0;
00390       try{
00391         ch = lexical_cast<uint16_t>( elem.Get()->GetAttribute(hw_ch_nr_) );
00392       }
00393       catch(bad_lexical_cast &)
00394       {
00395         string ex_str(type_ + " -- ");
00396         ex_str += "Tag <"+ xml_sine_config_ + "> : Channel is not a number!";
00397         throw(std::invalid_argument(ex_str));
00398       }
00399       if( channel_info_.find(ch) ==  channel_info_.end() )
00400       {
00401         string ex_str(type_ + " -- ");
00402         ex_str += "Tag <"+ xml_sine_config_ + "> - Channel "+ lexical_cast<string>(ch) +" not set!";
00403         throw(std::invalid_argument(ex_str));
00404       }
00405 
00406       setSineConfigInMultimap(ch, getSineConfig(elem) );
00407     }
00408     else
00409       throw(std::invalid_argument("EEGSimulator::setChannelSineConfig -- Tag not equal to \""+hw_chset_ch_+"\"!"));
00410 }
00411 
00412 //---------------------------------------------------------------------------------------
00413 
00414 void EEGSimulator::setSineConfigInMultimap(boost::uint16_t ch, EEGSimMsgParser::SineConfig config)
00415 {
00416 
00417   std::pair< std::multimap<boost::uint16_t,EEGSimMsgParser::SineConfig>::iterator,
00418              std::multimap<boost::uint16_t,EEGSimMsgParser::SineConfig>::iterator > range(sine_configs_.equal_range(ch-1));
00419 
00420   bool found = 0;
00421   for(std::multimap<boost::uint16_t, EEGSimMsgParser::SineConfig> ::iterator it(range.first);
00422       it != range.second; it++)
00423   {
00424     if(config.freq_ == it->second.freq_)
00425     {
00426       it->second = config;
00427       found = 1;
00428     }
00429   }
00430 
00431   if(!found)
00432     sine_configs_.insert(  make_pair(ch-1, config ) );
00433 
00434 }
00435 
00436 //---------------------------------------------------------------------------------------
00437 
00438 void EEGSimulator::checkEEGConfigAttributes(ticpp::Iterator<ticpp::Element>const &elem)
00439 {
00440 
00441   if(!elem.Get()->HasAttribute(xml_scaling_))
00442   {
00443     string ex_str(type_ + " -- ");
00444     ex_str += "Tag <"+xml_eeg_config_+"> given, EEG scaling ("+xml_scaling_+") not given!";
00445     throw(std::invalid_argument(ex_str));
00446   }
00447   if(!elem.Get()->HasAttribute(xml_offset_))
00448   {
00449     string ex_str(type_ + " -- ");
00450     ex_str += "Tag <"+xml_eeg_config_+"> given, EEG offset ("+xml_offset_+") not given!";
00451     throw(std::invalid_argument(ex_str));
00452   }
00453 }
00454 
00455 //---------------------------------------------------------------------------------------
00456 
00457 void EEGSimulator::checkSineConfigAttributes(ticpp::Iterator<ticpp::Element>const &elem)
00458 {
00459 
00460   if(!elem.Get()->HasAttribute(xml_frequ_))
00461   {
00462     string ex_str(type_ + " -- ");
00463     ex_str += "Tag <"+xml_sine_config_+"> given, EEG scaling ("+xml_frequ_+") not given!";
00464     throw(std::invalid_argument(ex_str));
00465   }
00466   if(!elem.Get()->HasAttribute(xml_amplitude_))
00467   {
00468     string ex_str(type_ + " -- ");
00469     ex_str += "Tag <"+xml_sine_config_+"> given, EEG offset ("+xml_amplitude_+") not given!";
00470     throw(std::invalid_argument(ex_str));
00471   }
00472   if(!elem.Get()->HasAttribute(xml_phase_))
00473   {
00474     string ex_str(type_ + " -- ");
00475     ex_str += "Tag <"+xml_sine_config_+"> given, EEG offset ("+xml_phase_+") not given!";
00476     throw(std::invalid_argument(ex_str));
00477   }
00478 }
00479 
00480 //-----------------------------------------------------------------------------
00481 
00482 EEGSimMsgParser::EEGConfig EEGSimulator::getEEGConfig(ticpp::Iterator<ticpp::Element>const &elem)
00483 {
00484   checkEEGConfigAttributes(elem);
00485 
00486   EEGSimMsgParser::EEGConfig eeg_cfg;
00487   try
00488   {
00489     eeg_cfg.scaling_ = lexical_cast<double>( elem.Get()->GetAttribute(xml_scaling_) );
00490     eeg_cfg.offset_  = lexical_cast<double>( elem.Get()->GetAttribute(xml_offset_) );
00491   }
00492   catch(bad_lexical_cast &)
00493   {
00494     string ex_str(type_ + " -- ");
00495     ex_str += "Tag <"+xml_eeg_config_+"> given, but scaling or offset is not a number!";
00496     throw(std::invalid_argument(ex_str));
00497   }
00498 
00499   return(eeg_cfg);
00500 }
00501 
00502 //-----------------------------------------------------------------------------
00503 
00504 EEGSimMsgParser::SineConfig EEGSimulator::getSineConfig(ticpp::Iterator<ticpp::Element>const &elem)
00505 {
00506 
00507   checkSineConfigAttributes(elem);
00508 
00509   EEGSimMsgParser::SineConfig sine_cfg;
00510   try
00511   {
00512     sine_cfg.freq_ = lexical_cast<double>( elem.Get()->GetAttribute(xml_frequ_) );
00513     sine_cfg.amplitude_  = lexical_cast<double>( elem.Get()->GetAttribute(xml_amplitude_) );
00514     sine_cfg.phase_  = lexical_cast<double>( elem.Get()->GetAttribute(xml_phase_) );
00515   }
00516   catch(bad_lexical_cast &)
00517   {
00518     string ex_str(type_ + " -- ");
00519     ex_str += "Tag <"+xml_sine_config_+"> given, but frequency, amplitude or phase is not a number!";
00520     throw(std::invalid_argument(ex_str));
00521   }
00522 
00523   return(sine_cfg);
00524 }
00525 
00526 //-----------------------------------------------------------------------------
00527 
00528 void EEGSimulator::handleAsyncRead(const boost::system::error_code& ec,
00529                                      std::size_t bytes_transferred )
00530 {
00531   try
00532   {
00533     if(ec)
00534       throw(std::runtime_error("EEGSimulator::handleAsyncRead() -- Error handling async read -- bytes transferred: " + bytes_transferred));
00535 
00536     str_buffer_.clear();
00537     std::istream is(&message_buffer_);
00538     std::getline(is, str_buffer_);
00539     is.get();
00540 
00541 
00542     boost::system::error_code  ec;
00543 
00544     parser_.parseMessage();
00545     EEGSimMsgParser::MessageType msg_type = parser_.getMessageType();
00546     std::map<boost::uint16_t, EEGSimMsgParser::EEGConfig> eeg;
00547     std::multimap<boost::uint16_t, EEGSimMsgParser::SineConfig> sine;
00548 
00549     switch (msg_type)
00550     {
00551     case EEGSimMsgParser::Invalid:
00552       throw(std::invalid_argument("Error -- Got invalid message type!"));
00553       break;
00554     case EEGSimMsgParser::GetConfig:
00555       socket_.send(boost::asio::buffer(parser_.buildConfigMsgString(eeg_config_, sine_configs_)),
00556                    0,ec);
00557       if(ec)
00558         throw(std::runtime_error("Error -- " + ec.message()  ));
00559       boost::asio::async_read_until(socket_,
00560                                     message_buffer_, '\n',
00561                                     boost::bind(&EEGSimulator::handleAsyncRead, this,
00562                                                 boost::asio::placeholders::error,
00563                                                 boost::asio::placeholders::bytes_transferred) );
00564 
00565       return;
00566 
00567     case EEGSimMsgParser::Config:
00568 
00569       parser_.getConfigs(eeg, sine);
00570       if(eeg.size())
00571         updateEEGConfig(eeg);
00572       if(sine.size())
00573         updateSineConfig(sine);
00574       break;
00575 
00576     default:
00577       throw(std::invalid_argument("Error -- Could not determine message type!"));
00578       break;
00579     }
00580     socket_.send(boost::asio::buffer( parser_.getOKMsg() ), 0,ec);
00581   }
00582   catch(std::exception& e)
00583   {
00584     boost::system::error_code  ec;
00585     socket_.send(boost::asio::buffer( parser_.getErrorMsg() + e.what() ), 0, ec);
00586 
00587     std::cerr << "  *** EEG Simulator -- Connection closed to client: " << peer_ip_ << std::endl;
00588 
00589     socket_.close(ec);
00590     peer_ip_.clear();
00591     acceptor_.async_accept(socket_, boost::bind(&EEGSimulator::acceptHandler, this,
00592                                                 boost::asio::placeholders::error));
00593 
00594     return;
00595   }
00596 
00597   boost::asio::async_read_until(socket_,
00598                           message_buffer_, '\n',
00599                           boost::bind(&EEGSimulator::handleAsyncRead, this,
00600                                       boost::asio::placeholders::error,
00601                                       boost::asio::placeholders::bytes_transferred) );
00602 
00603 }
00604 
00605 //-----------------------------------------------------------------------------
00606 
00607 void EEGSimulator::acceptHandler(const boost::system::error_code& error)
00608 {
00609   // if already conencted with a client --> abort connection
00610   // if not needed, delete connected_ member
00611 
00612   if (!error)
00613   {
00614     peer_ip_ = socket_.remote_endpoint().address().to_string();
00615     boost::asio::async_read_until(socket_,
00616                             message_buffer_, '\n',
00617                             boost::bind(&EEGSimulator::handleAsyncRead,
00618                                         this,
00619                                         boost::asio::placeholders::error,
00620                                         boost::asio::placeholders::bytes_transferred)
00621                             );
00622   }
00623   else
00624   {
00625     std::cerr << "EEGSimulator::acceptHandler" << error << std::endl;
00626   }
00627 
00628   if(!socket_.is_open())
00629     acceptor_.async_accept(socket_, boost::bind(&EEGSimulator::acceptHandler, this,
00630                                                 boost::asio::placeholders::error));
00631 }
00632 
00633 //-----------------------------------------------------------------------------
00634 
00635 void EEGSimulator::updateEEGConfig(std::map<boost::uint16_t, EEGSimMsgParser::EEGConfig>& eeg)
00636 {
00637 
00638   boost::unique_lock<boost::shared_mutex> lock(rw_);
00639 
00640   // TODO: modify sine- or eeg config maps
00641   std::cout << "updateEEGConfig" << std::endl;
00642   eeg_config_ = eeg;
00643   lock.unlock();
00644 
00645 }
00646 
00647 //-----------------------------------------------------------------------------
00648 
00649 void EEGSimulator::updateSineConfig(std::multimap<boost::uint16_t, EEGSimMsgParser::SineConfig>& sine)
00650 {
00651 
00652   boost::unique_lock<boost::shared_mutex> lock(rw_);
00653 
00654   // TODO: modify sine- or eeg config maps
00655   std::cout << "updateSineConfig" << std::endl;
00656   sine_configs_ = sine;
00657   lock.unlock();
00658 
00659 }
00660 
00661 //-----------------------------------------------------------------------------
00662 
00663 }  // tobiss
 All Data Structures Files Functions Variables