MythTV  master
networkcontrol.h
Go to the documentation of this file.
1 #ifndef NETWORKCONTROL_H_
2 #define NETWORKCONTROL_H_
3 
4 #include <deque>
5 using namespace std;
6 
7 #include <QWaitCondition>
8 #include <QStringList>
9 #include <QTcpSocket>
10 #include <QRunnable>
11 #include <QMutex>
12 #include <QEvent>
13 
14 #include "mthread.h"
15 #include "serverpool.h"
16 
17 class MainServer;
18 class QTextStream;
19 
20 // Locking order
21 // clientLock -> ncLock
22 // -> nrLock
23 
24 class 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 
45 class NetworkCommand : public QObject
46 {
47  Q_OBJECT
48  public:
49  NetworkCommand(NetworkControlClient *cli, const QString& c)
50  {
51  m_command = c.trimmed();
52  m_client = cli;
53  m_args = m_command.simplified().split(" ");
54  }
55 
57  {
58  if (this == &nc)
59  return *this;
60  m_command = nc.m_command;
61  m_client = nc.m_client;
62  m_args = m_command.simplified().split(" ");
63  return *this;
64  }
65 
66  QString getCommand() { return m_command; }
67  NetworkControlClient *getClient() { return m_client; }
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;
74  NetworkControlClient *m_client {nullptr};
75  QStringList m_args;
76 };
77 
78 class NetworkControlCloseEvent : public QEvent
79 {
80  public:
82  QEvent(kEventType), m_networkControlClient(ncc) {}
83 
84  NetworkControlClient *getClient() { return m_networkControlClient; }
85 
86  static Type kEventType;
87 
88  private:
89  NetworkControlClient *m_networkControlClient {nullptr};
90 };
91 
92 class NetworkControl;
93 class MythUIType;
94 
95 class NetworkControl : public ServerPool, public QRunnable
96 {
97  Q_OBJECT
98 
99  public:
100  NetworkControl();
101  ~NetworkControl() override;
102 
103  private slots:
104  void newConnection(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, 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 
132  void processNetworkControlCommand(NetworkCommand *nc);
133 
134  void deleteClient(NetworkControlClient *ncc);
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 QMutex m_clientLock {QMutex::Recursive};
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 
155  MThread *m_commandThread {nullptr};
156  bool m_stopCommandThread {false}; // protected by ncLock
157 };
158 
159 #endif
160 
161 /* vim: set expandtab tabstop=4 shiftwidth=4: */
162 
e
QDomElement e
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1423
NetworkCommand
Definition: networkcontrol.h:46
NetworkCommand::m_args
QStringList m_args
Definition: networkcontrol.h:75
NetworkControl::m_networkControlCommands
QList< NetworkCommand * > m_networkControlCommands
Definition: networkcontrol.h:148
slots
std::array< unsigned int, 4 > slots
Definition: element.cpp:38
NetworkControl::processLiveTV
QString processLiveTV(NetworkCommand *nc)
NetworkControl::m_ncLock
QMutex m_ncLock
Definition: networkcontrol.h:149
NetworkControl::m_keyMap
QMap< QString, int > m_keyMap
Definition: networkcontrol.h:142
ServerPool
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:60
NetworkControl
Definition: networkcontrol.h:96
arg
arg(title).arg(filename).arg(doDelete))
NetworkControlClient::commandReceived
void commandReceived(QString &)
MainServer
Definition: mainserver.h:115
NetworkControlClient
Definition: networkcontrol.h:25
NetworkControlCloseEvent
Definition: networkcontrol.h:79
NetworkCommand::NetworkCommand
NetworkCommand(NetworkControlClient *cli, const QString &c)
Definition: networkcontrol.h:49
uint
unsigned int uint
Definition: compat.h:140
NetworkCommand::operator=
NetworkCommand & operator=(NetworkCommand const &nc)
Definition: networkcontrol.h:56
NetworkControl::m_keyTextMap
QMap< int, QString > m_keyTextMap
Definition: networkcontrol.h:143
NetworkControl::m_nrLock
QMutex m_nrLock
Definition: networkcontrol.h:153
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:66
NetworkCommand::getArgCount
int getArgCount()
Definition: networkcontrol.h:69
NetworkCommand::m_command
QString m_command
Definition: networkcontrol.h:73
NetworkControlClient::getTextStream
QTextStream * getTextStream()
Definition: networkcontrol.h:32
NetworkCommand::m_client
NetworkControlClient * m_client
Definition: networkcontrol.h:74
NetworkControlCloseEvent::NetworkControlCloseEvent
NetworkControlCloseEvent(NetworkControlClient *ncc)
Definition: networkcontrol.h:81
serverpool.h
NetworkControl::m_jumpMap
QMap< QString, QString > m_jumpMap
Definition: networkcontrol.h:141
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
mthread.h
NetworkControl::m_ncCond
QWaitCondition m_ncCond
Definition: networkcontrol.h:150
NetworkCommand::getClient
NetworkControlClient * getClient()
Definition: networkcontrol.h:67
NetworkControlCloseEvent::kEventType
static Type kEventType
Definition: networkcontrol.h:86
NetworkControl::m_clients
QList< NetworkControlClient * > m_clients
Definition: networkcontrol.h:146
NetworkCommand::getCommand
QString getCommand()
Definition: networkcontrol.h:66
NetworkControlCloseEvent::getClient
NetworkControlClient * getClient()
Definition: networkcontrol.h:84
NetworkCommand::getArg
QString getArg(int arg)
Definition: networkcontrol.h:68
NetworkControlClient::getSocket
QTcpSocket * getSocket()
Definition: networkcontrol.h:31
NetworkControl::m_networkControlReplies
QList< NetworkCommand * > m_networkControlReplies
Definition: networkcontrol.h:152