/* GNU General Public License version 3 notice Copyright (C) 2012 Mihawk . All rights reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see < http://www.gnu.org/licenses/ >. */ #ifndef SSHCLIENT_H #define SSHCLIENT_H #include #include class QProcess; class QDateTime; class App; /** SSH Client exclusively for connecting to QWNET's central. It depends on openssh installed on the system and opens a process with the ssh binary to make all the communication. @author Mihawk @file SshClient.h */ class SshClient : public QObject { Q_OBJECT public: // Error codes enum Error { NoError, ConnectionTimedOutError }; /** Constructor @param app The main application @param parent The parent object if possible */ explicit SshClient(App* app, QObject *parent = 0); /** Destructor */ ~SshClient(); /** Connects to a given ssh server. @param user Username @param host Hostname */ bool connectToHost(const QString& user, const QString &host); /** Returns whether we are connected right now. @return True if connected, false otherwise */ bool isConnected() const; /** Disconnects from current server. */ void disconnectFromHost(); /** Writes data to the server. @param data Data to write */ void write(const QString& data); signals: /** Emitted when an error occurs. @param errorCode The error code */ void error(Error errorCode); /** Emitted when we just connected to a server. */ void connected(); protected: /** Handles events that occur periodically like PING PONG and connection timeout. @param e Pointer to the event that happened */ void timerEvent(QTimerEvent *e); private slots: /** Called when there is data to be read coming from the server. */ void read(); /** Called when the ssh binary process has exited. @param exitCode The exit code */ void exited(int exitCode); private: App* myApp; // The app QProcess* myProcess; // The ssh process QRegExp* myCommandRegex; // Regex used on every command, so it was made persistent bool myConnectedFlag; // Are we connected? int myConnectionTimerID; // Times out connection process int myPingTimerID; // PING? PONG! Checks if the connection is still alive int myPongTimerID; // PING? PONG! Checks if the connection is still alive QStringList myClients; // List of other clients connected to central QString myUsername; // My username on central QString myHostname; // Central's hostname QStringList myRoles; // My roles on central QStringList myCommands; // The list of commands that I can issue /** Parses all commands coming from the server and execute the appropriate command. @param time Time the message arrived @param command The command that just arrived @param commandData The command data */ void parse(const QDateTime& time, const QString& command, const QString& commandData); /** Ping and pong functions used to keep the connection alive. */ void ping(); void pong(); /** Reconnects to the server. */ void reconnect(); }; #endif // SSHCLIENT_H