bool ZeroConfDiscoverEventHandler::handle(osgGA::Event* event, osg::Object* object, osg::NodeVisitor* nv) {
    
    


    if ((event->getName() == "/zeroconf/service-added") || (event->getName() == "/zeroconf/service-removed"))
    {
        std::string host(""), type("");
        unsigned int port(0);
            
        event->getUserValue("host", host);
        event->getUserValue("port", port);
        event->getUserValue("type", type);
        
        host = IOSUtils::lookupHost(host);
        
        if (type == httpServiceType())
        {
            std::ostringstream ss;
            ss << "http://" << host << ":" << port << "/interface.p3d";
            if (event->getName() == "/zeroconf/service-added") {
                if (!P3DAppInterface::instance()->getRemoteFiles()->add(ss.str())) {
                    OSG_WARN << "ZeroConfDiscoverEventHandler :: could not add " << ss.str() << " to remote files" << std::endl;
                }
            }
            else
            {
                if (!P3DAppInterface::instance()->getRemoteFiles()->remove(ss.str())) {
                    OSG_WARN << "ZeroConfDiscoverEventHandler :: could not remove " << ss.str() << " to remote files" << std::endl;
                }
            }
            
            P3DAppInterface::instance()->refreshInterface();
            
            return true;
        }
        else if (type == oscServiceType())
        {
            if (event->getName() == "/zeroconf/service-added")
            {
                P3DAppInterface::instance()->getOscController()->addAutoDiscoveredHostAndPort(host, port);
                P3DAppInterface::instance()->getOscController()->checkConnection();
            }
            else {
                P3DAppInterface::instance()->getOscController()->removeAutoDiscoveredHostAndPort(host, port);
                P3DAppInterface::instance()->getOscController()->checkConnection();
            }
            
            P3DAppInterface::instance()->refreshInterface();
            return true;
        }
    }
    
    if (!event->asGUIEventAdapter()) {
        forwardEvent(*event);
        return true;
    }
    
    return osgGA::GUIEventHandler::handle(event, object, nv);
}
void ZeroConfDiscoverEventHandler::setup(P3DAppInterface* app)
{
    // setup zeroconf
 
    std::vector<std::string> service_types;
    service_types.push_back(httpServiceType());
    service_types.push_back(oscServiceType());
    
    for(std::vector<std::string>::iterator i = service_types.begin(); i != service_types.end(); ++i)
    {
        osgGA::Device* device = osgDB::readFile<osgGA::Device>((*i)+".discover.zeroconf");
        if (device)
        {
            app->getViewer()->addDevice(device);
            
        }
        else
            OSG_WARN << "ZeroConfDiscoverEventHandler :: could not add zeroconf-device: " << (*i) << std::endl;
    }

}
bool ZeroConfDiscoverEventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv)
{
    IOSViewer* viewer = dynamic_cast<IOSViewer*>(&aa);
    if (!viewer)
        return false;

    // forward all key + user-events to devices
    if ((ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN) ||
            (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP) ||
            (ea.getEventType() == osgGA::GUIEventAdapter::USER))
    {
        for(osgViewer::View::Devices::iterator i = viewer->getDevices().begin(); i != viewer->getDevices().end(); ++i)
        {
            if ((*i)->getCapabilities() & osgGA::Device::SEND_EVENTS)
                (*i)->sendEvent(ea);
        }
    }

    if (ea.getEventType() == osgGA::GUIEventAdapter::USER)
    {
        // std::cout << "user-event: " << ea.getName() << std::endl;

        if (ea.getName() == "/zeroconf/service-added")
        {
            std::string host(""), type("");
            unsigned int port(0);

            ea.getUserValue("host", host);
            ea.getUserValue("port", port);
            ea.getUserValue("type", type);

            if (type == httpServiceType())
            {
                viewer->readScene(host, port);
            }
            else if (type == oscServiceType() && (!_discoveredDevice.valid()))
            {
                removeAllSendingOSCDevices(viewer);
                startEventForwarding(viewer, host, port);
            }
        }
        else if (ea.getName() == "/zeroconf/service-removed")
        {
            std::string type("");
            ea.getUserValue("type", type);

            if (type == httpServiceType())
                viewer->showMaintenanceScene();
            else if(type == oscServiceType())
            {
                removeAllSendingOSCDevices(viewer);
                _discoveredDevice = NULL;
            }
        }
    }
    if (ea.isMultiTouchEvent() && (ea.getTouchData()->getNumTouchPoints() == 1)) {
        if (ea.getTouchData()->begin()->tapCount == 3) {
            viewer->reloadDevices();

        }
    }
    return false;
}