void LadybugPlayerThread::seekToFrame(int frameId)
{
  QMutexLocker lockB(&mBufferMutex);
  mBuffer.clear();

  QMutexLocker lock(&mStreamMutex);
  mStream.seekToFrame(frameId);
}
Beispiel #2
0
void CSimulationServer::Run()
{
    Logger::Info << __PRETTY_FUNCTION__ << std::endl;
    
    boost::array<char,HEADER_SIZE> header;

    // create an acceptor on the shared I/O service
    boost::asio::ip::tcp::acceptor acceptor( m_service );
    
    // create an endpoint for IPv4 with m_port as a port number
    boost::asio::ip::tcp::endpoint endpoint( boost::asio::ip::tcp::v4(), m_port );
    Logger::Notice << "PSCAD will use port " << m_port << std::endl;
    
    // open the acceptor at the endpoint
    acceptor.open( endpoint.protocol() );
    acceptor.set_option( boost::asio::ip::tcp::acceptor::reuse_address(true) );
    acceptor.bind( endpoint );
    acceptor.listen();

    while( !m_quit )
    {
        // TODO: this blocks - makes m_quit rather pointless
        // create and accept the client connection
        boost::asio::ip::tcp::socket socket( m_service );
        acceptor.accept(socket);
        
        // read the header of the next received packet
        boost::asio::read( socket, boost::asio::buffer(header) );
        Logger::Debug << "PSCAD - received " << header.data() << std::endl;
        
        // message handler based on header type
        if( strcmp( header.data(), "RST" ) == 0 )
        {
            boost::unique_lock<boost::shared_mutex> lockA(m_state.m_mutex);
            boost::unique_lock<boost::shared_mutex> lockB(m_command.m_mutex);
            Logger::Debug << "PSCAD - obtained mutex as writer" << std::endl;
            size_t bytes = m_state.m_length * sizeof(double);
            
            // read the message body into the state table
            boost::asio::read( socket, boost::asio::buffer(m_state.m_data, bytes) );
            
            // copy the message body into the command table
            if( m_state.m_length != m_command.m_length )
            {
                // this implementation requires m_state == m_command
                Logger::Error << "Failed to handle RST message: " << 
                    "state and command are not uniform" << std::endl;
            }
            else
            {
                for( size_t i = 0; i < m_state.m_length; i++ )
                {
                    m_command.m_data[i] = m_state.m_data[i];
                }
            }
            
            Logger::Debug << "PSCAD - released writer mutex" << std::endl;
        }
        else if( strcmp( header.data(), "GET" ) == 0 )
        {
            boost::shared_lock<boost::shared_mutex> lock(m_command.m_mutex);
            Logger::Debug << "PSCAD - obtained mutex as reader" << std::endl;
            size_t bytes = m_command.m_length * sizeof(double);
            
            // write the command table as a response
            boost::asio::write( socket, boost::asio::buffer(m_command.m_data, bytes) );
            Logger::Debug << "PSCAD - released reader mutex" << std::endl;
        }
        else if( strcmp( header.data(), "SET" ) == 0)
        {
            boost::unique_lock<boost::shared_mutex> lock(m_state.m_mutex);
            Logger::Debug << "PSCAD - obtained mutex as writer" << std::endl;
            size_t bytes = m_state.m_length * sizeof(double);
            
            // read the message body into the state table
            boost::asio::read( socket, boost::asio::buffer(m_state.m_data, bytes) );
            Logger::Debug << "PSCAD - released writer mutex" << std::endl;
        }
        else if( strcmp( header.data(), "QUIT" ) == 0 )
        {
            Stop();
        }
        else
        {
            Logger::Warn << "PSCAD - received unhandled message" << std::endl;
        }
        
        socket.close();
    }
}