camino-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Camino-devel] camino/src main-unittest.cpp common/MsgCoder.cp...


From: Philippe Fremy
Subject: [Camino-devel] camino/src main-unittest.cpp common/MsgCoder.cp...
Date: Sun, 26 Jan 2003 15:32:20 -0500

CVSROOT:        /cvsroot/camino
Module name:    camino
Changes by:     Philippe Fremy <address@hidden> 03/01/26 15:32:20

Modified files:
        src            : main-unittest.cpp 
        src/common     : MsgCoder.cpp MsgCoder.h MsgDecoder.cpp 
                         MsgDecoder.h TestMsgDecoder.cpp 
                         TestMsgDecoder.h 
        src/server     : CaminoServer.cpp CaminoServer.h Player.cpp 
                         Player.h 

Log message:
        encode/decode a new message

Patches:
Index: camino/src/common/MsgCoder.cpp
diff -u camino/src/common/MsgCoder.cpp:1.3 camino/src/common/MsgCoder.cpp:1.4
--- camino/src/common/MsgCoder.cpp:1.3  Tue Jan 21 17:27:41 2003
+++ camino/src/common/MsgCoder.cpp      Sun Jan 26 15:32:19 2003
@@ -2,7 +2,7 @@
 **
 ** Camino
 **
-** Version : $Id: MsgCoder.cpp,v 1.3 2003/01/21 22:27:41 pfremy Exp $
+** Version : $Id: MsgCoder.cpp,v 1.4 2003/01/26 20:32:19 pfremy Exp $
 **
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 14/01/2003
@@ -40,6 +40,13 @@
 {
        // Don't forget the spaces between each value
 
-       *this << ConnectServer << " " << 0 << " " << playerName; //<< endl;
+       *this << ConnectServer << " " << 0 << " " << playerName << endl;
+       
+       device()->flush();
+}
+
+void MsgCoder::sendMsg( NetMsgSubtype msgLevel, const QString & msg )
+{
+       *this << Msg << " " << msgLevel << " " << msg << endl;
        device()->flush();
 }
Index: camino/src/common/MsgCoder.h
diff -u camino/src/common/MsgCoder.h:1.4 camino/src/common/MsgCoder.h:1.5
--- camino/src/common/MsgCoder.h:1.4    Sun Jan 26 11:50:09 2003
+++ camino/src/common/MsgCoder.h        Sun Jan 26 15:32:19 2003
@@ -3,7 +3,7 @@
 ** Camino
 **
 **
-** Version : $Id: MsgCoder.h,v 1.4 2003/01/26 16:50:09 pfremy Exp $
+** Version : $Id: MsgCoder.h,v 1.5 2003/01/26 20:32:19 pfremy Exp $
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 14/01/2003
 ** Copyright: Pascal Audoux, Philippe Fremy 2003
@@ -58,10 +58,7 @@
        TeamIsBeingChosen,      //! 4 players game, servers is currentely 
choosing the team composition
        TeamsComposition,       //! 4 players game, servers informs client 
about the team composition
 
-       ChatMsg,                        //! Client/Server sends a chat message
-       InfoMsg,                        //! Client/Server sends an information 
message
-       DebugMsg,                       //! Client/Server sends a debug message
-
+       Msg,                    //! Client/Server sends message, subtype is 
(Chat, Info, Debug)
        TilesForYou,            //! Server distributes one or many tiles to a 
client
        ActivePlayer,           //! Server informs all client which player is 
active
        PlayerPlaysAMove,       //! Client informs server about its move
@@ -88,6 +85,15 @@
        LastMsg
 };
 
+enum NetMsgSubtype {
+    SubtypeFirstType = 0,
+
+    DebugMsg,
+    InfoMsg,
+    ChatMsg,
+
+    SubtypeLastType
+};
 
 
 class MsgCoder : public QTextStream
@@ -100,6 +106,8 @@
        //! from QTextStream: void setDevice( QIODevice * );
 
        void connectServer( QString playerName );
+       void sendMsg( NetMsgSubtype msgLevel, const QString & msg );
+
 private:
        MsgCoder( MsgCoder & );
        MsgCoder & operator=( MsgCoder & );
Index: camino/src/common/MsgDecoder.cpp
diff -u camino/src/common/MsgDecoder.cpp:1.4 
camino/src/common/MsgDecoder.cpp:1.5
--- camino/src/common/MsgDecoder.cpp:1.4        Tue Jan 21 17:27:41 2003
+++ camino/src/common/MsgDecoder.cpp    Sun Jan 26 15:32:19 2003
@@ -2,7 +2,7 @@
 **
 ** Camino
 **
-** Version : $Id: MsgDecoder.cpp,v 1.4 2003/01/21 22:27:41 pfremy Exp $
+** Version : $Id: MsgDecoder.cpp,v 1.5 2003/01/26 20:32:19 pfremy Exp $
 **
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 15/01/2003
@@ -49,25 +49,39 @@
 
 void MsgDecoder::slotDecode()
 {
+       int t, st;
 //     qDebug("MsgDecoder::slotDecode()");
-       long type, subtype;
-       _ts >> type;
-       _ts >> subtype;
+       _ts >> t;
+       _ts >> st;
+
+       _type = (NetMsgType) t;
+       _subtype = (NetMsgSubtype) st;
 //     qDebug("type = %ld subtype = %ld", type, subtype );
 
-       switch( type ) {
+       switch( _type ) {
                case ConnectServer:
                                decodeServerConnect();
                                break;
+
+               case Msg:
+                               decodeMsg();
+                               break;
                default:
-                       qWarning( "MsgDecoder::slotDecode() - Unknown NetMsg 
type received : %ld", type );
+                       qWarning( "MsgDecoder::slotDecode() - Unknown NetMsg 
type received : %d", _type );
        };
 }
 
 void MsgDecoder::decodeServerConnect()
 {
-               qDebug( "MsgDecoder::decodeServerConnect()");
-               QString playerName;
-               _ts >> playerName;
-               recvServerConnect( playerName );
+    qDebug( "MsgDecoder::decodeServerConnect()");
+    QString playerName;
+    _ts >> playerName;
+    recvServerConnect( playerName );
+}
+
+void MsgDecoder::decodeMsg()
+{
+    QString msg;
+    _ts >> msg;
+    recvMsg( msg );
 }
Index: camino/src/common/MsgDecoder.h
diff -u camino/src/common/MsgDecoder.h:1.6 camino/src/common/MsgDecoder.h:1.7
--- camino/src/common/MsgDecoder.h:1.6  Sun Jan 26 14:39:40 2003
+++ camino/src/common/MsgDecoder.h      Sun Jan 26 15:32:19 2003
@@ -5,7 +5,7 @@
 ** MsgDecoder.h
 ** Decoder for socket msg
 **
-** Version : $Id: MsgDecoder.h,v 1.6 2003/01/26 19:39:40 pfremy Exp $
+** Version : $Id: MsgDecoder.h,v 1.7 2003/01/26 20:32:19 pfremy Exp $
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 14/01/2003
 ** Copyright: Pascal Audoux, Philippe Fremy 2003
@@ -35,6 +35,7 @@
 #include <qtextstream.h>
 
 // application specific include files
+#include "common/MsgCoder.h"
 
 
 /*              ------------------------------
@@ -56,6 +57,7 @@
        void setDevice( QIODevice * socket );
        
        virtual void recvServerConnect( QString /* playerName */ ) {}
+       virtual void recvMsg( QString /* msg */ ) {}
 
 public slots:
        /** call this to tell the Decoder that there is some data to decode
@@ -73,10 +75,15 @@
        virtual void slotDecode();
 
 protected:
+       void decodeServerConnect();
+       void decodeMsg();
+
        QIODevice * _socket;
        QTextStream _ts;
 
-       void decodeServerConnect();
+       NetMsgType _type; 
+       NetMsgSubtype _subtype;
+
 };
 
 #endif // MSGDECODER_H
Index: camino/src/common/TestMsgDecoder.cpp
diff -u camino/src/common/TestMsgDecoder.cpp:1.3 
camino/src/common/TestMsgDecoder.cpp:1.4
--- camino/src/common/TestMsgDecoder.cpp:1.3    Tue Jan 21 17:27:41 2003
+++ camino/src/common/TestMsgDecoder.cpp        Sun Jan 26 15:32:19 2003
@@ -2,7 +2,7 @@
 **
 ** Camino
 **
-** Version : $Id: TestMsgDecoder.cpp,v 1.3 2003/01/21 22:27:41 pfremy Exp $
+** Version : $Id: TestMsgDecoder.cpp,v 1.4 2003/01/26 20:32:19 pfremy Exp $
 **
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 14/01/2003
@@ -149,9 +149,38 @@
        _buf->flush();
        _buf->at(0);
 
-       qDebug("Socket content : '%s'", (const char *) *_socketContent );
+//     qDebug("Socket content : '%s'", (const char *) *_socketContent );
 
        _dec->slotDecode();
        checkEquals( _dec->s1, playerName );
 }
 
+void TestMsgDecoder::testSendMsg()
+{
+       QString msg = "Msg";
+
+       _cod->sendMsg( DebugMsg, msg + "D" );
+       _buf->flush();
+
+       _cod->sendMsg( InfoMsg, msg + "I" );
+       _buf->flush();
+
+       _cod->sendMsg( ChatMsg, msg + "C" );
+       _buf->putch( 0 );
+       _buf->flush();
+       _buf->at(0);
+
+//     qDebug("Socket content : '%s'", (const char *) *_socketContent );
+
+       _dec->slotDecode();
+       checkEquals( _dec->subtype(), DebugMsg );
+       checkEquals( _dec->s1, msg + "D" );
+
+       _dec->slotDecode();
+       checkEquals( _dec->subtype(), InfoMsg );
+       checkEquals( _dec->s1, msg + "I" );
+
+       _dec->slotDecode();
+       checkEquals( _dec->subtype(), ChatMsg );
+       checkEquals( _dec->s1, msg + "C" );
+}
Index: camino/src/common/TestMsgDecoder.h
diff -u camino/src/common/TestMsgDecoder.h:1.3 
camino/src/common/TestMsgDecoder.h:1.4
--- camino/src/common/TestMsgDecoder.h:1.3      Tue Jan 21 17:27:41 2003
+++ camino/src/common/TestMsgDecoder.h  Sun Jan 26 15:32:19 2003
@@ -5,7 +5,7 @@
 ** TestMsgDecoder.h
 ** this is a template for all .h test files
 **
-** Version : $Id: TestMsgDecoder.h,v 1.3 2003/01/21 22:27:41 pfremy Exp $
+** Version : $Id: TestMsgDecoder.h,v 1.4 2003/01/26 20:32:19 pfremy Exp $
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 14/01/2003
 **
@@ -34,22 +34,7 @@
 #include "cppunit/TestCase.h"
 #include "cppunit/extensions/HelperMacros.h"
 
-/** This class is used to actually receive the server messages */
-class MockDecoder : public MsgDecoder
-{
-public:
-       MockDecoder( QIODevice * device = 0L)
-               : MsgDecoder( device ) {}
-
-       virtual ~MockDecoder() {}
-
-       virtual void recvServerConnect( QString playerName ) {
-               s1 = playerName;
-       }
-
-       QString s1, s2, s3;
-       long val1, val2, val3;
-};
+class MockDecoder;
 
 
 
@@ -72,16 +57,19 @@
 
        CPPUNIT_TEST( testCreation );
        CPPUNIT_TEST( testServerConnect );
+       CPPUNIT_TEST( testSendMsg );
 
        CPPUNIT_TEST_SUITE_END();
 
 
        // declare the test functions
-       void testServerConnect();
-       void testCreation();
        void testQDataStream();
        void testQTextStream();
 
+       void testCreation();
+       void testServerConnect();
+       void testSendMsg();
+
 
 protected:
        QCString * _socketContent;
@@ -90,6 +78,31 @@
        MsgCoder * _cod;
 
        // declare some members
+};
+
+/** This class is used to actually receive the server messages */
+class MockDecoder : public MsgDecoder
+{
+public:
+       MockDecoder( QIODevice * device = 0L)
+               : MsgDecoder( device ) {}
+
+       ~MockDecoder() {}
+
+       void recvServerConnect( QString playerName ) {
+               s1 = playerName;
+       }
+
+       void recvMsg( QString msg ) {
+           s1 = msg;
+       }
+
+       NetMsgType type() { return _type; }
+       NetMsgSubtype subtype() { return _subtype; }
+
+       QString s1, s2, s3;
+       long val1, val2, val3;
+
 };
 
 
Index: camino/src/main-unittest.cpp
diff -u camino/src/main-unittest.cpp:1.3 camino/src/main-unittest.cpp:1.4
--- camino/src/main-unittest.cpp:1.3    Sun Jan 19 17:59:01 2003
+++ camino/src/main-unittest.cpp        Sun Jan 26 15:32:18 2003
@@ -3,7 +3,6 @@
 #include <cppunit/extensions/TestFactoryRegistry.h>
 
 #include "common/PhilTestRunner.h"
-#include "common/TestMsgCoder.h"
 
 //! Use a text runner and fetch automatically the existing test suites
 int automaticTextRun()
Index: camino/src/server/CaminoServer.cpp
diff -u camino/src/server/CaminoServer.cpp:1.4 
camino/src/server/CaminoServer.cpp:1.5
--- camino/src/server/CaminoServer.cpp:1.4      Sun Jan 26 14:39:40 2003
+++ camino/src/server/CaminoServer.cpp  Sun Jan 26 15:32:19 2003
@@ -2,7 +2,7 @@
 **
 ** Camino
 **
-** Version : $Id: CaminoServer.cpp,v 1.4 2003/01/26 19:39:40 pfremy Exp $
+** Version : $Id: CaminoServer.cpp,v 1.5 2003/01/26 20:32:19 pfremy Exp $
 **
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 19/01/2003
@@ -64,9 +64,14 @@
         // XXX: uneso->sendConnectionOk();
 }
 
-void CaminoServer::recvServerConnect( QString playerName )
+void CaminoServer::recvServerConnect( QString hostName, QString playerName )
 {
         qDebug("CaminoServer - recv client name : %s", playerName.latin1() );
+
+        // check if player name is valid
+        // if player name is invalid notify all clients
+
+        emit sig_playerConnected( playerName, hostName );
 }
 
 
Index: camino/src/server/CaminoServer.h
diff -u camino/src/server/CaminoServer.h:1.4 
camino/src/server/CaminoServer.h:1.5
--- camino/src/server/CaminoServer.h:1.4        Sun Jan 26 14:39:40 2003
+++ camino/src/server/CaminoServer.h    Sun Jan 26 15:32:19 2003
@@ -5,7 +5,7 @@
 ** CaminoServer.h
 ** Server for the game
 **
-** Version : $Id: CaminoServer.h,v 1.4 2003/01/26 19:39:40 pfremy Exp $
+** Version : $Id: CaminoServer.h,v 1.5 2003/01/26 20:32:19 pfremy Exp $
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 19/01/2003
 ** Copyright: Pascal Audoux, Philippe Fremy 2003
@@ -56,14 +56,14 @@
 
        void newConnection ( int numSocket );
 
-       void recvServerConnect( QString playerName );
+       void recvServerConnect( QString hostName, QString playerName );
 
 signals:
        void sig_readEvent( int );
        void sig_newConnection( QSocket * );
        void sig_endConnection( QString name );
 
-       void sig_PlayerConnected( QString playerName, QString clientIP );
+       void sig_playerConnected( QString hostName, QString playerName );
 
 protected:
        QPtrList<Player> _playerList;
Index: camino/src/server/Player.cpp
diff -u camino/src/server/Player.cpp:1.1 camino/src/server/Player.cpp:1.2
--- camino/src/server/Player.cpp:1.1    Sun Jan 26 14:39:40 2003
+++ camino/src/server/Player.cpp        Sun Jan 26 15:32:19 2003
@@ -2,7 +2,7 @@
 **
 ** Camino
 **
-** Version : $Id: Player.cpp,v 1.1 2003/01/26 19:39:40 pfremy Exp $
+** Version : $Id: Player.cpp,v 1.2 2003/01/26 20:32:19 pfremy Exp $
 **
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 14/01/2003
@@ -36,6 +36,7 @@
     : MsgDecoder( socket )
 {
     _caminoServer = caminoServer;
+    _socket = socket;
 
     connect( socket, SIGNAL( readyRead() ), SLOT( slotDecode() ) );
     qDebug("Player::Player - Socket = %p, caminoServer = %p", socket, 
caminoServer );
@@ -52,5 +53,5 @@
 void Player::recvServerConnect( QString playerName )
 {
     qDebug("Player - recvServerConnect - %s", playerName.latin1() );
-    _caminoServer->recvServerConnect( playerName );
+    _caminoServer->recvServerConnect( _socket->address().toString(), 
playerName );
 }
Index: camino/src/server/Player.h
diff -u camino/src/server/Player.h:1.1 camino/src/server/Player.h:1.2
--- camino/src/server/Player.h:1.1      Sun Jan 26 14:39:40 2003
+++ camino/src/server/Player.h  Sun Jan 26 15:32:19 2003
@@ -5,7 +5,7 @@
 ** Player.h
 ** this is a template for all .h files
 **
-** Version : $Id: Player.h,v 1.1 2003/01/26 19:39:40 pfremy Exp $
+** Version : $Id: Player.h,v 1.2 2003/01/26 20:32:19 pfremy Exp $
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 14/01/2003
 ** Copyright: Pascal Audoux, Philippe Fremy 2003
@@ -60,6 +60,7 @@
 protected:
     CaminoServer * _caminoServer;
     QString _playerName;
+    QSocket * _socket;
 
 
 };




reply via email to

[Prev in Thread] Current Thread [Next in Thread]