2 Copyright (C) 2010 Parallel Realities
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "SDL_mutex.h"
24 MedalServer::MedalServer()
26 lock = SDL_CreateMutex();
35 MedalServer::~MedalServer()
43 bool MedalServer::connect(const char *privateKey)
45 if (SDLNet_ResolveHost(&ip, MEDAL_SERVER_HOST, MEDAL_SERVER_PORT) == -1)
47 printf("ERROR: SDLNet_ResolveHost: %s\n", SDLNet_GetError());
51 debug(("Connected %s to %s:%d\n", privateKey, MEDAL_SERVER_HOST, MEDAL_SERVER_PORT));
53 strncpy(this->privateKey, privateKey, sizeof this->privateKey);
60 extern char *strtok_r(char *s1, const char *s2, char **lasts);
63 int MedalServer::postMedal(const char *str)
73 strncpy(medal, str, sizeof medal);
75 for (unsigned int i = 0 ; i < strlen(medal) ; i++)
77 if (medal[i] == ' ' || medal[i] == '#' || medal[i] == ',')
83 debug(("Attempting to post medal 'MBS_%s'\n", medal));
85 TCPsocket socket = SDLNet_TCP_Open(&ip);
89 printf("ERROR: SDLNet_TCP_Open: %s\n", SDLNet_GetError());
93 char *in = new char[1024];
96 snprintf(out, sizeof out, "GET /addMedal/%s/MBS_%s HTTP/1.1\nHost: %s\nUser-Agent:BWMBS%.2f-%d\n\n", privateKey, medal, MEDAL_SERVER_HOST, VERSION, RELEASE);
98 //printf("%s\n", out);
100 int len = strlen(out) + 1;
102 if (SDLNet_TCP_Send(socket, (void*)out, len) < len)
104 printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
105 printf("Disconnected\n");
106 SDLNet_TCP_Close(socket);
111 SDLNet_TCP_Recv(socket, in, 512);
113 //printf("%s\n", in);
116 char *token = strtok_r(in, "\n", &store);
118 while (token != NULL)
120 if (strstr(token, "MEDAL_RESPONSE"))
122 sscanf(token, "%*s %d %[^\n\r]", &response, message);
126 strncpy(rubyMessage, message, sizeof rubyMessage);
135 token = strtok_r(NULL, "\n", &store);
138 debug(("MedalServer Response: %d '%s'\n", response, message))
142 SDLNet_TCP_Close(socket);
147 const char *MedalServer::getMessage()
152 bool MedalServer::hasRuby()
157 const char *MedalServer::getRubyMessage()
164 void MedalServer::close()
168 SDL_DestroyMutex(lock);