Skip to content

Commit

Permalink
Use different sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Oct 12, 2023
1 parent 54fb509 commit fe39416
Show file tree
Hide file tree
Showing 16 changed files with 382 additions and 211 deletions.
4 changes: 3 additions & 1 deletion Car-Application.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
"random": "cpp",
"string_view": "cpp",
"numbers": "cpp",
"semaphore": "cpp"
"semaphore": "cpp",
"set": "cpp",
"unordered_set": "cpp"
}
}
}
16 changes: 13 additions & 3 deletions backend/.vscode/compile_commands.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
[
{
"directory": "/home/pass_is_1/Car-Application/backend",
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-fvisibility=hidden", "-fvisibility-inlines-hidden", "-O3", "-std=c++17", "-Iinclude", "-I", "/home/pass_is_1/.xmake/packages/f/fmt/10.1.1/f6aacaa0e06a42ca8786f360cc1d31c5/include", "-I", "/home/pass_is_1/.xmake/packages/s/spdlog/v1.12.0/e24126f106564f7c97234caa2b9c81c5/include", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp/1.3.0/0f4509c6b55d49128c21c834ef94e6a0/include/oatpp-1.3.0/oatpp", "-I", "/home/pass_is_1/.xmake/packages/t/tl_expected/v1.1.0/57774aec28a24eb8b84e9c45d2905d73/include", "-DNDEBUG", "-o", "build/.objs/backend/linux/x86_64/release/src/websocket/WSListener.cpp.o", "src/websocket/WSListener.cpp"],
"file": "src/websocket/WSListener.cpp"
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-fvisibility=hidden", "-fvisibility-inlines-hidden", "-O3", "-std=c++17", "-Iinclude", "-I", "/home/pass_is_1/.xmake/packages/f/fmt/10.1.1/f6aacaa0e06a42ca8786f360cc1d31c5/include", "-I", "/home/pass_is_1/.xmake/packages/s/spdlog/v1.12.0/e24126f106564f7c97234caa2b9c81c5/include", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp-websocket/1.3.0/74ca6ba37ddc4ed596561e2625c0b306/include/oatpp-1.3.0/oatpp-websocket", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp/1.3.0/0f4509c6b55d49128c21c834ef94e6a0/include/oatpp-1.3.0/oatpp", "-I", "/home/pass_is_1/.xmake/packages/t/tl_expected/v1.1.0/57774aec28a24eb8b84e9c45d2905d73/include", "-DNDEBUG", "-o", "build/.objs/backend/linux/x86_64/release/src/rooms/Peer.cpp.o", "src/rooms/Peer.cpp"],
"file": "src/rooms/Peer.cpp"
},
{
"directory": "/home/pass_is_1/Car-Application/backend",
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-fvisibility=hidden", "-fvisibility-inlines-hidden", "-O3", "-std=c++17", "-Iinclude", "-I", "/home/pass_is_1/.xmake/packages/f/fmt/10.1.1/f6aacaa0e06a42ca8786f360cc1d31c5/include", "-I", "/home/pass_is_1/.xmake/packages/s/spdlog/v1.12.0/e24126f106564f7c97234caa2b9c81c5/include", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp/1.3.0/0f4509c6b55d49128c21c834ef94e6a0/include/oatpp-1.3.0/oatpp", "-I", "/home/pass_is_1/.xmake/packages/t/tl_expected/v1.1.0/57774aec28a24eb8b84e9c45d2905d73/include", "-DNDEBUG", "-o", "build/.objs/backend/linux/x86_64/release/src/App.cpp.o", "src/App.cpp"],
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-fvisibility=hidden", "-fvisibility-inlines-hidden", "-O3", "-std=c++17", "-Iinclude", "-I", "/home/pass_is_1/.xmake/packages/f/fmt/10.1.1/f6aacaa0e06a42ca8786f360cc1d31c5/include", "-I", "/home/pass_is_1/.xmake/packages/s/spdlog/v1.12.0/e24126f106564f7c97234caa2b9c81c5/include", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp-websocket/1.3.0/74ca6ba37ddc4ed596561e2625c0b306/include/oatpp-1.3.0/oatpp-websocket", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp/1.3.0/0f4509c6b55d49128c21c834ef94e6a0/include/oatpp-1.3.0/oatpp", "-I", "/home/pass_is_1/.xmake/packages/t/tl_expected/v1.1.0/57774aec28a24eb8b84e9c45d2905d73/include", "-DNDEBUG", "-o", "build/.objs/backend/linux/x86_64/release/src/rooms/Lobby.cpp.o", "src/rooms/Lobby.cpp"],
"file": "src/rooms/Lobby.cpp"
},
{
"directory": "/home/pass_is_1/Car-Application/backend",
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-fvisibility=hidden", "-fvisibility-inlines-hidden", "-O3", "-std=c++17", "-Iinclude", "-I", "/home/pass_is_1/.xmake/packages/f/fmt/10.1.1/f6aacaa0e06a42ca8786f360cc1d31c5/include", "-I", "/home/pass_is_1/.xmake/packages/s/spdlog/v1.12.0/e24126f106564f7c97234caa2b9c81c5/include", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp-websocket/1.3.0/74ca6ba37ddc4ed596561e2625c0b306/include/oatpp-1.3.0/oatpp-websocket", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp/1.3.0/0f4509c6b55d49128c21c834ef94e6a0/include/oatpp-1.3.0/oatpp", "-I", "/home/pass_is_1/.xmake/packages/t/tl_expected/v1.1.0/57774aec28a24eb8b84e9c45d2905d73/include", "-DNDEBUG", "-o", "build/.objs/backend/linux/x86_64/release/src/rooms/Room.cpp.o", "src/rooms/Room.cpp"],
"file": "src/rooms/Room.cpp"
},
{
"directory": "/home/pass_is_1/Car-Application/backend",
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-fvisibility=hidden", "-fvisibility-inlines-hidden", "-O3", "-std=c++17", "-Iinclude", "-I", "/home/pass_is_1/.xmake/packages/f/fmt/10.1.1/f6aacaa0e06a42ca8786f360cc1d31c5/include", "-I", "/home/pass_is_1/.xmake/packages/s/spdlog/v1.12.0/e24126f106564f7c97234caa2b9c81c5/include", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp-websocket/1.3.0/74ca6ba37ddc4ed596561e2625c0b306/include/oatpp-1.3.0/oatpp-websocket", "-I", "/home/pass_is_1/.xmake/packages/o/oatpp/1.3.0/0f4509c6b55d49128c21c834ef94e6a0/include/oatpp-1.3.0/oatpp", "-I", "/home/pass_is_1/.xmake/packages/t/tl_expected/v1.1.0/57774aec28a24eb8b84e9c45d2905d73/include", "-DNDEBUG", "-o", "build/.objs/backend/linux/x86_64/release/src/App.cpp.o", "src/App.cpp"],
"file": "src/App.cpp"
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
add_rules("mode.debug", "mode.release")

add_requires("nlohmann_json")

target("json-schema-validator")
set_kind("$(kind)")
set_languages("cxx17")
add_packages("nlohmann_json")

add_headerfiles("src/(nlohmann/json-schema.hpp)")
add_includedirs("src", { public = true })

add_files("src/**.cpp")
add_headerfiles("src/*.hpp")

if is_plat("windows") then
add_defines("_WIN32")
end
33 changes: 33 additions & 0 deletions backend/repository/packages/j/json-schema-validator-/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package("json-schema-validator-")
set_homepage("https://github.com/pboettch/json-schema-validator")
set_description("JSON schema validator for JSON for Modern C++")

add_urls("https://github.com/pboettch/json-schema-validator/archive/refs/tags/$(version).tar.gz",
"https://github.com/pboettch/json-schema-validator.git")
add_versions("2.1.0", "83f61d8112f485e0d3f1e72d51610ba3924b179926a8376aef3c038770faf202")

add_deps("nlohmann_json")

on_install(function (package)
os.cp(path.join(package:scriptdir(), "port", "xmake.lua"), "xmake.lua")
local configs = {}
if package:config("shared") then
package:add("defines", "JSON_SCHEMA_VALIDATOR_EXPORTS")
end
import("package.tools.xmake").install(package, configs)
end)

on_test(function (package)
assert(package:check_cxxsnippets({test = [[
#include <nlohmann/json-schema.hpp>
using nlohmann::json;
using nlohmann::json_schema::json_validator;
static json person_schema = R"({})"_json;
static void test() {
json_validator validator;
validator.set_root_schema(person_schema);
}
]]}, {configs = {languages = "c++17"}}))
end)
4 changes: 0 additions & 4 deletions backend/src/App.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "./controller/MyController.hpp"
#include "./AppComponent.hpp"

#include "oatpp/network/Server.hpp"
Expand All @@ -13,9 +12,6 @@ void run() {
/* Get router component */
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);

/* Create MyController and add all of its endpoints to router */
router->addController(std::make_shared<MyController>());

/* Get connection handler component */
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler, "http");

Expand Down
4 changes: 2 additions & 2 deletions backend/src/AppComponent.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef AppComponent_hpp
#define AppComponent_hpp

#include "websocket/WSListener.hpp"
#include "rooms/Lobby.hpp"

#include "oatpp/web/server/AsyncHttpConnectionHandler.hpp"
#include "oatpp/web/server/HttpRouter.hpp"
Expand Down Expand Up @@ -65,7 +65,7 @@ class AppComponent {
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, websocketConnectionHandler)("websocket", [] {
OATPP_COMPONENT(std::shared_ptr<oatpp::async::Executor>, executor);
auto connectionHandler = oatpp::websocket::AsyncConnectionHandler::createShared(executor);
connectionHandler->setSocketInstanceListener(std::make_shared<WSInstanceListener>());
connectionHandler->setSocketInstanceListener(std::make_shared<Lobby>());
return connectionHandler;
}());

Expand Down
70 changes: 0 additions & 70 deletions backend/src/controller/MyController.hpp

This file was deleted.

44 changes: 44 additions & 0 deletions backend/src/rooms/Lobby.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include "Lobby.hpp"

v_int32 Lobby::obtainNewUserId() {
return m_userIdCounter ++;
}

std::shared_ptr<Room> Lobby::getOrCreateRoom(const oatpp::String& roomName) {
std::lock_guard<std::mutex> lock(m_roomsMutex);
std::shared_ptr<Room>& room = m_rooms[roomName];
if(!room) {
room = std::make_shared<Room>(roomName);
}
return room;
}

void Lobby::onAfterCreate_NonBlocking(const std::shared_ptr<AsyncWebSocket>& socket, const std::shared_ptr<const ParameterMap>& params) {

auto roomName = params->find("roomName")->second;
auto nickname = params->find("nickname")->second;
auto room = getOrCreateRoom(roomName);

auto peer = std::make_shared<Peer>(socket, room, nickname, obtainNewUserId());
socket->setListener(peer);

room->addPeer(peer);
room->sendMessage(nickname + " joined " + roomName);

}

void Lobby::onBeforeDestroy_NonBlocking(const std::shared_ptr<AsyncWebSocket>& socket) {

auto peer = std::static_pointer_cast<Peer>(socket->getListener());
auto nickname = peer->getNickname();
auto room = peer->getRoom();

room->removePeerByUserId(peer->getUserId());

room->sendMessage(nickname + " left the room");

/* Remove circle `std::shared_ptr` dependencies */
socket->setListener(nullptr);

}
51 changes: 51 additions & 0 deletions backend/src/rooms/Lobby.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#ifndef ASYNC_SERVER_ROOMS_LOBBY_HPP
#define ASYNC_SERVER_ROOMS_LOBBY_HPP

#include "./Room.hpp"

#include "oatpp-websocket/AsyncConnectionHandler.hpp"

#include <unordered_map>
#include <mutex>

class Lobby : public oatpp::websocket::AsyncConnectionHandler::SocketInstanceListener {
public:
std::atomic<v_int32> m_userIdCounter;
std::unordered_map<oatpp::String, std::shared_ptr<Room>> m_rooms;
std::mutex m_roomsMutex;
public:

Lobby()
: m_userIdCounter(0)
{}

/**
* Generate id for new user
* @return
*/
v_int32 obtainNewUserId();

/**
* Get room by name or create new one if not exists.
* @param roomName
* @return
*/
std::shared_ptr<Room> getRoom(const oatpp::String& roomName);

public:

/**
* Called when socket is created
*/
void onAfterCreate_NonBlocking(const std::shared_ptr<AsyncWebSocket>& socket, const std::shared_ptr<const ParameterMap>& params) override;

/**
* Called before socket instance is destroyed.
*/
void onBeforeDestroy_NonBlocking(const std::shared_ptr<AsyncWebSocket>& socket) override;

};


#endif //ASYNC_SERVER_ROOMS_LOBBY_HPP
71 changes: 71 additions & 0 deletions backend/src/rooms/Peer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

#include "Peer.hpp"
#include "Room.hpp"

void Peer::sendMessage(const oatpp::String& message) {

class SendMessageCoroutine : public oatpp::async::Coroutine<SendMessageCoroutine> {
private:
oatpp::async::Lock* m_lock;
std::shared_ptr<AsyncWebSocket> m_websocket;
oatpp::String m_message;
public:

SendMessageCoroutine(oatpp::async::Lock* lock,
const std::shared_ptr<AsyncWebSocket>& websocket,
const oatpp::String& message)
: m_lock(lock)
, m_websocket(websocket)
, m_message(message)
{}

Action act() override {
return oatpp::async::synchronize(m_lock, m_websocket->sendOneFrameTextAsync(m_message)).next(finish());
}

};

m_asyncExecutor->execute<SendMessageCoroutine>(&m_writeLock, m_socket, message);

}

std::shared_ptr<Room> Peer::getRoom() {
return m_room;
}

oatpp::String Peer::getNickname() {
return m_nickname;
}

v_int32 Peer::getUserId() {
return m_userId;
}

oatpp::async::CoroutineStarter Peer::onPing(const std::shared_ptr<AsyncWebSocket>& socket, const oatpp::String& message) {
return oatpp::async::synchronize(&m_writeLock, socket->sendPongAsync(message));
}

oatpp::async::CoroutineStarter Peer::onPong(const std::shared_ptr<AsyncWebSocket>& socket, const oatpp::String& message) {
return nullptr; // do nothing
}

oatpp::async::CoroutineStarter Peer::onClose(const std::shared_ptr<AsyncWebSocket>& socket, v_uint16 code, const oatpp::String& message) {
return nullptr; // do nothing
}

oatpp::async::CoroutineStarter Peer::readMessage(const std::shared_ptr<AsyncWebSocket>& socket, v_uint8 opcode, p_char8 data, oatpp::v_io_size size) {

if(size == 0) { // message transfer finished

auto wholeMessage = m_messageBuffer.toString();
m_messageBuffer.setCurrentPosition(0);

m_room->sendMessage(m_nickname + ": " + wholeMessage);

} else if(size > 0) { // message frame received
m_messageBuffer.writeSimple(data, size);
}

return nullptr; // do nothing

}
Loading

0 comments on commit fe39416

Please sign in to comment.