/* 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 QStringList; 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; QProcess* myProcess; QRegExp* myCommandRegex; bool myConnectedFlag; int myConnectionTimerID; int myPingTimerID; int myPongTimerID; QStringList* myClients; QString myUsername; QString myHostname; /** 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