From 74a4063b957583123a81d7f80603a0c008eb43b6 Mon Sep 17 00:00:00 2001 From: krhougs Date: Fri, 9 Sep 2022 02:07:16 +0800 Subject: [PATCH] task spawner --- .gitignore | 156 +++++++++++++ .idea/.gitignore | 8 + .idea/customTargets.xml | 8 + .idea/modules.xml | 8 + .idea/remote-targets.xml | 22 ++ .idea/service_network.iml | 14 ++ .idea/sshConfigs.xml | 8 + .idea/vcs.xml | 6 + Cargo.lock | 463 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 11 + psn_peer/Cargo.toml | 12 + psn_peer/src/lib.rs | 18 ++ psn_peer/src/runtime.rs | 56 +++++ service_broker/Cargo.toml | 11 + service_broker/src/main.rs | 74 ++++++ service_worker/Cargo.toml | 11 + service_worker/src/main.rs | 3 + src/lib.rs | 3 + 18 files changed, 892 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/customTargets.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/remote-targets.xml create mode 100644 .idea/service_network.iml create mode 100644 .idea/sshConfigs.xml create mode 100644 .idea/vcs.xml create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 psn_peer/Cargo.toml create mode 100644 psn_peer/src/lib.rs create mode 100644 psn_peer/src/runtime.rs create mode 100644 service_broker/Cargo.toml create mode 100644 service_broker/src/main.rs create mode 100644 service_worker/Cargo.toml create mode 100644 service_worker/src/main.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2933aa9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,156 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + + + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/customTargets.xml b/.idea/customTargets.xml new file mode 100644 index 0000000..d04cab1 --- /dev/null +++ b/.idea/customTargets.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..48f58f4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/remote-targets.xml b/.idea/remote-targets.xml new file mode 100644 index 0000000..aa6191b --- /dev/null +++ b/.idea/remote-targets.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/service_network.iml b/.idea/service_network.iml new file mode 100644 index 0000000..4ed1795 --- /dev/null +++ b/.idea/service_network.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/sshConfigs.xml b/.idea/sshConfigs.xml new file mode 100644 index 0000000..420b356 --- /dev/null +++ b/.idea/sshConfigs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..e8f20e9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,463 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "async-dnssd" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98efc05996cc8d660e88841fcffb75aa71be5339c9ae559a8c8016c048420b82" +dependencies = [ + "bitflags", + "futures-channel", + "futures-core", + "futures-executor", + "futures-util", + "libc", + "log", + "pin-utils", + "pkg-config", + "tokio", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bytes" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "futures" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f21eda599937fba36daeb58a22e8f5cee2d14c4a17b5b7739c7c8e5e3b8230c" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30bdd20c28fadd505d0fd6712cdfcb0d4b5648baf45faef7f852afb2399bb050" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e5aa3de05362c3fb88de6531e6296e85cde7739cccad4b9dfeeb7f6ebce56bf" + +[[package]] +name = "futures-executor" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff63c23854bee61b6e9cd331d523909f238fc7636290b96826e9cfa5faa00ab" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbf4d2a7a308fd4578637c0b17c7e1c7ba127b8f6ba00b29f717e9655d85eb68" + +[[package]] +name = "futures-macro" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42cd15d1c7456c04dbdf7e88bcd69760d74f3a798d6444e16974b505b0e62f17" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b20ba5a92e727ba30e72834706623d94ac93a725410b6a6b6fbc1b07f7ba56" + +[[package]] +name = "futures-task" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6508c467c73851293f390476d4491cf4d227dbabcd4170f3bb6044959b294f1" + +[[package]] +name = "futures-util" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" + +[[package]] +name = "lock_api" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f80bf5aacaf25cbfc8210d1cfb718f2bf3b11c4c54e5afe36c236853a8ec390" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "mio" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys", +] + +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0" + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "psn_peer" +version = "0.1.0" +dependencies = [ + "async-dnssd", + "bytes", + "futures", + "tokio", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "service_broker" +version = "0.1.0" +dependencies = [ + "bytes", + "psn_peer", + "tokio", +] + +[[package]] +name = "service_network" +version = "0.1.0" + +[[package]] +name = "service_worker" +version = "0.1.0" +dependencies = [ + "bytes", + "psn_peer", + "tokio", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4614a76b2a8be0058caa9dbbaf66d988527d86d003c11a94fbd335d7661edcef" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" + +[[package]] +name = "socket2" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tokio" +version = "1.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89797afd69d206ccd11fb0ea560a44bbb87731d020670e79416d442919257d42" +dependencies = [ + "autocfg", + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "once_cell", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" + +[[package]] +name = "windows_i686_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" + +[[package]] +name = "windows_i686_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b726b7f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "service_network" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[workspace] +members = ["service_broker", "service_worker", "psn_peer"] diff --git a/psn_peer/Cargo.toml b/psn_peer/Cargo.toml new file mode 100644 index 0000000..7e304d3 --- /dev/null +++ b/psn_peer/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "psn_peer" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-dnssd = "0.5.0" +bytes = "1.2.1" +futures = "0.3.24" +tokio = { version = "1.21.0", features = ["full"] } diff --git a/psn_peer/src/lib.rs b/psn_peer/src/lib.rs new file mode 100644 index 0000000..567ecb2 --- /dev/null +++ b/psn_peer/src/lib.rs @@ -0,0 +1,18 @@ +use std::sync::{Arc, RwLock}; + +pub mod runtime; + +#[derive(Clone, Debug)] +pub enum PeerRole { + PrUndefined = 0, + PrLocalWorker = 1, + PrRemoteWorker = 2, + PrBroker = 3, +} + +#[derive(Debug)] +pub struct PeerConfig { + pub role: PeerRole, +} + +pub type WrappedPeerConfig = Arc>; diff --git a/psn_peer/src/runtime.rs b/psn_peer/src/runtime.rs new file mode 100644 index 0000000..f09a708 --- /dev/null +++ b/psn_peer/src/runtime.rs @@ -0,0 +1,56 @@ +use crate::PeerConfig; +use futures::future::try_join_all; +use futures::TryFuture; +use std::fmt::Debug; +use std::future::Future; +use std::sync::Arc; +use tokio::runtime::Runtime; +use tokio::sync::RwLock; +use tokio::task::JoinHandle; + +pub type WrappedAsyncRuntimeContext = Arc>; +pub type WrappedRuntime = Arc>; + +pub struct AsyncRuntimeContext { + pub config: Arc>, +} + +impl AsyncRuntimeContext { + pub fn init(config: PeerConfig) -> WrappedAsyncRuntimeContext { + Arc::new(RwLock::new(AsyncRuntimeContext { + config: Arc::new(RwLock::new(config)), + })) + } + + pub fn spawn( + ctx_w: WrappedAsyncRuntimeContext, + rt_w: WrappedRuntime, + f: F, + ) -> JoinHandle<::Output> + where + T: Future + Send + 'static, + T::Output: Send + 'static, + F: FnOnce(WrappedAsyncRuntimeContext, WrappedRuntime) -> T + Send + 'static, + { + let future = f(ctx_w, rt_w.clone()); + let rt = rt_w.try_read().unwrap(); + let handle = rt.spawn(future); + drop(rt); + + handle + } + + pub fn block_on_all(rt_w: WrappedRuntime, iter: I) -> () + where + I: IntoIterator, + I::Item: TryFuture, + <::Item as TryFuture>::Error: Debug, + { + let rt = rt_w.try_read().unwrap(); + rt.block_on(try_join_all(iter)).expect("block_on_all"); + } +} + +pub fn init(config: PeerConfig) -> WrappedAsyncRuntimeContext { + AsyncRuntimeContext::init(config).clone() +} diff --git a/service_broker/Cargo.toml b/service_broker/Cargo.toml new file mode 100644 index 0000000..446c352 --- /dev/null +++ b/service_broker/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "service_broker" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bytes = "1.2.1" +psn_peer = { version = "0.1.0", path = "../psn_peer" } +tokio = { version = "1.21.0", features = ["full"] } diff --git a/service_broker/src/main.rs b/service_broker/src/main.rs new file mode 100644 index 0000000..117fb87 --- /dev/null +++ b/service_broker/src/main.rs @@ -0,0 +1,74 @@ +use psn_peer::runtime::{ + init as init_async_runtime, AsyncRuntimeContext, WrappedAsyncRuntimeContext, WrappedRuntime, +}; +use psn_peer::{PeerConfig, PeerRole}; +use std::sync::Arc; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpListener; +use tokio::runtime::Builder; +use tokio::sync::RwLock; + +fn main() { + let config = PeerConfig { + role: PeerRole::PrBroker, + }; + + let rt_w = Arc::new(RwLock::new( + Builder::new_multi_thread().enable_all().build().unwrap(), + )); + + let rt = rt_w.try_read().unwrap(); + let _guard = rt.enter(); + drop(rt); + + let ctx_wrapper = init_async_runtime(config); + // let ctx = ctx_wrapper.read().unwrap(); + // drop(ctx); + // let ctx_wrapper = ctx_wrapper.clone(); + + AsyncRuntimeContext::block_on_all( + rt_w.clone(), + vec![ + AsyncRuntimeContext::spawn( + ctx_wrapper.clone(), + rt_w.clone(), + Box::new(move |cw, r| tcp_server(cw, r, "0.0.0.0:11451".to_owned())), + ), + AsyncRuntimeContext::spawn( + ctx_wrapper.clone(), + rt_w.clone(), + Box::new(move |cw, r| tcp_server(cw, r, "0.0.0.0:11452".to_owned())), + ), + ], + ); +} + +async fn tcp_server(_ctx_w: WrappedAsyncRuntimeContext, rt_w: WrappedRuntime, addr: String) { + println!("Listening on {}", addr); + let listener = TcpListener::bind(addr).await.unwrap(); + + loop { + let (mut socket, _) = listener.accept().await.unwrap(); + let rt = rt_w.read().await; + rt.spawn(async move { + let mut ret: Vec = Vec::new(); + + loop { + ret.clear(); + let n = socket + .read_buf(&mut ret) + .await + .expect("failed to read data from socket"); + + if n == 0 { + return; + } + + println!("n = {}, r = {:?}", n, ret); + socket.write_all(&ret).await.expect("failed to write."); + } + }); + drop(rt); + // drop(ctx); + } +} diff --git a/service_worker/Cargo.toml b/service_worker/Cargo.toml new file mode 100644 index 0000000..8b75b1a --- /dev/null +++ b/service_worker/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "service_worker" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bytes = "1.2.1" +psn_peer = { version = "0.1.0", path = "../psn_peer" } +tokio = { version = "1.21.0", features = ["full"] } diff --git a/service_worker/src/main.rs b/service_worker/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/service_worker/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..211bfff --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +}