summaryrefslogblamecommitdiffstats
path: root/src/server/net/client.h
blob: 389d05c992d172db0d0022fe33d5d4701ca9e3e1 (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                 
                       
                          
                     
                    

                                        
                   








                     
 



                             
        









                               

                                            
 















                                                                                                  

                                                                                
                                                                                    

 
                            

                              
                                                      
                             
                              
                                                             
 
        
 
                                   
                     

                                    


                                                                                                                                      

                              
                                   
                                                   



















                                                                                                       




                                                                  
                                                                            
                            

              
                                                                           
                          



                      
#ifndef CLIENT_H_
#define CLIENT_H_

#include <QtCore>
#include <QHostAddress>
#include <QAbstractSocket>
#include <QSslSocket>
#include <QSslError>
#include "../../shared/networkmessage.h"

//class QSslSocket;

struct ClientLogin
{
	bool accept;
	QString name;
	QString host;
	QString ip;
};


class Client : public QObject
{
	Q_OBJECT

public:	

	enum class State
	{
		Authenticating,
		Idle,
		VncClient,
		VncServer,
		Locked
	};

	explicit Client(QSslSocket* socket);
	~Client();

	// Getters
	inline const State    state() const { return _state; }
	inline const bool     isAuthed() const { return _authed == 2; }
	inline const QString& name() const { return _name; }
	inline const QString& host() const { return _host; }
	inline const QString  ip() const { return _socket->peerAddress().toString(); }
	inline const int      id() const { return _id; }
	inline const int      desiredProjectionSource() const { return _desiredProjectionSource; }
	inline const int      currentProjectionSource() const { return _currentProjectionSource; }

	// Setters
	inline void setProjectionSource(bool enable) { _isProjectionSource = enable; }
	inline void setDesiredProjectionSource(int source) { _desiredProjectionSource = source; }
	inline void setTutor(bool enable){ _isTutor = enable; }

	// To be replaced by states
	inline const bool isActiveVncClient() const { return _activeVncClient; }
	inline const bool isActiveVncServer() const { return _vncPort > 0; }
	inline const bool isProjectionSource() const { return _isProjectionSource; }


	//Send message stuff
	void startVncServer();
	void stopVncServer();
	void startVncClient(Client const * const to );
	void stopVncClient();
	void lockScreen(bool);
	void requestThumb(const int width, const int height);

private:

	QSslSocket * const _socket;
	State _state;

	static int _clientIdCounter;
	int _authed; // 0 = challenge sent, awaiting reply 1 = challenge ok, client challenge replied, awaiting login, 2 = ESTABLISHED
	QString _name;
	QString _host;
	QByteArray _challenge;
	qint64 _pingTimeout;
	NetworkMessage _fromClient;
	int _timerIdAuthTimeout, _timerPingTimeout;
	int _id; // this client's unique id
	// If this client should be projected to from another client, the other
	// client's id is set here. 0 otherwise. This is not currently used and it is
	// questionable if this makes sense, as it might just be confusing if several
	// groups students watch different other students. Also, visualizing such a
	// situation in the GUI in a meaningful way would be hard.
	int _desiredProjectionSource;
	bool _isProjectionSource; // Tells whether this client is currently the VNC broadcast source.
	int _currentProjectionSource;
	QString _vncRwPass, _vncRoPass;
	int _vncPort; // VNCserver state. Greater 0 -> active on this port. Equals 0 -> no server.
	bool _activeVncClient; // Flag indicating that the client is displaying a remote screen via VNC
	bool _isTutor; // Flag indicating that the client has been set as a tutor

	void handleMsg();
	void sendMessage(NetworkMessage& message);

protected:
	void timerEvent(QTimerEvent* event);

signals:
	void authenticating(Client* client, ClientLogin* request);
	void authenticated(Client* client);
	void thumbUpdated(Client* client, const QPixmap& thumb);
	void vncServerStateChange(Client* client);
	void vncClientStateChange(Client* client, int lastProjectionSource);
	void disconnected();

private slots:
	void onDataArrival(); // triggered if data is available for reading
	void disconnect();

};

#endif /* CLIENT_H_ */