PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vp_plugins/test_sptsd/main.cpp

http://cupsfilter.googlecode.com/
C++ | 191 lines | 154 code | 33 blank | 4 comment | 5 complexity | b62da5584bb215deac56af867a604785 MD5 | raw file
  1#include <qapplication.h>
  2#include <qglobal.h>
  3#include <qstring.h>
  4#include <qdatastream.h>
  5#include <qsocket.h>
  6#include <qfile.h>
  7#include <qwaitcondition.h>
  8
  9class ClientSocket : public QSocket
 10{
 11    Q_OBJECT
 12    
 13public:
 14    ClientSocket( const QString &host, Q_UINT16 port )
 15    {	
 16	socket = new QSocket(this);
 17	connect( socket, SIGNAL(connected()), SLOT(socketConnected()) );
 18	connect( socket, SIGNAL(readyRead()), SLOT(readSocketData()) );
 19	connect( socket, SIGNAL(connectionClosed()), SLOT(socketConnectionClosed()) );
 20
 21	// connect to the server
 22        socket->connectToHost( host, port );
 23    }
 24
 25    ~ClientSocket()
 26    {
 27    }
 28    
 29private slots:
 30
 31    void socketConnected()
 32    {
 33        qDebug("Connected");
 34	Q_INT32 packetSize = 0, commandType = 10;
 35	QString s = "";
 36	QDataStream out(socket);	
 37	QFile psFile("./test.txt");
 38	QByteArray ba, cba;
 39
 40	switch (commandType) {
 41	
 42	    case 10:
 43		s = "[123];:;";
 44		break;
 45	    case 100:
 46		s = "[123];:;qqqq;:;sva;:;NC";
 47		break;
 48	    case 101:
 49		s = "[123];:;NC;:;";
 50		break;
 51	    case 202:
 52		s = "[123];:;INSERT INTO SAMP (id) VALUES (300); SELECT * FROM SAMP;;:;";
 53		break;
 54	    case 300:
 55		s = "[123];:;NC;:;";
 56		break;
 57	    case 500:
 58		break;
 59	    case 600:
 60		s = "[123];:;usr1;:;";	    
 61		break;
 62	    case 6000:
 63		psFile.open(IO_ReadOnly);
 64		ba =  psFile.readAll();
 65		psFile.close();
 66	//	qDebug("Uncompressed size is " + QString("%1").arg(ba.size()) + " bytes");
 67		cba = qCompress(ba);
 68		break;
 69	    default:
 70		qDebug("Unknown command");
 71		return;
 72	}
 73	
 74	if (commandType == 6000) 
 75	    cba = qCompress(ba);
 76	else
 77	    cba = s.utf8();
 78	
 79	packetSize = sizeof(packetSize) + sizeof(commandType) + cba.size();
 80	qDebug("Writing " + QString("%1").arg(cba.size()) + " bytes of data (" + QString("%1").arg(packetSize) + " bytes total packet size)" );
 81	out << packetSize;
 82	out << commandType;
 83	if (s != "")
 84	    qDebug("Sending string: " + s);
 85	out << cba;
 86
 87    socket->flush();
 88
 89//    QEventLoop loop; QTimer::singleShot(10000, &loop, SLOT(quit())); loop.exec();
 90    QWaitCondition sleep;
 91    sleep.wait(1000);   // wait here
 92
 93    commandType = 600;
 94    s = "[123];:;usr1;:;";
 95    cba = s.utf8();
 96    packetSize = sizeof(packetSize) + sizeof(commandType) + cba.size();
 97    qDebug("Writing " + QString("%1").arg(cba.size()) + " bytes of data (" + QString("%1").arg(packetSize) + " bytes total packet size)" );
 98    out << packetSize;
 99    out << commandType;
100    qDebug("Sending string: " + s);
101    out << cba;
102    socket->flush();
103    sleep.wait(7000);   // wait here
104
105    commandType = 300;
106    s = "[123];:;NC;:;";
107    cba = s.utf8();
108    packetSize = sizeof(packetSize) + sizeof(commandType) + cba.size();
109    qDebug("Writing " + QString("%1").arg(cba.size()) + " bytes of data (" + QString("%1").arg(packetSize) + " bytes total packet size)" );
110    out << packetSize;
111    out << commandType;
112    qDebug("Sending string: " + s);
113    out << cba;
114    socket->flush();
115    sleep.wait(7000);   // wait here
116
117    commandType = 400;
118    s = "[123];:;usr1;:;CC;:;";
119    cba = s.utf8();
120    packetSize = sizeof(packetSize) + sizeof(commandType) + cba.size();
121    qDebug("Writing " + QString("%1").arg(cba.size()) + " bytes of data (" + QString("%1").arg(packetSize) + " bytes total packet size)" );
122    out << packetSize;
123    out << commandType;
124    qDebug("Sending string: " + s);
125    out << cba;
126    socket->flush();
127    sleep.wait(14000);   // wait here
128
129    commandType = 100;
130    s = "[123];:;SL9PRT.PDF;:;CC;:;usr1;:;";
131    cba = s.utf8();
132    packetSize = sizeof(packetSize) + sizeof(commandType) + cba.size();
133    qDebug("Writing " + QString("%1").arg(cba.size()) + " bytes of data (" + QString("%1").arg(packetSize) + " bytes total packet size)" );
134    out << packetSize;
135    out << commandType;
136    qDebug("Sending string: " + s);
137    out << cba;
138    socket->flush();
139    sleep.wait(4000);   // wait here
140
141    socket->close();
142    return;
143//	qApp->quit();
144    }
145
146void readSocketData()
147{
148    QDataStream socketStream(this);
149    Q_INT32 size, type;
150    QByteArray ba;
151    QString s;
152    socketStream >> size;
153    socketStream >> type;
154    socketStream >> ba;
155    s = ba.data();
156    qDebug(s);
157    return;
158}
159    
160void socketConnectionClosed()
161{
162    qDebug("Disconnected");
163    qApp->quit();
164}
165    
166private:
167
168    QSocket *socket;
169    
170};
171
172
173
174
175int main(int argc, char *argv[])
176{
177    bool useGUI = false;
178
179    QApplication qApp(argc, argv, useGUI);
180
181    qDebug ("\n");
182
183    qDebug("Connecting to localhost:4243");
184    ClientSocket sock("localhost", 4243);
185        
186    return qApp.exec();
187}
188
189#include "main.moc"
190
191