very ugly spare your eyes
This commit is contained in:
parent
7258056e1c
commit
1d0a382eed
7 changed files with 51 additions and 36 deletions
|
@ -275,11 +275,6 @@ Communication between the master server and clients will be done over a WebSocke
|
||||||
<td>Password</td>
|
<td>Password</td>
|
||||||
<td>String</td>
|
<td>String</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
|
||||||
<td class="center">3</td>
|
|
||||||
<td>Server Id</td>
|
|
||||||
<td>Packed Unsigned Short</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<table style="margin-right: 8px; margin-bottom: 8px;">
|
<table style="margin-right: 8px; margin-bottom: 8px;">
|
||||||
|
|
|
@ -17,11 +17,14 @@ namespace sosc {
|
||||||
|
|
||||||
class MasterClient {
|
class MasterClient {
|
||||||
public:
|
public:
|
||||||
|
MasterClient() = delete;
|
||||||
explicit MasterClient(const ScapeConnection& client);
|
explicit MasterClient(const ScapeConnection& client);
|
||||||
bool Process(const db::Queries* queries);
|
bool Process(const db::Queries* queries);
|
||||||
|
|
||||||
bool Close();
|
bool Close();
|
||||||
bool Close(const Packet& message);
|
bool Close(const Packet& message);
|
||||||
|
|
||||||
|
~MasterClient();
|
||||||
private:
|
private:
|
||||||
enum MasterToClientId {
|
enum MasterToClientId {
|
||||||
kLoginResponse = 0,
|
kLoginResponse = 0,
|
||||||
|
@ -47,11 +50,11 @@ class MasterClientPool : public Pool<MasterClient, ctx::MasterClientContext> {
|
||||||
protected:
|
protected:
|
||||||
void SetupQueries(db::Queries* queries) override;
|
void SetupQueries(db::Queries* queries) override;
|
||||||
bool ProcessClient(
|
bool ProcessClient(
|
||||||
MasterClient& client,
|
MasterClient* client,
|
||||||
ctx::MasterClientContext* context,
|
ctx::MasterClientContext* context,
|
||||||
const db::Queries* queries) override
|
const db::Queries* queries) override
|
||||||
{
|
{
|
||||||
return client.Process(queries);
|
return client->Process(queries);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -59,11 +62,14 @@ protected:
|
||||||
|
|
||||||
class MasterIntra {
|
class MasterIntra {
|
||||||
public:
|
public:
|
||||||
|
MasterIntra() = delete;
|
||||||
explicit MasterIntra(const IntraClient& client);
|
explicit MasterIntra(const IntraClient& client);
|
||||||
bool Process(const db::Queries* queries);
|
bool Process(const db::Queries* queries);
|
||||||
|
|
||||||
bool Close();
|
bool Close();
|
||||||
bool Close(const Packet& message);
|
bool Close(const Packet& message);
|
||||||
|
|
||||||
|
~MasterIntra();
|
||||||
private:
|
private:
|
||||||
bool Authentication(Packet& pck);
|
bool Authentication(Packet& pck);
|
||||||
bool StatusUpdate(Packet& pck);
|
bool StatusUpdate(Packet& pck);
|
||||||
|
@ -99,11 +105,11 @@ class MasterIntraPool : public Pool<MasterIntra, ctx::MasterIntraContext> {
|
||||||
protected:
|
protected:
|
||||||
void SetupQueries(db::Queries* queries) override;
|
void SetupQueries(db::Queries* queries) override;
|
||||||
bool ProcessClient
|
bool ProcessClient
|
||||||
(MasterIntra& client,
|
(MasterIntra* client,
|
||||||
ctx::MasterIntraContext* context,
|
ctx::MasterIntraContext* context,
|
||||||
const db::Queries* queries) override
|
const db::Queries* queries) override
|
||||||
{
|
{
|
||||||
return client.Process(queries);
|
return client->Process(queries);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ bool master_intra(uint16_t port, const sosc::poolinfo_t& info) {
|
||||||
using namespace sosc;
|
using namespace sosc;
|
||||||
|
|
||||||
IntraServer server;
|
IntraServer server;
|
||||||
IntraClient client;
|
IntraClient* client;
|
||||||
if(!server.Listen(port))
|
if(!server.Listen(port))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ bool master_intra(uint16_t port, const sosc::poolinfo_t& info) {
|
||||||
pool.Configure(info);
|
pool.Configure(info);
|
||||||
pool.Start();
|
pool.Start();
|
||||||
|
|
||||||
while(server.Accept(&client))
|
while(server.Accept(client = new IntraClient()))
|
||||||
pool.AddClient(MasterIntra(client));
|
pool.AddClient(MasterIntra(client));
|
||||||
|
|
||||||
pool.Stop();
|
pool.Stop();
|
||||||
|
@ -60,7 +60,7 @@ bool master_client(uint16_t port, const sosc::poolinfo_t& info) {
|
||||||
using namespace sosc;
|
using namespace sosc;
|
||||||
|
|
||||||
ScapeServer server;
|
ScapeServer server;
|
||||||
ScapeConnection client;
|
ScapeConnection* client;
|
||||||
if(!server.Listen(port, true))
|
if(!server.Listen(port, true))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ bool master_client(uint16_t port, const sosc::poolinfo_t& info) {
|
||||||
pool.Configure(info);
|
pool.Configure(info);
|
||||||
pool.Start();
|
pool.Start();
|
||||||
|
|
||||||
while(server.Accept(&client))
|
while(server.Accept(client = new ScapeConnection()))
|
||||||
pool.AddClient(MasterClient(client));
|
pool.AddClient(MasterClient(client));
|
||||||
|
|
||||||
pool.Stop();
|
pool.Stop();
|
||||||
|
@ -79,7 +79,7 @@ bool slave(uint16_t port, const sosc::poolinfo_t& info) {
|
||||||
using namespace sosc;
|
using namespace sosc;
|
||||||
|
|
||||||
ScapeServer server;
|
ScapeServer server;
|
||||||
ScapeConnection client;
|
ScapeConnection* client;
|
||||||
if(!server.Listen(port))
|
if(!server.Listen(port))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ bool slave(uint16_t port, const sosc::poolinfo_t& info) {
|
||||||
pool.Configure(info);
|
pool.Configure(info);
|
||||||
pool.Start();
|
pool.Start();
|
||||||
|
|
||||||
while(server.Accept(&client))
|
while(server.Accept(client = new ScapeConnection()))
|
||||||
pool.AddClient(SlaveClient(client));
|
pool.AddClient(SlaveClient(client));
|
||||||
|
|
||||||
pool.Stop();
|
pool.Stop();
|
||||||
|
|
|
@ -72,7 +72,7 @@ private:
|
||||||
std::string body;
|
std::string body;
|
||||||
|
|
||||||
inline static bool IsValidOpCode(int op) {
|
inline static bool IsValidOpCode(int op) {
|
||||||
return !(op == 0x0 || op == 0x1 || op == 0x2
|
return (op == 0x0 || op == 0x1 || op == 0x2
|
||||||
|| op == 0x8 || op == 0x9 || op == 0xA);
|
|| op == 0x8 || op == 0x9 || op == 0xA);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,7 +36,7 @@ public:
|
||||||
void Configure(const poolinfo_t& info);
|
void Configure(const poolinfo_t& info);
|
||||||
|
|
||||||
void Start();
|
void Start();
|
||||||
bool AddClient(T client);
|
bool AddClient(T* client);
|
||||||
int ClientCount();
|
int ClientCount();
|
||||||
inline bool IsOpen() const {
|
inline bool IsOpen() const {
|
||||||
return this->is_open;
|
return this->is_open;
|
||||||
|
@ -46,7 +46,7 @@ public:
|
||||||
protected:
|
protected:
|
||||||
virtual void SetupQueries(db::Queries* queries) {};
|
virtual void SetupQueries(db::Queries* queries) {};
|
||||||
virtual bool ProcessClient
|
virtual bool ProcessClient
|
||||||
(T& client, U* context, const db::Queries* queries) = 0;
|
(T* client, U* context, const db::Queries* queries) = 0;
|
||||||
private:
|
private:
|
||||||
bool IsStackFull(int stackCount) const;
|
bool IsStackFull(int stackCount) const;
|
||||||
bool CanAddStack() const;
|
bool CanAddStack() const;
|
||||||
|
@ -58,7 +58,7 @@ private:
|
||||||
|
|
||||||
void Start();
|
void Start();
|
||||||
|
|
||||||
void AddClient(T client);
|
void AddClient(T* client);
|
||||||
int ClientCount();
|
int ClientCount();
|
||||||
inline bool IsOpen() const {
|
inline bool IsOpen() const {
|
||||||
return this->is_open;
|
return this->is_open;
|
||||||
|
@ -74,7 +74,7 @@ private:
|
||||||
bool is_open;
|
bool is_open;
|
||||||
bool is_running;
|
bool is_running;
|
||||||
|
|
||||||
std::list<T> clients;
|
std::list<T*> clients;
|
||||||
std::mutex clients_mtx;
|
std::mutex clients_mtx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ bool Pool<T,U>::CanAddStack() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T, class U>
|
template<class T, class U>
|
||||||
bool Pool<T,U>::AddClient(T client) {
|
bool Pool<T,U>::AddClient(T* client) {
|
||||||
if(!this->is_open)
|
if(!this->is_open)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -214,7 +214,7 @@ void Pool<T,U>::Stack::Start() {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T, class U>
|
template<class T, class U>
|
||||||
void Pool<T,U>::Stack::AddClient(T client) {
|
void Pool<T,U>::Stack::AddClient(T* client) {
|
||||||
if(!this->is_open || !this->is_running)
|
if(!this->is_open || !this->is_running)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -248,9 +248,10 @@ void Pool<T,U>::Stack::StackThread() {
|
||||||
|
|
||||||
this->clients_mtx.lock();
|
this->clients_mtx.lock();
|
||||||
if(!this->pool->ProcessClient
|
if(!this->pool->ProcessClient
|
||||||
(*client, &this->pool->context, &this->queries))
|
(client, &this->pool->context, &this->queries))
|
||||||
{
|
{
|
||||||
this->clients.erase(client);
|
this->clients.erase(client);
|
||||||
|
delete client;
|
||||||
}
|
}
|
||||||
this->clients_mtx.unlock();
|
this->clients_mtx.unlock();
|
||||||
}
|
}
|
||||||
|
@ -269,8 +270,11 @@ void Pool<T,U>::Stack::Stop() {
|
||||||
|
|
||||||
this->is_running = false;
|
this->is_running = false;
|
||||||
this->thread->join();
|
this->thread->join();
|
||||||
|
|
||||||
delete this->thread;
|
delete this->thread;
|
||||||
|
|
||||||
|
for(auto client : this->clients)
|
||||||
|
delete client;
|
||||||
|
|
||||||
this->is_open = false;
|
this->is_open = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,9 +12,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div id="login" class="hidden section">
|
<div id="login" class="hidden section">
|
||||||
<h1>Log In</h1>
|
<h1>Log In</h1>
|
||||||
<div class="error">
|
<div class="hidden error"></div>
|
||||||
test error test test error test test error test test error test test error test test error test test error test test error test test error test
|
|
||||||
</div>
|
|
||||||
<label>
|
<label>
|
||||||
Username: <br/>
|
Username: <br/>
|
||||||
<input type="text" id="login-user" />
|
<input type="text" id="login-user" />
|
||||||
|
@ -23,7 +21,7 @@
|
||||||
Password: <br/>
|
Password: <br/>
|
||||||
<input type="password" id="login-pwd" />
|
<input type="password" id="login-pwd" />
|
||||||
</label>
|
</label>
|
||||||
<input type="button" value="Log In" />
|
<input type="button" value="Log In" onclick="attempt_login();" />
|
||||||
<input type="button" value="Register" onclick="show_section('register');" />
|
<input type="button" value="Register" onclick="show_section('register');" />
|
||||||
</div>
|
</div>
|
||||||
<div id="register" class="hidden section">
|
<div id="register" class="hidden section">
|
||||||
|
|
|
@ -21,19 +21,27 @@ let receive_callbacks = {};
|
||||||
function attempt_login() {
|
function attempt_login() {
|
||||||
let section = document.getElementById("login");
|
let section = document.getElementById("login");
|
||||||
let error = section.getElementsByClassName("error");
|
let error = section.getElementsByClassName("error");
|
||||||
let buttons = filter(
|
let lock_fields = filter(
|
||||||
to_array(section.getElementsByTagName("input")),
|
to_array(section.getElementsByTagName("input")),
|
||||||
x => (x.type === "button" || x.type === "submit")
|
x => ["submit", "button", "text", "password"].indexOf(x.type) !== -1
|
||||||
);
|
);
|
||||||
|
|
||||||
for(let i in buttons)
|
for_each(lock_fields, x => x.disabled = true);
|
||||||
buttons[i].disabled = true;
|
ws.send(pack(
|
||||||
|
kClientToMaster.LoginRequest, [
|
||||||
|
document.getElementById("login-user").value,
|
||||||
|
document.getElementById("login-pwd").value
|
||||||
|
]
|
||||||
|
));
|
||||||
|
|
||||||
receive_callbacks[kMasterToClient.LoginResponse] = (pck) => {
|
receive_callbacks[kMasterToClient.LoginResponse] = pck => {
|
||||||
|
if(pck.regions[0][0] === 0) {
|
||||||
|
error.innerHTML = "Username or password was incorrect."
|
||||||
|
} else {
|
||||||
|
alert("allo");
|
||||||
|
}
|
||||||
|
|
||||||
|
for_each(lock_fields, x => x.disabled = false);
|
||||||
for(let i in buttons)
|
|
||||||
buttons[i].disabled = false;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -578,4 +586,8 @@ function filter(x, f) {
|
||||||
|
|
||||||
function to_array(x) {
|
function to_array(x) {
|
||||||
return Array.prototype.slice.call(x);
|
return Array.prototype.slice.call(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
function concat(x, y) {
|
||||||
|
return Array.prototype.concat.call(x, y);
|
||||||
}
|
}
|
Loading…
Reference in a new issue