Statistics
| Branch: | Tag: | Revision:

dvbd / src / dvbsched.cpp @ c6e4f54d

History | View | Annotate | Download (5.56 KB)

1
/*
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
  bool convertToPS = false;
55
  int priority = 10;
56
  bool removeSpecified = false;
57
  bool noConflicts = false;
58
  int removeJob = -1;
59

    
60
  while (true) {
61
    static struct option long_options[] = {
62
      {"priority", 1, 0, 'p'},
63
      {"socket", 1, 0, 's'},
64
      {"ps", 0, 0, 'P'},
65
      {"remove", 1, 0, 'r'},
66
      {"noconflicts", 0, 0, 'n'},
67
      {0, 0, 0, 0}
68
    };
69
 
70
    int option_index = 0;
71
    int c = getopt_long(argc, argv, "r:Pd:s:", long_options, &option_index);
72
    if (c == -1)
73
      break;
74
    switch (c) {
75
    case 'p':
76
      priority = toInt(optarg);
77
      break;
78
    case 's':
79
      socketFile = optarg;
80
      break;
81
    case 'P':
82
      convertToPS = true;
83
      break;
84
    case 'r':
85
      removeSpecified = true;
86
      removeJob = toInt(optarg);
87
      break;
88
    case 'n':
89
      noConflicts = true;
90
      break;
91
    default:
92
      usage(argv[0]);
93
      return 1;
94
    }
95
  }
96

    
97
  if (!removeSpecified && argc - optind != 5) {
98
    if (argc - optind != 0)
99
      std::cerr << "Incorrect number of arguments passed (expected 5, got " 
100
                << argc - optind << ")\n\n";
101

    
102
    usage(argv[0]);
103
    return 1;
104
  }
105

    
106
  ClientConnection client(socketFile);
107
  if (!client) {
108
    std::cerr << "Failed to connect to server:\n" << client.getError() << std::endl;
109
    return 1;
110
  }
111

    
112
  if (removeSpecified) {
113
    if (!client.removeJob(removeJob)) {
114
      std::cerr << "Failed to remove job: " << client.getError() << "\n";
115
      return 1;
116
    }
117
    return 0;
118
  }
119

    
120
  // Parse the command line arguments
121
  std::string type(argv[optind++]);
122
  std::string channel(argv[optind++]);
123
  time_t scheduleTime = convertTime(argv[optind++]);
124
  unsigned duration = toInt(argv[optind++]);
125
  std::string outputFile = argv[optind++];
126

    
127
  // Schedule
128

    
129
  int job = client.schedule(type, channel, convertToPS, scheduleTime, duration, priority, outputFile);
130

    
131
  if (job < 0) {
132
    std::cerr << "Failed to schedule job: " << client.getError() << "\n";
133
    return 1;
134
  }
135

    
136
  std::cout << "Job number is " << job << std::endl;
137

    
138
  StringList messages;
139
  client.checkSchedule(messages);
140

    
141
  if (!messages.empty()) {
142
    std::cout << "The following schedule problems were identified:\n";
143
    for (StringList::iterator i = messages.begin(); i != messages.end(); i++)
144
      std::cout << *i << "\n";
145

    
146
    if (!noConflicts) {
147
      std::cout << "Do you still wish to schedule the job?\n";
148

    
149
      char answer;
150
      while (std::cin >> answer) {
151
        switch (tolower(answer)) {
152
        case 'y':
153
          std::cout << "Job Scheduled\n";
154
          return 0;
155
        case 'n':
156
          client.removeJob(job);
157
          std::cout << "Job not scheduled.\n";
158
          return 0;
159
        default:
160
          std::cout << "Please answer Y or N\n";
161
          break;
162
        }
163
      }
164
    }
165
    else
166
      client.removeJob(job);
167
  }
168

    
169
}
170

    
171
time_t convertTime(const char *theTime)
172
{
173
  // Check that the date time is OK
174
  char *argv[2] = { const_cast<char *>(theTime), 0 };
175
  time_t result = parsetime( 1, argv );
176
  if (result == 0) {
177
    std::cerr << "Fatal error: failed to parse date time: " << theTime << "\n";
178
    exit(1);
179
  }
180
  return result;
181
}
182

    
183
void usage(const char *progname) {
184
  std::cerr << "Usage: " << progname << " [OPTION] TYPE CHANNEL TIME DURATION OUTPUT\n"
185
            << "       " << progname << " -r JOB\n"
186
            << "Schedule DVB recording for channel CHANNEL, using DVB-card\n"
187
            << "\n"
188
            << "TYPE can be dvb-t, dvb-s or dvb-c.\n"
189
            << "TIME is time to schedule recording. format is\n"
190
            << "     same as for at(1)."
191
            << "DURATION is time to stream for in minutes.\n"
192
            << "OUTPUT is the output file, must be owned by the user\n"
193
            << "       and writable by dvbd.\n"
194
            << "\n"
195
            << "  -p, --priority         Priority of stream (default is 10\n"
196
            << "                         higher priorities may dislodge lower\n"
197
            << "                         ones).\n"
198
            << "  -r, --remove           Remove a job from the schedule\n"
199
            << "  -s, --socket           Specify socket to connect to\n"
200
            << "                         (default is " << DEFAULT_SOCKET_FILE << ")\n"
201
            << "  -P, --ps               Convert stream to program stream\n"
202
            << "  -n, --noconflicts      Don't schedule if it makes a conflict\n";
203
}
204

    
205
// For parsetime
206
extern "C" 
207
int yywrap()
208
{
209
  return 1;
210
}