main.cpp 6.25 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2018 Savoir-faire Linux Inc.
3
 *
4 5 6
 *  Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
7
 *  Author: Simon Zeni <simon.zeni@savoirfairelinux.com>
8 9 10
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 3 of the License, or
12 13 14 15 16 17 18 19 20 21 22
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
23

24 25 26 27 28
#include <iostream>
#include <thread>
#include <cstring>
#include <signal.h>
#include <getopt.h>
29
#include <cstdlib>
30

31 32 33 34 35 36 37
#include "dring/dring.h"

#include "logger.h"

#if REST_API
#include "restcpp/restclient.h"
#else
38
#include "dbus/dbusclient.h"
39 40
#endif

41
#include "fileutils.h"
42

Adrien Béraud's avatar
Adrien Béraud committed
43
static int ringFlags = 0;
44 45 46 47 48 49 50
static int port = 8080;

#if REST_API
    static std::unique_ptr<RestClient> restClient;
#else
    static std::unique_ptr<DBusClient> dbusClient;
#endif
51

52 53
static void
print_title()
54
{
55 56
    std::cout
        << "Ring Daemon " << DRing::version()
57
        << ", by Savoir-faire Linux 2004-2018" << std::endl
58
        << "https://www.ring.cx/" << std::endl
59 60 61 62
#ifdef RING_VIDEO
        << "[Video support enabled]" << std::endl
#endif
        << std::endl;
63 64
}

65 66
static void
print_usage()
67
{
68 69 70 71
    std::cout << std::endl <<
    "-c, --console \t- Log in console (instead of syslog)" << std::endl <<
    "-d, --debug \t- Debug mode (more verbose)" << std::endl <<
    "-p, --persistent \t- Stay alive after client quits" << std::endl <<
72
    "--port \t- Port to use for the rest API. Default is 8080" << std::endl <<
73
    "--auto-answer \t- Force automatic answer to incoming calls" << std::endl <<
74
    "-h, --help \t- Print help" << std::endl;
75 76 77 78 79
}

// Parse command line arguments, setting debug options or printing a help
// message accordingly.
// returns true if we should quit (i.e. help was printed), false otherwise
80 81
static bool
parse_args(int argc, char *argv[], bool& persistent)
82
{
83 84 85 86 87 88 89
    int consoleFlag = false;
    int debugFlag = false;
    int helpFlag = false;
    int versionFlag = false;
    int autoAnswer = false;

    const struct option long_options[] = {
90 91 92 93 94 95
        /* These options set a flag. */
        {"debug", no_argument, NULL, 'd'},
        {"console", no_argument, NULL, 'c'},
        {"persistent", no_argument, NULL, 'p'},
        {"help", no_argument, NULL, 'h'},
        {"version", no_argument, NULL, 'v'},
96
        {"auto-answer", no_argument, &autoAnswer, true},
97
        {"port", optional_argument, NULL, 'x'},
98 99 100 101 102 103 104
        {0, 0, 0, 0} /* Sentinel */
    };

    while (true) {
        /* getopt_long stores the option index here. */
        int option_index = 0;

105
        auto c = getopt_long(argc, argv, "dcphvx:", long_options, &option_index);
106 107 108 109 110 111

        // end of the options
        if (c == -1)
            break;

        switch (c) {
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            case 'd':
                debugFlag = true;
                break;

            case 'c':
                consoleFlag = true;
                break;

            case 'p':
                persistent = true;
                break;

            case 'h':
            case '?':
                helpFlag = true;
                break;

            case 'v':
                versionFlag = true;
                break;

133 134 135 136
            case 'x':
                port = std::atoi(optarg);
                break;

137 138 139 140 141 142 143
            default:
                break;
        }
    }

    if (helpFlag) {
        print_usage();
144 145 146 147
        return true;
    }

    if (versionFlag) {
148
        // We've always print the title/version, so we can just exit
149
        return true;
150
    }
151 152 153 154 155 156 157

    if (consoleFlag)
        ringFlags |= DRing::DRING_FLAG_CONSOLE_LOG;

    if (debugFlag)
        ringFlags |= DRing::DRING_FLAG_DEBUG;

158 159 160
    if (autoAnswer)
        ringFlags |= DRing::DRING_FLAG_AUTOANSWER;

161
    return false;
162 163
}

164 165
static void
signal_handler(int code)
166
{
167
    std::cerr << "Caught signal " << strsignal(code)
168
              << ", terminating..." << std::endl;
169

170 171 172 173
    // Unset signal handlers
    signal(SIGHUP, SIG_DFL);
    signal(SIGINT, SIG_DFL);
    signal(SIGTERM, SIG_DFL);
174

175 176 177 178 179 180 181 182
    // Interrupt the process
#if REST_API
    if (restClient)
        restClient->exit();
#else
    if (dbusClient)
        dbusClient->exit();
#endif
183 184
}

185 186
int
main(int argc, char *argv [])
187 188 189
{
    // make a copy as we don't want to modify argv[0], copy it to a vector to
    // guarantee that memory is correctly managed/exception safe
190
    std::string programName {argv[0]};
191 192 193
    std::vector<char> writable(programName.size() + 1);
    std::copy(programName.begin(), programName.end(), writable.begin());

194
    ring::fileutils::set_program_dir(writable.data());
195

196 197
#ifdef TOP_BUILDDIR
    if (!getenv("CODECS_PATH"))
198
        setenv("CODECS_PATH", TOP_BUILDDIR "/src/media/audio/codecs", 1);
199 200 201 202 203 204 205 206
#endif

    print_title();

    bool persistent = false;
    if (parse_args(argc, argv, persistent))
        return 0;

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    // TODO: Block signals for all threads but the main thread, decide how/if we should
    // handle other signals
    signal(SIGINT, signal_handler);
    signal(SIGHUP, signal_handler);
    signal(SIGTERM, signal_handler);

#if REST_API
    try {
        restClient.reset(new RestClient {port, ringFlags, persistent});
    } catch (const std::exception& ex) {
        std::cerr << "One does not simply initialize the rest client: " << ex.what() << std::endl;
        return 1;
    }

    if (restClient)
        return restClient->event_loop();
    else
        return 1;
#else
226 227
    // initialize client/library
    try {
228
        dbusClient.reset(new DBusClient {ringFlags, persistent});
229 230 231 232 233
    } catch (const std::exception& ex) {
        std::cerr << "One does not simply initialize the DBus client: " << ex.what() << std::endl;
        return 1;
    }

234 235 236 237 238
    if (dbusClient)
        return dbusClient->event_loop();
    else
        return 1;
#endif
239 240

}