summaryrefslogtreecommitdiff
path: root/src/tools/dcpomatic_server_cli.cc
blob: 023099034ccdd8fba4aecf751aae857ce767bcf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "lib/config.h"
#include "lib/dcp_video.h"
#include "lib/exceptions.h"
#include "lib/util.h"
#include "lib/config.h"
#include "lib/image.h"
#include "lib/file_log.h"
#include "lib/null_log.h"
#include "lib/version.h"
#include "lib/encode_server.h"
#include "lib/dcpomatic_log.h"
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <iostream>
#include <stdexcept>
#include <cstring>
#include <vector>

using std::cerr;
using std::string;
using std::cout;
using std::shared_ptr;

static void
help (string n)
{
	cerr << "Syntax: " << n << " [OPTION]\n"
	     << "  -v, --version      show DCP-o-matic version\n"
	     << "  -h, --help         show this help\n"
	     << "  -t, --threads      number of parallel encoding threads to use\n"
	     << "  --verbose          be verbose to stdout\n"
	     << "  --log              write a log file of activity\n";
}

int
main (int argc, char* argv[])
{
	ArgFixer fixer(argc, argv);
	auto const program_name = fixer.argv()[0];

	dcpomatic_setup_path_encoding ();
	dcpomatic_setup ();

	int num_threads = Config::instance()->server_encoding_threads ();
	bool verbose = false;
	bool write_log = false;

	int option_index = 0;
	while (true) {
		static struct option long_options[] = {
			{ "version", no_argument, 0, 'v'},
			{ "help", no_argument, 0, 'h'},
			{ "threads", required_argument, 0, 't'},
			{ "verbose", no_argument, 0, 'A'},
			{ "log", no_argument, 0, 'B'},
			{ 0, 0, 0, 0 }
		};

		int c = getopt_long(fixer.argc(), fixer.argv(), "vht:AB", long_options, &option_index);

		if (c == -1) {
			break;
		}

		switch (c) {
		case 'v':
			cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
			exit (EXIT_SUCCESS);
		case 'h':
			help(program_name);
			exit (EXIT_SUCCESS);
		case 't':
			num_threads = atoi (optarg);
			break;
		case 'A':
			verbose = true;
			break;
		case 'B':
			write_log = true;
			break;
		}
	}

	if (write_log) {
		dcpomatic_log.reset (new FileLog("dcpomatic_server_cli.log"));
	}

	EncodeServer server (verbose, num_threads);

	try {
		server.run ();
	} catch (boost::system::system_error& e) {
		if (e.code() == boost::system::errc::address_in_use) {
			cerr << program_name << ": address already in use.  Is another DCP-o-matic server instance already running?\n";
			exit (EXIT_FAILURE);
		}
		cerr << program_name << ": " << e.what() << "\n";
	} catch (std::exception& e) {
		cerr << program_name << ": failed to start server; " << e.what() << "\n";
	}
	return 0;
}