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