MythTV master
networkcontrol.h
Go to the documentation of this file.
1#ifndef NETWORKCONTROL_H_
2#define NETWORKCONTROL_H_
3
4#include <deque>
5
6#include <QWaitCondition>
7#include <QStringList>
8#include <QTcpSocket>
9#include <QRunnable>
10#include <QMutex>
11#include <QEvent>
12#include <QRecursiveMutex>
13
14#include "libmythbase/mthread.h"
16
17class MainServer;
18class QTextStream;
19
20// Locking order
21// clientLock -> ncLock
22// -> nrLock
23
24class NetworkControlClient : public QObject
25{
26 Q_OBJECT
27 public:
28 explicit NetworkControlClient(QTcpSocket *s);
29 ~NetworkControlClient() override;
30
31 QTcpSocket *getSocket() { return m_socket; }
32 QTextStream *getTextStream() { return m_textStream; }
33
34 signals:
35 void commandReceived(QString&);
36
37 public slots:
38 void readClient();
39
40 private:
41 QTcpSocket *m_socket {nullptr};
42 QTextStream *m_textStream {nullptr};
43};
44
45class NetworkCommand : public QObject
46{
47 Q_OBJECT
48 public:
49 NetworkCommand(NetworkControlClient *cli, const QString& c)
50 : m_command(c.trimmed()),
51 m_client(cli)
52 {
53 m_args = m_command.simplified().split(" ");
54 }
55
57 {
58 if (this == &nc)
59 return *this;
61 m_client = nc.m_client;
62 m_args = m_command.simplified().split(" ");
63 return *this;
64 }
65
66 QString getCommand() { return m_command; }
68 QString getArg(int arg) { return m_args[arg]; }
69 int getArgCount() { return m_args.size(); }
70 QString getFrom(int arg);
71
72 private:
73 QString m_command;
75 QStringList m_args;
76};
77
78class NetworkControlCloseEvent : public QEvent
79{
80 public:
82 QEvent(kEventType), m_networkControlClient(ncc) {}
83
85
86 static const Type kEventType;
87
88 private:
90};
91
92class NetworkControl;
93class MythUIType;
94
95class NetworkControl : public ServerPool, public QRunnable
96{
97 Q_OBJECT
98
99 public:
101 ~NetworkControl() override;
102
103 private slots:
104 void newControlConnection(QTcpSocket *client);
105 void receiveCommand(QString &command);
106 void deleteClient(void);
107
108 protected:
109 void run(void) override; // QRunnable
110
111 private:
112 QString processJump(NetworkCommand *nc);
113 QString processKey(NetworkCommand *nc);
115 QString processPlay(NetworkCommand *nc, int clientID);
116 QString processQuery(NetworkCommand *nc);
117 static QString processSet(NetworkCommand *nc);
118 static QString processMessage(NetworkCommand *nc);
119 static QString processNotification(NetworkCommand *nc);
120 QString processTheme(NetworkCommand *nc);
121 QString processHelp(NetworkCommand *nc);
122
123 void notifyDataAvailable(void);
124 void sendReplyToClient(NetworkControlClient *ncc, const QString &reply);
125 void customEvent(QEvent *e) override; // QObject
126
127 static QString listRecordings(const QString& chanid = "", const QString& starttime = "");
128 static QString listSchedule(const QString& chanID = "") ;
129 static QString listChannels(uint start, uint limit) ;
130 static QString saveScreenshot(NetworkCommand *nc);
131
133
135
136 static QString getWidgetType(MythUIType *type);
137
138 QString m_prompt {"# "};
139 bool m_gotAnswer {false};
140 QString m_answer {""};
141 QMap <QString, QString> m_jumpMap;
142 QMap <QString, int> m_keyMap;
143 QMap <int, QString> m_keyTextMap;
144
145 mutable QRecursiveMutex m_clientLock;
146 QList<NetworkControlClient*> m_clients;
147
148 QList<NetworkCommand*> m_networkControlCommands; // protected by ncLock
149 QMutex m_ncLock;
150 QWaitCondition m_ncCond; // protected by ncLock
151
152 QList<NetworkCommand*> m_networkControlReplies;
153 QMutex m_nrLock;
154
156 bool m_stopCommandThread {false}; // protected by ncLock
157};
158
159#endif
160
161/* vim: set expandtab tabstop=4 shiftwidth=4: */
162
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
The base class on which all widgets and screens are based.
Definition: mythuitype.h:86
NetworkControlClient * getClient()
QString getArg(int arg)
NetworkCommand & operator=(NetworkCommand const &nc)
QStringList m_args
QString getFrom(int arg)
NetworkCommand(NetworkControlClient *cli, const QString &c)
NetworkControlClient * m_client
QString getCommand()
NetworkControlClient(QTcpSocket *s)
void commandReceived(QString &)
QTcpSocket * getSocket()
QTcpSocket * m_socket
~NetworkControlClient() override
QTextStream * m_textStream
QTextStream * getTextStream()
NetworkControlCloseEvent(NetworkControlClient *ncc)
NetworkControlClient * m_networkControlClient
NetworkControlClient * getClient()
static const Type kEventType
void processNetworkControlCommand(NetworkCommand *nc)
QList< NetworkCommand * > m_networkControlReplies
static QString processSet(NetworkCommand *nc)
static QString getWidgetType(MythUIType *type)
QRecursiveMutex m_clientLock
static QString processNotification(NetworkCommand *nc)
QString processJump(NetworkCommand *nc)
QString processLiveTV(NetworkCommand *nc)
void receiveCommand(QString &command)
QList< NetworkControlClient * > m_clients
QString processQuery(NetworkCommand *nc)
QMap< int, QString > m_keyTextMap
static QString processMessage(NetworkCommand *nc)
void customEvent(QEvent *e) override
QString processTheme(NetworkCommand *nc)
static QString listChannels(uint start, uint limit)
QMap< QString, int > m_keyMap
QWaitCondition m_ncCond
QString processPlay(NetworkCommand *nc, int clientID)
static QString listSchedule(const QString &chanID="")
void deleteClient(void)
void newControlConnection(QTcpSocket *client)
QList< NetworkCommand * > m_networkControlCommands
MThread * m_commandThread
QString processKey(NetworkCommand *nc)
void notifyDataAvailable(void)
static QString listRecordings(const QString &chanid="", const QString &starttime="")
void run(void) override
QString processHelp(NetworkCommand *nc)
void sendReplyToClient(NetworkControlClient *ncc, const QString &reply)
QMap< QString, QString > m_jumpMap
~NetworkControl() override
static QString saveScreenshot(NetworkCommand *nc)
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:60
unsigned int uint
Definition: freesurround.h:24