GXmlDirectNetwork.h

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Raphael Langerhorst                             *
00003  *   raphael-langerhorst@gmx.at                                            *
00004  *                                                                         *
00005  *   Permission is hereby granted, free of charge, to any person obtaining *
00006  *   a copy of this software and associated documentation files (the       *
00007  *   "Software"), to deal in the Software without restriction, including   *
00008  *   without limitation the rights to use, copy, modify, merge, publish,   *
00009  *   distribute, sublicense, and/or sell copies of the Software, and to    *
00010  *   permit persons to whom the Software is furnished to do so, subject to *
00011  *   the following conditions:                                             *
00012  *                                                                         *
00013  *   The above copyright notice and this permission notice shall be        *
00014  *   included in all copies or substantial portions of the Software.       *
00015  *                                                                         *
00016  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
00017  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
00018  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
00019  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
00020  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
00021  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
00022  *   OTHER DEALINGS IN THE SOFTWARE.                                       *
00023  ***************************************************************************/
00024 
00025 #ifndef GXMLDIRECTNETWORKH
00026 #define GXMLDIRECTNETWORKH
00027 
00028 #include "GXmlNetwork.h"
00029 #include "GNetworkMessageQueue.h"
00030 
00031 #include <QTcpServer>
00032 #include <QTcpSocket>
00033 
00034 #include <QMap>
00035 #include <QMutex>
00036 
00037 namespace GWE
00038 {
00039 
00040 
00047 class GXmlDirectNetworkConnection
00048 {
00049   protected:
00053     QTcpSocket* Socket;
00054     
00064     QString ConnectionName;
00065     
00066   public:
00067   
00072     GXmlDirectNetworkConnection(QTcpSocket* socket, const QString& id = QString());
00073     
00077     virtual ~GXmlDirectNetworkConnection();
00078     
00082     void setConnectionName(const QString& id);
00083     
00087     QString getConnectionName() const;
00088     
00092     QTcpSocket* getSocket();
00093 };
00094 
00095 
00135 class GXmlDirectNetwork : public GXmlNetwork
00136 {
00137   Q_OBJECT
00138   
00139   protected:
00140   
00144     QString NetworkID;
00145     
00162     QString MasterServer;
00163   
00168     QTcpServer Server;
00169     
00173     QMap<QString,GXmlDirectNetworkConnection*> Connections;
00174     
00180     QMap<const QTcpSocket*,GXmlDirectNetworkConnection*> IncomingConnections;
00181     
00187     QMap<const QTcpSocket*,QString> SocketNameMapping;
00188     
00193     GNetworkMessageQueue MessageQueue;
00194     
00199     int Port;
00200     
00205     QMutex ReceiveMutex;
00206   
00207   public:
00215     GXmlDirectNetwork(QString networkid, QString master, int port = 7471, QObject* parent = NULL);
00216     
00220     virtual ~GXmlDirectNetwork();
00221     
00222   protected:
00223   
00228     void removeConnection(const QString& destination);
00229     
00234     void connectSocketSignals(QTcpSocket* socket);
00235     
00245     QByteArray readSection(QIODevice* datasource, const QByteArray& begin, const QByteArray& ending);
00246     
00247   protected slots:
00248   
00252     void socketError(QAbstractSocket::SocketError socketError);
00253     
00258     void socketDisconnected();
00259     
00266     void processPendingClientConnection();
00267     
00273     void receiveData();
00274     
00279     void sendQueuedMessages(QString destination);
00280     
00286     void socketConnected();
00287   
00288   public:
00289     
00290     //BEGIN IMPLEMENTATION OF GXmlNetwork
00291     
00295    bool isConnected();
00296 
00300    QString getNetworkId() const;
00301    
00305    QString getMasterServer() const;
00306 
00307   public slots:
00308   
00312    void setNetworkId(const QString& id);
00313    
00317    void setMasterServer(const QString& master);
00318    
00322    bool initNetwork();
00323    
00330    bool closeNetwork();
00331    
00335    void setPassword(const QString& password);
00336  
00337    // DATA TRANSPORTING
00338    
00353    bool send(QDomElement data, const QString& destination);
00354 
00359    bool send(const QString& data);
00360    
00364    bool flushOutput();
00365    
00369    void makeDestinationAvailable(const QString& destination);
00370    
00374    bool isDestinationAvailable(const QString& destination);
00375    
00376    //END IMPLEMENTATION OF GXmlNetwork
00377 };
00378 
00379 }
00380 
00381 #endif

Generated on Thu Nov 16 07:49:12 2006 for G System by  doxygen 1.4.7