Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

remotecontrol.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2002 by Yann Hodique                                    *
00003  *   Yann.Hodique@lifl.fr                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "remotecontrol.h"
00012 #include "version.h"
00013 #include "qnet.h"
00014 #include "ChatSession.h"
00015 #include "domutil.h"
00016 
00017 #include <qregexp.h>
00018 #include <qmessagebox.h>
00019 
00020 RemoteControlClientSocket::RemoteControlClientSocket(int sock, QObject *parent, const char *name) :
00021 QSocket( parent, name ) {
00022     line = 0;
00023     connect( this, SIGNAL(readyRead()),
00024              SLOT(readClient()) );
00025     connect( this, SIGNAL(connectionClosed()),
00026              SLOT(deleteLater()) );
00027     setSocket( sock );
00028 }
00029 
00030 RemoteControlClientSocket::~RemoteControlClientSocket() {}
00031 
00032 void RemoteControlClientSocket::readClient() {
00033     QTextStream ts( this );
00034     while ( canReadLine() ) {
00035         QString str = ts.readLine();
00036         emit logText( tr("Read: '%1'\n").arg(str) );
00037         emit processText(str);
00038 
00039         line++;
00040     }
00041 }
00042 
00043 
00044 RemoteControlSimpleServer::RemoteControlSimpleServer(int port, QObject* parent) :
00045 QServerSocket( port, 1, parent ) {
00046     if ( !ok() ) {
00047         QMessageBox::critical(0,"Error","Failed to bind to port " + QString::number(port));
00048     }
00049 }
00050 
00051 RemoteControlSimpleServer::~RemoteControlSimpleServer() {}
00052 
00053 void RemoteControlSimpleServer::newConnection( int socket ) {
00054     RemoteControlClientSocket *s = new RemoteControlClientSocket( socket, this );
00055     emit newConnect( s );
00056 }
00057 
00058 
00059 
00060 RemoteControlServerInfo::RemoteControlServerInfo(QMtp * mtp, int port, QTextBrowser *log, const QString & pass) {
00061     RemoteControlSimpleServer *server = new RemoteControlSimpleServer(port, this );
00062 
00063     m_mtp = mtp;
00064     infoText = log;
00065     remote_pass = pass;
00066     connect( server, SIGNAL(newConnect(RemoteControlClientSocket*)),
00067              SLOT(newConnect(RemoteControlClientSocket*)) );
00068     
00069     // Commands :
00070     commands.insert("list",&RemoteControlServerInfo::listCommand);
00071     commands.insert("exit",&RemoteControlServerInfo::exitCommand);
00072     commands.insert("commands",&RemoteControlServerInfo::commandsCommand);
00073     commands.insert("send",&RemoteControlServerInfo::sendCommand);
00074     commands.insert("sync",&RemoteControlServerInfo::synchroCommand);
00075     commands.insert("desync",&RemoteControlServerInfo::desynchroCommand);
00076     commands.insert("connect",&RemoteControlServerInfo::connectCommand);
00077     commands.insert("disconnect",&RemoteControlServerInfo::disconnectCommand);
00078 }
00079 
00080 RemoteControlServerInfo::~RemoteControlServerInfo() {}
00081 
00082 void RemoteControlServerInfo::newConnect( RemoteControlClientSocket *s ) {
00083     infoText->append( tr("New connection\n") );
00084 
00085     QTextStream(s) << "Welcome to " CLIENT " remote administration tool\nType \"exit\" to leave\n";
00086 
00087     if(remote_pass != QString::null) {
00088         connect (s, SIGNAL(processText(const QString&)),
00089                  SLOT(auth(const QString&)));
00090   QTextStream(s) << "Authenfication required\nPassword : ";
00091     }
00092     else {
00093         connect( s, SIGNAL(logText(const QString&)),
00094                  infoText, SLOT(append(const QString&)) );
00095         connect( s, SIGNAL(processText(const QString&)),
00096                  SLOT(processText(const QString&)));  
00097     }
00098     connect( s, SIGNAL(connectionClosed()),
00099              SLOT(connectionClosed()) );
00100 
00101     map.insert((int)s,s);
00102 }
00103 
00104 void RemoteControlServerInfo::auth(const QString & txt) {
00105     if(txt == remote_pass) {
00106         disconnect(sender(),SIGNAL(processText(const QString&)),
00107                    this,SLOT(auth(const QString&)));
00108         connect( sender(), SIGNAL(logText(const QString&)),
00109                  infoText, SLOT(append(const QString&)) );
00110         connect( sender(), SIGNAL(processText(const QString&)),
00111                  SLOT(processText(const QString&)));
00112     } else
00113         QTextStream((RemoteControlClientSocket*)sender()) << "Incorrect Password\nPassword : ";
00114 }
00115 
00116 void RemoteControlServerInfo::display(const QString& msg) {
00117     ChatSession *session = (ChatSession*)sender();
00118     SyncMap::Iterator it;
00119     if((it = sync_map.find(session)) != sync_map.end()) {
00120   for(QValueList<RemoteControlClientSocket*>::Iterator iterator = (*it).begin(); iterator != (*it).end(); ++ iterator)
00121       QTextStream(*iterator) << session->sessionName() << ": " << msg << endl;
00122     }
00123 }
00124 
00125 void RemoteControlServerInfo::connectionClosed() {
00126     infoText->append( tr("Client closed connection\n") );
00127     map.remove((int)sender());
00128 }
00129 
00130 void RemoteControlServerInfo::processText(const QString & txt) {
00131     RemoteControlClientSocket* socket = (RemoteControlClientSocket*)sender();
00132 
00133     bool ok(false);
00134     QRegExp reg("(\\w+)( .*)?");
00135     if(reg.exactMatch(txt)) {
00136   QString command = reg.cap(1);
00137   CommandMap::Iterator it;
00138   if((it = commands.find(command)) != commands.end()) { // found suitable command
00139       ok = true;
00140       (this->*(it.data()))(socket,txt);
00141   }
00142     }
00143     if (!ok) unknownCommand(socket,txt);
00144 }
00145 
00146 void RemoteControlServerInfo::listCommand(RemoteControlClientSocket *socket,const QString& txt) {
00147     QRegExp re("\\w+ +(\\w+) *");
00148     QTextStream stream(socket);
00149     if(re.exactMatch(txt)) {
00150   QString command = re.cap(1);
00151   if(command == "active") {
00152       stream << "Listing active sessions" << endl;
00153       for (QValueList<ChatSession*>::Iterator it = m_mtp->sessions.begin(); it != m_mtp->sessions.end(); ++it) {
00154     stream << "list : " << (*it)->sessionName() << endl;
00155       }
00156   }
00157   else if(command == "all") {
00158       stream << "Listing all sessions" << endl;
00159       QStringList list = DomUtil::readListEntry(m_mtp->m_document,"/general/sessions","session");
00160       for (QStringList::Iterator it = list.begin(); it != list.end(); ++it)
00161     stream << "list : " << (*it) << endl;
00162   }
00163   else if(command == "shared") {}
00164   else stream << "Unrecognized option \"" << command << "\"" << endl;
00165     }
00166     else stream << "Syntax error using command \"list\"" << endl;
00167 }
00168 
00169 void RemoteControlServerInfo::exitCommand(RemoteControlClientSocket *socket,const QString&) {
00170     socket->close();
00171 }
00172 
00173 void RemoteControlServerInfo::commandsCommand(RemoteControlClientSocket *socket,const QString&) {
00174     QTextStream stream(socket);
00175     for(CommandMap::Iterator it = commands.begin(); it != commands.end(); ++it)
00176   stream << "commands : " << it.key() << endl;
00177 } 
00178 
00179 void RemoteControlServerInfo::sendCommand(RemoteControlClientSocket *socket,const QString& txt) {
00180     QRegExp re("\\w+ +(\\w+) +([^ ].*)");
00181     QTextStream stream(socket);
00182     if(re.exactMatch(txt)) {
00183   QString session = re.cap(1);
00184   QValueList<ChatSession*>::Iterator it;
00185   bool ok(false);
00186   for(it = m_mtp->sessions.begin(); it != m_mtp->sessions.end(); ++it) {
00187       if((*it)->sessionName() == session) {
00188     ok = true;
00189     QString message = re.cap(2);
00190     (*it)->send(message);
00191     break;
00192       }
00193   }
00194   if (!ok) stream << "Session \"" << session << "\" does not exist" << endl;
00195     }
00196     else
00197   stream << "Syntax error using command \"send\"" << endl;
00198 }
00199 
00200 void RemoteControlServerInfo::synchroCommand(RemoteControlClientSocket *socket,const QString& txt) {
00201     QRegExp re("\\w+ +(\\w+) *");
00202     QTextStream stream(socket);
00203     if(re.exactMatch(txt)) {
00204   QString session = re.cap(1);
00205   QValueList<ChatSession*>::Iterator it;
00206   bool ok(false);
00207   for(it = m_mtp->sessions.begin(); it != m_mtp->sessions.end(); ++it) {
00208       if((*it)->sessionName() == session) {
00209     stream << "Synchro with session \"" << session << "\"" << endl;
00210     SyncMap::Iterator iterator;
00211     if((iterator = sync_map.find(*it)) == sync_map.end()) {
00212         QValueList<RemoteControlClientSocket*> list;
00213         list.append(socket);
00214         sync_map.insert(*it,list);
00215         connect(*it,SIGNAL(outputMessage(const QString&)),
00216           this, SLOT(display(const QString&)));
00217     }
00218     else
00219         (*iterator).append(socket);
00220     ok = true;
00221     break;
00222       }
00223   }
00224   if (!ok) stream << "Session \"" << session << "\" does not exist" << endl;
00225     }
00226     else
00227   stream << "Syntax error using command \"sync\"" << endl;
00228 }
00229 
00230 void RemoteControlServerInfo::desynchroCommand(RemoteControlClientSocket *socket,const QString& txt) {
00231     QRegExp re("\\w+ +(\\w+) *");
00232     QTextStream stream(socket);
00233     if(re.exactMatch(txt)) {
00234   QString session = re.cap(1);
00235   QValueList<ChatSession*>::Iterator it;
00236   bool ok(false);
00237   for(it = m_mtp->sessions.begin(); it != m_mtp->sessions.end(); ++it) {
00238       if((*it)->sessionName() == session) {
00239     stream << "Desynchro with session \"" << session << "\"" << endl;
00240     sync_map.remove(*it);
00241     if(sync_map.begin() == sync_map.end()) {
00242         disconnect(*it,SIGNAL(outputMessage(const QString&)),
00243              this, SLOT(display(const QString&)));
00244     }
00245     ok = true;
00246     break;
00247       }
00248   }
00249   if (!ok) stream << "Session \"" << session << "\" does not exist" << endl;
00250     }
00251     else
00252   stream << "Syntax error using command \"desync\"" << endl;
00253 }
00254 
00255 void RemoteControlServerInfo::connectCommand(RemoteControlClientSocket *socket,const QString& txt) {
00256     QRegExp re("\\w+ +(\\w+) *");
00257     QTextStream stream(socket);
00258     if(re.exactMatch(txt)) {
00259   QString session = re.cap(1);
00260   bool ok(false);
00261   QStringList list = DomUtil::readListEntry(m_mtp->m_document,"/general/sessions","session");
00262   for (QStringList::Iterator it = list.begin(); it != list.end(); ++it)
00263       if((*it) == session) {
00264     stream << "Connect session \"" << session << "\"" << endl;
00265     m_mtp->launchSession(session);
00266     ok = true;
00267     break;
00268       }
00269   if (!ok) stream << "Session \"" << session << "\" does not exist" << endl;
00270     }
00271     else
00272   stream << "Syntax error using command \"connect\"" << endl;
00273 }
00274 
00275 void RemoteControlServerInfo::disconnectCommand(RemoteControlClientSocket *socket,const QString& txt) {
00276     QRegExp re("\\w+ +(\\w+) *");
00277     QTextStream stream(socket);
00278     if(re.exactMatch(txt)) {
00279   QString session = re.cap(1);
00280   QValueList<ChatSession*>::Iterator it;
00281   bool ok(false);
00282   for(it = m_mtp->sessions.begin(); it != m_mtp->sessions.end(); ++it) {
00283       if((*it)->sessionName() == session) {
00284     ok = true;
00285     m_mtp->closeTab(*it);
00286     break;
00287       }
00288   }
00289   if (!ok) stream << "Session \"" << session << "\" does not exist" << endl;
00290     }
00291     else
00292   stream << "Syntax error using command \"send\"" << endl;
00293 }
00294 
00295 void RemoteControlServerInfo::unknownCommand(RemoteControlClientSocket *socket, const QString& txt) {
00296     QTextStream(socket) << "Unknown command \"" << txt << "\"" << endl;
00297 }

Generated on Sat May 10 15:09:28 2003 for qnet by doxygen1.3