void display(void)
{
/*
    std::cout << "FH timeStamp: " << FrameHandler::the()->getTimeStamp()
              << std::endl;

    std::cout <<   "xform0.translation: " << gXForm->getTranslation()
              << "\nxform0.rotation: " << gXForm->getRotation()
              << std::endl;
*/

    FrameHandler::the()->frame();

    commitChangesAndClear();

#if 0
    for(UInt32 i = 0; i < g_anim.size(); ++i)
    {
        g_anim[i].anim->getTimeSensor()->dump();
    }
#endif

    mgr->redraw();

    osgSleep(100);
}
// Redraw the window
void display(SimpleSceneManager *mgr)
{
    mgr->redraw();

    //Commit and Clear the change list
    //Since this is a single aspect, non-clustered application
    //The ChangeList can be cleared periodically
    commitChangesAndClear();
}
void WindowDrawTask::execute(HardwareContext *pContext, DrawEnv *pEnv)
{
    Window *pWindow = pEnv->getWindow();

    OSG_ASSERT(pWindow != NULL);

    switch(_uiTypeTask)
    {
        case Init:
        {
#ifdef OSG_DUMP_WINTASK
            fprintf(stderr, "Init\n");
            fflush(stderr);
#endif
            if(_bCreatePrivateContext == true)
                pWindow->init();

            pWindow->doActivate   ();
            pWindow->doFrameInit  (_bReinitExtFunctions);
            pWindow->setupGL      ();
            pWindow->setOpenGLInit();

            if(_oInitFunc)
            {
                _oInitFunc();
            }

            if(pWindow->getKeepContextActive() == false)
                pWindow->doDeactivate();
        }
        break;

        case Activate:
        {
#ifdef OSG_DUMP_WINTASK
            fprintf(stderr, "Activate\n");
            fflush(stderr);
#endif
            if(pWindow->getKeepContextActive() == false)
                pWindow->doActivate();
        }
        break;

        case FrameInit:
        {
#ifdef OSG_DUMP_WINTASK
            fprintf(stderr, "FrameInit\n");
            fflush(stderr);
#endif

            if(pWindow->getKeepContextActive() == false)
                pWindow->doActivate();

            pWindow->doFrameInit();
        }
        break;

        case FrameExit:
        {
#ifdef OSG_DUMP_WINTASK
            fprintf(stderr, "FrameExit\n");
            fflush(stderr);
#endif
            pWindow->doFrameExit();

            if(pWindow->getKeepContextActive() == false)
                pWindow->doDeactivate();

            commitChangesAndClear();
        }
        break;

        case WaitAtBarrier:
        {
#ifdef OSG_DUMP_WINTASK
            fprintf(stderr, "WaitAtBarrier\n");
            fflush(stderr);
#endif
            OSG_ASSERT(_pBarrier != NULL);
            _pBarrier->enter();
        }
        break;

        case Swap:
        {           
#ifdef OSG_DUMP_WINTASK
            fprintf(stderr, "Swap\n");
            fflush(stderr);
#endif
            pWindow->doSwap();

#ifdef OSG_SWAP_BARRIER
            OSG_ASSERT(_pBarrier != NULL);

            _pBarrier->enter();
#endif
        }
        break;

        case EndThread:
        {
            if(pWindow->getKeepContextActive() == false)
                pWindow->doActivate();

            pWindow->doFrameExit();
            
#ifdef OSG_WITH_CUDA
            if(0x0000 != (pWindow->getInitState() & 
                          HardwareContext::CudaInitialized))
            {
                cudaThreadExit();
            }
#endif
            pWindow->doDeactivate();
            pWindow->_pContextThread->endRunning();
        }
        break;

        default:
            break;
    }
}
void ClusterServer::doSync(bool applyToChangelist)
{
    // do we have a cluster window?
    if(_clusterWindow == NULL)
    {
        do
        {
            // recive
            _aspect->receiveSync(*_connection,applyToChangelist);
        }
        while(_clusterWindow == NULL);

        // get server id
        for(_serverId = 0;
                (_clusterWindow->getServers(_serverId) != _serviceName) &&
                (_serverId < _clusterWindow->getMFServers()->size());
                _serverId++) ;

        // server connected and cluster window found
        SINFO << "Start server " << _serviceName
              << " with id "     << _serverId
              << std::endl;

        // now the window is responsible for connection and aspect

        _clusterWindow->getNetwork()->setMainConnection(_connection);
        _clusterWindow->getNetwork()->setAspect        (_aspect);

        _connection = NULL;
        _aspect     = NULL;

        _clusterWindow->setDrawerId(_serverId);
        _clusterWindow->serverInit(_window,_serverId);
    }

    RemoteAspect *aspect     = _clusterWindow->getNetwork()->getAspect();
    Connection   *connection =
        _clusterWindow->getNetwork()->getMainConnection();

    // sync with render clinet
    aspect->receiveSync(*connection, applyToChangelist);

    // sync with render client
    if(_clusterWindow->getInterleave())
    {
        // if the reminder of the division of interleave and
        // framecount is equal to the servers id, the right
        // sync point for the current render frame is reached
        while( ( _clusterWindow->getFrameCount() %
                 _clusterWindow->getInterleave() )  !=
                (_serverId%_clusterWindow->getInterleave()) )
        {
            aspect->receiveSync(*connection, applyToChangelist);
        }
    }

    if(applyToChangelist)
    {
        commitChanges();
    }
    else
    {
        commitChangesAndClear();
    }
}
void RemoteAspect::receiveSync(Connection &connection, bool applyToChangelist)
{
    bool                              finish    = false;
    UInt8                             cmd;
    FieldContainerFactoryBase        *fcFactory = FieldContainerFactory::the();
    FieldContainerVector              newContainers;
    RemoteAspectFieldContainerMapper  mapper;
    ChangeList                       *pChangeList =
        Thread::getCurrentChangeList();

    if(_statistics)
    {
        _statistics->getElem(statSyncTime)->start();
    }

    connection.selectChannel();
    connection.getValue(_remoteAspectId);

    // register mapper into factory
    mapper._remoteAspect = this;

    fcFactory->setMapper(&mapper);

    do
    {
        connection.getValue(cmd);

        switch(cmd)
        {
            case SYNCENDED:
            {
                finish = true;

#ifndef OSG_REMOTE_ASPECT_SILENT
                SLOG << "Receive SYNCENDED\n";
#endif
            }
            break;

            case NEWTYPE:
            {
                receiveNewType(connection, fcFactory);
            }
            break;

            case CREATED:
            {
                receiveCreated(connection, fcFactory, newContainers);
            }
            break;

            case CHANGED:
            {
                receiveChanged(connection, fcFactory);
            }
            break;

            case ADDREFED:
            {
                receiveAddRefed(connection, fcFactory, pChangeList);
            }
            break;

            case SUBREFED:
            {
                receiveSubRefed(connection, fcFactory, pChangeList);
            }
            break;
            
            case IDMAPPING:
            {
                receiveIdMapping(connection);
            }
            break;
            
            default:
            {
                SFATAL << "Unknown tag:" << Int32(cmd) << std::endl;
                throw RemoteSyncError();
            }
        }
    } 
    while(!finish);

#ifndef OSG_REMOTE_ASPECT_SILENT
    PLOG << std::flush;
#endif

    pChangeList->commitDelayedSubRefs();

#ifdef OSG_DEBUG
    FieldContainerVector::const_iterator fcIt  = newContainers.begin();
    FieldContainerVector::const_iterator fcEnd = newContainers.end  ();

    for(; fcIt != fcEnd; ++fcIt)
    {
        if((*fcIt)->getRefCount() <= 1)
        {
            SWARNING << "New container type '" << (*fcIt)->getType().getName()
                     << "' local id '" << (*fcIt)->getId()
                     << "' remote id '"
                     << static_cast<UInt32>(_remoteFC[(*fcIt)->getId()])
                     << "' dies because of missing ref counts."
                     << std::endl;
        }
    }
#endif // OSG_DEBUG

    if(applyToChangelist)
    {
        commitChanges();
    }
    else
    {
        commitChangesAndClear();
    }

    // unregister mapper into factory
    fcFactory->setMapper(NULL);

    if(_statistics)
    {
        _statistics->getElem(statSyncTime)->stop();
    }
}