Statistics
| Branch: | Tag: | Revision:

dvbd / src / dvbsched.cpp @ 900cea42

History | View | Annotate | Download (5.65 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
void usage(const char *progname);
36
time_t convertTime(const char *theTime);
37

    
38
int main(int argc, char *argv[])
39
{
40
  // improve iostream performance
41
  std::ios::sync_with_stdio(false);
42

    
43
  std::string socketFile = DEFAULT_SOCKET_FILE;
44
  bool convertToPS = false;
45
  int priority = 10;
46
  bool removeSpecified = false;
47
  bool noConflicts = false;
48
  int removeJob = -1;
49
  std::string extraInfo = "";
50

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

    
92
  if (!removeSpecified && argc - optind != 5) {
93
    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
  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
  // 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
  int job = client.schedule(type, channel, convertToPS, 
125
                            scheduleTime, duration, priority, 
126
                            outputFile, extraInfo);
127

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

    
133
  std::cout << "Job number is " << job << std::endl;
134

    
135
  StringList messages;
136
  client.checkSchedule(messages);
137

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

    
143
    if (!noConflicts) {
144
      std::cout << "Do you still wish to schedule the job?\n";
145

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

    
168
}
169

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

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

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