code cleanup
git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@6264 30fe4595-0a0c-4342-8851-515496e4dcbd Former-commit-id: b460ca83dfc1ce9c5639480b519a2d83e52a2963 Former-commit-id: 221b539567768556bf816ab4758e8e92421a0b53
This commit is contained in:
parent
9b8ca93d1d
commit
2e008d8803
2 changed files with 24 additions and 66 deletions
|
@ -21,9 +21,16 @@
|
||||||
@author <a href=mailto:madbad82@gmail.com>MadBad</a>
|
@author <a href=mailto:madbad82@gmail.com>MadBad</a>
|
||||||
@version $Id$
|
@version $Id$
|
||||||
*/
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include <curl/multi.h>
|
||||||
|
#include <playerpref.h>
|
||||||
|
#include <tgf.h>
|
||||||
|
#include "tgfclient.h"
|
||||||
#include "webserver.h"
|
#include "webserver.h"
|
||||||
|
|
||||||
|
|
||||||
//string splitting utils
|
//string splitting utils
|
||||||
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
|
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
|
||||||
std::stringstream ss(s);
|
std::stringstream ss(s);
|
||||||
|
@ -39,6 +46,7 @@ std::vector<std::string> split(const std::string &s, char delim) {
|
||||||
split(s, delim, elems);
|
split(s, delim, elems);
|
||||||
return elems;
|
return elems;
|
||||||
}
|
}
|
||||||
|
|
||||||
//to string (from c++11)
|
//to string (from c++11)
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::string to_string(T value)
|
std::string to_string(T value)
|
||||||
|
@ -48,52 +56,12 @@ std::string to_string(T value)
|
||||||
return os.str() ;
|
return os.str() ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// WEBSERVER class and utilus
|
|
||||||
struct MemoryStruct {
|
|
||||||
char *memory;
|
|
||||||
size_t size;
|
|
||||||
};
|
|
||||||
/*
|
|
||||||
static size_t
|
|
||||||
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
|
||||||
{
|
|
||||||
size_t realsize = size * nmemb;
|
|
||||||
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
|
|
||||||
|
|
||||||
mem->memory =(char*)realloc(mem->memory, mem->size + realsize + 1);
|
|
||||||
if(mem->memory == NULL) {
|
|
||||||
// out of memory!
|
|
||||||
printf("not enough memory (realloc returned NULL)\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(&(mem->memory[mem->size]), contents, realsize);
|
|
||||||
mem->size += realsize;
|
|
||||||
mem->memory[mem->size] = 0;
|
|
||||||
|
|
||||||
return realsize;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
static size_t WriteStringCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
static size_t WriteStringCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
||||||
{
|
{
|
||||||
((std::string*)userp)->append((char*)contents, size * nmemb);
|
((std::string*)userp)->append((char*)contents, size * nmemb);
|
||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//webserver requirements
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sstream>
|
|
||||||
//#include <curl/curl.h>
|
|
||||||
#include <curl/multi.h>
|
|
||||||
#include <playerpref.h>
|
|
||||||
|
|
||||||
//webserver utility
|
|
||||||
//replace
|
|
||||||
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
|
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
|
||||||
if(from.empty())
|
if(from.empty())
|
||||||
return;
|
return;
|
||||||
|
@ -104,8 +72,6 @@ void replaceAll(std::string& str, const std::string& from, const std::string& to
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const int WEBSERVER_IDLE = 0;
|
const int WEBSERVER_IDLE = 0;
|
||||||
const int WEBSERVER_SENDING = 1;
|
const int WEBSERVER_SENDING = 1;
|
||||||
const int WEBSERVER_RECEIVING = 2;
|
const int WEBSERVER_RECEIVING = 2;
|
||||||
|
@ -391,15 +357,10 @@ void NotificationManager::updateWebserverStatusUi(){
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//initialize the notification manager
|
||||||
NotificationManager notifications;
|
NotificationManager notifications;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
CURLM* multi_handle;
|
CURLM* multi_handle;
|
||||||
//extern std::vector<std::string> msglist;
|
|
||||||
extern int webserverState;
|
|
||||||
|
|
||||||
/* START webserver*/
|
/* START webserver*/
|
||||||
WebServer::WebServer(){
|
WebServer::WebServer(){
|
||||||
|
@ -436,9 +397,9 @@ int WebServer::updateAsyncStatus(){
|
||||||
if( this->handle_count>0){
|
if( this->handle_count>0){
|
||||||
GfLogInfo("############################# ASYNC WAITING UPDATES: %i\n", this->handle_count);
|
GfLogInfo("############################# ASYNC WAITING UPDATES: %i\n", this->handle_count);
|
||||||
//display some UI to the user to inform him we are waiting a reply from the server
|
//display some UI to the user to inform him we are waiting a reply from the server
|
||||||
webserverState=2;
|
webserverState=WEBSERVER_RECEIVING;
|
||||||
}else{
|
}else{
|
||||||
webserverState=0;
|
webserverState=WEBSERVER_IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
CURLMsg *msg;
|
CURLMsg *msg;
|
||||||
|
@ -456,7 +417,7 @@ webserverState=0;
|
||||||
fprintf(stderr, "CURL error code: %d\n", msg->data.result);
|
fprintf(stderr, "CURL error code: %d\n", msg->data.result);
|
||||||
|
|
||||||
//something went wrong. anyway we are no more busy
|
//something went wrong. anyway we are no more busy
|
||||||
webserverState=0;
|
webserverState=WEBSERVER_IDLE;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -533,8 +494,7 @@ int WebServer::addAsyncRequest(std::string const data){
|
||||||
CURL* curl = NULL;
|
CURL* curl = NULL;
|
||||||
struct curl_httppost *formpost=NULL;
|
struct curl_httppost *formpost=NULL;
|
||||||
struct curl_httppost *lastptr=NULL;
|
struct curl_httppost *lastptr=NULL;
|
||||||
//struct MemoryStruct chunk;
|
|
||||||
//
|
|
||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
if(curl) {
|
if(curl) {
|
||||||
|
|
||||||
|
@ -567,8 +527,8 @@ int WebServer::addAsyncRequest(std::string const data){
|
||||||
curl_multi_add_handle(multi_handle, curl);
|
curl_multi_add_handle(multi_handle, curl);
|
||||||
|
|
||||||
//pending request
|
//pending request
|
||||||
//webserverBusy=true;
|
webserverState=WEBSERVER_SENDING;
|
||||||
webserverState=1;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -577,7 +537,9 @@ int WebServer::sendGenericRequest (std::string data, std::string& serverReply){
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
|
|
||||||
GfLogInfo("WebServer - SENDING data to server:\n %s \n", data.c_str());
|
GfLogInfo("WebServer - SENDING data to server:\n %s \n", data.c_str());
|
||||||
webserverState=1;
|
|
||||||
|
webserverState=WEBSERVER_SENDING;
|
||||||
|
|
||||||
//insert "data=" before the actual data
|
//insert "data=" before the actual data
|
||||||
data.insert(0,"data=");
|
data.insert(0,"data=");
|
||||||
const char *postthis=data.c_str();
|
const char *postthis=data.c_str();
|
||||||
|
@ -622,8 +584,11 @@ webserverState=1;
|
||||||
//
|
//
|
||||||
|
|
||||||
GfLogInfo("WebServer - RECEIVING data from server:\n %s\n", this->curlServerReply.c_str());
|
GfLogInfo("WebServer - RECEIVING data from server:\n %s\n", this->curlServerReply.c_str());
|
||||||
webserverState=2;
|
|
||||||
|
webserverState=WEBSERVER_RECEIVING;
|
||||||
|
|
||||||
serverReply = this->curlServerReply;
|
serverReply = this->curlServerReply;
|
||||||
|
|
||||||
//empty the string
|
//empty the string
|
||||||
this->curlServerReply.clear();
|
this->curlServerReply.clear();
|
||||||
}
|
}
|
||||||
|
@ -839,10 +804,8 @@ int WebServer::sendRaceEnd (int race_id, int endposition){
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//initialize the web server
|
//initialize the web server
|
||||||
WebServer webServer;
|
WebServer webServer;
|
||||||
|
|
||||||
/* END webserver*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,10 +23,7 @@
|
||||||
*/
|
*/
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <tgf.h>
|
|
||||||
#include "tgfclient.h"
|
|
||||||
|
|
||||||
class NotificationManager {
|
class NotificationManager {
|
||||||
|
|
||||||
|
@ -72,8 +69,6 @@ class NotificationManager {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class WebServer {
|
class WebServer {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Reference in a new issue