HandshakerTester.cpp
Go to the documentation of this file.00001 #include <QObject>
00002 #include "Handshaker.h"
00003 #include "SlotAllocator.h"
00004 #include "Gnutella/LocalPeer.h"
00005 #include "Gnutella/Bootstrapping/NodeCache.h"
00006 #include "Networks/Transports/TcpConnection.h"
00007
00008
00009 using namespace Gnutella;
00010
00011 using Gnutella::Handshaking::Handshaker;
00012 using Gnutella::Handshaking::SlotAllocator;
00013 using Gnutella::Bootstrapping::NodeCache;
00014 using Networks::Transports::Connection;
00015 using Networks::Transports::TcpConnection;
00016
00017
00018 class HandshakerTester : public QObject
00019 {
00020 Q_OBJECT
00021
00022 public:
00023 HandshakerTester (QCoreApplication *, quint16,NodeType);
00024 ~HandshakerTester();
00025 void listenToNetwork();
00026 void connectToNetwork();
00027
00028
00029 private slots:
00030 void newConnection();
00031 void handshakingCompleted (Connection *, NodeInfo);
00032 void handshakingFailed (Connection *, Gnutella::Handshaking::HandshakeError);
00033 void acceptHandshakeConnection (QTcpSocket *);
00034
00035 private:
00036 QCoreApplication *app;
00037 LocalPeer *localPeer;
00038 NodeCache *nodeCache;
00039 SlotAllocator *slotAllocator;
00040 Handshaker *handshaker;
00041 TcpConnection *tcpConnection;
00042 quint16 port;
00043 QTcpServer *server;
00044 };
00045
00046 HandshakerTester::HandshakerTester (QCoreApplication *app_ , quint16 port_,NodeType type)
00047 {
00048 app = app_;
00049 port = port_;
00050
00051 localPeer = new LocalPeer (type,port);
00052 nodeCache = new NodeCache;
00053 slotAllocator = new SlotAllocator (localPeer);
00054 handshaker = new Handshaker (localPeer, nodeCache, slotAllocator);
00055 tcpConnection = new TcpConnection;
00056 server = new QTcpServer();
00057
00058 QObject::connect (handshaker, SIGNAL (handshakingCompleted (Connection *, NodeInfo)),
00059 this, SLOT (handshakingCompleted (Connection *, NodeInfo)));
00060 QObject::connect (handshaker, SIGNAL (handshakingFailed (Connection *, Gnutella::Handshaking::HandshakeError)),
00061 this, SLOT (handshakingFailed (Connection *, Gnutella::Handshaking::HandshakeError)));
00062 QObject::connect (server, SIGNAL(newConnection()), this, SLOT(newConnection()));
00063
00064
00065 }
00066
00067
00068
00069 HandshakerTester::~HandshakerTester()
00070 {
00071 delete handshaker;
00072 delete slotAllocator;
00073 delete nodeCache;
00074 delete localPeer;
00075 delete server;
00076 }
00077
00078 void HandshakerTester::connectToNetwork()
00079 {
00080
00081 NodeAddress nodeAddress ("127.0.0.1",port);
00082 handshaker->requestConnection (tcpConnection, nodeAddress);
00083
00084 }
00085
00086
00087 void HandshakerTester::listenToNetwork()
00088 {
00089
00090 server->listen(QHostAddress::Any,port);
00091 }
00092
00093 void HandshakerTester::handshakingCompleted (Connection *, NodeInfo)
00094 {
00095 delete tcpConnection;
00096 server->close();
00097
00098
00099 }
00100
00101 void HandshakerTester::handshakingFailed (Connection *, Gnutella::Handshaking::HandshakeError)
00102 {
00103
00104
00105 server->close();
00106 }
00107
00108 void HandshakerTester::newConnection()
00109 {
00110 QTcpSocket *socket = server->nextPendingConnection();
00111 qDebug() << "Accepting connection from " << socket->peerAddress().toString() << ":" << socket->peerPort();
00112 acceptHandshakeConnection (socket);
00113 }
00114
00115 void HandshakerTester::acceptHandshakeConnection (QTcpSocket *socket)
00116 {
00117 Connection *connection = new TcpConnection (socket);
00118
00119 handshaker->acceptConnection (connection);
00120 }
00121
00122
00123 int main(int argc, char *argv[])
00124 {
00125 QCoreApplication app(argc, argv);
00126 qRegisterMetaType<qint64>("qint64");
00127
00128 HandshakerTester handshakeTesterServer (&app,1000,TypeLeaf);
00129 HandshakerTester handshakeTesterClient (&app,1000,TypeLeaf);
00130 qDebug() << "Listening to port 1000";
00131 handshakeTesterServer.listenToNetwork();
00132 qDebug() << "connecting to port 1000";
00133 handshakeTesterClient.connectToNetwork();
00134 return app.exec();
00135 }
00136
00137 #include "HandshakerTester.moc"