2 Copyright (C) 2010-2011 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.
23 MedalServer::MedalServer()
25 lock = SDL_CreateMutex();
34 MedalServer::~MedalServer()
42 bool MedalServer::connect(const char *privateKey)
44 if (SDLNet_ResolveHost(&ip, MEDAL_SERVER_HOST, MEDAL_SERVER_PORT) == -1)
46 printf("ERROR: SDLNet_ResolveHost: %s\n", SDLNet_GetError());
50 debug(("Connected %s to %s:%d\n", privateKey, MEDAL_SERVER_HOST, MEDAL_SERVER_PORT));
52 strlcpy(this->privateKey, privateKey, sizeof this->privateKey);
59 extern char *strtok_r(char *s1, const char *s2, char **lasts);
62 int MedalServer::postMedal(const char *str)
72 strlcpy(medal, str, sizeof medal);
74 for (unsigned int i = 0 ; i < strlen(medal) ; i++)
76 if (medal[i] == ' ' || medal[i] == '#' || medal[i] == ',')
82 debug(("Attempting to post medal 'MBS_%s'\n", medal));
84 TCPsocket socket = SDLNet_TCP_Open(&ip);
88 printf("ERROR: SDLNet_TCP_Open: %s\n", SDLNet_GetError());
94 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);
96 //printf("%s\n", out);
98 int len = strlen(out) + 1;
100 if (SDLNet_TCP_Send(socket, (void*)out, len) < len)
102 printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
103 printf("Disconnected\n");
104 SDLNet_TCP_Close(socket);
109 char *in = new char[1024];
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 strlcpy(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);