MythTV  master
mainserver.h
Go to the documentation of this file.
1 #ifndef MAINSERVER_H_
2 #define MAINSERVER_H_
3 
4 #include <QReadWriteLock>
5 #include <QStringList>
6 #include <QRunnable>
7 #include <QEvent>
8 #include <QMutex>
9 #include <QHash>
10 #include <QMap>
11 
12 #include <vector>
13 using namespace std;
14 
15 #include "tv.h"
16 #include "playbacksock.h"
17 #include "mthreadpool.h"
18 #include "encoderlink.h"
19 #include "filetransfer.h"
20 #include "scheduler.h"
21 #include "livetvchain.h"
22 #include "autoexpire.h"
23 #include "mythsocket.h"
24 #include "mythdeque.h"
25 #include "mythdownloadmanager.h"
26 
27 #ifdef DeleteFile
28 #undef DeleteFile
29 #endif
30 
31 class QUrl;
32 class MythServer;
33 class QTimer;
34 class FileSystemInfo;
35 class MetadataFactory;
36 class FreeSpaceUpdater;
37 
39 {
40  friend class MainServer;
41  public:
42  DeleteStruct(MainServer *ms, QString filename, QString title,
43  uint chanid, QDateTime recstartts, QDateTime recendts,
44  uint recordedId,
45  bool forceMetadataDelete) :
46  m_ms(ms), m_filename(filename), m_title(title),
47  m_chanid(chanid), m_recstartts(recstartts),
48  m_recendts(recendts), m_recordedid(recordedId),
49  m_forceMetadataDelete(forceMetadataDelete), m_fd(-1), m_size(0)
50  {
51  }
52 
53  DeleteStruct(MainServer *ms, QString filename, int fd, off_t size) :
54  m_ms(ms), m_filename(filename), m_chanid(0), m_recordedid(0),
55  m_forceMetadataDelete(false), m_fd(fd), m_size(size)
56  {
57  }
58 
59  protected:
61  QString m_filename;
62  QString m_title;
64  QDateTime m_recstartts;
65  QDateTime m_recendts;
68  int m_fd;
70 };
71 
72 class DeleteThread : public QRunnable, public DeleteStruct
73 {
74  public:
75  DeleteThread(MainServer *ms, QString filename, QString title, uint chanid,
76  QDateTime recstartts, QDateTime recendts, uint recordingId,
77  bool forceMetadataDelete) :
78  DeleteStruct(ms, filename, title, chanid, recstartts,
79  recendts, recordingId, forceMetadataDelete) {}
80  void start(void)
81  { MThreadPool::globalInstance()->startReserved(this, "DeleteThread"); }
82  void run(void) override; // QRunnable
83 };
84 
85 class TruncateThread : public QRunnable, public DeleteStruct
86 {
87  public:
88  TruncateThread(MainServer *ms, QString filename, int fd, off_t size) :
89  DeleteStruct(ms, filename, fd, size) {}
90  void start(void)
91  { MThreadPool::globalInstance()->start(this, "Truncate"); }
92  void run(void) override; // QRunnable
93 };
94 
95 class RenameThread : public QRunnable
96 {
97 public:
98  RenameThread(MainServer &ms, PlaybackSock &pbs, QString src, QString dst)
99  : m_ms(ms), m_pbs(pbs), m_src(src), m_dst(dst) {}
100  void run(void) override; // QRunnable
101 
102 private:
103  static QMutex m_renamelock;
104 
107  QString m_src, m_dst;
108 };
109 
110 class MainServer : public QObject, public MythSocketCBs
111 {
112  Q_OBJECT
113 
114  friend class DeleteThread;
115  friend class TruncateThread;
116  friend class FreeSpaceUpdater;
117  friend class RenameThread;
118  public:
119  MainServer(bool master, int port,
120  QMap<int, EncoderLink *> *tvList,
122 
123  ~MainServer();
124 
125  void Stop(void);
126 
127  void customEvent(QEvent *e) override; // QObject
128 
129  bool isClientConnected(bool onlyBlockingClients = false);
130  void ShutSlaveBackendsDown(QString &haltcmd);
131 
132  void ProcessRequest(MythSocket *sock);
133 
134  void readyRead(MythSocket *socket) override; // MythSocketCBs
135  void connectionClosed(MythSocket *socket) override; // MythSocketCBs
136  void connectionFailed(MythSocket *socket) override // MythSocketCBs
137  { (void)socket; }
138  void connected(MythSocket *socket) override // MythSocketCBs
139  { (void)socket; }
140 
141  void DeletePBS(PlaybackSock *pbs);
142 
143  size_t GetCurrentMaxBitrate(void);
144  void BackendQueryDiskSpace(QStringList &strlist, bool consolidated,
145  bool allHosts);
146  void GetFilesystemInfos(QList<FileSystemInfo> &fsInfos,
147  bool useCache=true);
148 
149  int GetExitCode() const { return m_exitCode; }
150 
151  void UpdateSystemdStatus(void);
152 
153  protected slots:
154  void reconnectTimeout(void);
155  void deferredDeleteSlot(void);
156  void autoexpireUpdate(void);
157 
158  private slots:
159  void NewConnection(qt_socket_fd_t socketDescriptor);
160 
161  private:
162 
163  void ProcessRequestWork(MythSocket *sock);
164  void HandleAnnounce(QStringList &slist, QStringList commands,
165  MythSocket *socket);
166  void HandleDone(MythSocket *socket);
167 
168  void GetActiveBackends(QStringList &hosts);
169  void HandleActiveBackendsQuery(PlaybackSock *pbs);
170  void HandleIsActiveBackendQuery(QStringList &slist, PlaybackSock *pbs);
171  void HandleMoveFile(PlaybackSock *pbs, const QString &storagegroup,
172  const QString &src, const QString &dst);
173  bool HandleDeleteFile(QStringList &slist, PlaybackSock *pbs);
174  bool HandleDeleteFile(QString filename, QString storagegroup,
175  PlaybackSock *pbs = nullptr);
176  void HandleQueryRecordings(QString type, PlaybackSock *pbs);
177  void HandleQueryRecording(QStringList &slist, PlaybackSock *pbs);
178  void HandleStopRecording(QStringList &slist, PlaybackSock *pbs);
179  void DoHandleStopRecording(RecordingInfo &recinfo, PlaybackSock *pbs);
180  void HandleDeleteRecording(QString &chanid, QString &starttime,
181  PlaybackSock *pbs, bool forceMetadataDelete,
182  bool forgetHistory);
183  void HandleDeleteRecording(QStringList &slist, PlaybackSock *pbs,
184  bool forceMetadataDelete);
185  void DoHandleDeleteRecording(RecordingInfo &recinfo, PlaybackSock *pbs,
186  bool forceMetadataDelete, bool expirer=false,
187  bool forgetHistory=false);
188  void HandleUndeleteRecording(QStringList &slist, PlaybackSock *pbs);
189  void DoHandleUndeleteRecording(RecordingInfo &recinfo, PlaybackSock *pbs);
190  void HandleForgetRecording(QStringList &slist, PlaybackSock *pbs);
191  void HandleRescheduleRecordings(const QStringList &request,
192  PlaybackSock *pbs);
193  bool HandleAddChildInput(uint inputid);
194  void HandleGoToSleep(PlaybackSock *pbs);
195  void HandleQueryFreeSpace(PlaybackSock *pbs, bool allBackends);
196  void HandleQueryFreeSpaceSummary(PlaybackSock *pbs);
197  void HandleQueryCheckFile(QStringList &slist, PlaybackSock *pbs);
198  void HandleQueryFileExists(QStringList &slist, PlaybackSock *pbs);
199  void HandleQueryFindFile(QStringList &slist, PlaybackSock *pbs);
200  void HandleQueryFileHash(QStringList &slist, PlaybackSock *pbs);
201  void HandleQueryGuideDataThrough(PlaybackSock *pbs);
202  void HandleGetPendingRecordings(PlaybackSock *pbs, QString table = "", int recordid=-1);
203  void HandleGetScheduledRecordings(PlaybackSock *pbs);
204  void HandleGetConflictingRecordings(QStringList &slist, PlaybackSock *pbs);
205  void HandleGetExpiringRecordings(PlaybackSock *pbs);
206  void HandleSGGetFileList(QStringList &sList, PlaybackSock *pbs);
207  void HandleSGFileQuery(QStringList &sList, PlaybackSock *pbs);
208  void HandleGetFreeInputInfo(PlaybackSock *pbs, uint excluded_input);
209  void HandleGetNextFreeRecorder(QStringList &slist, PlaybackSock *pbs);
210  void HandleGetFreeRecorder(PlaybackSock *pbs);
211  void HandleGetFreeRecorderCount(PlaybackSock *pbs);
212  void HandleGetFreeRecorderList(PlaybackSock *pbs);
213  void HandleGetConnectedRecorderList(PlaybackSock *pbs);
214  void HandleRecorderQuery(QStringList &slist, QStringList &commands,
215  PlaybackSock *pbs);
216  void HandleSetNextLiveTVDir(QStringList &commands, PlaybackSock *pbs);
217  void HandleFileTransferQuery(QStringList &slist, QStringList &commands,
218  PlaybackSock *pbs);
219  void HandleGetRecorderNum(QStringList &slist, PlaybackSock *pbs);
220  void HandleGetRecorderFromNum(QStringList &slist, PlaybackSock *pbs);
221  void HandleMessage(QStringList &slist, PlaybackSock *pbs);
222  void HandleSetVerbose(QStringList &slist, PlaybackSock *pbs);
223  void HandleSetLogLevel(QStringList &slist, PlaybackSock *pbs);
224  void HandleGenPreviewPixmap(QStringList &slist, PlaybackSock *pbs);
225  void HandlePixmapLastModified(QStringList &slist, PlaybackSock *pbs);
226  void HandlePixmapGetIfModified(const QStringList &slist, PlaybackSock *pbs);
227  void HandleIsRecording(QStringList &slist, PlaybackSock *pbs);
228  void HandleCheckRecordingActive(QStringList &slist, PlaybackSock *pbs);
229  void HandleFillProgramInfo(QStringList &slist, PlaybackSock *pbs);
230  void HandleSetChannelInfo(QStringList &slist, PlaybackSock *pbs);
231  void HandleRemoteEncoder(QStringList &slist, QStringList &commands,
232  PlaybackSock *pbs);
233  void HandleLockTuner(PlaybackSock *pbs, int cardid = -1);
234  void HandleFreeTuner(int cardid, PlaybackSock *pbs);
235  void HandleCutMapQuery(const QString &chanid, const QString &starttime,
236  PlaybackSock *pbs, bool commbreak);
237  void HandleCommBreakQuery(const QString &chanid, const QString &starttime,
238  PlaybackSock *pbs);
239  void HandleCutlistQuery(const QString &chanid, const QString &starttime,
240  PlaybackSock *pbs);
241  void HandleBookmarkQuery(const QString &chanid, const QString &starttime,
242  PlaybackSock *pbs);
243  void HandleSetBookmark(QStringList &tokens, PlaybackSock *pbs);
244  void HandleSettingQuery(QStringList &tokens, PlaybackSock *pbs);
245  void HandleSetSetting(QStringList &tokens, PlaybackSock *pbs);
246  void HandleScanVideos(PlaybackSock *pbs);
247  void HandleScanMusic(const QStringList &slist, PlaybackSock *pbs);
248  void HandleMusicTagUpdateVolatile(const QStringList &slist, PlaybackSock *pbs);
249  void HandleMusicTagUpdateMetadata(const QStringList &slist, PlaybackSock *pbs);
250  void HandleMusicFindAlbumArt(const QStringList &slist, PlaybackSock *pbs);
251  void HandleMusicTagGetImage(const QStringList &slist, PlaybackSock *pbs);
252  void HandleMusicTagAddImage(const QStringList &slist, PlaybackSock *pbs);
253  void HandleMusicTagRemoveImage(const QStringList &slist, PlaybackSock *pbs);
254  void HandleMusicTagChangeImage(const QStringList &slist, PlaybackSock *pbs);
255  void HandleMusicCalcTrackLen(const QStringList &slist, PlaybackSock *pbs);
256  void HandleMusicFindLyrics(const QStringList &slist, PlaybackSock *pbs);
257  void HandleMusicGetLyricGrabbers(const QStringList &slist, PlaybackSock *pbs);
258  void HandleMusicSaveLyrics(const QStringList &slist, PlaybackSock *pbs);
259  void HandleVersion(MythSocket *socket, const QStringList &slist);
260  void HandleBackendRefresh(MythSocket *socket);
261  void HandleQueryLoad(PlaybackSock *pbs);
262  void HandleQueryUptime(PlaybackSock *pbs);
263  void HandleQueryHostname(PlaybackSock *pbs);
264  void HandleQueryMemStats(PlaybackSock *pbs);
265  void HandleQueryTimeZone(PlaybackSock *pbs);
266  void HandleBlockShutdown(bool blockShutdown, PlaybackSock *pbs);
267  void HandleDownloadFile(const QStringList &command, PlaybackSock *pbs);
268  void HandleSlaveDisconnectedEvent(const MythEvent &event);
269 
270  void SendResponse(MythSocket *sock, QStringList &commands);
271  void SendErrorResponse(MythSocket *sock, const QString &error);
272  void SendErrorResponse(PlaybackSock *pbs, const QString &error);
273  void SendSlaveDisconnectedEvent(const QList<uint> &offlineEncoderIDs,
274  bool needsReschedule);
275 
276  void getGuideDataThrough(QDateTime &GuideDataThrough);
277 
278  PlaybackSock *GetSlaveByHostname(const QString &hostname);
279  PlaybackSock *GetMediaServerByHostname(const QString &hostname);
280  PlaybackSock *GetPlaybackBySock(MythSocket *socket);
281  FileTransfer *GetFileTransferByID(int id);
282  FileTransfer *GetFileTransferBySock(MythSocket *socket);
283 
284  QString LocalFilePath(const QString &path, const QString &wantgroup);
285 
286  int GetfsID(QList<FileSystemInfo>::iterator fsInfo);
287 
288  void DoTruncateThread(DeleteStruct *ds);
289  void DoDeleteThread(DeleteStruct *ds);
290  void DeleteRecordedFiles(DeleteStruct *ds);
291  void DoDeleteInDB(DeleteStruct *ds);
292 
293  LiveTVChain *GetExistingChain(const QString &id);
294  LiveTVChain *GetExistingChain(const MythSocket *sock);
295  LiveTVChain *GetChainWithRecording(const ProgramInfo &pginfo);
296  void AddToChains(LiveTVChain *chain);
297  void DeleteChain(LiveTVChain *chain);
298 
299  void SetExitCode(int exitCode, bool closeApplication);
300 
301  static int DeleteFile(const QString &filename, bool followLinks,
302  bool deleteBrokenSymlinks = false);
303  static int OpenAndUnlink(const QString &filename);
304  static bool TruncateAndClose(ProgramInfo *pginfo,
305  int fd, const QString &filename,
306  off_t fsize);
307 
308  vector<LiveTVChain*> liveTVChains;
310 
311  QMap<int, EncoderLink *> *encoderList;
312 
315 
316  QReadWriteLock sockListLock;
317  vector<PlaybackSock *> playbackList;
318  vector<FileTransfer *> fileTransferList;
319  QSet<MythSocket*> controlSocketList;
320  vector<MythSocket*> decrRefSocketList;
321 
324  QWaitCondition masterFreeSpaceListWait;
325  QStringList masterFreeSpaceList;
326 
327  QTimer *masterServerReconnect; // audited ref #5318
329 
330  bool ismaster;
331 
332  QMutex deletelock;
334 
336 
340 
342  {
344  QDateTime ts;
345  };
346 
348  QTimer *deferredDeleteTimer; // audited ref #5318
350 
351  QTimer *autoexpireUpdateTimer; // audited ref #5318
352  static QMutex truncate_and_close_lock;
353 
354  QMap<QString, int> fsIDcache;
356  QList<FileSystemInfo> fsInfosCache;
358 
360  QMap<QString, QString> m_downloadURLs;
361 
363 
364  typedef QHash<QString,QString> RequestedBy;
366 
367  bool m_stopped;
368 
370 };
371 
372 #endif
373 
374 /* vim: set expandtab tabstop=4 shiftwidth=4: */
QWaitCondition masterFreeSpaceListWait
Definition: mainserver.h:324
FreeSpaceUpdater *volatile masterFreeSpaceListUpdater
Definition: mainserver.h:323
void connected(MythSocket *socket) override
Definition: mainserver.h:138
QHash< QString, QString > RequestedBy
Definition: mainserver.h:364
bool m_stopped
Definition: mainserver.h:367
QMutex m_downloadURLsLock
Definition: mainserver.h:359
int m_exitCode
Definition: mainserver.h:362
unsigned int slots[4]
Definition: element.c:38
RenameThread(MainServer &ms, PlaybackSock &pbs, QString src, QString dst)
Definition: mainserver.h:98
void startReserved(QRunnable *runnable, QString debugName, int waitForAvailMS=0)
void start(QRunnable *runnable, QString debugName, int priority=0)
QMutex deletelock
Definition: mainserver.h:332
void start(void)
Definition: mainserver.h:90
RequestedBy m_previewRequestedBy
Definition: mainserver.h:365
QDateTime m_recstartts
Definition: mainserver.h:64
TruncateThread(MainServer *ms, QString filename, int fd, off_t size)
Definition: mainserver.h:88
static const uint kMasterServerReconnectTimeout
Definition: mainserver.h:369
static void error(const char *str,...)
Definition: vbi.c:41
QTimer * autoexpireUpdateTimer
Definition: mainserver.h:351
bool m_forceMetadataDelete
Definition: mainserver.h:67
bool masterBackendOverride
Definition: mainserver.h:335
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:34
DeleteThread(MainServer *ms, QString filename, QString title, uint chanid, QDateTime recstartts, QDateTime recendts, uint recordingId, bool forceMetadataDelete)
Definition: mainserver.h:75
unsigned int uint
Definition: compat.h:140
vector< MythSocket * > decrRefSocketList
Definition: mainserver.h:320
QList< FileSystemInfo > fsInfosCache
Definition: mainserver.h:356
qintptr qt_socket_fd_t
Definition: mythqtcompat.h:4
void start(void)
Definition: mainserver.h:80
QTimer * deferredDeleteTimer
Definition: mainserver.h:348
#define off_t
vector< FileTransfer * > fileTransferList
Definition: mainserver.h:318
Holds information on recordings and videos.
Definition: programinfo.h:66
static int run(MythMediaDevice *dev=nullptr, bool startRandomShow=false)
This class is used as a container for messages.
Definition: mythevent.h:15
MetadataFactory * metadatafactory
Definition: mainserver.h:314
QMutex liveTVChainsLock
Definition: mainserver.h:309
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
QMutex deferredDeleteLock
Definition: mainserver.h:347
off_t m_size
Definition: mainserver.h:69
QString m_src
Definition: mainserver.h:107
QTimer * masterServerReconnect
Definition: mainserver.h:327
uint m_recordedid
Definition: mainserver.h:66
AutoExpire * m_expirer
Definition: mainserver.h:338
QMutex fsInfosCacheLock
Definition: mainserver.h:357
string hostname
Definition: caa.py:17
void connectionFailed(MythSocket *socket) override
Definition: mainserver.h:136
int GetExitCode() const
Definition: mainserver.h:149
QString m_filename
Definition: mainserver.h:61
PlaybackSock * masterServer
Definition: mainserver.h:328
QString m_title
Definition: mainserver.h:62
QSet< MythSocket * > controlSocketList
Definition: mainserver.h:319
DeleteStruct(MainServer *ms, QString filename, int fd, off_t size)
Definition: mainserver.h:53
static MThreadPool * globalInstance(void)
QMap< QString, int > fsIDcache
Definition: mainserver.h:354
MainServer & m_ms
Definition: mainserver.h:105
QMutex m_addChildInputLock
Definition: mainserver.h:339
Definition: pbs.py:1
QMap< int, EncoderLink * > * encoderList
Definition: mainserver.h:311
MThreadPool threadPool
Definition: mainserver.h:333
QMap< QString, QString > m_downloadURLs
Definition: mainserver.h:360
Used to expire recordings to make space for new recordings.
Definition: autoexpire.h:61
AutoExpire * expirer
QStringList masterFreeSpaceList
Definition: mainserver.h:325
uint m_chanid
Definition: mainserver.h:63
QReadWriteLock sockListLock
Definition: mainserver.h:316
vector< LiveTVChain * > liveTVChains
Definition: mainserver.h:308
QDateTime m_recendts
Definition: mainserver.h:65
DeleteStruct(MainServer *ms, QString filename, QString title, uint chanid, QDateTime recstartts, QDateTime recendts, uint recordedId, bool forceMetadataDelete)
Definition: mainserver.h:42
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:26
MainServer * m_ms
Definition: mainserver.h:60
Scheduler * sched
vector< PlaybackSock * > playbackList
Definition: mainserver.h:317
static QMutex m_renamelock
Definition: mainserver.h:103
bool ismaster
Definition: mainserver.h:330
MythDeque< DeferredDeleteStruct > deferredDeleteList
Definition: mainserver.h:349
QMutex masterFreeSpaceListLock
Definition: mainserver.h:322
PlaybackSock & m_pbs
Definition: mainserver.h:106
MythServer * mythserver
Definition: mainserver.h:313
Keeps track of recordings in a current LiveTV instance.
Definition: livetvchain.h:31
QMap< int, EncoderLink * > tvList
static QMutex truncate_and_close_lock
Definition: mainserver.h:352
Scheduler * m_sched
Definition: mainserver.h:337
MythDeque is similar to QPtrQueue, while being based off deque, this allows that items that are not a...
Definition: mythdeque.h:28
QMutex fsIDcacheLock
Definition: mainserver.h:355