Add user interface

This commit is contained in:
hiimgoodpack 2021-03-25 19:59:07 -04:00
parent fff4e5b3f3
commit 859e1b2835
Signed by untrusted user: hiimgoodpack
GPG key ID: 4E0E62733C14AE69
6 changed files with 668 additions and 54 deletions

View file

@ -15,7 +15,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
CPPFLAGS=-lstdc++ -lm -lpthread -lcurl -std=c++11 -ggdb -Wall CPPFLAGS=-lstdc++ -lm -lpthread -lcurl -ljsoncpp -std=c++11 -ggdb -Wall -march=native
CXX=ccache clang CXX=ccache clang
all: main all: main
@ -24,8 +24,10 @@ websocket.o: websocket.h
$(CXX) $(CPPFLAGS) -x c++ -DWEBSOCKET_IMPLEMENTATION -c -o websocket.o websocket.h $(CXX) $(CPPFLAGS) -x c++ -DWEBSOCKET_IMPLEMENTATION -c -o websocket.o websocket.h
http.o: http.h http.o: http.h
$(CXX) $(CPPFLAGS) -x c++ -DHTTP_IMPLEMENTATION -c -o http.o http.h $(CXX) $(CPPFLAGS) -x c++ -DHTTP_IMPLEMENTATION -c -o http.o http.h
brainlet.o: brainlet.h
$(CXX) $(CPPFLAGS) -x c++ -DBRAINLET_IMPLEMENTATION -c -o brainlet.o brainlet.h
main.o: main.cpp main.o: main.cpp
$(CXX) $(CPPFLAGS) -c -o main.o main.cpp $(CXX) $(CPPFLAGS) -I/usr/include/gtkmm-3.0 -I/usr/lib/gtkmm-3.0/include -I/usr/include/giomm-2.4 -I/usr/lib/giomm-2.4/include -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glibmm-2.4 -I/usr/lib/glibmm-2.4/include -I/usr/include/sigc++-2.0 -I/usr/lib/sigc++-2.0/include -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/lzo -I/usr/include/pixman-1 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cloudproviders -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -I/usr/include/at-spi-2.0 -I/usr/include/cairomm-1.0 -I/usr/lib/cairomm-1.0/include -I/usr/include/pangomm-1.4 -I/usr/lib/pangomm-1.4/include -I/usr/include/atkmm-1.6 -I/usr/lib/atkmm-1.6/include -I/usr/include/gtk-3.0/unix-print -I/usr/include/gdkmm-3.0 -I/usr/lib/gdkmm-3.0/include -c -o main.o main.cpp
main: websocket.o http.o main.o main: websocket.o http.o brainlet.o main.o
$(CXX) $(CPPFLAGS) websocket.o http.o main.o $(CXX) $(CPPFLAGS) -lgtkmm-3.0 -latkmm-1.6 -lgdkmm-3.0 -lgiomm-2.4 -lgtk-3 -lgdk-3 -lz -latk-1.0 -lcairo-gobject -lgio-2.0 -lpangomm-1.4 -lglibmm-2.4 -lcairomm-1.0 -lsigc-2.0 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -lcairo -lgdk_pixbuf-2.0 -lgobject-2.0 -lglib-2.0 websocket.o http.o brainlet.o main.o

236
brainlet.h Normal file
View file

@ -0,0 +1,236 @@
// Copyright (c) 2021 hiimgoodpack
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef BRAINLET_H
#define BRAINLET_H
#include <string>
#include <memory> // std::unique_ptr
#include <cstdint>
#include <functional>
#include "websocket.h"
namespace Brainlet {
enum Opcode {
HELLO = 0,
YOO = 1,
YOO_ACK = 2,
ACTION_CREATE_MESSAGE = 3,
EVENT_NEW_MESSAGE = 4,
ACTION_UPDATE_STATUS = 5,
EVENT_CHANNEL_MEMBERS = 6,
EVENT_CHANNEL_MEMBER_UPDATE = 7,
};
const std::size_t minChannelNameLength = 3;
const std::size_t maxChannelNameLength = 32;
const std::size_t idLength = 24;
typedef struct {
std::string id;
std::string name;
} Channel;
typedef struct {
std::string authorId;
std::string message;
} Message;
typedef struct {
std::string id;
std::string name;
std::string color; ///< Color represented in #rrggbb
} User;
class Client : private User {
private:
enum State {
NOT_CONNECTED,
WAIT_HELLO,
WAIT_ACK,
READY
};
std::string domain;
std::string token;
std::unique_ptr<WebSocket> socket = nullptr;
State state = NOT_CONNECTED;
static void onReceive(Client* client, std::string buffer);
public:
std::function<void(Channel)> onNewChannel = nullptr;
std::function<void(Message, Channel)> onNewMessage = nullptr;
Client(const std::string& domain);
void processNextEvent();
void login(const std::string& name, const std::string& password);
void sendMessage(const std::string& message, const std::string& channelId);
};
}
#endif
#if defined(BRAINLET_IMPLEMENTATION) && !defined(BRAINLET_IMPLEMENTED)
#include "http.h"
#include <cstring> // std::strncmp
#include <stdexcept>
#include <iostream>
#include <sstream> // std::istringstream, std::ostringstream
#include <cstdio> // std::snprintf
#include <chrono>
#include <stdexcept>
#include <errno.h>
#include <cassert>
#include <cstdlib> // std;:strtol
#include <algorithm> // std::for_each
#include <string.h> // strerror
#include <json/json.h>
namespace Brainlet {
Client::Client(const std::string& domain) : domain(domain) {}
void Client::processNextEvent() {
socket->run_until(
// Stop in 250 microseconds
std::chrono::system_clock::now() +
std::chrono::microseconds(250)
);
}
void Client::login(const std::string& name, const std::string& password) {
Http::Request request("http://" + domain + "/api/v1/users/token/create");
// TODO: Escape &
const std::string postFields = "username="+name+"&password="+password+"&alsoSetCookie=true";
request.setPostFields(postFields);
request.perform();
Json::Value value;
std::istringstream(request.buffer) >> value;
bool didError = value.get("error", true).asBool();
if (didError) {
// TODO: Use exception
std::cerr << "Error while logging in: " << value.get("message", "Server did not specify error").asString() << "\n";
return;
}
token = value.get("token", "Unknown token").asString();
id = value["user"].get("id", "0").asString();
color = value["user"].get("color", "#ffffff").asString();
socket.reset(new WebSocket("ws://" + domain + "/gateway"));
state = WAIT_HELLO;
socket->onReceive = std::bind(onReceive, this, std::placeholders::_1);
}
void Client::onReceive(Client* client, std::string buffer) {
Json::Value data;
Opcode opcode;
{
char* characterAfterType = nullptr;
errno = 0;
opcode = (Opcode)std::strtol(buffer.c_str(), &characterAfterType, 10);
if (errno != 0) {
// TODO: Use exception
std::cerr << "Failed to get opcode: " << strerror(errno) << "\n";
return;
}
// Skip opcode number and ending @
unsigned int charactersToSkip = characterAfterType - buffer.c_str() + 1;
std::istringstream stream(buffer);
for (unsigned int i = 0; i < charactersToSkip; i++) {
char _; stream >> _;
}
stream >> data;
}
switch (client->state) {
case NOT_CONNECTED: throw std::logic_error("Buffer was received while client is not connected");
case WAIT_HELLO: {
if (opcode != HELLO) {
std::cerr << "Failed authentication: Expected HELLO packet, received: " << buffer << "\n";
client->socket->close();
return;
}
client->state = WAIT_ACK;
char bufferToSend[256];
std::snprintf(bufferToSend, sizeof(bufferToSend), R"(%i@{"token":"%s"})",
YOO, client->token.c_str());
client->socket->send(bufferToSend);
break;
}
case WAIT_ACK: {
if (opcode != YOO_ACK) {
std::cerr << "Failed authentication: Expected YOO_ACK packet, received: " << buffer << "\n";
client->socket->close();
return;
}
client->state = READY;
const Json::Value channels = data["channels"];
for (Json::ArrayIndex i = 0; i < channels.size(); ++i) {
const Json::Value channelValue = channels[i];
const Channel channelObject = {
.id = channelValue["_id"].asString(),
.name = channelValue["title"].asString()
};
if (client->onNewChannel)
client->onNewChannel(channelObject);
}
}
case READY: {
switch (opcode) {
case EVENT_NEW_MESSAGE: {
Channel channel = {
.id = data["channel"]["_id"].asString(),
.name = data["channel"]["title"].asString()
};
Message message = {
.authorId = data["author"]["_id"].asString(),
.message = data["content"].asString()
};
if (client->onNewMessage)
client->onNewMessage(message, channel);
}
default: {
std::cerr << "Unknown opcode " << opcode << ". Buffer " << buffer << "\n";
break;
}
}
}
}
}
void Client::sendMessage(const std::string& message, const std::string& channelId) {
std::ostringstream bufferToSend;
// TODO: Escape " from message
bufferToSend << ACTION_CREATE_MESSAGE << "@" << R"({"content":")" << message << R"(","channel":{"_id":")" << channelId << R"("}})";
socket->send(bufferToSend.str());
}
}
#endif

288
design.glade Normal file
View file

@ -0,0 +1,288 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021 hiimgoodpack
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkPaned" id="Chat">
<property name="visible">True</property>
<property name="can-focus">True</property>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="vexpand">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox" id="Channels">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
</packing>
</child>
<child>
<!-- n-columns=3 n-rows=3 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="vexpand">True</property>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBox" id="Messages">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkListBoxRow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">No channel has been opened.
Select a channel on the box to the left.</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="width">3</property>
<property name="height">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="Message">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="hexpand">True</property>
<property name="placeholder-text" translatable="yes">Message</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">2</property>
<property name="width">3</property>
</packing>
</child>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
</packing>
</child>
</object>
<object class="GtkMessageDialog" id="Error">
<property name="can-focus">False</property>
<property name="type-hint">dialog</property>
<property name="buttons">ok</property>
<property name="text" translatable="yes">An error has occured</property>
<property name="secondary-text" translatable="yes">[Error message goes here]</property>
</object>
<!-- n-columns=3 n-rows=5 -->
<object class="GtkGrid" id="Login">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="label" translatable="yes">Log into Brainlet</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="width">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="label" translatable="yes">Domain:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="Domain">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="placeholder-text" translatable="yes">example.com</property>
<property name="input-purpose">url</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="label" translatable="yes">Username:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="Username">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="max-length">32</property>
<property name="width-chars">32</property>
<property name="caps-lock-warning">False</property>
<property name="input-purpose">name</property>
<property name="input-hints">GTK_INPUT_HINT_LOWERCASE | GTK_INPUT_HINT_UPPERCASE_CHARS | GTK_INPUT_HINT_NONE</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="label" translatable="yes">Password:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="Password">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="hexpand">True</property>
<property name="max-length">128</property>
<property name="visibility">False</property>
<property name="width-chars">32</property>
<property name="input-purpose">password</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">3</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="LoginButton">
<property name="label" translatable="yes">Log in</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">4</property>
<property name="width">3</property>
</packing>
</child>
</object>
<object class="GtkWindow" id="Window">
<property name="can-focus">False</property>
<property name="title" translatable="yes">Brainlet</property>
<child>
<placeholder/>
</child>
</object>
</interface>

7
http.h
View file

@ -32,6 +32,7 @@ namespace Http {
class Request { class Request {
private: private:
CURL* handle; CURL* handle;
std::string url;
std::string postFields; std::string postFields;
static std::size_t write(void* buffer, std::size_t blocks, std::size_t blockSize, std::string* result); static std::size_t write(void* buffer, std::size_t blocks, std::size_t blockSize, std::string* result);
@ -40,7 +41,7 @@ namespace Http {
std::string buffer; std::string buffer;
long int status; long int status;
Request(const char* const url); Request(const std::string& url);
void setPostFields(std::string fields); void setPostFields(std::string fields);
void perform(); void perform();
~Request(); ~Request();
@ -53,10 +54,10 @@ namespace Http {
#define HTTP_IMPLEMENTED #define HTTP_IMPLEMENTED
namespace Http { namespace Http {
Request::Request(const char* const url) { Request::Request(const std::string& newUrl) : url(newUrl) {
handle = curl_easy_init(); handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status); curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write);

163
main.cpp
View file

@ -17,50 +17,135 @@
#include "http.h" #include "http.h"
#include "websocket.h" #include "websocket.h"
#include "brainlet.h"
#include <cstring> // std::strcpy
#include <memory> // std::unique_ptr
#include <algorithm> // std::for_each
#include <gtkmm.h>
#include <unordered_map>
#include <vector>
#include <string> #include <string>
#include <cstring> // std::strncmp
#include <cstdint>
#include <stdexcept>
#include <iostream>
enum State { std::unique_ptr<Brainlet::Client> client = nullptr;
WAIT_HELLO,
WAIT_ACK, std::unordered_map<Gtk::ListBoxRow*, std::string> channelRowToId;
READY std::unordered_map<std::string, std::vector<Brainlet::Message>> messages;
}; std::string currentChannelId;
Gtk::Window* window;
Gtk::Widget* loginInterface;
Gtk::Entry* domainEntry;
Gtk::Entry* usernameEntry;
Gtk::Entry* passwordEntry;
Gtk::Button* loginButton;
Gtk::Widget* chatInterface;
Gtk::ListBox* channelList;
Gtk::ListBox* messageList;
Gtk::Entry* messageEntry;
gboolean update(gpointer) {
client->processNextEvent();
return G_SOURCE_CONTINUE;
}
void addChannel(const Brainlet::Channel channel) {
auto row = Gtk::make_managed<Gtk::ListBoxRow>();
auto name = Gtk::make_managed<Gtk::Label>();
name->set_halign(Gtk::ALIGN_START);
name->set_text(channel.name);
row->add(*name);
channelList->add(*row);
row->show_all_children();
row->show();
channelRowToId.emplace(row, channel.id);
messages.emplace(
std::piecewise_construct,
std::forward_as_tuple(channel.id),
std::forward_as_tuple()
);
}
void addMessage(const Brainlet::Message message) {
auto row = Gtk::make_managed<Gtk::ListBoxRow>();
auto text = Gtk::make_managed<Gtk::Label>();
text->set_halign(Gtk::ALIGN_START);
std::string messageFormatted = message.authorId + ": " + message.message;
text->set_text(messageFormatted.c_str());
row->add(*text);
messageList->add(*row);
row->show_all_children();
row->show();
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
Http::Request tokenRequest("http://localhost:3005/api/v1/users/token/create"); Glib::RefPtr<Gtk::Application> app = Gtk::Application::create("org.hiimgoodpack.brainlet_client");
tokenRequest.setPostFields("username=TestUser&password=12345678&alsoSetCookie=true}"); Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("./design.glade");
tokenRequest.perform();
std::cout << tokenRequest.status << " " << tokenRequest.buffer; builder->get_widget("Window", window);
WebSocket socket("ws://localhost:3005/gateway");
State state = WAIT_HELLO; builder->get_widget("Login", loginInterface);
socket.onReceive = [&](std::string message) { builder->get_widget("Domain", domainEntry);
std::cout << "Received: " << message << "\n"; builder->get_widget("Username", usernameEntry);
switch (state) { builder->get_widget("Password", passwordEntry);
case WAIT_HELLO: { builder->get_widget("LoginButton", loginButton);
if (std::strncmp(message.c_str(), "0@", 2) == 0) {
state = WAIT_ACK; builder->get_widget("Chat", chatInterface);
socket.send("1@{\"token\":\"my totally real token\""); builder->get_widget("Channels", channelList);
} else { builder->get_widget("Messages", messageList);
std::cerr << "Failed authentication: Expected packet type 0 (HELLO), received buffer: " << message << "\n"; builder->get_widget("Message", messageEntry);
socket.close();
} auto login = [&]() {
break; if (client.get() != nullptr) return;
} window->remove();
case WAIT_ACK: { window->add(*chatInterface);
if (std::strncmp(message.c_str(), "2@", 2) == 0) {
state = READY; client.reset(new Brainlet::Client(domainEntry->get_text()));
} else { client->login(usernameEntry->get_text(), passwordEntry->get_text());
std::cerr << "Failed authentication: Expected packet type 2 (YOO_ACK), received buffer: " << message << "\n";
socket.close(); client->onNewChannel = addChannel;
} client->onNewMessage = [&](const Brainlet::Message message, const Brainlet::Channel channel) {
} messages.at(channel.id).push_back(message);
case READY: break; if (channel.id == currentChannelId)
} addMessage(message);
};
g_timeout_add((1.0f/5.0f)*1000.0f, update, nullptr);
}; };
socket.run(); passwordEntry->signal_activate().connect(login);
loginButton->signal_clicked().connect(login);
channelList->signal_row_activated().connect([&](Gtk::ListBoxRow* row) {
// Clear messages in messageList
messageList->foreach(std::bind(std::mem_fn(&Gtk::ListBox::remove), messageList, std::placeholders::_1));
// Add messages in the channel selected
currentChannelId = channelRowToId.at(row);
const std::vector<Brainlet::Message>& channelMessages = messages.at(currentChannelId);
std::for_each(channelMessages.begin(), channelMessages.end(), addMessage);
});
messageEntry->signal_activate().connect([&]() {
// TODO: Show message locally first to avoid latency
// Also show some sort of unread indicator
client->sendMessage(messageEntry->get_text(), currentChannelId);
messageEntry->set_text("");
});
window->add(*loginInterface);
app->run(*window);
} }

View file

@ -32,7 +32,7 @@ class WebSocketError : public std::runtime_error {
using runtime_error::runtime_error; using runtime_error::runtime_error;
}; };
class WebSocket { class WebSocket : public websocketpp::lib::asio::io_service {
private: private:
websocketpp::client<websocketpp::config::asio_client> client; websocketpp::client<websocketpp::config::asio_client> client;
websocketpp::connection_hdl hdl; websocketpp::connection_hdl hdl;
@ -42,8 +42,7 @@ class WebSocket {
static void onCloseHandler(WebSocket*); static void onCloseHandler(WebSocket*);
public: public:
WebSocket(const char* const url); WebSocket(const std::string& url);
void run();
void send(std::string buffer); void send(std::string buffer);
void close(); void close();
@ -58,9 +57,13 @@ class WebSocket {
#define WEBSOCKET_IMPLEMENTED #define WEBSOCKET_IMPLEMENTED
#include <websocketpp/client.hpp> #include <websocketpp/client.hpp>
#include <iostream>
WebSocket::WebSocket(const char* const url) { WebSocket::WebSocket(const std::string& url) {
client.init_asio(); client.clear_access_channels(websocketpp::log::alevel::all);
client.clear_error_channels(websocketpp::log::elevel::all);
client.init_asio(reinterpret_cast<websocketpp::lib::asio::io_service*>(this));
client.set_open_handler(std::bind(onConnectHandler, this, std::placeholders::_1)); client.set_open_handler(std::bind(onConnectHandler, this, std::placeholders::_1));
client.set_message_handler(std::bind(onReceiveHandler, this, std::placeholders::_2)); client.set_message_handler(std::bind(onReceiveHandler, this, std::placeholders::_2));
@ -79,6 +82,7 @@ void WebSocket::onConnectHandler(WebSocket* socket, websocketpp::connection_hdl
socket->onConnect(); socket->onConnect();
} }
void WebSocket::onReceiveHandler(WebSocket* socket, websocketpp::config::asio_client::message_type::ptr message) { void WebSocket::onReceiveHandler(WebSocket* socket, websocketpp::config::asio_client::message_type::ptr message) {
std::cerr << "Received: " << message->get_payload() << "\n";
if (socket->onReceive) if (socket->onReceive)
socket->onReceive(message->get_payload()); socket->onReceive(message->get_payload());
} }
@ -87,10 +91,8 @@ void WebSocket::onCloseHandler(WebSocket* socket) {
socket->onClose(); socket->onClose();
} }
void WebSocket::run() {
client.run();
}
void WebSocket::send(std::string buffer) { void WebSocket::send(std::string buffer) {
std::cerr << "Sending: " << buffer << "\n";
client.send(hdl, buffer, websocketpp::frame::opcode::text); client.send(hdl, buffer, websocketpp::frame::opcode::text);
} }
void WebSocket::close() { void WebSocket::close() {