Login working, segfaults sometimes

This commit is contained in:
Simon Robertshaw 2012-01-25 17:21:55 +00:00
parent 3505bcc275
commit 8b80942b16
16 changed files with 164 additions and 34 deletions

19
src/Controller.h Normal file
View File

@ -0,0 +1,19 @@
/*
* Controller.h
*
* Created on: Jan 25, 2012
* Author: Simon
*/
#ifndef CONTROLLER_H_
#define CONTROLLER_H_
class ControllerCallback
{
public:
ControllerCallback() {}
virtual void ControllerExit() {}
virtual ~ControllerCallback() {}
};
#endif /* CONTROLLER_H_ */

View File

@ -40,35 +40,31 @@ Client::~Client()
http_done();
}
LoginStatus Client::Login(string username, string password)
LoginStatus Client::Login(string username, string password, User & user)
{
lastError = "";
std::stringstream urlStream;
std::stringstream hashStream;
unsigned char cHashData[16];
char passwordHash[33];
char totalHash[33];
//Build hash of username + password
struct md5_context md5;
md5_init(&md5);
md5_update(&md5, (unsigned char *)username.c_str(), username.length());
md5_update(&md5, (unsigned char *)"-", 1);
md5_update(&md5, (unsigned char *)password.c_str(), password.length());
md5_final(cHashData, &md5);
//Make sure hash is Hex
//char * cHashHex = (char *)malloc(33);
for(int i = 0; i < 16; i++)
{
hashStream << hexChars[cHashData[i]>>4] << hexChars[cHashData[i]&15];
//cHashHex[i*2] = hex[cHashData[i]>>4];
//cHashHex[i*2+1] = hex[cHashData[i]&15];
}
//cHashHex[32] = 0;
user.ID = 0;
user.Username = "";
user.SessionID = "";
user.SessionKey = "";
//Doop
md5_ascii(passwordHash, (const unsigned char *)password.c_str(), password.length());
passwordHash[32] = 0;
hashStream << username << "-" << passwordHash;
md5_ascii(totalHash, (const unsigned char *)(hashStream.str().c_str()), hashStream.str().length());
totalHash[32] = 0;
char * data;
int dataStatus, dataLength;
char * postNames[] = { "Username", "Hash", NULL };
char * postDatas[] = { (char*)username.c_str(), (char*)hashStream.str().c_str() };
int postLengths[] = { username.length(), hashStream.str().length() };
char * postDatas[] = { (char*)username.c_str(), totalHash };
int postLengths[] = { username.length(), 32 };
data = http_multipart_post("http://" SERVER "/Login.json", postNames, postDatas, postLengths, NULL, NULL, NULL, &dataStatus, &dataLength);
//data = http_auth_get("http://" SERVER "/Login.json", (char*)username.c_str(), (char*)password.c_str(), NULL, &dataStatus, &dataLength);
std::cout << data << std::endl;
@ -84,6 +80,13 @@ LoginStatus Client::Login(string username, string password)
free(data);
if(tempStatus.Value() == 1)
{
json::Number userIDTemp = objDocument["UserID"];
json::String sessionIDTemp = objDocument["SessionID"];
json::String sessionKeyTemp = objDocument["SessionKey"];
user.Username = username;
user.ID = userIDTemp.Value();
user.SessionID = sessionIDTemp.Value();
user.SessionKey = sessionKeyTemp.Value();
return LoginOkay;
}
else
@ -99,6 +102,10 @@ LoginStatus Client::Login(string username, string password)
return LoginError;
}
}
else
{
lastError = http_ret_text(dataStatus);
}
if(data)
{
free(data);

View File

@ -9,6 +9,7 @@
#include "search/Thumbnail.h"
#include "search/Save.h"
#include "Singleton.h"
#include "User.h"
enum LoginStatus
{
@ -28,7 +29,7 @@ private:
public:
Client();
~Client();
LoginStatus Login(string username, string password);
LoginStatus Login(string username, string password, User & user);
void ClearThumbnailRequests();
std::vector<Save*> * SearchSaves(int start, int count, string query, string sort, int & resultCount);
Thumbnail * GetPreview(int saveID, int saveDate);

View File

@ -208,7 +208,7 @@ void md5_transform(unsigned buf[4], const unsigned char inraw[64])
buf[3] += d;
}
static char hex[] = "0123456789abcdef";
static char hexChars[] = "0123456789abcdef";
void md5_ascii(char *result, unsigned char const *buf, unsigned len)
{
struct md5_context md5;
@ -224,8 +224,8 @@ void md5_ascii(char *result, unsigned char const *buf, unsigned len)
for (i=0; i<16; i++)
{
result[i*2] = hex[(hash[i]>>4)&0xF];
result[i*2+1] = hex[hash[i]&0x0F];
result[i*2] = hexChars[(hash[i]>>4)&0xF];
result[i*2+1] = hexChars[hash[i]&0x0F];
}
result[32] = 0;
}

29
src/client/User.h Normal file
View File

@ -0,0 +1,29 @@
/*
* User.h
*
* Created on: Jan 25, 2012
* Author: Simon
*/
#ifndef USER_H_
#define USER_H_
#include <string>
class User
{
public:
int ID;
std::string Username;
std::string SessionID;
std::string SessionKey;
User(int id, std::string username):
ID(id),
Username(username)
{
}
};
#endif /* USER_H_ */

View File

@ -11,9 +11,23 @@
using namespace std;
class GameController::LoginCallback: public ControllerCallback
{
GameController * cc;
public:
LoginCallback(GameController * cc_) { cc = cc_; }
virtual void ControllerExit()
{
cc->gameModel->SetUser(cc->loginWindow->GetUser());
delete cc->loginWindow;
cc->loginWindow = NULL;
}
};
GameController::GameController():
search(NULL),
renderOptions(NULL)
renderOptions(NULL),
loginWindow(NULL)
{
gameView = new GameView();
gameModel = new GameModel();
@ -135,7 +149,7 @@ void GameController::OpenSearch()
void GameController::OpenLogin()
{
loginWindow = new LoginController();
loginWindow = new LoginController(new LoginCallback(this));
ui::Engine::Ref().ShowWindow(loginWindow->GetView());
}

View File

@ -25,6 +25,7 @@ private:
RenderController * renderOptions;
LoginController * loginWindow;
public:
class LoginCallback;
GameController();
~GameController();
GameView * GetView();

View File

@ -11,7 +11,8 @@ GameModel::GameModel():
sim(NULL),
ren(NULL),
currentSave(NULL),
currentBrush(new Brush(ui::Point(4, 4)))
currentBrush(new Brush(ui::Point(4, 4))),
currentUser(0, "")
{
sim = new Simulation();
ren = new Renderer(ui::Engine::Ref().g, sim);
@ -132,6 +133,17 @@ Renderer * GameModel::GetRenderer()
return ren;
}
User GameModel::GetUser()
{
return currentUser;
}
void GameModel::SetUser(User user)
{
currentUser = user;
notifyUserChanged();
}
void GameModel::SetPaused(bool pauseState)
{
sim->sys_pause = pauseState?1:0;
@ -211,3 +223,11 @@ void GameModel::notifyActiveToolChanged()
observers[i]->NotifyActiveToolChanged(this);
}
}
void GameModel::notifyUserChanged()
{
for(int i = 0; i < observers.size(); i++)
{
observers[i]->NotifyUserChanged(this);
}
}

View File

@ -7,6 +7,7 @@
#include "Renderer.h"
#include "GameView.h"
#include "Brush.h"
#include "client/User.h"
#include "Tool.h"
#include "Menu.h"
@ -29,6 +30,7 @@ private:
Simulation * sim;
Renderer * ren;
Tool * activeTool;
User currentUser;
void notifyRendererChanged();
void notifySimulationChanged();
void notifyPausedChanged();
@ -37,6 +39,7 @@ private:
void notifyMenuListChanged();
void notifyToolListChanged();
void notifyActiveToolChanged();
void notifyUserChanged();
public:
GameModel();
~GameModel();
@ -53,7 +56,8 @@ public:
vector<Tool*> GetToolList();
void SetActiveMenu(Menu * menu);
Menu * GetActiveMenu();
User GetUser();
void SetUser(User user);
Simulation * GetSimulation();
Renderer * GetRenderer();
};

View File

@ -305,6 +305,18 @@ void GameView::NotifySimulationChanged(GameModel * sender)
{
}
void GameView::NotifyUserChanged(GameModel * sender)
{
if(!sender->GetUser().ID)
{
loginButton->SetText("Login");
}
else
{
loginButton->SetText(sender->GetUser().Username);
}
}
void GameView::NotifyPausedChanged(GameModel * sender)
{

View File

@ -48,6 +48,7 @@ public:
void NotifyMenuListChanged(GameModel * sender);
void NotifyToolListChanged(GameModel * sender);
void NotifyActiveToolChanged(GameModel * sender);
void NotifyUserChanged(GameModel * sender);
virtual void OnMouseMove(int x, int y, int dx, int dy);
virtual void OnMouseDown(int x, int y, unsigned button);
virtual void OnMouseUp(int x, int y, unsigned button);

View File

@ -18,7 +18,7 @@ Engine::Engine():
mousey_(0),
mousexp_(0),
mouseyp_(0),
FpsLimit(0.0f),
FpsLimit(60.0f),
windows(stack<Window*>()),
lastBuffer(NULL),
prevBuffers(stack<pixel*>()),

View File

@ -6,8 +6,9 @@
*/
#include "LoginController.h"
#include "client/User.h"
LoginController::LoginController() {
LoginController::LoginController(ControllerCallback * callback) {
// TODO Auto-generated constructor stub
loginView = new LoginView();
loginModel = new LoginModel();
@ -15,6 +16,8 @@ LoginController::LoginController() {
loginView->AttachController(this);
loginModel->AddObserver(loginView);
this->callback = callback;
}
void LoginController::Login(string username, string password)
@ -22,6 +25,11 @@ void LoginController::Login(string username, string password)
loginModel->Login(username, password);
}
User LoginController::GetUser()
{
return loginModel->GetUser();
}
void LoginController::Exit()
{
if(ui::Engine::Ref().GetWindow() == loginView)
@ -29,6 +37,8 @@ void LoginController::Exit()
ui::Engine::Ref().CloseWindow();
loginView = NULL;
}
if(callback)
callback->ControllerExit();
}
LoginController::~LoginController() {

View File

@ -11,6 +11,8 @@
#include <string>
#include "LoginView.h"
#include "LoginModel.h"
#include "Controller.h"
#include "client/User.h"
using namespace std;
@ -19,12 +21,13 @@ class LoginModel;
class LoginController {
LoginView * loginView;
LoginModel * loginModel;
ControllerCallback * callback;
public:
LoginController();
LoginController(ControllerCallback * callback = NULL);
void Login(string username, string password);
void Exit();
LoginView * GetView() { return loginView; }
User GetUser();
virtual ~LoginController();
};

View File

@ -7,7 +7,9 @@
#include "LoginModel.h"
LoginModel::LoginModel() {
LoginModel::LoginModel():
currentUser(0, "")
{
// TODO Auto-generated constructor stub
}
@ -17,7 +19,7 @@ void LoginModel::Login(string username, string password)
statusText = "Logging in...";
loginStatus = false;
notifyStatusChanged();
LoginStatus status = Client::Ref().Login(username, password);
LoginStatus status = Client::Ref().Login(username, password, currentUser);
switch(status)
{
case LoginOkay:
@ -41,6 +43,11 @@ string LoginModel::GetStatusText()
return statusText;
}
User LoginModel::GetUser()
{
return currentUser;
}
bool LoginModel::GetStatus()
{
return loginStatus;

View File

@ -21,12 +21,14 @@ class LoginModel {
string statusText;
bool loginStatus;
void notifyStatusChanged();
User currentUser;
public:
LoginModel();
void Login(string username, string password);
void AddObserver(LoginView * observer);
string GetStatusText();
bool GetStatus();
User GetUser();
virtual ~LoginModel();
};