From b7a38658b05e0e4ebd59274098fee90cbc23e311 Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Wed, 5 Jul 2023 09:10:28 +0200 Subject: [PATCH 1/2] added cpp example --- examples/cpp_client_example/CMakeLists.txt | 33 ++++++ examples/cpp_client_example/main.cpp | 131 +++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 examples/cpp_client_example/CMakeLists.txt create mode 100644 examples/cpp_client_example/main.cpp diff --git a/examples/cpp_client_example/CMakeLists.txt b/examples/cpp_client_example/CMakeLists.txt new file mode 100644 index 0000000..597a7f9 --- /dev/null +++ b/examples/cpp_client_example/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.1) +project(SocketIOClientExample) + +set(CMAKE_CXX_STANDARD 11) + +# Set the path to the Socket.IO Client C++ library +set(SOCKET_IO_CLIENT_CPP_PATH /path/to/socket.io-client-cpp) + +# Add the Socket.IO Client C++ library include directory +include_directories(${SOCKET_IO_CLIENT_CPP_PATH}/src) + +# Add the Socket.IO Client C++ library source files +set(SOCKET_IO_CLIENT_CPP_SOURCE_FILES + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet.h + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet.cpp + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_op.h + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_op.cpp + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_socket.h + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_packet_socket.cpp + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_streambuf.h + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_streambuf.cpp + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket.h + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket.cpp + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket_impl.h + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_socket_impl.cpp + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_client_impl.h + ${SOCKET_IO_CLIENT_CPP_PATH}/src/internal/sio_client_impl.cpp + ${SOCKET_IO_CLIENT_CPP_PATH}/src/sio_client.h + ${SOCKET_IO_CLIENT_CPP_PATH}/src/sio_client.cpp +) + +# Add the executable target +add_executable(SocketIOClientExample main.cpp ${SOCKET_IO_CLIENT_CPP_SOURCE_FILES}) diff --git a/examples/cpp_client_example/main.cpp b/examples/cpp_client_example/main.cpp new file mode 100644 index 0000000..0920f6f --- /dev/null +++ b/examples/cpp_client_example/main.cpp @@ -0,0 +1,131 @@ +#include +#include +#include + +class SocketIOClient { +public: + SocketIOClient(const std::string& serverUrl) : client_(sio::socket::create()) + { + client_->set_open_listener([&]() { + onConnected(); + }); + + client_->set_close_listener([&](sio::client::close_reason const& reason) { + onDisconnected(); + }); + + client_->set_fail_listener([&]() { + onConnectionFailed(); + }); + + client_->set_reconnect_listener([&](unsigned int reconnectionAttempts, unsigned int delay) { + onReconnecting(reconnectionAttempts, delay); + }); + + client_->set_socket_close_listener([&]() { + onSocketClosed(); + }); + + client_->connect(serverUrl); + } + + void generateText(const std::string& prompt) + { + sio::message::list messages; + messages.push(sio::string_message::create(prompt)); + messages.push(sio::int_message::create(-1)); + messages.push(sio::int_message::create(1024)); + client_->socket()->emit("generate_text", messages); + } + + void cancelGeneration() + { + client_->socket()->emit("cancel_generation"); + } + + void onConnected() + { + std::cout << "Connected to the LoLLMs server" << std::endl; + // Perform actions upon successful connection + // ... + } + + void onDisconnected() + { + std::cout << "Disconnected from the server" << std::endl; + // Perform actions upon disconnection + // ... + } + + void onConnectionFailed() + { + std::cout << "Connection to the server failed" << std::endl; + // Perform actions upon connection failure + // ... + } + + void onReconnecting(unsigned int reconnectionAttempts, unsigned int delay) + { + std::cout << "Reconnecting to the server (attempt " << reconnectionAttempts << ") in " << delay << " milliseconds" << std::endl; + // Perform actions upon reconnection attempt + // ... + } + + void onSocketClosed() + { + std::cout << "Socket closed" << std::endl; + // Perform actions upon socket closure + // ... + } + +private: + sio::client client_; +}; + +int main() +{ + // Create a SocketIOClient instance and connect to the server + SocketIOClient client("http://localhost:9601"); + + // Event handler for receiving generated text chunks + client.client_->socket()->on("text_chunk", [&](const sio::event& event) { + const std::string chunk = event.get_message()->get_string(); + std::cout << "Received chunk: " << chunk << std::endl; + // Append the chunk to the output or perform any other required actions + // ... + }); + + // Event handler for receiving generated text + client.client_->socket()->on("text_generated", [&](const sio::event& event) { + const std::string text = event.get_message()->get_string(); + std::cout << "Text generated: " << text << std::endl; + // Toggle button visibility or perform any other required actions + // ... + }); + + // Event handler for error during text generation + client.client_->socket()->on("buzzy", [&](const sio::event& event) { + const std::string error = event.get_message()->get_string(); + std::cerr << "Server is busy. Wait for your turn. Error: " << error << std::endl; + // Handle the error or perform any other required actions + // ... + }); + + // Event handler for generation cancellation + client.client_->socket()->on("generation_canceled", [&](const sio::event& event) { + // Toggle button visibility or perform any other required actions + // ... + }); + + // Trigger the "generate_text" event when needed + std::string prompt = "Enter your prompt here"; + client.generateText(prompt); + + // Trigger the "cancel_generation" event when needed + client.cancelGeneration(); + + // Run the event loop + client.client_->sync_close(); + + return 0; +} From c23636df2d2395fc1aa047513beb3d742f8b6d5d Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Wed, 5 Jul 2023 13:18:35 +0200 Subject: [PATCH 2/2] added example --- examples/cpp_client_example/.gitignore | 2 ++ examples/cpp_client_example/build.py | 48 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 examples/cpp_client_example/.gitignore create mode 100644 examples/cpp_client_example/build.py diff --git a/examples/cpp_client_example/.gitignore b/examples/cpp_client_example/.gitignore new file mode 100644 index 0000000..7c63aaf --- /dev/null +++ b/examples/cpp_client_example/.gitignore @@ -0,0 +1,2 @@ +# Ignore the cloned Socket.IO Client C++ library +socket.io-client-cpp/ diff --git a/examples/cpp_client_example/build.py b/examples/cpp_client_example/build.py new file mode 100644 index 0000000..0d9b60a --- /dev/null +++ b/examples/cpp_client_example/build.py @@ -0,0 +1,48 @@ +import os +import subprocess +import platform + +repo_dir = 'socket.io-client-cpp' + +# Check if the repository directory exists +if os.path.exists(repo_dir): + # If it exists, perform a git pull to update the repository + os.chdir(repo_dir) + subprocess.run(['git', 'pull']) +else: + # If it doesn't exist, clone the Socket.IO Client C++ library + subprocess.run(['git', 'clone', 'https://github.com/socketio/socket.io-client-cpp.git', repo_dir]) + + # Build the Socket.IO Client C++ library + os.chdir(repo_dir) + +if platform.system() == 'Windows': + os.makedirs('build', exist_ok=True) + os.chdir('build') + + # Detect if running on Windows Command Prompt (cmd) or PowerShell + shell = os.environ.get('SHELL') + if shell and 'powershell' in shell.lower(): + subprocess.run(['cmake', '..']) + subprocess.run(['cmake', '--build', '.', '--config', 'Release']) + else: + # Use the Visual Studio build tools + subprocess.run(['cmake', '-G', 'Visual Studio 16 2019', '..']) + subprocess.run(['cmake', '--build', '.', '--config', 'Release']) + +else: + subprocess.run(['mkdir', 'build']) + os.chdir('build') + subprocess.run(['cmake', '..']) + subprocess.run(['make']) + +# Compile the C++ code +os.chdir('../..') +if platform.system() == 'Windows': + subprocess.run(['cl', '/EHsc', '/Isocket.io-client-cpp/src', + 'main.cpp', 'socket.io-client-cpp/build/Release/sioclient.lib', '/FeSocketIOClientExample.exe']) +else: + subprocess.run(['g++', '-std=c++11', '-Isocket.io-client-cpp/src', + 'main.cpp', 'socket.io-client-cpp/build/libsioclient.a', '-o', 'SocketIOClientExample']) + +print('Compilation completed successfully.')