Statistics
| Branch: | Tag: | Revision:

dvbd / src / dvbsched.cpp @ a0d61e1f

History | View | Annotate | Download (5.31 KB)

1 bfdb7446 jak
/*
2
  Copyright 2003 John Knottenbelt
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17
*/
18
19
#include "config.h"
20
#include "dvbd.h"
21
#include "utils.h"
22
#include "stringutil.h"
23
#include "select.h"
24
#include "clientconnection.h"
25
#include "unixclientsocket.h"
26
#include "parsetime.h"
27
28
#include <string>
29
#include <iostream>
30
#include <time.h>
31
#include <getopt.h>
32
#include <cerrno>
33
#include <fstream>
34
35
// Unfortunately, large file support (LFS) in gcc 3.2, 3.3 is broken
36
// on Debian sarge/sid. Work around with LFS_WORKAROUND
37
38
#define LFS_WORKAROUND
39
40
#ifdef LFS_WORKAROUND
41
#include <fcntl.h>
42
#include <ext/stdio_filebuf.h>
43
#endif
44
45
void usage(const char *progname);
46
time_t convertTime(const char *theTime);
47
48
int main(int argc, char *argv[])
49
{
50
  // improve iostream performance
51
  std::ios::sync_with_stdio(false);
52
53
  std::string socketFile = DEFAULT_SOCKET_FILE;
54 59be6a47 jak
  bool convertToPS = false;
55 bfdb7446 jak
  int priority = 10;
56 a0d61e1f jak
  bool removeSpecified = false;
57
  int removeJob = -1;
58 bfdb7446 jak
59
  while (true) {
60
    static struct option long_options[] = {
61
      {"priority", 1, 0, 'p'},
62
      {"socket", 1, 0, 's'},
63 59be6a47 jak
      {"ps", 0, 0, 'P'},
64 a0d61e1f jak
      {"remove", 1, 0, 'r'},
65 bfdb7446 jak
      {0, 0, 0, 0}
66
    };
67
 
68
    int option_index = 0;
69 a0d61e1f jak
    int c = getopt_long(argc, argv, "r:Pd:s:", long_options, &option_index);
70 bfdb7446 jak
    if (c == -1)
71
      break;
72
    switch (c) {
73
    case 'p':
74
      priority = toInt(optarg);
75
      break;
76
    case 's':
77
      socketFile = optarg;
78
      break;
79 59be6a47 jak
    case 'P':
80
      convertToPS = true;
81 bfdb7446 jak
      break;
82 a0d61e1f jak
    case 'r':
83
      removeSpecified = true;
84
      removeJob = toInt(optarg);
85
      break;
86 bfdb7446 jak
    default:
87
      usage(argv[0]);
88
      return 1;
89
    }
90
  }
91
92 a0d61e1f jak
  if (!removeSpecified && argc - optind != 5) {
93 bfdb7446 jak
    if (argc - optind != 0)
94
      std::cerr << "Incorrect number of arguments passed (expected 5, got " 
95
                << argc - optind << ")\n\n";
96
97
    usage(argv[0]);
98
    return 1;
99
  }
100
101 a0d61e1f jak
  ClientConnection client(socketFile);
102
  if (!client) {
103
    std::cerr << "Failed to connect to server:\n" << client.getError() << std::endl;
104
    return 1;
105
  }
106
107
  if (removeSpecified) {
108
    if (!client.removeJob(removeJob)) {
109
      std::cerr << "Failed to remove job: " << client.getError() << "\n";
110
      return 1;
111
    }
112
    return 0;
113
  }
114
115 bfdb7446 jak
  // Parse the command line arguments
116
  std::string type(argv[optind++]);
117
  std::string channel(argv[optind++]);
118
  time_t scheduleTime = convertTime(argv[optind++]);
119
  unsigned duration = toInt(argv[optind++]);
120
  std::string outputFile = argv[optind++];
121
122
  // Schedule
123
124 a0d61e1f jak
  int job = client.schedule(type, channel, convertToPS, scheduleTime, duration, priority, outputFile);
125 bfdb7446 jak
126 a0d61e1f jak
  if (job < 0) {
127
    std::cerr << "Failed to schedule job: " << client.getError() << "\n";
128
    return 1;
129
  }
130 bfdb7446 jak
131 a0d61e1f jak
  std::cout << "Job number is " << job << std::endl;
132
133
  StringList messages;
134
  client.checkSchedule(messages);
135
136
  if (!messages.empty()) {
137
    std::cout << "The following schedule problems were identified:\n";
138
    for (StringList::iterator i = messages.begin(); i != messages.end(); i++)
139
      std::cout << *i << "\n";
140
141
    std::cout << "Do you still wish to schedule the job?\n";
142
143
    char answer;
144
    while (std::cin >> answer) {
145
      switch (tolower(answer)) {
146
      case 'y':
147
        std::cout << "Job Scheduled\n";
148
        return 0;
149
      case 'n':
150
        client.removeJob(job);
151
        std::cout << "Job not scheduled.\n";
152
        return 0;
153
      default:
154
        std::cout << "Please answer Y or N\n";
155
        break;
156 bfdb7446 jak
      }
157
    }
158 a0d61e1f jak
  }
159 bfdb7446 jak
160
}
161
162
time_t convertTime(const char *theTime)
163
{
164
  // Check that the date time is OK
165
  char *argv[2] = { const_cast<char *>(theTime), 0 };
166
  time_t result = parsetime( 1, argv );
167
  if (result == 0) {
168
    std::cerr << "Fatal error: failed to parse date time: " << theTime << "\n";
169
    exit(1);
170
  }
171
  return result;
172
}
173
174
void usage(const char *progname) {
175
  std::cerr << "Usage: " << progname << " [OPTION] TYPE CHANNEL TIME DURATION OUTPUT\n"
176 a0d61e1f jak
            << "       " << progname << " -r JOB\n"
177 bfdb7446 jak
            << "Schedule DVB recording for channel CHANNEL, using DVB-card\n"
178
            << "\n"
179
            << "TYPE can be dvb-t, dvb-s or dvb-c.\n"
180
            << "TIME is time to schedule recording. format is\n"
181
            << "     same as for at(1)."
182
            << "DURATION is time to stream for in minutes.\n"
183
            << "OUTPUT is the output file, must be owned by the user\n"
184
            << "       and writable by dvbd.\n"
185
            << "\n"
186
            << "  -p, --priority         Priority of stream (default is 10\n"
187
            << "                         higher priorities may dislodge lower\n"
188
            << "                         ones).\n"
189 a0d61e1f jak
            << "  -r, --remove           Remove a job from the schedule\n"
190 bfdb7446 jak
            << "  -s, --socket           Specify socket to connect to\n"
191 59be6a47 jak
            << "                         (default is " << DEFAULT_SOCKET_FILE << ")\n"
192
            << "  -P, --ps               Convert stream to program stream\n";
193 bfdb7446 jak
}
194
195
// For parsetime
196
extern "C" 
197
int yywrap()
198
{
199
  return 1;
200
}