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