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