Statistics
| Branch: | Tag: | Revision:

dvbd / src / dvbsched.cpp @ bcb74979

History | View | Annotate | Download (5.67 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 bcb74979 Csaba Kiraly
#include <stdlib.h>
35 bfdb7446 jak
36
void usage(const char *progname);
37
time_t convertTime(const char *theTime);
38
39
int main(int argc, char *argv[])
40
{
41
  // improve iostream performance
42
  std::ios::sync_with_stdio(false);
43
44
  std::string socketFile = DEFAULT_SOCKET_FILE;
45 59be6a47 jak
  bool convertToPS = false;
46 bfdb7446 jak
  int priority = 10;
47 a0d61e1f jak
  bool removeSpecified = false;
48 c6e4f54d jak
  bool noConflicts = false;
49 a0d61e1f jak
  int removeJob = -1;
50 2243610b jak
  std::string extraInfo = "";
51 bfdb7446 jak
52
  while (true) {
53
    static struct option long_options[] = {
54
      {"priority", 1, 0, 'p'},
55
      {"socket", 1, 0, 's'},
56 59be6a47 jak
      {"ps", 0, 0, 'P'},
57 a0d61e1f jak
      {"remove", 1, 0, 'r'},
58 c6e4f54d jak
      {"noconflicts", 0, 0, 'n'},
59 2243610b jak
      {"extra", 1, 0, 'e'},
60 bfdb7446 jak
      {0, 0, 0, 0}
61
    };
62
 
63
    int option_index = 0;
64 2243610b jak
    int c = getopt_long(argc, argv, "Pne:r:d:s:", long_options, &option_index);
65 bfdb7446 jak
    if (c == -1)
66
      break;
67
    switch (c) {
68
    case 'p':
69
      priority = toInt(optarg);
70
      break;
71
    case 's':
72
      socketFile = optarg;
73
      break;
74 59be6a47 jak
    case 'P':
75
      convertToPS = true;
76 bfdb7446 jak
      break;
77 a0d61e1f jak
    case 'r':
78
      removeSpecified = true;
79
      removeJob = toInt(optarg);
80
      break;
81 c6e4f54d jak
    case 'n':
82
      noConflicts = true;
83
      break;
84 2243610b jak
    case 'e':
85
      extraInfo = optarg;
86
      break;
87 bfdb7446 jak
    default:
88
      usage(argv[0]);
89
      return 1;
90
    }
91
  }
92
93 a0d61e1f jak
  if (!removeSpecified && argc - optind != 5) {
94 bfdb7446 jak
    if (argc - optind != 0)
95
      std::cerr << "Incorrect number of arguments passed (expected 5, got " 
96
                << argc - optind << ")\n\n";
97
98
    usage(argv[0]);
99
    return 1;
100
  }
101
102 a0d61e1f jak
  ClientConnection client(socketFile);
103
  if (!client) {
104
    std::cerr << "Failed to connect to server:\n" << client.getError() << std::endl;
105
    return 1;
106
  }
107
108
  if (removeSpecified) {
109
    if (!client.removeJob(removeJob)) {
110
      std::cerr << "Failed to remove job: " << client.getError() << "\n";
111
      return 1;
112
    }
113
    return 0;
114
  }
115
116 bfdb7446 jak
  // Parse the command line arguments
117
  std::string type(argv[optind++]);
118
  std::string channel(argv[optind++]);
119
  time_t scheduleTime = convertTime(argv[optind++]);
120
  unsigned duration = toInt(argv[optind++]);
121
  std::string outputFile = argv[optind++];
122
123
  // Schedule
124
125 2243610b jak
  int job = client.schedule(type, channel, convertToPS, 
126
                            scheduleTime, duration, priority, 
127
                            outputFile, extraInfo);
128 bfdb7446 jak
129 a0d61e1f jak
  if (job < 0) {
130
    std::cerr << "Failed to schedule job: " << client.getError() << "\n";
131
    return 1;
132
  }
133 bfdb7446 jak
134 a0d61e1f jak
  std::cout << "Job number is " << job << std::endl;
135
136
  StringList messages;
137
  client.checkSchedule(messages);
138
139
  if (!messages.empty()) {
140
    std::cout << "The following schedule problems were identified:\n";
141
    for (StringList::iterator i = messages.begin(); i != messages.end(); i++)
142
      std::cout << *i << "\n";
143
144 c6e4f54d jak
    if (!noConflicts) {
145
      std::cout << "Do you still wish to schedule the job?\n";
146
147
      char answer;
148
      while (std::cin >> answer) {
149
        switch (tolower(answer)) {
150
        case 'y':
151
          std::cout << "Job Scheduled\n";
152
          return 0;
153
        case 'n':
154
          client.removeJob(job);
155
          std::cout << "Job not scheduled.\n";
156
          return 0;
157
        default:
158
          std::cout << "Please answer Y or N\n";
159
          break;
160
        }
161 bfdb7446 jak
      }
162
    }
163 60b52bfe jak
    else {
164 c6e4f54d jak
      client.removeJob(job);
165 60b52bfe jak
      std::cout << "Job not scheduled.\n";
166
    }
167 a0d61e1f jak
  }
168 bfdb7446 jak
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 a0d61e1f jak
            << "       " << progname << " -r JOB\n"
186 bfdb7446 jak
            << "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 a0d61e1f jak
            << "  -r, --remove           Remove a job from the schedule\n"
199 bfdb7446 jak
            << "  -s, --socket           Specify socket to connect to\n"
200 59be6a47 jak
            << "                         (default is " << DEFAULT_SOCKET_FILE << ")\n"
201 c6e4f54d jak
            << "  -P, --ps               Convert stream to program stream\n"
202 2243610b jak
            << "  -n, --noconflicts      Don't schedule if it makes a conflict\n"
203
            << "  -e, --extra            Associate some extra information with the\n"
204
            << "                         recording.\n";
205 bfdb7446 jak
}
206
207
// For parsetime
208
extern "C" 
209
int yywrap()
210
{
211
  return 1;
212
}