00001 #ifndef NETWORKCONTROL_H_
00002 #define NETWORKCONTROL_H_
00003
00004 #include <deque>
00005 using namespace std;
00006
00007 #include <QWaitCondition>
00008 #include <QStringList>
00009 #include <QTcpSocket>
00010 #include <QRunnable>
00011 #include <QMutex>
00012 #include <QEvent>
00013
00014 #include "mthread.h"
00015 #include "serverpool.h"
00016
00017 class MainServer;
00018 class QTextStream;
00019
00020
00021
00022
00023
00024 class NetworkControlClient : public QObject
00025 {
00026 Q_OBJECT
00027 public:
00028 NetworkControlClient(QTcpSocket *);
00029 ~NetworkControlClient();
00030
00031 QTcpSocket *getSocket() { return m_socket; }
00032 QTextStream *getTextStream() { return m_textStream; }
00033
00034 signals:
00035 void commandReceived(QString&);
00036
00037 public slots:
00038 void readClient();
00039
00040 private:
00041 QTcpSocket *m_socket;
00042 QTextStream *m_textStream;
00043 };
00044
00045 class NetworkCommand : public QObject
00046 {
00047 Q_OBJECT
00048 public:
00049 NetworkCommand(NetworkControlClient *cli, QString c)
00050 {
00051 m_command = c.trimmed();
00052 m_client = cli;
00053 m_args = m_command.simplified().split(" ");
00054 }
00055
00056 NetworkCommand &operator=(NetworkCommand const &nc)
00057 {
00058 m_command = nc.m_command;
00059 m_client = nc.m_client;
00060 m_args = m_command.simplified().split(" ");
00061 return *this;
00062 }
00063
00064 QString getCommand() { return m_command; }
00065 NetworkControlClient *getClient() { return m_client; }
00066 QString getArg(int arg) { return m_args[arg]; }
00067 int getArgCount() { return m_args.size(); }
00068 QString getFrom(int arg);
00069
00070 private:
00071 QString m_command;
00072 NetworkControlClient *m_client;
00073 QStringList m_args;
00074 };
00075
00076 class NetworkControlCloseEvent : public QEvent
00077 {
00078 public:
00079 NetworkControlCloseEvent(NetworkControlClient *ncc) :
00080 QEvent(kEventType), m_networkControlClient(ncc) {}
00081
00082 NetworkControlClient *getClient() { return m_networkControlClient; }
00083
00084 static Type kEventType;
00085
00086 private:
00087 NetworkControlClient * m_networkControlClient;
00088 };
00089
00090 class NetworkControl;
00091
00092 class NetworkControl : public ServerPool, public QRunnable
00093 {
00094 Q_OBJECT
00095
00096 public:
00097 NetworkControl();
00098 ~NetworkControl();
00099
00100 private slots:
00101 void newConnection(QTcpSocket *socket);
00102 void receiveCommand(QString &command);
00103 void deleteClient(void);
00104
00105 protected:
00106 void run(void);
00107
00108 private:
00109 QString processJump(NetworkCommand *nc);
00110 QString processKey(NetworkCommand *nc);
00111 QString processLiveTV(NetworkCommand *nc);
00112 QString processPlay(NetworkCommand *nc, int clientID);
00113 QString processQuery(NetworkCommand *nc);
00114 QString processSet(NetworkCommand *nc);
00115 QString processMessage(NetworkCommand *nc);
00116 QString processHelp(NetworkCommand *nc);
00117
00118 void notifyDataAvailable(void);
00119 void sendReplyToClient(NetworkControlClient *ncc, QString &reply);
00120 void customEvent(QEvent *e);
00121
00122 QString listRecordings(QString chanid = "", QString starttime = "");
00123 QString listSchedule(const QString& chanID = "") const;
00124 QString listChannels(const uint start, const uint limit) const;
00125 QString saveScreenshot(NetworkCommand *nc);
00126
00127 void processNetworkControlCommand(NetworkCommand *nc);
00128
00129 void deleteClient(NetworkControlClient *ncc);
00130
00131 QString prompt;
00132 bool gotAnswer;
00133 QString answer;
00134 QMap <QString, QString> jumpMap;
00135 QMap <QString, int> keyMap;
00136 QMap <int, QString> keyTextMap;
00137
00138 mutable QMutex clientLock;
00139 QList<NetworkControlClient*> clients;
00140
00141 QList<NetworkCommand*> networkControlCommands;
00142 QMutex ncLock;
00143 QWaitCondition ncCond;
00144
00145 QList<NetworkCommand*> networkControlReplies;
00146 QMutex nrLock;
00147
00148 MThread *commandThread;
00149 bool stopCommandThread;
00150 };
00151
00152 #endif
00153
00154
00155