void OculusSocketServerApp::update()
{
    if( mOculusVR  ) {
        orientation = mOculusVR->getOrientation();

        boost::format fmt("{ \"m\" : \"orientation\", \"o\" : [%f,%f,%f,%f] }");
        
        fmt % orientation.w;
        fmt % orientation.v.x;
        fmt % orientation.v.y;
        fmt % orientation.v.z;

        mServer.write(fmt.str());
    }
    
    if(mOculusVR && mOculusVR->isConnected()){
        if(!mOculusConnected){
            sendHMDConfig();
            mOculusConnected = true;
        }
    } else {
        mOculusConnected = false;
    }
    
    mServer.poll();
    mOculusVR->handleMessages();
}
void OculusSocketServerApp::sendHMDConfig(){

    if(mOculusVR && mOculusVR->isConnected() && mSocketConnected) {
        
        float fov = mOculusVR->getFov();
        float ipd = mOculusVR->getIPD();
        float lensDistance = mOculusVR->getLensSeparationDistance();
        float eyeToScreen = mOculusVR->getEyeToScreenDistance();
        Vec4f distortionValues = mOculusVR->getDistortionParams();
        Vec2f screenSize = mOculusVR->getScreenSize();
        Vec2f screenResolution = mOculusVR->getScreenResolution();
        
        boost::format fmt("{ \"m\" : \"config\", \"fov\" : %f, \"interpupillaryDistance\" : %f, \"eyeToScreen\" : %f, \"lensDistance\" : %f, \"distortion\" : [%f,%f,%f,%f], \"screenSize\" : [%f,%f], \"screenResolution\" : [%f,%f] }");
        
        fmt % fov;
        fmt % ipd;
        fmt % eyeToScreen;
        fmt % lensDistance;
        fmt % distortionValues.x;
        fmt % distortionValues.y;
        fmt % distortionValues.z;
        fmt % distortionValues.w;
        fmt % screenSize.x;
        fmt % screenSize.y;
        fmt % screenResolution.x;
        fmt % screenResolution.y;
        
        mServer.write(fmt.str());
    }
}
void OculusSocketServerApp::shutdown(){
    if(mSocketConnected){
        console() << "Closing socket connection." << std::endl;
        mServer.close();
    }
    
    if(mOculusVR){
        console() << "Disposing of Oculus." << std::endl;
        mOculusVR->destroy();
    }
    console() << "Done with shutdown()" << std::endl;
}
void OculusSocketServerApp::draw()
{
    gl::setMatricesWindow(getWindowWidth(), getWindowHeight());
    
    glColor3f(1.0, 1.0, 1.0);
    gl::draw(mBackgroundTexture);
    
    glColor3f(0.16078431372549, 0.83921568627451, 0.88235294117647);
    if(mSocketConnected) {
        gl::drawSolidRect( Rectf(358, 30, 370, 42) );
    }

    if(mOculusVR && mOculusVR->isConnected()) {
        gl::drawSolidRect( Rectf(358, 104, 370, 116) );
    }
}