void speech_synthesizer::send_message(const tts_message& msg)
 {
   threading::lock message_lock(message_mutex);
   if(status!=status_empty)
     return;
   current_message=msg;
   status=status_message;
   message_available.signal();
 }
 bool speech_synthesizer::get_message(tts_message& msg)
 {
   threading::lock message_lock(message_mutex);
   if(status==status_stopped)
     return false;
   while(status!=status_message)
     {
       message_available.wait(message_mutex);
       if(status==status_stopped)
         return false;
     }
   msg=current_message;
   status=status_empty;
   return true;
 }
 void speech_synthesizer::signal_stop()
 {
   threading::lock message_lock(message_mutex);
   status=status_stopped;
   message_available.signal();
 }
Example #4
0
/*
 * Handles asyncronous read operations
 *  - sets message_ from server(s)
 *  - removes any disconnected servers
 */
void Communication::ReadHandler(const boost::system::error_code& error,
                                std::size_t bytes_transferred,
                                boost::shared_ptr <boost::array<char, 30> > buffer,
                                boost::shared_ptr <std::string> server_name,
                                boost::shared_ptr<boost::asio::ip::tcp::socket> soc)
{
    com_mutex_.lock();

    // successful read
    if (bytes_transferred == 30)
    {
        boost::mutex::scoped_lock message_lock(message_mutex_);

        // let an unread message be read
        while (!is_picked_message_)
        {
            getting_message_condition_->wait(message_lock);
        }

        // write message_ from buffer
        message_ = buffer->data();
        message_ = message_.substr(0, 30);

        // clear the read buffer
        buffer->assign(0);

        is_picked_message_ = false;
        is_set_message_ = true;
        setting_message_condition_->notify_one();

        // async_read next message on this server
        boost::asio::async_read(*soc,
                                boost::asio::buffer(*buffer, 30),
                                boost::bind(&Communication::ReadHandler,
                                            this,
                                            boost::asio::placeholders::error,
                                            boost::asio::placeholders::bytes_transferred,
                                            buffer,
                                            server_name,
                                            soc));
    }

    else  // server is disconnected and will be removed
    {
        out_mutex_.lock();
        std::cout << "\n[" << boost::this_thread::get_id()
                  << "] << " << *server_name << " is now disconnected \n" << std::endl;
        out_mutex_.unlock();


        // close this socket
        soc->close();

        for (unsigned i=0; i<sockets_.size(); i++)
        {
            // find the closed socket
            if (!sockets_[i]->is_open())
            {
                // try to reconnect to this server
                std::vector<Connection> disconnected_server;
                disconnected_server.push_back(connections_[i]);
                Connect(1, disconnected_server);

                // erase all server related elements
                sockets_.erase(sockets_.begin()+i);
                servers_.erase(servers_.begin()+i);
                buf_.erase(buf_.begin()+i);
                is_reading_.erase(is_reading_.begin()+i);

                break;
            }
        }

        // check if all servers are disconnected
        if (sockets_.size() == 0)
        {
            // report for waiting on <RETURN> keystroke
            NoServerReport();
        }
    }

    com_mutex_.unlock();
}