Tidying.
[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
22 #include "lib/config.h"
23 #include "lib/config.h"
24 #include "lib/dcp_video.h"
25 #include "lib/dcpomatic_log.h"
26 #include "lib/encode_server.h"
27 #include "lib/exceptions.h"
28 #include "lib/file_log.h"
29 #include "lib/image.h"
30 #include "lib/null_log.h"
31 #include "lib/util.h"
32 #include "lib/version.h"
33 #include <boost/algorithm/string.hpp>
34 #include <boost/array.hpp>
35 #include <boost/asio.hpp>
36 #include <boost/thread.hpp>
37 #include <boost/thread/condition.hpp>
38 #include <boost/thread/mutex.hpp>
39 #include <errno.h>
40 #include <getopt.h>
41 #include <unistd.h>
42 #include <cstring>
43 #include <iostream>
44 #include <stdexcept>
45 #include <vector>
46
47
48 using std::cerr;
49 using std::cout;
50 using std::shared_ptr;
51 using std::string;
52
53
54 static void
55 help (string n)
56 {
57         cerr << "Syntax: " << n << " [OPTION]\n"
58              << "  -v, --version      show DCP-o-matic version\n"
59              << "  -h, --help         show this help\n"
60              << "  -t, --threads      number of parallel encoding threads to use\n"
61              << "  --verbose          be verbose to stdout\n"
62              << "  --log              write a log file of activity\n";
63 }
64
65
66 int
67 main (int argc, char* argv[])
68 {
69         dcpomatic_setup_path_encoding ();
70         dcpomatic_setup ();
71
72         int num_threads = Config::instance()->server_encoding_threads ();
73         bool verbose = false;
74         bool write_log = false;
75
76         int option_index = 0;
77         while (true) {
78                 static struct option long_options[] = {
79                         { "version", no_argument, 0, 'v'},
80                         { "help", no_argument, 0, 'h'},
81                         { "threads", required_argument, 0, 't'},
82                         { "verbose", no_argument, 0, 'A'},
83                         { "log", no_argument, 0, 'B'},
84                         { 0, 0, 0, 0 }
85                 };
86
87                 int c = getopt_long (argc, argv, "vht:AB", long_options, &option_index);
88
89                 if (c == -1) {
90                         break;
91                 }
92
93                 switch (c) {
94                 case 'v':
95                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
96                         exit (EXIT_SUCCESS);
97                 case 'h':
98                         help (argv[0]);
99                         exit (EXIT_SUCCESS);
100                 case 't':
101                         num_threads = atoi (optarg);
102                         break;
103                 case 'A':
104                         verbose = true;
105                         break;
106                 case 'B':
107                         write_log = true;
108                         break;
109                 }
110         }
111
112         if (write_log) {
113                 dcpomatic_log.reset (new FileLog("dcpomatic_server_cli.log"));
114         }
115
116         EncodeServer server (verbose, num_threads);
117
118         try {
119                 server.run ();
120         } catch (boost::system::system_error& e) {
121                 if (e.code() == boost::system::errc::address_in_use) {
122                         cerr << argv[0] << ": address already in use.  Is another DCP-o-matic server instance already running?\n";
123                         exit (EXIT_FAILURE);
124                 }
125                 cerr << argv[0] << ": " << e.what() << "\n";
126         } catch (std::exception& e) {
127                 cerr << argv[0] << ": failed to start server; " << e.what() << "\n";
128         }
129         return 0;
130 }