diff --git a/flake.lock b/flake.lock index cbca8099..4407bd60 100644 --- a/flake.lock +++ b/flake.lock @@ -1213,6 +1213,7 @@ "rsshub": "rsshub", "rycee": "rycee", "slate": "slate", + "sockpp": "sockpp", "sops-nix": "sops-nix", "sqlite-orm": "sqlite-orm", "tgbot-cpp": "tgbot-cpp", @@ -1269,6 +1270,22 @@ "type": "github" } }, + "sockpp": { + "flake": false, + "locked": { + "lastModified": 1707078447, + "narHash": "sha256-lV3K6OGs4bFaCKKu1FeMwTMT5Q47Gxl+GzVf4rNnYjE=", + "owner": "fpagliughi", + "repo": "sockpp", + "rev": "04002daccc8f66a77edf019124089a3ce966b2e3", + "type": "github" + }, + "original": { + "owner": "fpagliughi", + "repo": "sockpp", + "type": "github" + } + }, "sops-nix": { "inputs": { "nixpkgs": [ diff --git a/flake.nix b/flake.nix index c4286287..59774234 100644 --- a/flake.nix +++ b/flake.nix @@ -63,6 +63,7 @@ zxorm = { url = "github:CHN-beta/zxorm"; flake = false; }; openxlsx = { url = "github:troldal/OpenXLSX"; flake = false; }; sqlite-orm = { url = "github:fnc12/sqlite_orm"; flake = false; }; + sockpp = { url = "github:fpagliughi/sockpp"; flake = false; }; # does not support lfs yet # nixos-wallpaper = { url = "git+https://git.chn.moe/chn/nixos-wallpaper.git"; flake = false; }; @@ -193,6 +194,12 @@ buildInputs = [ pkgs.clang-tools_18 ]; CMAKE_EXPORT_COMPILE_COMMANDS = "1"; }; + winjob = pkgs.mkShell.override { stdenv = pkgs.gcc14Stdenv; } + { + inputsFrom = [ pkgs.localPackages.winjob ]; + buildInputs = [ pkgs.clang-tools_18 ]; + CMAKE_EXPORT_COMPILE_COMMANDS = "1"; + }; }; }; } diff --git a/local/pkgs/default.nix b/local/pkgs/default.nix index ce0f0638..86f2f4f4 100644 --- a/local/pkgs/default.nix +++ b/local/pkgs/default.nix @@ -79,7 +79,8 @@ inputs: rec ufo = inputs.pkgs.callPackage ./ufo { inherit concurrencpp biu glad matplotplusplus zpp-bits; tbb = inputs.pkgs.tbb_2021_11; }; chn-bsub = inputs.pkgs.callPackage ./chn-bsub { inherit biu; }; - winjob = inputs.pkgs.callPackage ./winjob { inherit sqlite-orm; stdenv = inputs.pkgs.gcc14Stdenv; }; + winjob = inputs.pkgs.callPackage ./winjob { stdenv = inputs.pkgs.gcc14Stdenv; }; + sockpp = inputs.pkgs.callPackage ./sockpp.nix { src = inputs.topInputs.sockpp; }; fromYaml = content: builtins.fromJSON (builtins.readFile (inputs.pkgs.runCommand "toJSON" {} diff --git a/local/pkgs/sockpp.nix b/local/pkgs/sockpp.nix new file mode 100644 index 00000000..49117d3f --- /dev/null +++ b/local/pkgs/sockpp.nix @@ -0,0 +1,7 @@ +{ stdenv, src, cmake, pkg-config }: stdenv.mkDerivation +{ + name = "sockpp"; + inherit src; + nativeBuildInputs = [ cmake pkg-config ]; + buildInputs = []; +} diff --git a/local/pkgs/winjob/.envrc b/local/pkgs/winjob/.envrc index 5c078735..fdd24a78 100644 --- a/local/pkgs/winjob/.envrc +++ b/local/pkgs/winjob/.envrc @@ -1 +1 @@ -use flake .#hpcstat +use flake .#winjob diff --git a/local/pkgs/winjob/CMakeLists.txt b/local/pkgs/winjob/CMakeLists.txt index e3b2432c..01fccd44 100644 --- a/local/pkgs/winjob/CMakeLists.txt +++ b/local/pkgs/winjob/CMakeLists.txt @@ -13,7 +13,6 @@ endif() set(WINJOB_VERSION "unknown" CACHE STRING "Version of the winjob") find_package(Boost REQUIRED COMPONENTS headers filesystem) -find_package(SqliteOrm REQUIRED) find_package(nlohmann_json REQUIRED) find_package(range-v3 REQUIRED) @@ -21,7 +20,7 @@ add_executable(winjob src/winjob.cpp) # target_compile_features(winjob PRIVATE cxx_std_26) target_compile_options(winjob PRIVATE "-std=c++26") target_include_directories(winjob PRIVATE ${PROJECT_SOURCE_DIR}/include) -target_link_libraries(winjob PRIVATE Boost::headers Boost::filesystem sqlite_orm::sqlite_orm +target_link_libraries(winjob PRIVATE Boost::headers Boost::filesystem nlohmann_json::nlohmann_json range-v3::range-v3) target_compile_definitions(winjob PRIVATE winjob_VERSION="${winjob_VERSION}") @@ -29,7 +28,7 @@ add_executable(winjobd src/winjobd.cpp) # target_compile_features(winjob PRIVATE cxx_std_26) target_compile_options(winjobd PRIVATE "-std=c++26") target_include_directories(winjobd PRIVATE ${PROJECT_SOURCE_DIR}/include) -target_link_libraries(winjobd PRIVATE Boost::headers Boost::filesystem sqlite_orm::sqlite_orm +target_link_libraries(winjobd PRIVATE Boost::headers Boost::filesystem nlohmann_json::nlohmann_json range-v3::range-v3) target_compile_definitions(winjobd PRIVATE winjob_VERSION="${winjob_VERSION}") diff --git a/local/pkgs/winjob/default.nix b/local/pkgs/winjob/default.nix index f1e96643..1acc8bf9 100644 --- a/local/pkgs/winjob/default.nix +++ b/local/pkgs/winjob/default.nix @@ -1,11 +1,11 @@ { stdenv, cmake, pkg-config, version ? null, lib, - nlohmann_json, range-v3, sqlite-orm, boost + nlohmann_json, range-v3, boost }: stdenv.mkDerivation { name = "winjob"; src = ./.; - buildInputs = [ nlohmann_json range-v3 sqlite-orm boost ]; + buildInputs = [ nlohmann_json range-v3 boost ]; nativeBuildInputs = [ cmake pkg-config ]; cmakeFlags = lib.optionals (version != null) [ "-DWINJOB_VERSION=${version}" ]; } diff --git a/local/pkgs/winjob/src/winjob.cpp b/local/pkgs/winjob/src/winjob.cpp index 0395e23b..40023a08 100644 --- a/local/pkgs/winjob/src/winjob.cpp +++ b/local/pkgs/winjob/src/winjob.cpp @@ -1,4 +1,21 @@ +# include +# include + int main() { - -} + boost::asio::io_context io_context; + boost::asio::local::stream_protocol::endpoint ep("/tmp/winjobd.sock"); + // send a message to the server + boost::asio::local::stream_protocol::socket socket(io_context); + socket.connect(ep); + std::string message = "Hello, world!\n"; + boost::asio::write(socket, boost::asio::buffer(message)); + // receive a message from the server + boost::asio::streambuf buf; + boost::asio::read_until(socket, buf, '\n'); + std::istream is(&buf); + std::string line; + std::getline(is, line); + std::cout << "Received: " << line << '\n'; + return 0; +} \ No newline at end of file diff --git a/local/pkgs/winjob/src/winjobd.cpp b/local/pkgs/winjob/src/winjobd.cpp index 0395e23b..a9746833 100644 --- a/local/pkgs/winjob/src/winjobd.cpp +++ b/local/pkgs/winjob/src/winjobd.cpp @@ -1,4 +1,43 @@ +# include +# include + int main() { - + boost::asio::io_context io_context; + boost::asio::local::stream_protocol::endpoint ep("/tmp/winjobd.sock"); + boost::asio::local::stream_protocol::acceptor acceptor(io_context, ep); + auto getuid = [](boost::asio::local::stream_protocol::socket& socket) + { + struct ucred ucred; + socklen_t len = sizeof(ucred); + if (getsockopt(socket.native_handle(), SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) + { + std::cerr << "Failed to get SO_PEERCRED\n"; + return 0u; + } + return ucred.uid; + }; + std::function func = + [&](const boost::system::error_code& ec, boost::asio::local::stream_protocol::socket socket) + { + if (ec) + { + std::cerr << "Failed to accept connection\n"; + return; + } + std::cout << "Accepted connection\n"; + boost::asio::streambuf buf; + boost::asio::read_until(socket, buf, '\n'); + std::istream is(&buf); + std::string line; + std::getline(is, line); + std::cout << "Received: " << line << '\n'; + std::cout << "Peer UID: " << getuid(socket) << '\n'; + // write a message to the client + std::string message = "thanks for the message\n"; + boost::asio::write(socket, boost::asio::buffer(message)); + acceptor.async_accept(func); + }; + acceptor.async_accept(func); + io_context.run(); }