elianor bannie 17 fois | Rhooo, je l'ai retrouvé : qbot.cpp
Code :
- #include <qapplication.h>
- #include <qwidget.h>
- #include <qptrlist.h>
- #include <qlabel.h>
- #include "qthreadbot.h"
- // le nombre de thread de surveillance
- #define NB_THREADS 10
- typedef QPtrList<QThreadBot> threadBotList;
- int main (int argc, char **argv) {
- QApplication myApp (argc, argv);
- threadBotList myBotList;
- QWidget *centralWidget = new QWidget ();
- centralWidget->setGeometry (0, 0, 300, 20 * (NB_THREADS + 1));
- /* Maintenant, dans cet Objet, on met les afficheurs
- de chaque thread de bot */
- for (int i=0; i<NB_THREADS; i++) {
- QThreadBot *aBot = new QThreadBot ();
- QLabel *lab = new QLabel (centralWidget, "threadLabel" );
- lab->setGeometry (10, (20*i+1), 200, 15);
- lab->setText ("creating thread" );
- myBotList.append (aBot);
- QObject::connect (aBot, SIGNAL (stateChanged (const QString& )),
- lab, SLOT (setText (const QString& )));
- }
- myApp.setMainWidget (centralWidget);
- centralWidget->show ();
- for (QThreadBot *bot = myBotList.first (); bot; bot = myBotList.next ()) {
- bot->start ();
- }
- return myApp.exec ();
- }
|
qthreadbot.h
Code :
- #ifndef __QTHREADBOT_H__
- #define __QTHREADBOT_H__
- #include <qobject.h>
- #include <qthread.h>
- #include <qmutex.h>
- class QThreadBot : public QObject, public QThread {
- Q_OBJECT
- private:
- int previousNumber;
- int _remainSeconds;
- static int _posted;
- QMutex _firstPostMutex;
- QString *prevMessage;
- public:
- QThreadBot () : QObject (0, "aThread" ), QThread () {
- qDebug ("Creating a thread\n" );
- _remainSeconds = 0;
- previousNumber = 0;
- prevMessage = NULL;
- }
- /* redefines run method */
- void run ();
- protected:
- int getCurrentPostNumber ();
- void doMyFirstPost (bool first);
- int computeSleepTime (int nbPosts);
- void setRemainMessage ();
- void emitMessage (const QString &str);
- signals:
- void stateChanged (const QString &str);
- };
- #endif // __QTHREADBOT_H__
|
et qthreadbot.cpp
Code :
- #include <qapplication.h>
- #include "qthreadbot.h"
- #include <curl/curl.h>
- #include <curl/types.h>
- #include <curl/easy.h>
- struct MemoryStruct {
- char *memory;
- size_t size;
- };
- /* initialisation du membre statique */
- int QThreadBot::_posted = 0;
- size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
- register int realsize = size * nmemb;
- struct MemoryStruct *mem = (struct MemoryStruct *)data;
- mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
- if (mem->memory) {
- memcpy(&(mem->memory[mem->size]), ptr, realsize);
- mem->size += realsize;
- mem->memory[mem->size] = 0;
- }
- return realsize;
- }
- void QThreadBot::run () {
- qDebug ("starting thread...\n" );
- emitMessage ("thread started" );
- while (1) {
- while (_remainSeconds != 0) {
- setRemainMessage ();
- _remainSeconds--;
- sleep (1);
- }
- int nb = getCurrentPostNumber ();
- if (nb == 997 || 998 || 999) {
- doMyFirstPost (true);
- }
- if (nb < previousNumber) {
- /* c'est la loose, on a rate le split, mais on dit qu'on existe */
- doMyFirstPost (false);
- }
- previousNumber = nb;
- /* ce n'est pas encore le moment de poster, on se rendors */
- computeSleepTime (nb);
- }
- }
- int QThreadBot::getCurrentPostNumber () {
- int result = 0;
- emitMessage ("getting post number" );
- CURL *curl_handle;
- struct MemoryStruct chunk;
- chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
- chunk.size = 0; /* no data at this point */
- /* init the curl session */
- curl_handle = curl_easy_init();
- /* specify URL to get */
- curl_easy_setopt(curl_handle, CURLOPT_URL, "http://forum.hardware.fr/forum1.php3?config=&interface=0&cat=10" );
- /* send all data to this function */
- curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- /* we pass our 'chunk' struct to the callback function */
- curl_easy_setopt(curl_handle, CURLOPT_FILE, (void *)&chunk);
- /* get it! */
- curl_easy_perform(curl_handle);
- /* cleanup curl stuff */
- curl_easy_cleanup(curl_handle);
- emitMessage ("parsing forum page" );
- qDebug ("Page size : %d\n", chunk.size);
- char *blablaPosition = strstr (chunk.memory, "BlaBla@Programmation" );
- /* le premier style amene sur le createur de topic */
- char *stylePosition = strstr (blablaPosition, "text1small" );
- /* le second sur le nb de posts */
- stylePosition = strstr (stylePosition + 10, "text1small" );
- /* 11 octets apres le style, on trouve le debut du nombre */
- char *numberPosition = stylePosition + 12;
- for (char *currentPosition = numberPosition;
- *currentPosition >= '0' &&
- *currentPosition <= '9';
- currentPosition ++) {
- result *= 10;
- result += (*currentPosition - '0');
- }
- qDebug ("found currentNumber : %d \n", result);
- /* nettoyage */
- free (chunk.memory);
- return result;
- }
- void QThreadBot::doMyFirstPost (bool first){
- CURL *curl;
- CURLcode res;
- int canPost;
- _firstPostMutex.lock ();
- canPost = ! _posted;
- _posted = 1;
- _firstPostMutex.unlock ();
- emitMessage ("posting first post" );
- struct MemoryStruct chunk;
- chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
- chunk.size = 0; /* no data at this point */
- curl = curl_easy_init();
- if(curl && canPost) {
- /* First set the URL that is about to receive our POST. This URL can
- just as well be a https:// URL if that is what should receive the
- data. */
- curl_easy_setopt(curl, CURLOPT_URL,
- "http://forum.hardware.fr/bdd.php3?interface=0&config=" );
- /* Now specify the POST data */
- if (first) {
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "pseudo=kadreg&password=LOLDTC&MsgIcon=1&contenu=daLoozeBot>Encore%20un%20bide&signature=1&smiley=0&email=1&subcat=&post=36787&stickold=&cat=10&numrep="e=&page=22&verifrequet=1100&p=1&sondage=&cache=&owntopic=&config=&sujet=BlaBla@Programmation" );
- } else {
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "pseudo=kadreg&password=LOLDTC&MsgIcon=1&contenu=daLooseBot>je%20suis%20une%20merde&signature=1&smiley=0&email=1&subcat=&post=36787&stickold=&cat=10&numrep="e=&page=22&verifrequet=1100&p=1&sondage=&cache=&owntopic=&config=&sujet=BlaBla@Programmation" );
- }
- /* send all data to this function */
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- /* we pass our 'chunk' struct to the callback function */
- curl_easy_setopt(curl, CURLOPT_FILE, (void *)&chunk);
- /* Perform the request, res will get the return code */
- res = curl_easy_perform(curl);
- /* always cleanup */
- curl_easy_cleanup(curl);
- }
- emitMessage ("first post done, thanks all" );
- }
- void QThreadBot::setRemainMessage () {
- QString mess ("sleeping for %1 seconds " );
- emitMessage (mess.arg (_remainSeconds, 0, 10));
- }
- int QThreadBot::computeSleepTime (int nbPosts) {
- int result = 0;
- if (nbPosts < 500) result = 3600;
- else if (nbPosts < 900) result = 180;
- else if (nbPosts < 940) result = 60;
- else if (nbPosts < 990) result = 10;
- else result = 1;
- _remainSeconds = result;
- return result;
- }
- void QThreadBot::emitMessage (const QString &mess) {
- if (prevMessage) delete prevMessage;
- prevMessage = new QString (mess);
- qApp->lock ();
- emit stateChanged (*prevMessage);
- qApp->unlock ();
- }
|
|