Skip to content

Commit 404f9b0

Browse files
committed
Modified websocket_server.cpp
1 parent c3f8b63 commit 404f9b0

File tree

1 file changed

+42
-69
lines changed

1 file changed

+42
-69
lines changed

src/websocket_s/websocket_server.cpp

Lines changed: 42 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
1-
#ifdef _WIN32
2-
#define _WIN32_WINNT 0x0A00
3-
#endif
4-
51
#include "process_image.h"
62
#include <boost/asio.hpp>
73
#include <boost/beast.hpp>
84
#include <cstdlib>
5+
#include <grpcpp/grpcpp.h>
96
#include <iostream>
10-
#include <liboai.h>
117
#include <opencv2/opencv.hpp>
128
#include <sstream>
139
#include <string>
10+
#include "message.grpc.pb.h"
1411

1512
// Namespace declarations
1613
namespace asio = boost::asio;
1714
using tcp = boost::asio::ip::tcp;
1815
namespace websocket = boost::beast::websocket;
1916

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;
3143

44+
server->Wait();
45+
}
46+
47+
// WebSocket session function
3248
void do_session(tcp::socket socket) {
3349
try {
3450
websocket::stream<tcp::socket> ws(std::move(socket));
@@ -46,81 +62,38 @@ void do_session(tcp::socket socket) {
4662
boost::beast::buffers_to_string(buffer.data());
4763

4864
// Log the received message
49-
std::cout << "Message from client: " << receivedMessage << std::endl;
65+
std::cout << "WebSocket received message: " << receivedMessage
66+
<< std::endl;
5067

5168
// Respond to the client
5269
std::string response = "Server received: " + receivedMessage;
5370
ws.write(asio::buffer(response));
5471
}
5572
} catch (const std::exception &e) {
5673
// 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;
10075
}
10176
}
10277

10378
int main() {
10479
try {
80+
// Start gRPC server in a separate thread
81+
std::thread grpcThread(RunGrpcServer);
82+
83+
// Start WebSocket server
10584
asio::io_context ioc;
10685
tcp::acceptor acceptor(ioc, tcp::endpoint(tcp::v4(), 8080));
10786

10887
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-
}
11788

11889
while (true) {
11990
tcp::socket socket(ioc);
12091
acceptor.accept(socket); // Wait for a client to connect
12192

122-
std::thread(&reply, std::move(socket)).detach();
93+
std::thread(&do_session, std::move(socket)).detach();
12394
}
95+
96+
grpcThread.join(); // Ensure the gRPC thread runs indefinitely
12497
} catch (const std::exception &e) {
12598
std::cout << "Server Error: " << e.what() << std::endl;
12699
}

0 commit comments

Comments
 (0)