1
- #ifdef _WIN32
2
- #define _WIN32_WINNT 0x0A00
3
- #endif
4
-
5
1
#include " process_image.h"
6
2
#include < boost/asio.hpp>
7
3
#include < boost/beast.hpp>
8
4
#include < cstdlib>
5
+ #include < grpcpp/grpcpp.h>
9
6
#include < iostream>
10
- #include < liboai.h>
11
7
#include < opencv2/opencv.hpp>
12
8
#include < sstream>
13
9
#include < string>
10
+ #include " message.grpc.pb.h"
14
11
15
12
// Namespace declarations
16
13
namespace asio = boost::asio;
17
14
using tcp = boost::asio::ip::tcp;
18
15
namespace websocket = boost::beast::websocket;
19
16
20
- // Function to test PyTorch integration
21
- // void testPyTorch() {
22
- // // Create a 3x3 random tensor
23
- // torch::Tensor tensor = torch::rand({3, 3});
24
- // std::cout << "Testing PyTorch Installation:" << std::endl;
25
- // std::cout << "Random Tensor:\n" << tensor << std::endl;
26
- //
27
- // // Perform a simple operation
28
- // tensor = tensor * 2;
29
- // std::cout << "Tensor after multiplication:\n" << tensor << std::endl;
30
- // }
17
+ // gRPC Service Implementation
18
+ class MessageServiceImpl final : public MessageService::Service {
19
+ public:
20
+ grpc::Status SendMessage (grpc::ServerContext *context,
21
+ const MessageRequest *request,
22
+ MessageResponse *reply) override {
23
+ std::string receivedMessage = request->message ();
24
+ std::cout << " gRPC received message: " << receivedMessage << std::endl;
25
+
26
+ // Respond to the gRPC client
27
+ reply->set_response (" Server received: " + receivedMessage);
28
+ return grpc::Status::OK;
29
+ }
30
+ };
31
+
32
+ // Function to run the gRPC server
33
+ void RunGrpcServer () {
34
+ std::string serverAddress (" 0.0.0.0:50051" );
35
+ MessageServiceImpl service;
36
+
37
+ grpc::ServerBuilder builder;
38
+ builder.AddListeningPort (serverAddress, grpc::InsecureServerCredentials ());
39
+ builder.RegisterService (&service);
40
+
41
+ std::unique_ptr<grpc::Server> server (builder.BuildAndStart ());
42
+ std::cout << " gRPC server listening on " << serverAddress << std::endl;
31
43
44
+ server->Wait ();
45
+ }
46
+
47
+ // WebSocket session function
32
48
void do_session (tcp::socket socket) {
33
49
try {
34
50
websocket::stream<tcp::socket> ws (std::move (socket));
@@ -46,81 +62,38 @@ void do_session(tcp::socket socket) {
46
62
boost::beast::buffers_to_string (buffer.data ());
47
63
48
64
// Log the received message
49
- std::cout << " Message from client: " << receivedMessage << std::endl;
65
+ std::cout << " WebSocket received message: " << receivedMessage
66
+ << std::endl;
50
67
51
68
// Respond to the client
52
69
std::string response = " Server received: " + receivedMessage;
53
70
ws.write (asio::buffer (response));
54
71
}
55
72
} catch (const std::exception &e) {
56
73
// General exception handling
57
- std::cout << " Error: " << e.what () << std::endl;
58
- }
59
- }
60
-
61
- void reply (tcp::socket socket) {
62
- try {
63
- websocket::stream<tcp::socket> ws (std::move (socket));
64
- ws.accept (); // Accept the WebSocket handshake
65
- liboai::OpenAI open_ai;
66
- liboai::Conversation chat_box;
67
-
68
- chat_box.SetSystemData (" You are a professional physics chatbox" );
69
-
70
- boost::beast::multi_buffer buffer;
71
- if (open_ai.auth .SetKeyEnv (" OPENAI" )) {
72
- std::cout << " Did this even work" << ' \n ' ;
73
- try {
74
- while (true ) {
75
- ws.read (buffer); // Blocking read
76
- std::string chat_message =
77
- boost::beast::buffers_to_string (buffer.data ());
78
- std::cout << " Received message: " << chat_message << ' \n ' ;
79
-
80
- chat_box.AddUserData (chat_message);
81
-
82
- liboai::Response response =
83
- open_ai.ChatCompletion ->create (" gpt-4o-mini" , chat_box);
84
-
85
- chat_box.Update (response);
86
-
87
- // Send a confirmation message
88
- ws.write (asio::buffer (chat_box.GetLastResponse ()));
89
- std::cout << " Confirmation sent to client\n " ;
90
-
91
- buffer.consume (
92
- buffer.size ()); // Clear the buffer for the next message
93
- }
94
- } catch (std::exception &e) {
95
- std::cout << e.what () << ' \n ' ;
96
- }
97
- }
98
- } catch (const std::exception &e) {
99
- std::cout << e.what () << ' \n ' ;
74
+ std::cout << " WebSocket Error: " << e.what () << std::endl;
100
75
}
101
76
}
102
77
103
78
int main () {
104
79
try {
80
+ // Start gRPC server in a separate thread
81
+ std::thread grpcThread (RunGrpcServer);
82
+
83
+ // Start WebSocket server
105
84
asio::io_context ioc;
106
85
tcp::acceptor acceptor (ioc, tcp::endpoint (tcp::v4 (), 8080 ));
107
86
108
87
std::cout << " WebSocket server listening on port 8080..." << std::endl;
109
- const char *openai_key = std::getenv (" OPENAI" );
110
- if (openai_key) {
111
- std::cout << " OpenAI Key: " << openai_key << std::endl;
112
- } else {
113
- std::cerr << " Error: OPENAI environment variable is not set."
114
- << std::endl;
115
- return 1 ; // Exit or handle error appropriately
116
- }
117
88
118
89
while (true ) {
119
90
tcp::socket socket (ioc);
120
91
acceptor.accept (socket); // Wait for a client to connect
121
92
122
- std::thread (&reply , std::move (socket)).detach ();
93
+ std::thread (&do_session , std::move (socket)).detach ();
123
94
}
95
+
96
+ grpcThread.join (); // Ensure the gRPC thread runs indefinitely
124
97
} catch (const std::exception &e) {
125
98
std::cout << " Server Error: " << e.what () << std::endl;
126
99
}
0 commit comments