Statistics
| Branch: | Tag: | Revision:

dvbd / dvbsched.cpp @ a7dee730

History | View | Annotate | Download (4.69 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

    
55
  int priority = 10;
56

    
57
  while (true) {
58
    static struct option long_options[] = {
59
      {"priority", 1, 0, 'p'},
60
      {"socket", 1, 0, 's'},
61
      {0, 0, 0, 0}
62
    };
63
 
64
    int option_index = 0;
65
    int c = getopt_long(argc, argv, "d:s:", long_options, &option_index);
66
    if (c == -1)
67
      break;
68
    switch (c) {
69
    case 'p':
70
      priority = toInt(optarg);
71
      break;
72
    case 's':
73
      socketFile = optarg;
74
      break;
75
      
76
      break;
77
    default:
78
      usage(argv[0]);
79
      return 1;
80
    }
81
  }
82

    
83
  if (argc - optind != 5) {
84
    if (argc - optind != 0)
85
      std::cerr << "Incorrect number of arguments passed (expected 5, got " 
86
                << argc - optind << ")\n\n";
87

    
88
    usage(argv[0]);
89
    return 1;
90
  }
91

    
92
  // Parse the command line arguments
93
  std::string type(argv[optind++]);
94
  std::string channel(argv[optind++]);
95
  time_t scheduleTime = convertTime(argv[optind++]);
96
  unsigned duration = toInt(argv[optind++]);
97
  std::string outputFile = argv[optind++];
98

    
99
  ClientConnection client(socketFile);
100
  if (!client) {
101
    std::cerr << "Fatal error: failed to connect to server on socket " << socketFile << std::endl;
102
    return 1;
103
  }
104

    
105
  // Schedule
106

    
107
    int job = client.schedule(type, channel, scheduleTime, duration, priority, outputFile);
108

    
109
    if (job < 0) {
110
      std::cerr << "Failed to schedule job\n";
111
      return 1;
112
    }
113

    
114
    std::cout << "Job number is " << job << std::endl;
115

    
116
    StringList messages;
117
    client.checkSchedule(messages);
118

    
119
    if (!messages.empty()) {
120
      std::cout << "The following schedule problems were identified:\n";
121
      for (StringList::iterator i = messages.begin(); i != messages.end(); i++)
122
        std::cout << *i << "\n";
123

    
124
      std::cout << "Do you still wish to schedule the job?\n";
125

    
126
      char answer;
127
      while (std::cin >> answer) {
128
        switch (tolower(answer)) {
129
        case 'y':
130
          std::cout << "Scheduled as job " << job << "\n";
131
          return 0;
132
        case 'n':
133
          client.removeJob(job);
134
          std::cout << "Job not scheduled.\n";
135
          return 0;
136
        default:
137
          std::cout << "Please answer Y or N\n";
138
          break;
139
        }
140
      }
141
    }
142

    
143
}
144

    
145
time_t convertTime(const char *theTime)
146
{
147
  // Check that the date time is OK
148
  char *argv[2] = { const_cast<char *>(theTime), 0 };
149
  time_t result = parsetime( 1, argv );
150
  if (result == 0) {
151
    std::cerr << "Fatal error: failed to parse date time: " << theTime << "\n";
152
    exit(1);
153
  }
154
  return result;
155
}
156

    
157
void usage(const char *progname) {
158
  std::cerr << "Usage: " << progname << " [OPTION] TYPE CHANNEL TIME DURATION OUTPUT\n"
159
            << "Schedule DVB recording for channel CHANNEL, using DVB-card\n"
160
            << "\n"
161
            << "TYPE can be dvb-t, dvb-s or dvb-c.\n"
162
            << "TIME is time to schedule recording. format is\n"
163
            << "     same as for at(1)."
164
            << "DURATION is time to stream for in minutes.\n"
165
            << "OUTPUT is the output file, must be owned by the user\n"
166
            << "       and writable by dvbd.\n"
167
            << "\n"
168
            << "  -p, --priority         Priority of stream (default is 10\n"
169
            << "                         higher priorities may dislodge lower\n"
170
            << "                         ones).\n"
171
            << "  -s, --socket           Specify socket to connect to\n"
172
            << "                         (default is " << DEFAULT_SOCKET_FILE << ")\n";
173
}
174

    
175
// For parsetime
176
extern "C" 
177
int yywrap()
178
{
179
  return 1;
180
}