Statistics
| Branch: | Tag: | Revision:

dvbd / dvblist.cpp @ bfdb7446

History | View | Annotate | Download (4.58 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
26
#include <string>
27
#include <iostream>
28
#include <time.h>
29
#include <getopt.h>
30
#include <cerrno>
31
#include <fstream>
32
33
void usage(const char *progname);
34
35
int main(int argc, char *argv[])
36
{
37
  // improve iostream performance
38
  std::ios::sync_with_stdio(false);
39
40
  std::string socketFile = DEFAULT_SOCKET_FILE;
41
  int priority = 10;
42
  bool listConnections = false;
43
  bool listSchedule = false;
44
  bool listNextJob = false;
45
46
  while (true) {
47
    static struct option long_options[] = {
48
      {"priority", 1, 0, 'p'},
49
      {"socket", 1, 0, 's'},
50
      {"connections", 0, 0, 'c'},
51
      {"schedule", 0, 0, 'h'},
52
      {"nextjob", 0, 0, 'n'},
53
      {0, 0, 0, 0}
54
    };
55
 
56
    int option_index = 0;
57
    int c = getopt_long(argc, argv, "chnp:s:", long_options, &option_index);
58
    if (c == -1)
59
      break;
60
    switch (c) {
61
    case 'p':
62
      priority = toInt(optarg);
63
      break;
64
    case 's':
65
      socketFile = optarg;
66
      break;
67
    case 'c':
68
      listConnections = true;
69
      break;
70
    case 'h':
71
      listSchedule = true;
72
      break;
73
    case 'n':
74
      listNextJob = true;
75
      break;
76
77
    default:
78
      usage(argv[0]);
79
      return 1;
80
    }
81
  }
82
83
  ClientConnection client(socketFile);
84
  if (!client) {
85
    std::cerr << "Failed to connect to server on socket " << socketFile << std::endl;
86
    return 1;
87
  }
88
89
  // List current connections
90
  if (listConnections) {
91
    StringList connections;
92
    if (!client.listConnections(connections)) {
93
      std::cerr << "Failed to list connections on server.\n";
94
      return 1;
95
    }
96
97
    std::cout << "Current connections are:\n";
98
    for (StringList::iterator i = connections.begin(); i != connections.end(); i++)
99
      std::cout << *i << "\n";
100
    return 0;
101
  }
102
103
  if (listSchedule) {
104
    StringList schedule;
105
    if (!client.listSchedule(schedule)) {
106
      std::cerr << "Failed to list schedules on server.\n";
107
      return 1;
108
    }
109
110
    std::cout << "Schedule is:\n";
111
    for (StringList::iterator i = schedule.begin(); i != schedule.end(); i++)
112
      std::cout << *i << "\n";
113
    return 0;
114
  }
115
116
  if (listNextJob) {
117
    time_t nextJobTime;
118
    if (!client.getNextJobTime(nextJobTime)) {
119
      std::cerr << "No next job!\n";
120
      return 1;
121
    }
122
    else {
123
      std::cout << stripTrailingWS(ctime(&nextJobTime)) << "\n";
124
      return 0;
125
    }
126
  }
127
128
  if (argc - optind != 1) {
129
    if (argc - optind != 0)
130
      std::cerr << "Incorrect number of arguments passed (expected 1, got " 
131
                << argc - optind << ")\n\n";
132
133
    usage(argv[0]);
134
    return 1;
135
  }
136
137
  // Parse the command line arguments
138
  std::string type(argv[optind++]);
139
140
  StringList channels;
141
  if (!client.getChannels(type, priority, channels)) {
142
    std::cerr << "Failed to get channels from server.\n";
143
    return 1;
144
  }
145
146
  std::cout << type << " channels available to tune at priority " << priority << "\n";
147
  for (StringList::iterator i = channels.begin(); i != channels.end(); i++)
148
    std::cout << *i << "\n";
149
150
  return 0;
151
}
152
153
void usage(const char *progname) {
154
  std::cerr << "Usage: " << progname << " [OPTION] TYPE\n"
155
            << "List DVB channels of type TYPE available for a given priority\n"
156
            << "\n"
157
            << "TYPE can be dvb-t, dvb-s or dvb-c.\n"
158
            << "\n"
159
            << "  -p, --priority         Priority of stream (default is 10\n"
160
            << "                         higher priorities may dislodge lower\n"
161
            << "                         ones).\n"
162
            << "  -s, --socket           Specify socket to connect to\n"
163
            << "                         (default is " << DEFAULT_SOCKET_FILE << ")\n"
164
            << "  -c, --connections      List current connections\n"
165
            << "                         (no need to give TYPE)\n"
166
            << "  -h, --schedule         List upcoming recordings\n"
167
            << "                         (no need to give TYPE)\n"
168
            << "  -n, --nextjob          Show date of next job\n";
169
170
}