6dca8064c77163a730c8aa6b4846f6fe140a2e7b
[dcpomatic.git] / src / tools / dcpomatic_server_cli.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "lib/config.h"
22 #include "lib/dcp_video.h"
23 #include "lib/exceptions.h"
24 #include "lib/util.h"
25 #include "lib/config.h"
26 #include "lib/image.h"
27 #include "lib/file_log.h"
28 #include "lib/null_log.h"
29 #include "lib/version.h"
30 #include "lib/encode_server.h"
31 #include <boost/array.hpp>
32 #include <boost/asio.hpp>
33 #include <boost/algorithm/string.hpp>
34 #include <boost/thread.hpp>
35 #include <boost/thread/mutex.hpp>
36 #include <boost/thread/condition.hpp>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <getopt.h>
40 #include <iostream>
41 #include <stdexcept>
42 #include <cstring>
43 #include <vector>
44
45 using std::cerr;
46 using std::string;
47 using std::cout;
48 using boost::shared_ptr;
49
50 static void
51 help (string n)
52 {
53         cerr << "Syntax: " << n << " [OPTION]\n"
54              << "  -v, --version      show DCP-o-matic version\n"
55              << "  -h, --help         show this help\n"
56              << "  -t, --threads      number of parallel encoding threads to use\n"
57              << "  --verbose          be verbose to stdout\n"
58              << "  --log              write a log file of activity\n";
59 }
60
61 int
62 main (int argc, char* argv[])
63 {
64         dcpomatic_setup_path_encoding ();
65         dcpomatic_setup ();
66
67         int num_threads = Config::instance()->server_encoding_threads ();
68         bool verbose = false;
69         bool write_log = false;
70
71         int option_index = 0;
72         while (true) {
73                 static struct option long_options[] = {
74                         { "version", no_argument, 0, 'v'},
75                         { "help", no_argument, 0, 'h'},
76                         { "threads", required_argument, 0, 't'},
77                         { "verbose", no_argument, 0, 'A'},
78                         { "log", no_argument, 0, 'B'},
79                         { 0, 0, 0, 0 }
80                 };
81
82                 int c = getopt_long (argc, argv, "vht:AB", long_options, &option_index);
83
84                 if (c == -1) {
85                         break;
86                 }
87
88                 switch (c) {
89                 case 'v':
90                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
91                         exit (EXIT_SUCCESS);
92                 case 'h':
93                         help (argv[0]);
94                         exit (EXIT_SUCCESS);
95                 case 't':
96                         num_threads = atoi (optarg);
97                         break;
98                 case 'A':
99                         verbose = true;
100                         break;
101                 case 'B':
102                         write_log = true;
103                         break;
104                 }
105         }
106
107         shared_ptr<Log> log;
108         if (write_log) {
109                 log.reset (new FileLog ("dcpomatic_server_cli.log"));
110         } else {
111                 log.reset (new NullLog);
112         }
113
114         EncodeServer server (log, verbose, num_threads);
115
116         try {
117                 server.run ();
118         } catch (boost::system::system_error& e) {
119                 if (e.code() == boost::system::errc::address_in_use) {
120                         cerr << argv[0] << ": address already in use.  Is another DCP-o-matic server instance already running?\n";
121                         exit (EXIT_FAILURE);
122                 }
123                 cerr << argv[0] << ": " << e.what() << "\n";
124         } catch (std::exception& e) {
125                 cerr << argv[0] << ": failed to start server; " << e.what() << "\n";
126         }
127         return 0;
128 }