Пример #1
0
Controller::Controller() :
    _deviceNeedResolve(0),
    _lastTimeFocused(0),
    _service(&_udpDiscovery, this),
    _udpDiscoveryTimer(this)
{
    _service.moveToThread(&_serviceThread);
    _serviceThread.start();

    _view = new View(&_model);
    _bonjourBrowser = new BonjourServiceBrowser(this);

    connect(&_udpDiscovery, SIGNAL(devicesFound(const QList<Device *> &)), this, SLOT(updateDevices(const QList<Device *> &)));
    connect(&_udpDiscovery, SIGNAL(pongReceived(const QString&)), this, SLOT(onPong(const QString &)));

    connect(_bonjourBrowser, SIGNAL(currentBonjourRecordsChanged(const QList<BonjourRecord> &)),
            this, SLOT(updateRecords(const QList<BonjourRecord> &)));
    connect(_bonjourBrowser, SIGNAL(error(DNSServiceErrorType)),
            this, SLOT(error(DNSServiceErrorType)));

    connect(_view, SIGNAL(sendFile(const QString&, const QList<QUrl>&, DataType)),
            this, SLOT(onSendFile(const QString&, const QList<QUrl>&, DataType)));
    connect(_view, SIGNAL(sendText(const QString&, const QString&, DataType)),
            this, SLOT(onSendText(const QString&, const QString&, DataType)));
    connect(_view, SIGNAL(cancelTransfert(const QString&)),
            this, SLOT(onCancelTransfert(const QString&)));
    connect(_view, SIGNAL(focused()), this, SLOT(onWindowFocused()));
    connect(_view, SIGNAL(forceRefresh()), this, SLOT(onForceRefresh()));

    connect(_view, SIGNAL(registerService()),
            &_service, SLOT(serviceRegister()));
    connect(_view, SIGNAL(unregisterService()),
            &_service, SLOT(serviceUnregister()));
    connect(_view, SIGNAL(deleteFromHistory(int)),
            &_service, SLOT(onDeleteFromHistory(int)));
    connect(_view, SIGNAL(clearHistoryTriggered()),
            &_service, SLOT(onClearHistory()));
    connect(_view, SIGNAL(serviceNameChanged()),
            &_service, SLOT(onTimerOut()));
    connect(_view, SIGNAL(cancelIncomingTransfert()),
            &_service, SLOT(deleteFileReset()));

    connect(&_service, SIGNAL(historyChanged(const QList<HistoryElement>&)),
            _view, SLOT(onHistoryChanged(const QList<HistoryElement>&)));
    connect(&_service, SIGNAL(historyElementProgressUpdated(unsigned)),
            _view, SLOT(historyElementProgressUpdated(unsigned)));

    connect(&_service, SIGNAL(serviceError(ServiceErrorState,bool)),
            _view, SLOT(onServiceError(ServiceErrorState,bool)));
    connect(&_service, SIGNAL(receivingFile(const QString&,int)),
             _view, SLOT(onReceivingFile(const QString&,int)));
    connect(&_service, SIGNAL(receivingFolder(const QString&,int)),
            _view, SLOT(onReceivingFolder(const QString&,int)));
    connect(&_service, SIGNAL(receivingText(const QString&)),
            _view, SLOT(onReceivingText(const QString&)));
    connect(&_service, SIGNAL(receivingUrl(const QString&)),
            _view, SLOT(onReceivingUrl(const QString&)));

    connect(&_model, SIGNAL(newDeviceCreated(Device*)),
            this, SLOT(onNewDeviceCreated(Device*)));
    connect(&_model, SIGNAL(deviceRemoved()),
            _view, SLOT(updateDevices()));

    connect(&_updater, SIGNAL(updateNeeded(const QString&,const QString&)),
            _view, SLOT(onUpdateNeeded(const QString&,const QString&)));

    checkForBonjourState();
    _view->updateDevices();
    _view->onHistoryChanged(_service.getHistory());

    _bonjourBrowser->browseForServiceType(QLatin1String("_fdnd._tcp."));

    connect(&_udpDiscoveryTimer, SIGNAL(timeout()), &_udpDiscovery, SLOT(startDiscovery()));
    _udpDiscoveryTimer.start(UDP_DISCOVERY_INTERVAL);
    _udpDiscovery.startDiscovery();

    createSendTo();
}
int MDnsSdListener::Handler::runCommand(SocketClient *cli,
                                        int argc, char **argv) {
    if (argc < 2) {
        char* msg = NULL;
        asprintf( &msg, "Invalid number of arguments to mdnssd: %i", argc);
        ALOGW("%s", msg);
        cli->sendMsg(ResponseCode::CommandParameterError, msg, false);
        free(msg);
        return -1;
    }

    char* cmd = argv[1];

    if (strcmp(cmd, "discover") == 0) {
        if (argc != 4) {
            cli->sendMsg(ResponseCode::CommandParameterError,
                    "Invalid number of arguments to mdnssd discover", false);
            return 0;
        }
        int requestId = atoi(argv[2]);
        char *serviceType = argv[3];

        discover(cli, NULL, serviceType, NULL, requestId, 0);
    } else if (strcmp(cmd, "stop-discover") == 0) {
        stop(cli, argc, argv, "discover");
    } else if (strcmp(cmd, "register") == 0) {
        if (argc != 6) {
            cli->sendMsg(ResponseCode::CommandParameterError,
                    "Invalid number of arguments to mdnssd register", false);
            return 0;
        }
        int requestId = atoi(argv[2]);
        char *serviceName = argv[3];
        char *serviceType = argv[4];
        int port = atoi(argv[5]);
        char *interfaceName = NULL; // will use all
        char *domain = NULL;        // will use default
        char *host = NULL;          // will use default hostname
        int textLen = 0;
        void *textRecord = NULL;

        serviceRegister(cli, requestId, interfaceName, serviceName,
                serviceType, domain, host, port, textLen, textRecord);
    } else if (strcmp(cmd, "stop-register") == 0) {
        stop(cli, argc, argv, "register");
    } else if (strcmp(cmd, "resolve") == 0) {
        if (argc != 6) {
            cli->sendMsg(ResponseCode::CommandParameterError,
                    "Invalid number of arguments to mdnssd resolve", false);
            return 0;
        }
        int requestId = atoi(argv[2]);
        char *interfaceName = NULL;  // will use all
        char *serviceName = argv[3];
        char *regType = argv[4];
        char *domain = argv[5];
        resolveService(cli, requestId, interfaceName, serviceName, regType, domain);
    } else if (strcmp(cmd, "stop-resolve") == 0) {
        stop(cli, argc, argv, "resolve");
    } else if (strcmp(cmd, "start-service") == 0) {
        if (mMonitor->startService()) {
            cli->sendMsg(ResponseCode::CommandOkay, "Service Started", false);
        } else {
            cli->sendMsg(ResponseCode::ServiceStartFailed, "Service already running", false);
        }
    } else if (strcmp(cmd, "stop-service") == 0) {
        if (mMonitor->stopService()) {
            cli->sendMsg(ResponseCode::CommandOkay, "Service Stopped", false);
        } else {
            cli->sendMsg(ResponseCode::ServiceStopFailed, "Service still in use", false);
        }
    } else if (strcmp(cmd, "sethostname") == 0) {
        if (argc != 4) {
            cli->sendMsg(ResponseCode::CommandParameterError,
                    "Invalid number of arguments to mdnssd sethostname", false);
            return 0;
        }
        int requestId = atoi(argv[2]);
        char *hostname = argv[3];
        setHostname(cli, requestId, hostname);
    } else if (strcmp(cmd, "stop-sethostname") == 0) {
        stop(cli, argc, argv, "sethostname");
    } else if (strcmp(cmd, "getaddrinfo") == 0) {
        if (argc != 4) {
            cli->sendMsg(ResponseCode::CommandParameterError,
                    "Invalid number of arguments to mdnssd getaddrinfo", false);
            return 0;
        }
        int requestId = atoi(argv[2]);
        char *hostname = argv[3];
        char *interfaceName = NULL;  // default
        int protocol = 0;            // intelligient heuristic (both v4 + v6)
        getAddrInfo(cli, requestId, interfaceName, protocol, hostname);
    } else if (strcmp(cmd, "stop-getaddrinfo") == 0) {
        stop(cli, argc, argv, "getaddrinfo");
    } else {
        if (VDBG) ALOGE("Unknown cmd %s", cmd);
        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown mdnssd cmd", false);
        return 0;
    }
    return 0;
}