Skip to content

Commit 1197110

Browse files
committed
refactor(websocket): remove RuntimeExecutor and standardize on threadpool executor
unify execution model across websocket (App, Server, Session) migrate all examples to executor factory improve lifecycle consistency with core
2 parents 141bee0 + f17321b commit 1197110

16 files changed

Lines changed: 210 additions & 183 deletions

examples/advanced/src/server.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
* Vix.cpp
1212
*
1313
* This example demonstrates a fully featured, production-style WebSocket
14-
* server using the Vix.cpp runtime. It showcases how to combine:
14+
* server using the Vix.cpp executor abstraction. It showcases how to combine:
1515
*
1616
* • Asynchronous native WebSocket server
17-
* • RuntimeExecutor integration
17+
* • Thread pool executor integration
1818
* • Room-based messaging (join, leave, broadcast)
1919
* • Typed JSON protocol ("type" + "payload")
2020
* • Persistent message storage using SQLite (WAL enabled)
@@ -37,7 +37,7 @@
3737

3838
#include <vix.hpp>
3939

40-
#include <vix/executor/RuntimeExecutor.hpp>
40+
#include <vix/experimental/ThreadPoolExecutor.hpp>
4141
#include <vix/websocket.hpp>
4242
#include <vix/websocket/LongPolling.hpp>
4343
#include <vix/websocket/LongPollingBridge.hpp>
@@ -57,9 +57,13 @@ int main()
5757

5858
using njson = nlohmann::json;
5959

60-
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
60+
auto exec = vix::experimental::make_threadpool_executor(
61+
4, // min threads
62+
8, // max threads
63+
0 // default priority
64+
);
6165

62-
App wsApp{"config/config.json", exec};
66+
App wsApp{"config/config.json", std::move(exec)};
6367
auto &ws = wsApp.server();
6468

6569
WebSocketMetrics metrics;

examples/chat_room.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
//
4545

4646
#include <vix/config/Config.hpp>
47-
#include <vix/executor/RuntimeExecutor.hpp>
47+
#include <vix/experimental/ThreadPoolExecutor.hpp>
4848
#include <vix/websocket.hpp>
4949

5050
int main()
@@ -54,11 +54,15 @@ int main()
5454
// 1) Load config
5555
vix::config::Config cfg{"config/config.json"};
5656

57-
// 2) Runtime executor for async WebSocket processing
58-
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
57+
// 2) Thread pool executor for async WebSocket processing
58+
auto exec = vix::experimental::make_threadpool_executor(
59+
4, // min threads
60+
8, // max threads
61+
0 // default priority
62+
);
5963

6064
// 3) Construct the WebSocket server
61-
Server ws(cfg, exec);
65+
Server ws(cfg, std::move(exec));
6266

6367
// 4) On new connection
6468
ws.on_open(

examples/simple/src/app_example.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
* Vix.cpp
1212
*/
1313
#include <iostream>
14-
#include <memory>
1514

16-
#include <vix/executor/RuntimeExecutor.hpp>
15+
#include <vix/experimental/ThreadPoolExecutor.hpp>
1716
#include <vix/websocket.hpp>
1817

1918
using vix::websocket::App;
@@ -40,9 +39,13 @@ void handle_chat(
4039

4140
int main()
4241
{
43-
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
42+
auto exec = vix::experimental::make_threadpool_executor(
43+
4, // min threads
44+
8, // max threads
45+
0 // default priority
46+
);
4447

45-
App app{"config/config.json", exec};
48+
App app{"config/config.json", std::move(exec)};
4649

4750
(void)app.ws("/chat", handle_chat);
4851

examples/simple/src/minimal_ws_server.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@
1111
* Vix.cpp
1212
*/
1313
#include <iostream>
14-
#include <memory>
1514
#include <string>
1615

1716
#include <vix/config/Config.hpp>
18-
#include <vix/executor/RuntimeExecutor.hpp>
17+
#include <vix/experimental/ThreadPoolExecutor.hpp>
1918
#include <vix/websocket.hpp>
2019
#include <vix/websocket/protocol.hpp>
2120

2221
namespace ws = vix::websocket;
2322

2423
int main()
2524
{
26-
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
25+
auto exec = vix::experimental::make_threadpool_executor(
26+
4, // min threads
27+
8, // max threads
28+
0 // default priority
29+
);
2730

28-
ws::App app{"config/config.json", exec};
31+
ws::App app{"config/config.json", std::move(exec)};
2932
auto &server = app.server();
3033

3134
std::cout << "[minimal] WebSocket server starting on port "

examples/simple/src/simple_client.cpp

Lines changed: 80 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,60 @@
99
* that can be found in the License file.
1010
*
1111
* Vix.cpp
12-
* @brief Minimal WebSocket client example for Vix.cpp
12+
* @brief Minimal WebSocket client example for Vix.cpp
1313
*
14-
* This example demonstrates the simplest possible interactive WebSocket
15-
* client built with the Vix.cpp WebSocket module. It connects to a server,
16-
* listens for typed JSON messages, prints structured chat output, and allows
17-
* the user to send messages through a basic terminal prompt.
14+
* This example demonstrates the simplest possible interactive WebSocket
15+
* client built with the Vix.cpp WebSocket module. It connects to a server,
16+
* listens for typed JSON messages, prints structured chat output, and allows
17+
* the user to send messages through a basic terminal prompt.
1818
*
19-
* Core Features Demonstrated
20-
* ---------------------------
21-
* 1. Client Creation:
22-
* The example creates a WebSocket client targeting localhost:9090 and
23-
* automatically manages connection state through Vix.cpp abstractions.
19+
* Core Features Demonstrated
20+
* ---------------------------
21+
* 1. Client Creation:
22+
* The example creates a WebSocket client targeting localhost:9090 and
23+
* automatically manages connection state through Vix.cpp abstractions.
2424
*
25-
* 2. Typed JSON Protocol Handling:
26-
* Incoming frames are parsed using JsonMessage and routed based on their
27-
* "type":
28-
* • chat.system server/system events
29-
* • chat.message regular chat messages
30-
* • fallback prints raw JSON
25+
* 2. Typed JSON Protocol Handling:
26+
* Incoming frames are parsed using JsonMessage and routed based on their
27+
* "type":
28+
* • chat.system - server/system events
29+
* • chat.message - regular chat messages
30+
* • fallback - prints raw JSON
3131
*
32-
* 3. Auto-Reconnect:
33-
* The client will automatically attempt to reconnect every 3 seconds
34-
* if the connection is lost.
32+
* 3. Auto-Reconnect:
33+
* The client will automatically attempt to reconnect every 3 seconds
34+
* if the connection is lost.
3535
*
36-
* 4. Heartbeat / Keep-Alive:
37-
* A periodic ping is enabled to keep NAT/proxy connections alive.
36+
* 4. Heartbeat / Keep-Alive:
37+
* A periodic ping is enabled to keep NAT/proxy connections alive.
3838
*
39-
* 5. Interactive Input Loop:
40-
* The user enters a pseudonym and can then type messages in real time.
41-
* Typing "/quit" closes the session gracefully.
39+
* 5. Interactive Input Loop:
40+
* The user enters a pseudonym and can then type messages in real time.
41+
* Typing "/quit" closes the session gracefully.
4242
*
43-
* Intended Usage
44-
* --------------
45-
* This minimal client is ideal for:
46-
* Testing or debugging a Vix.cpp WebSocket server
47-
* Learning how to work with the JSON protocol
48-
* Building simple chat tools or monitoring utilities
49-
* Demonstrating the basics of WebSocket event handling in C++
43+
* Intended Usage
44+
* --------------
45+
* This minimal client is ideal for:
46+
* - Testing or debugging a Vix.cpp WebSocket server
47+
* - Learning how to work with the JSON protocol
48+
* - Building simple chat tools or monitoring utilities
49+
* - Demonstrating the basics of WebSocket event handling in C++
5050
*
51-
* How to Run
52-
* ----------
53-
* 1. Start a Vix WebSocket server (see simple_server.cpp).
54-
* 2. Build this example:
55-
* cmake -S . -B build && cmake --build build -j
56-
* 3. Run the client:
57-
* ./build/examples/simple/simple_client
58-
* 4. Type messages interactively, or use "/quit" to exit.
51+
* How to Run
52+
* ----------
53+
* 1. Start a Vix WebSocket server (see simple_server.cpp).
54+
* 2. Build this example:
55+
* cmake -S . -B build && cmake --build build -j
56+
* 3. Run the client:
57+
* ./build/examples/simple/simple_client
58+
* 4. Type messages interactively, or use "/quit" to exit.
5959
*
60-
* This is a deliberately minimal example—see the advanced client example for
61-
* support for rooms, reconnection logic, structured system events, and
62-
* persistent message workflows.
60+
* This is a deliberately minimal example. See the advanced client example for
61+
* support for rooms, reconnection logic, structured system events, and
62+
* persistent message workflows.
6363
*/
6464

65+
#include <chrono>
6566
#include <iostream>
6667
#include <string>
6768

@@ -75,64 +76,80 @@ int main()
7576

7677
auto client = Client::create("localhost", "9090", "/");
7778

78-
client->on_open([]
79-
{ std::cout << "[client] Connected ✅" << std::endl; });
79+
client->on_open(
80+
[]
81+
{
82+
std::cout << "[client] Connected ✅" << std::endl;
83+
});
8084

81-
client->on_message([](const std::string &msg)
82-
{
85+
client->on_message(
86+
[](const std::string &msg)
87+
{
8388
auto jm = JsonMessage::parse(msg);
8489

8590
if (!jm)
8691
{
87-
std::cout << msg << std::endl;
88-
return;
92+
std::cout << msg << std::endl;
93+
return;
8994
}
9095

9196
const std::string &type = jm->type;
9297

9398
if (type == "chat.system")
9499
{
95-
std::cout << "[system] " << jm->get_string("text") << std::endl;
100+
std::cout << "[system] " << jm->get_string("text") << std::endl;
96101
}
97102
else if (type == "chat.message")
98103
{
99-
std::string user = jm->get_string("user");
100-
if (user.empty())
101-
user = "anonymous";
102-
103-
std::cout << "[chat] " << user
104-
<< ": " << jm->get_string("text") << std::endl;
104+
std::string user = jm->get_string("user");
105+
if (user.empty())
106+
{
107+
user = "anonymous";
108+
}
109+
110+
std::cout << "[chat] " << user
111+
<< ": " << jm->get_string("text") << std::endl;
105112
}
106113
else
107114
{
108-
std::cout << msg << std::endl;
109-
} });
115+
std::cout << msg << std::endl;
116+
}
117+
});
110118

111-
client->on_close([]
112-
{ std::cout << "[client] Disconnected." << std::endl; });
119+
client->on_close(
120+
[]
121+
{
122+
std::cout << "[client] Disconnected." << std::endl;
123+
});
113124

114-
client->on_error([](const std::string &error)
115-
{ std::cerr << "[client] error: " << error << std::endl; });
125+
client->on_error(
126+
[](const std::string &error)
127+
{
128+
std::cerr << "[client] error: " << error << std::endl;
129+
});
116130

117131
client->enable_auto_reconnect(true, std::chrono::seconds(3));
118132
client->enable_heartbeat(std::chrono::seconds(20));
119133

120134
client->connect();
121135

122-
// Prompt username
123136
std::cout << "Pseudo: ";
124137
std::string user;
125138
std::getline(std::cin, user);
139+
126140
if (user.empty())
141+
{
127142
user = "anonymous";
143+
}
128144

129145
std::cout << "Type messages, /quit to exit\n";
130146

131-
// Message loop
132147
for (std::string line; std::getline(std::cin, line);)
133148
{
134149
if (line == "/quit")
150+
{
135151
break;
152+
}
136153

137154
client->send(
138155
"chat.message",

examples/simple/src/simple_server.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* simplest reference implementation, showing only the essential components:
1717
*
1818
* • Loading configuration (port, timeouts, etc.)
19-
* • Creating a RuntimeExecutor for async execution
19+
* • Creating a thread pool executor for async execution
2020
* • Starting a WebSocket server instance
2121
* • Reacting to connection events (on_open)
2222
* • Handling typed JSON messages (on_typed_message)
@@ -28,9 +28,9 @@
2828
* The WebSocket server automatically binds to the port defined in
2929
* config/config.json and manages all asynchronous I/O.
3030
*
31-
* 2. Runtime Integration:
32-
* The example uses Vix’s RuntimeExecutor to drive async execution,
33-
* aligning the WebSocket layer with the modern Vix runtime architecture.
31+
* 2. Executor Integration:
32+
* The example uses a thread pool executor to drive async execution,
33+
* aligning the WebSocket layer with the Vix executor abstraction.
3434
*
3535
* 3. Global Broadcast:
3636
* Messages received with type "chat.message" are broadcast to all
@@ -67,10 +67,8 @@
6767
* persistence, metrics, room routing, history replay, and auto-reconnect.
6868
*/
6969

70-
#include <memory>
71-
7270
#include <vix/config/Config.hpp>
73-
#include <vix/executor/RuntimeExecutor.hpp>
71+
#include <vix/experimental/ThreadPoolExecutor.hpp>
7472
#include <vix/websocket.hpp>
7573

7674
int main()
@@ -80,10 +78,14 @@ int main()
8078
// Load configuration from config/config.json
8179
vix::config::Config cfg{"config/config.json"};
8280

83-
// Runtime executor for async work
84-
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
81+
// Thread pool executor for async work
82+
auto exec = vix::experimental::make_threadpool_executor(
83+
4, // min threads
84+
8, // max threads
85+
0 // default priority
86+
);
8587

86-
Server ws(cfg, exec);
88+
Server ws(cfg, std::move(exec));
8789

8890
// On new connection: broadcast a welcome system message
8991
ws.on_open(

0 commit comments

Comments
 (0)