[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Camino-devel] camino/src/common MsgCoder.h MsgCoder.cpp MsgDe...
From: |
Philippe Fremy |
Subject: |
[Camino-devel] camino/src/common MsgCoder.h MsgCoder.cpp MsgDe... |
Date: |
Tue, 21 Jan 2003 17:27:42 -0500 |
CVSROOT: /cvsroot/camino
Module name: camino
Changes by: Philippe Fremy <address@hidden> 03/01/21 17:27:41
Modified files:
src/common : MsgCoder.h MsgCoder.cpp MsgDecoder.h
MsgDecoder.cpp TestMsgDecoder.h
TestMsgDecoder.cpp
Log message:
Finally, a working MsgCoder / Decoder
Patches:
Index: camino/src/common/MsgCoder.cpp
diff -u camino/src/common/MsgCoder.cpp:1.2 camino/src/common/MsgCoder.cpp:1.3
--- camino/src/common/MsgCoder.cpp:1.2 Sun Jan 19 17:34:47 2003
+++ camino/src/common/MsgCoder.cpp Tue Jan 21 17:27:41 2003
@@ -2,7 +2,7 @@
**
** Camino
**
-** Version : $Id: MsgCoder.cpp,v 1.2 2003/01/19 22:34:47 pfremy Exp $
+** Version : $Id: MsgCoder.cpp,v 1.3 2003/01/21 22:27:41 pfremy Exp $
**
** Author(s) : Philippe Fremy, Pascal Audoux
** Creation : 14/01/2003
@@ -30,16 +30,16 @@
MsgCoder::MsgCoder( QIODevice * socket)
+: QTextStream( socket )
{
- setPrintableData( true );
- if (socket != 0L) {
- setDevice( socket );
- }
+ setEncoding( Latin1 );
}
void MsgCoder::connectServer( QString playerName )
{
- *this << 1L << 1L << playerName;
- //flush();
+ // Don't forget the spaces between each value
+
+ *this << ConnectServer << " " << 0 << " " << playerName; //<< endl;
+ device()->flush();
}
Index: camino/src/common/MsgCoder.h
diff -u camino/src/common/MsgCoder.h:1.2 camino/src/common/MsgCoder.h:1.3
--- camino/src/common/MsgCoder.h:1.2 Sun Jan 19 17:34:47 2003
+++ camino/src/common/MsgCoder.h Tue Jan 21 17:27:41 2003
@@ -3,7 +3,7 @@
** Camino
**
**
-** Version : $Id: MsgCoder.h,v 1.2 2003/01/19 22:34:47 pfremy Exp $
+** Version : $Id: MsgCoder.h,v 1.3 2003/01/21 22:27:41 pfremy Exp $
** Author(s) : Philippe Fremy, Pascal Audoux
** Creation : 14/01/2003
** Copyright: Pascal Audoux, Philippe Fremy 2003
@@ -49,16 +49,29 @@
*/
-class MsgCoder : public QDataStream
+enum NetMsgType {
+ FirstMsg = 0,
+
+ ConnectServer,
+
+ LastMsg
+};
+
+
+
+class MsgCoder : public QTextStream
{
public:
MsgCoder( QIODevice * = 0L);
+ virtual ~MsgCoder() {}
//! from QTextStream: void setDevice( QIODevice * );
void connectServer( QString playerName );
-
+private:
+ MsgCoder( MsgCoder & );
+ MsgCoder & operator=( MsgCoder & );
};
#endif // MSG_CODER_H
Index: camino/src/common/MsgDecoder.cpp
diff -u camino/src/common/MsgDecoder.cpp:1.3
camino/src/common/MsgDecoder.cpp:1.4
--- camino/src/common/MsgDecoder.cpp:1.3 Sun Jan 19 17:34:47 2003
+++ camino/src/common/MsgDecoder.cpp Tue Jan 21 17:27:41 2003
@@ -2,7 +2,7 @@
**
** Camino
**
-** Version : $Id: MsgDecoder.cpp,v 1.3 2003/01/19 22:34:47 pfremy Exp $
+** Version : $Id: MsgDecoder.cpp,v 1.4 2003/01/21 22:27:41 pfremy Exp $
**
** Author(s) : Philippe Fremy, Pascal Audoux
** Creation : 15/01/2003
@@ -28,39 +28,46 @@
#include <qtextstream.h>
// application specific include files
-#include "MsgDecoder.h"
-#include "MsgDecoder.h"
+#include "common/MsgDecoder.h"
+#include "common/MsgCoder.h"
+#include "common/log.h"
MsgDecoder::MsgDecoder( QIODevice * socket )
{
- _socket = socket;
+ setDevice( socket );
+ _ts.setEncoding( QTextStream::Latin1 );
}
+void MsgDecoder::setDevice( QIODevice * socket )
+{
+ if (socket) {
+ _ts.setDevice( socket );
+ }
+}
+
+
void MsgDecoder::slotDecode()
{
- qDebug("MsgDecoder::slotDecode()");
- long type, subtype;
- QTextStream ts( _socket );
- // ts.open( IO_ReadOnly );
- ts >> type;
- ts >> subtype;
- qDebug("type = %ld subtype = %ld", type, subtype );
-
- switch( type ) {
- case 0:
- decodeServerConnect();
- break;
- default:
- qWarning( "Unknown NetMsg type
received : %ld", type );
- };
+// qDebug("MsgDecoder::slotDecode()");
+ long type, subtype;
+ _ts >> type;
+ _ts >> subtype;
+// qDebug("type = %ld subtype = %ld", type, subtype );
+
+ switch( type ) {
+ case ConnectServer:
+ decodeServerConnect();
+ break;
+ default:
+ qWarning( "MsgDecoder::slotDecode() - Unknown NetMsg
type received : %ld", type );
+ };
}
void MsgDecoder::decodeServerConnect()
{
qDebug( "MsgDecoder::decodeServerConnect()");
QString playerName;
- QTextStream ts( _socket );
- ts >> playerName;
+ _ts >> playerName;
recvServerConnect( playerName );
}
Index: camino/src/common/MsgDecoder.h
diff -u camino/src/common/MsgDecoder.h:1.3 camino/src/common/MsgDecoder.h:1.4
--- camino/src/common/MsgDecoder.h:1.3 Sun Jan 19 17:34:47 2003
+++ camino/src/common/MsgDecoder.h Tue Jan 21 17:27:41 2003
@@ -5,7 +5,7 @@
** MsgDecoder.h
** Decoder for socket msg
**
-** Version : $Id: MsgDecoder.h,v 1.3 2003/01/19 22:34:47 pfremy Exp $
+** Version : $Id: MsgDecoder.h,v 1.4 2003/01/21 22:27:41 pfremy Exp $
** Author(s) : Philippe Fremy, Pascal Audoux
** Creation : 14/01/2003
** Copyright: Pascal Audoux, Philippe Fremy 2003
@@ -32,6 +32,7 @@
// include files for QT
#include <qstring.h>
#include <qobject.h>
+#include <qtextstream.h>
// application specific include files
@@ -50,8 +51,9 @@
public:
/** Constructor */
MsgDecoder( QIODevice * socket = 0L );
+ virtual ~MsgDecoder() {}
- void setDevice( QIODevice * socket ) { _socket = socket; }
+ void setDevice( QIODevice * socket );
virtual void recvServerConnect( QString playerName ) {}
@@ -72,6 +74,7 @@
protected:
QIODevice * _socket;
+ QTextStream _ts;
void decodeServerConnect();
};
Index: camino/src/common/TestMsgDecoder.cpp
diff -u camino/src/common/TestMsgDecoder.cpp:1.2
camino/src/common/TestMsgDecoder.cpp:1.3
--- camino/src/common/TestMsgDecoder.cpp:1.2 Sun Jan 19 17:34:47 2003
+++ camino/src/common/TestMsgDecoder.cpp Tue Jan 21 17:27:41 2003
@@ -2,7 +2,7 @@
**
** Camino
**
-** Version : $Id: TestMsgDecoder.cpp,v 1.2 2003/01/19 22:34:47 pfremy Exp $
+** Version : $Id: TestMsgDecoder.cpp,v 1.3 2003/01/21 22:27:41 pfremy Exp $
**
** Author(s) : Philippe Fremy, Pascal Audoux
** Creation : 14/01/2003
@@ -37,82 +37,121 @@
void TestMsgDecoder::setUp()
{
- // add initialisation for every tests
+ _socketContent = new QCString();
+ _buf = new QBuffer( *_socketContent );
+ _buf->open( IO_ReadWrite );
+
+ _dec = new MockDecoder( _buf );
+ _cod = new MsgCoder( _buf );
}
void TestMsgDecoder::tearDown()
{
- // add finilisation for every test
+ delete _dec;
+ delete _cod;
+ _buf->close();
+ delete _buf;
+ delete _socketContent;
}
-void TestMsgDecoder::testServerConnect()
+void TestMsgDecoder::testQDataStream()
{
QCString mySocket;
QBuffer buf( mySocket );
-
buf.open( IO_ReadWrite );
-
- MockDecoder dec( &buf );
- MsgCoder cod( &buf );
-
- QString playerName = "Phil";
-
- cod.connectServer( playerName );
- qDebug("Socket content : %s", (const char *) mySocket);
- dec.slotDecode();
- qDebug( "s1 : '%s'", dec.s1.latin1() );
- checkEquals( dec.s1, playerName );
-}
-
-void TestMsgDecoder::testQDataStream()
-{
- QCString mySocket;
- // QBuffer buf( mySocket );
- QFile buf( "socket_buffer" );
- buf.open( IO_WriteOnly );
QDataStream ds( &buf );
ds.setPrintableData( true );
ds << 0 << 1 << 2 << QString("hop");
+ buf.putch( 0 );
buf.flush();
- buf.close();
- buf.open( IO_ReadOnly );
- QDataStream ds2( &buf );
- //qDebug( "Buffer : '%s'", (const char *) mySocket );
+ buf.at(0);
+
+// qDebug( "Buffer : '%s'", (const char *) mySocket );
+
int v1, v2, v3;
QString s;
- ds2 >> v1 >> v2 >> v3 >> s;
- qDebug( "v1=%d, v2=%d, v3=%d s = '%s'", v1, v2, v3, s.latin1() );
+ ds >> v1 >> v2 >> v3 >> s;
+// qDebug( "v1=%d, v2=%d, v3=%d s = '%s'", v1, v2, v3, s.latin1() );
+ checkEquals( v1, 0 );
+ checkEquals( v2, 1 );
+ checkEquals( v3, 2 );
+ checkEquals( s, "hop" );
}
void TestMsgDecoder::testQTextStream()
{
QCString mySocket;
QBuffer buf( mySocket );
- buf.open( IO_WriteOnly );
+ buf.open( IO_ReadWrite );
QTextStream ds( &buf );
+ ds.setEncoding( QTextStream::Latin1 );
+ ds << 0 << " " << 1 << " " << 2 << " " << QString("hop");
- ds << 0 << 1 << 2 << QString("hop");
+ buf.putch( 0 );
buf.flush();
- qDebug( "Buffer : '%s'", (const char *) mySocket );
- buf.close();
+ buf.at(0);
+// qDebug( "Buffer : '%s'", (const char *) mySocket );
- buf.open( IO_ReadOnly );
- QTextStream ds2( &buf );
int v1, v2, v3;
QString s;
- ds2 >> v1 >> v2 >> v3 >> s;
- qDebug( "v1=%d, v2=%d, v3=%d s = '%s'", v1, v2, v3, s.latin1() );
+ ds >> v1 >> v2 >> v3 >> s;
+// qDebug( "v1=%d, v2=%d, v3=%d s = '%s'", v1, v2, v3, s.latin1() );
+ checkEquals( v1, 0 );
+ checkEquals( v2, 1 );
+ checkEquals( v3, 2 );
+ checkEquals( s, "hop" );
+}
+
+
+// ----------------------- Here, the real tests begins
+
+
+void TestMsgDecoder::testCreation()
+{
+ /*
+ {
+ MsgDecoder md;
+ MsgCoder mc;
+ }
+
+ QCString s;
+ {
+ QBuffer buf( s );
+ QBuffer buf2( s );
+ buf.open( IO_ReadWrite );
+ buf2.open( IO_ReadWrite );
+ MsgDecoder md( &buf );
+ MsgCoder mc( &buf );
+ MockDecoder md2( &buf );
+ buf.close();
+ buf2.close();
+ MsgCoder();
+ MsgDecoder();
+ MockDecoder();
+ }
+
+ {
+ QBuffer buf( s );
+ // MsgCoder mc( &buf );
+ // MsgCoder mc2 = mc;
+ }
+ */
}
-/*
+
+void TestMsgDecoder::testServerConnect()
{
- QSocket socketPlayer1;
- MsgCoder coder( &socketPlayer1 );
- coder.serverConnect( s );
+ QString playerName = "Phil";
+
+ _cod->connectServer( playerName );
+ _buf->putch( 0 );
+ _buf->flush();
+ _buf->at(0);
- MsgDecoder decoder( QSocket * socketPlayer1 );
- decoder
+ qDebug("Socket content : '%s'", (const char *) *_socketContent );
+ _dec->slotDecode();
+ checkEquals( _dec->s1, playerName );
}
-*/
+
Index: camino/src/common/TestMsgDecoder.h
diff -u camino/src/common/TestMsgDecoder.h:1.2
camino/src/common/TestMsgDecoder.h:1.3
--- camino/src/common/TestMsgDecoder.h:1.2 Sun Jan 19 17:34:47 2003
+++ camino/src/common/TestMsgDecoder.h Tue Jan 21 17:27:41 2003
@@ -5,7 +5,7 @@
** TestMsgDecoder.h
** this is a template for all .h test files
**
-** Version : $Id: TestMsgDecoder.h,v 1.2 2003/01/19 22:34:47 pfremy Exp $
+** Version : $Id: TestMsgDecoder.h,v 1.3 2003/01/21 22:27:41 pfremy Exp $
** Author(s) : Philippe Fremy, Pascal Audoux
** Creation : 14/01/2003
**
@@ -29,10 +29,30 @@
#define TEST_MSG_DECODER_H
#include "MsgDecoder.h"
+#include "MsgCoder.h"
#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 TestMsgDecoder : public CppUnit::TestCase
{
public:
@@ -47,40 +67,31 @@
// declare the test suite
CPPUNIT_TEST_SUITE( TestMsgDecoder );
- CPPUNIT_TEST( testServerConnect );
CPPUNIT_TEST( testQDataStream );
CPPUNIT_TEST( testQTextStream );
+ CPPUNIT_TEST( testCreation );
+ CPPUNIT_TEST( testServerConnect );
+
CPPUNIT_TEST_SUITE_END();
// declare the test functions
void testServerConnect();
+ void testCreation();
void testQDataStream();
void testQTextStream();
protected:
+ QCString * _socketContent;
+ QBuffer * _buf;
+ MockDecoder * _dec;
+ MsgCoder * _cod;
+
// declare some members
};
-
-class MockDecoder : public MsgDecoder
-{
-
-public:
- MockDecoder( QIODevice * device)
- : MsgDecoder( device ) {}
-
-
- virtual void recvServerConnect( QString playerName ) {
- s1 = playerName;
- }
-
-
- QString s1, s2, s3;
- long val1, val2, val3;
-};
#endif // TEST_MSG_DECODER_H
- [Camino-devel] camino/src/common MsgCoder.h MsgCoder.cpp MsgDe...,
Philippe Fremy <=