Ejemplo n.º 1
0
int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QCoreApplication::setOrganizationName("Upmpd.org");
    QCoreApplication::setApplicationName("upplay");

    string a_config;

    QStringList params;
    for(int i = 1; i < argc; i++){
        QString param(argv[i]);
        params.push_back(param);
    }

    thisprog = argv[0];
    argc--; argv++;

    while (argc > 0 && **argv == '-') {
        (*argv)++;
        if (!(**argv))
            Usage();
        while (**argv)
            switch (*(*argv)++) {
            case 'c':   op_flags |= OPT_c; if (argc < 2)  Usage();
                a_config = *(++argv);
                argc--; goto b1;
            default: Usage();
            }
    b1: argc--; argv++;
    }

    if (argc > 0)
        Usage();

    if (Logger::getTheLog("stderr") == 0) {
        cerr << "Can't initialize log" << endl;
        return 1;
    }
    const char *cp;
    if ((cp = getenv("UPPLAY_LOGLEVEL"))) {
        Logger::getTheLog("")->setLogLevel(Logger::LogLevel(atoi(cp)));
    }

    LibUPnP *mylib = LibUPnP::getLibUPnP();
    if (!mylib) {
        cerr << "Can't get LibUPnP" << endl;
        return 1;
    }
    if (!mylib->ok()) {
        cerr << "Lib init failed: " <<
            mylib->errAsString("main", mylib->getInitError()) << endl;
        return 1;
    }
    if ((cp = getenv("UPPLAY_UPNPLOGFILENAME"))) {
        char *cp1 = getenv("UPPLAY_UPNPLOGLEVEL");
        int loglevel = LibUPnP::LogLevelNone;
        if (cp1) {
            loglevel = atoi(cp1);
        }
        loglevel = loglevel < 0 ? 0: loglevel;
        loglevel = loglevel > int(LibUPnP::LogLevelDebug) ? 
            int(LibUPnP::LogLevelDebug) : loglevel;

        if (loglevel != LibUPnP::LogLevelNone) {
            mylib->setLogFileName(cp, LibUPnP::LogLevel(loglevel));
        }
    }

    Application application(&app, params.size(), 0);
    if(!application.is_initialized()) 
        return 1;

    return app.exec();
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    thisprog = argv[0];

    int ret;
    while ((ret = getopt(argc, argv, "fhmLlrsSx")) != -1) {
        switch (ret) {
        case 'f': op_flags |= OPT_f; break;
        case 'h': Usage(stdout); break;
        case 'l':
            op_flags |= OPT_l;
            break;
        case 'L':
            op_flags |= OPT_L;
            break;
        case 'm':
            op_flags |= OPT_m;
            break;
        case 'r':
            op_flags |= OPT_r;
            break;
        case 's':
            op_flags |= OPT_s;
            break;
        case 'S':
            op_flags |= OPT_S;
            break;
        case 'x':
            op_flags |= OPT_x;
            break;
        default: Usage();
        }
    }
    //fprintf(stderr, "argc %d optind %d flgs: 0x%x\n", argc, optind, op_flags);

    // If we're not a server, try to contact one to avoid the
    // discovery timeout
    if (!(op_flags & OPT_S) && tryserver(op_flags, argc -optind, 
                                         &argv[optind])) {
        exit(0);
    }

    // At least one action needed. 
    if ((op_flags & ~(OPT_f|OPT_m)) == 0)
        Usage();

    LibUPnP *mylib = LibUPnP::getLibUPnP();
    if (!mylib) {
        cerr << "Can't get LibUPnP" << endl;
        return 1;
    }
    if (!mylib->ok()) {
        cerr << "Lib init failed: " <<
            mylib->errAsString("main", mylib->getInitError()) << endl;
        return 1;
    }
    // mylib->setLogFileName("/tmp/libupnp.log");

    vector<string> args;
    while (optind < argc) {
        args.push_back(argv[optind++]);
    }
    
    if ((op_flags & OPT_l)) {
        if (args.size())
            Usage();
        string out = showReceivers(op_flags);
        cout << out;
    } else if ((op_flags & OPT_L)) {
        if (args.size())
            Usage();
        string out = showSenders(op_flags);
        cout << out;
    } else if ((op_flags & OPT_r)) {
        if (args.size() < 2)
            Usage();
        setReceiversFromSender(args[0], vector<string>(args.begin() + 1,
                                                       args.end()));
    } else if ((op_flags & OPT_s)) {
        if (args.size() < 2)
            Usage();
        setReceiversFromReceiver(args[0], vector<string>(args.begin()+1,
                                                         args.end()));
    } else if ((op_flags & OPT_x)) {
        if (args.size() < 1)
            Usage();
        stopReceivers(args);
    } else if ((op_flags & OPT_S)) {
        exit(runserver());
    } else {
        Usage();
    }

    // If we get here, we have executed a local command. If -f is set,
    // fork and run the server code so that a next execution will use
    // this instead (and not incur the discovery timeout)
    if ((op_flags & OPT_f)) {
        // Father exits, son process becomes server
        if (daemon(0, 0) == 0)
            runserver();
    } 
    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QCoreApplication::setOrganizationName("Upmpd.org");
    QCoreApplication::setApplicationName("RaspiDAC");


    QStringList params;
    for(int i = 1; i < argc; i++){
        QString param(argv[i]);
        params.push_back(param);
    }

    thisprog = argv[0];
    argc--; argv++;

    while (argc > 0 && **argv == '-') {
        (*argv)++;
        if (!(**argv))
            Usage();
        while (**argv)
            switch (*(*argv)++) {
            case 'h':   op_flags |= OPT_h; Usage(); break;
            case 'v':   op_flags |= OPT_v; versionInfo(stdout); exit(0); break;
            default: Usage();
            }
        argc--; argv++;
    }

    if (argc > 0)
        Usage();

    if (Logger::getTheLog("stderr") == 0) {
        cerr << "Can't initialize log" << endl;
        return 1;
    }
    const char *cp;
    if ((cp = getenv("UPPLAY_LOGLEVEL"))) {
        Logger::getTheLog("")->setLogLevel(Logger::LogLevel(atoi(cp)));
    }
    QSettings settings;
    string ifname = qs2utf8s(settings.value("netifname").toString().trimmed());
    if (!ifname.empty()) {
        cerr << "Initializing library with interface " << ifname << endl;
    }
    
    // Note that the lib init may fail here if ifname is wrong.
    // The later discovery would call the lib init again (because
    // the singleton is still null), which would retry the init,
    // without an interface this time, which would probably succeed,
    // so that things may still mostly work, which is confusing and the
    // reason we do the retry here instead.
    LibUPnP *mylib = LibUPnP::getLibUPnP(false, 0, ifname);
    if (!mylib || !mylib->ok()) {
        if (mylib)
            cerr << mylib->errAsString("main", mylib->getInitError()) << endl;
        if (ifname.empty()) {
            QMessageBox::warning(0, "RaspiDAC", app.tr("Lib init failed"));
            return 1;
        } else {
            QMessageBox::warning(0, "RaspiDAC",
                                 app.tr("Lib init failed for ") +
                                 settings.value("netifname").toString() +
                                 app.tr(". Retrying with null interface"));
            mylib = LibUPnP::getLibUPnP();
            if (!mylib || !mylib->ok()) {
                QMessageBox::warning(0, "RaspiDAC", app.tr("Lib init failed"));
                return 1;
            }
        }
    }

    if ((cp = getenv("UPPLAY_UPNPLOGFILENAME"))) {
        char *cp1 = getenv("UPPLAY_UPNPLOGLEVEL");
        int loglevel = LibUPnP::LogLevelNone;
        if (cp1) {
            loglevel = atoi(cp1);
        }
        loglevel = loglevel < 0 ? 0: loglevel;
        loglevel = loglevel > int(LibUPnP::LogLevelDebug) ? 
            int(LibUPnP::LogLevelDebug) : loglevel;

        if (loglevel != LibUPnP::LogLevelNone) {
            if (mylib)
                mylib->setLogFileName(cp, LibUPnP::LogLevel(loglevel));
        }
    }

    Application application(&app);
    if(!application.is_initialized()) 
        return 1;

    return app.exec();
}