123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- /*
- GNU General Public License version 3 notice
- Copyright (C) 2012 Mihawk <luiz@netdome.biz>. 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 <QObject>
- #include <QStringList>
- 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 <luiz@netdome.biz>
- @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
|