- Update webserver

- cmake option webserver true

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@6517 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: 7fe702744737106027a8148bc380af46fe9d9044
Former-commit-id: b494d10e1ed3e6f7d54b4fcd67bcda68627500ed
This commit is contained in:
torcs-ng 2017-11-12 00:30:20 +00:00
parent 2a0a1f3dc5
commit d16d4eaf76
3 changed files with 970 additions and 934 deletions

View file

@ -84,7 +84,7 @@ MACRO(ADD_SD_COMPILE_OPTIONS)
SET(OPTION_SDL_JOYSTICK true CACHE BOOL "Use SDL for Joystick instead of PLIB")
CMAKE_DEPENDENT_OPTION(OPTION_SDL_FORCEFEEDBACK "Use SDL2 Haptics" true "OPTION_SDL_JOYSTICK;OPTION_SDL2" false)
SET(OPTION_WEBSERVER false CACHE BOOL "Build with WebServer functionality")
SET(OPTION_WEBSERVER true CACHE BOOL "Build with WebServer functionality")
SET(OPTION_CLIENT_SERVER false CACHE BOOL "Build with Client/Server network architecture")

View file

@ -294,22 +294,21 @@ void NotificationManager::runAnimation(){
void NotificationManager::removeOldUi(){
//if there is a prev screen
if( GfuiScreenIsActive(this->prevScreenHandle) ){
if( GfuiScreenIsActive(this->prevScreenHandle) )
{
//if there is some prev ui around hide it
if(this->notifyUiIdBg > 0){
if(this->notifyUiIdBg > 0)
{
GfuiVisibilitySet(this->prevScreenHandle, this->notifyUiIdBg, GFUI_INVISIBLE);
}
//iterate trougth ui and set them invisible
for (int i = 0; i < notifyUiId.size(); i++) {
for (int i = 0; i < notifyUiId.size(); i++)
{
GfuiVisibilitySet(this->prevScreenHandle, this->notifyUiId[i], GFUI_INVISIBLE);
}
}
//delete the prev ui's
@ -317,8 +316,8 @@ void NotificationManager::removeOldUi(){
this->notifyUiIdBg=-1;
}
void NotificationManager::createUi(){
void NotificationManager::createUi()
{
//create the new UI
this->notifyUiIdBg = GfuiMenuCreateStaticImageControl(this->screenHandle, this->menuXMLDescHdle, "slidebg");
GfuiVisibilitySet(this->screenHandle, this->notifyUiIdBg, GFUI_VISIBLE);
@ -328,8 +327,8 @@ void NotificationManager::createUi(){
int yposmod= ypos;
//iterate trougth lines
for (int i = 0; i < this->messageLines.size(); i++) {
for (int i = 0; i < this->messageLines.size(); i++)
{
int uiId;
uiId= GfuiMenuCreateLabelControl(this->screenHandle, this->menuXMLDescHdle, "slide");
@ -340,46 +339,40 @@ void NotificationManager::createUi(){
GfuiLabelSetText(this->screenHandle, uiId, this->messageLines[i].c_str());
GfuiVisibilitySet(this->screenHandle, uiId, GFUI_VISIBLE);
this->notifyUiId.push_back(uiId);
}
//reset ypos
GfParmSetNum(this->menuXMLDescHdle, "dynamic controls/slide", "y", "null", ypos);
}
void NotificationManager::updateWebserverStatusUi(){
void NotificationManager::updateWebserverStatusUi()
{
//todo: this was causing some random crash
//if there is some prev ui around hide it
if(this->notifyUiIdBusyIcon > 0 && this->screenHandle == this->prevScreenHandle){
if(this->notifyUiIdBusyIcon > 0 && this->screenHandle == this->prevScreenHandle)
{
GfuiVisibilitySet(this->prevScreenHandle, this->notifyUiIdBusyIcon, GFUI_INVISIBLE);
}
if(this->screenHandle > 0){
if(this->screenHandle > 0)
{
//if webserver is not IDLE display busy icon (sending or receiving)
if(webserverState != WEBSERVER_IDLE){
if(webserverState != WEBSERVER_IDLE)
{
std::string webServerIcon = "busyicon";
webServerIcon.append(to_string(webserverState));
this->notifyUiIdBusyIcon = GfuiMenuCreateStaticImageControl(this->screenHandle, this->menuXMLDescHdle, webServerIcon.c_str());
GfuiVisibilitySet(this->screenHandle, this->notifyUiIdBusyIcon, GFUI_VISIBLE);
}
}
}
}
}
//initialize the notification manager
NotificationManager notifications;
WebServer::WebServer(){
WebServer::WebServer()
{
//set some default
this->raceId = -1;
this->userId = -1;
@ -392,18 +385,19 @@ WebServer::WebServer(){
//initialize the configuration
this->readConfiguration();
}
WebServer::~WebServer(){
WebServer::~WebServer()
{
//cleanup curl
curl_multi_cleanup(this->multi_handle);
}
int WebServer::updateAsyncStatus(){
int WebServer::updateAsyncStatus()
{
//if we have some ordered async request pending
if( this->pendingAsyncRequestId == 0 && !this->orderedAsyncRequestQueque.empty() ){
if( this->pendingAsyncRequestId == 0 && !this->orderedAsyncRequestQueque.empty() )
{
webRequest_t nextRequest;
nextRequest = this->orderedAsyncRequestQueque.front();
@ -416,15 +410,17 @@ int WebServer::updateAsyncStatus(){
this->addAsyncRequest(nextRequest.data);
}
//perform the pending requests
curl_multi_perform(this->multi_handle, &this->handle_count);
if( this->handle_count > 0){
if( this->handle_count > 0)
{
GfLogInfo("WebServer: Number of async request waiting for a reply from the server: %i\n", this->handle_count);
//display some UI to the user to inform him we are waiting a reply from the server
webserverState=WEBSERVER_RECEIVING;
}else{
}
else
{
webserverState=WEBSERVER_IDLE;
}
@ -434,12 +430,16 @@ int WebServer::updateAsyncStatus(){
int http_status_code;
const char *szUrl;
while ((msg = curl_multi_info_read(this->multi_handle, &this->handle_count))) {
if (msg->msg == CURLMSG_DONE) {
while ((msg = curl_multi_info_read(this->multi_handle, &this->handle_count)))
{
if (msg->msg == CURLMSG_DONE)
{
eh = msg->easy_handle;
return_code = msg->data.result;
if(return_code!=CURLE_OK) {
if(return_code!=CURLE_OK)
{
fprintf(stderr, "CURL error code: %d\n", msg->data.result);
//something went wrong. anyway we are no more busy
@ -457,7 +457,8 @@ int WebServer::updateAsyncStatus(){
curl_easy_getinfo(eh, CURLINFO_EFFECTIVE_URL, &szUrl);
//the server replyed
if(http_status_code==200) {
if(http_status_code==200)
{
GfLogInfo("WebServer: successfull reply from the server from url: %s\n", szUrl);
GfLogInfo("WebServer: The reply from the server is:\n%s\n", this->curlServerReply.c_str());
@ -467,9 +468,12 @@ int WebServer::updateAsyncStatus(){
void *xmlReply;
xmlReply = GfParmReadBuf(const_cast<char*>(this->curlServerReply.c_str()));
if(GfParmExistsSection(xmlReply, "content")){
if(GfParmExistsSection(xmlReply, "content"))
{
int requestId = GfParmGetNum(xmlReply, "content", "request_id", "null",0);
if( requestId == this->pendingAsyncRequestId ){
if( requestId == this->pendingAsyncRequestId )
{
//reset the pending request id
this->pendingAsyncRequestId=0;
// remove this request from the queque... so we can continue with the next
@ -478,11 +482,13 @@ int WebServer::updateAsyncStatus(){
}
}
//the server want we display something?
if(GfParmExistsSection(xmlReply, "content/reply/messages")){
if(GfParmExistsSection(xmlReply, "content/reply/messages"))
{
int msgsCount = GfParmGetNum(xmlReply, "content/reply/messages", "number", "null",0);
if( msgsCount > 0 ){
if( msgsCount > 0 )
{
for( int dispatchedMsgs = 0; dispatchedMsgs < msgsCount; dispatchedMsgs = dispatchedMsgs + 1 )
{
std::string msgTag = "message";
@ -496,8 +502,10 @@ int WebServer::updateAsyncStatus(){
//race reply
//store the webServer assigned race id
if(GfParmExistsSection(xmlReply, "content/reply/races")){
if(GfParmGetNum(xmlReply, "content/reply/races", "id", "null", 0) != 0){
if(GfParmExistsSection(xmlReply, "content/reply/races"))
{
if(GfParmGetNum(xmlReply, "content/reply/races", "id", "null", 0) != 0)
{
this->raceId = (int)GfParmGetNum(xmlReply, "content/reply/races", "id", "null", 0);
GfLogInfo("WebServer: Assigned raceId by the server is: %i\n", this->raceId);
}
@ -505,31 +513,41 @@ int WebServer::updateAsyncStatus(){
//login reply
//store the webServer assigned race id
if(GfParmExistsSection(xmlReply, "content/reply/login")){
if(GfParmGetNum(xmlReply, "content/reply/login", "id", "null",0) != 0){
if(GfParmExistsSection(xmlReply, "content/reply/login"))
{
if(GfParmGetNum(xmlReply, "content/reply/login", "id", "null",0) != 0)
{
//store the webServer session and id assigned
this->sessionId = GfParmGetStr(xmlReply, "content/reply/login", "sessionid", "null");
this->userId = GfParmGetNum(xmlReply, "content/reply/login", "id", "null",0);
GfLogInfo("WebServer: Successfull Login as userId: %i\n", this->userId);
}else{
}
else
{
GfLogInfo("WebServer: Login Failed: Wrong username or password.\n");
notifications.msglist.push_back("WebServer: Login Failed: Wrong username or password.");
return 1;
}
GfLogInfo("WebServer: Assigned session id is: %s\n", this->sessionId);
}
//empty the string
this->curlServerReply.clear();
} else {
}
else
{
fprintf(stderr, "GET of %s returned http status code %d\n", szUrl, http_status_code);
}
curl_multi_remove_handle(this->multi_handle, eh);
curl_easy_cleanup(eh);
/* then cleanup the formpost chain */
//curl_formfree(formpost);
}
else {
else
{
fprintf(stderr, "error: after curl_multi_info_read(), CURLMsg=%d\n", msg->msg);
}
}
@ -537,7 +555,8 @@ int WebServer::updateAsyncStatus(){
return 0;
}
int WebServer::addAsyncRequest(std::string const data){
int WebServer::addAsyncRequest(std::string const data)
{
GfLogInfo("WebServer: Performing ASYNC request:\n%s\n", data.c_str());
CURL* curl = NULL;
@ -545,8 +564,9 @@ int WebServer::addAsyncRequest(std::string const data){
struct curl_httppost *lastptr=NULL;
curl = curl_easy_init();
if(curl) {
if(curl)
{
//set url to be called
curl_easy_setopt(curl, CURLOPT_URL, this->url);
@ -580,7 +600,9 @@ int WebServer::addAsyncRequest(std::string const data){
return 0;
}
int WebServer::addOrderedAsyncRequest(std::string data){
int WebServer::addOrderedAsyncRequest(std::string data)
{
//prepare the request object
webRequest_t request;
@ -590,10 +612,12 @@ int WebServer::addOrderedAsyncRequest(std::string data){
replaceAll(request.data, "{{request_id}}", to_string(request.id));
this->orderedAsyncRequestQueque.push_back(request);
return 0;
}
int WebServer::sendGenericRequest (std::string data, std::string& serverReply){
int WebServer::sendGenericRequest (std::string data, std::string& serverReply)
{
CURL *curl;
CURLcode res;
@ -607,8 +631,9 @@ int WebServer::sendGenericRequest (std::string data, std::string& serverReply){
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, this->url);
// send all data to this function
@ -631,13 +656,14 @@ int WebServer::sendGenericRequest (std::string data, std::string& serverReply){
res = curl_easy_perform(curl);
// Check for errors
if(res != CURLE_OK) {
if(res != CURLE_OK)
{
notifications.msglist.push_back("Failed to connect to the WebServer!");
GfLogInfo("WebServer: Unable to perform SYNC request some error occured:\n", data.c_str());
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
else {
else
{
//
// Now, our this->curlServerReply
// contains the remote file (aka serverReply).
@ -661,13 +687,14 @@ int WebServer::sendGenericRequest (std::string data, std::string& serverReply){
// we're done with libcurl, so clean it up
curl_global_cleanup();
}
return 0;
}
void WebServer::readConfiguration (){
void *configHandle;
char configFileUrl[256];
void WebServer::readConfiguration ()
{
//void *configHandle;
//char configFileUrl[256];
/*
//get the preferencies file location
sprintf(configFileUrl, "%s%s", GfLocalDir(), "config/webserver.xml");
@ -680,12 +707,14 @@ void WebServer::readConfiguration (){
*/
this->url ="http://www.madbad.altervista.org/speed-dreams/webserver.php";
//this->url ="http://masterserver.speed-dreams.fr/webserver.php";
// this->url ="http://localhost/speed-dreams/webserver.php";
//GfLogInfo("WebServer - webserver url is: %s\n", this->url);
}
int WebServer::readUserConfig (int userId){
int WebServer::readUserConfig (int userId)
{
void *prHandle;
char prefFileUrl[256];
char xmlPath[256];
@ -708,7 +737,8 @@ int WebServer::readUserConfig (int userId){
return 0;
}
int WebServer::sendLogin (int userId){
int WebServer::sendLogin (int userId)
{
std::string serverReply;
//read username and password and save it in as webserver properties
@ -745,7 +775,9 @@ int WebServer::sendLogin (int userId){
*/
return 0;
}
int WebServer::sendLogin (const char* username, const char* password){
int WebServer::sendLogin (const char* username, const char* password)
{
std::string serverReply;
//prepare the string to send
@ -769,7 +801,9 @@ int WebServer::sendLogin (const char* username, const char* password){
return 0;
}
int WebServer::sendLap (int race_id, double laptime, double fuel, int position, int wettness){
int WebServer::sendLap (int race_id, double laptime, double fuel, int position, int wettness)
{
/*
//Do some sanity-checks befor proceding... If something is wrong do nothing
//are we logged in?
@ -811,7 +845,9 @@ int WebServer::sendLap (int race_id, double laptime, double fuel, int position,
return 0;
}
int WebServer::sendRaceStart (int user_skill, const char *track_id, char *car_id, int type, void *setup, int startposition, const char *sdversion){
int WebServer::sendRaceStart (int user_skill, const char *track_id, char *car_id, int type, void *setup, int startposition, const char *sdversion)
{
std::string serverReply;
std::string mysetup;
std::string dataToSend;
@ -863,7 +899,9 @@ int WebServer::sendRaceStart (int user_skill, const char *track_id, char *car_id
return 0;
}
int WebServer::sendRaceEnd (int race_id, int endposition){
int WebServer::sendRaceEnd (int race_id, int endposition)
{
std::string serverReply;
//Do some sanity-checks befor proceding... If something is wrong do nothing
@ -910,5 +948,3 @@ int WebServer::sendRaceEnd (int race_id, int endposition){
//initialize the web server
TGFCLIENT_API WebServer webServer;
#endif //WEBSERVER