diff --git a/src/controller-v1.1/epollip/connector_controller.cpp b/src/controller-v1.1/epollip/connector_controller.cpp new file mode 100644 index 0000000..6b0d54c --- /dev/null +++ b/src/controller-v1.1/epollip/connector_controller.cpp @@ -0,0 +1,288 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFFER_SIZE 1024 +#define HTTP_SERVER_PORT 8080 + +std::vector threads; // 存储所有连接线程 +std::map sockets; // 存储套接字 +std::mutex sockets_mutex; // 用于同步访问 sockets +std::atomic exit_flag(false); // 原子退出标志 + + +// 维护连接的函数 +void KeepConnection(int sock) { + char buffer[BUFFER_SIZE]; + int nbytes; + while (!exit_flag) { + nbytes = recv(sock, buffer, BUFFER_SIZE, 0); + if (nbytes <= 0) { + std::cout << "Gateway disconnected" << std::endl; + break; + } + std::cout << "Received from gateway (Socket " << sock << "): " << buffer << std::endl; + } + + { + std::lock_guard lock(sockets_mutex); + sockets.erase(sock); // 从集合中移除套接字 + } + close(sock); +} +// 连接到网关的函数 +void ConnectToGateway(const std::string& ip, int port) { + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror("Could not create socket"); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + if (inet_pton(AF_INET, ip.c_str(), &server_addr.sin_addr) <= 0) { + perror("Invalid address"); + close(sock); + return; + } + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Connection to the server failed"); + close(sock); + return; + } + + { + std::lock_guard lock(sockets_mutex); + sockets[sock] = 1; // 将套接字添加到集合中 + } + + std::cout << "Connected to gateway at " << ip << ":" << port << " (Socket " << sock << ")" << std::endl; + + // 在新线程中维护连接 + threads.emplace_back(KeepConnection, sock); +} + +// 处理HTTP请求的函数 +void HandleHttpRequest(int client_socket) { + char buffer[BUFFER_SIZE]; + int bytes_read = recv(client_socket, buffer, sizeof(buffer), 0); + if (bytes_read <= 0) { + close(client_socket); + return; + } + + // 简单解析HTTP请求 + std::string http_request(buffer, bytes_read); + std::string ip; + int port; + + // 寻找IP和端口号 + auto host_pos = http_request.find("Host: "); + if (host_pos != std::string::npos) { + auto start = host_pos + std::string("Host: ").size(); + ip = http_request.substr(start, http_request.find("\r\n", start) - start); + } + + auto content_length_pos = http_request.find("Content-Length:"); + if (content_length_pos != std::string::npos) { + auto start = content_length_pos + std::string("Content-Length: ").size(); + int length = std::stoi(http_request.substr(start, http_request.find("\r\n", start) - start)); + if (length > 0) { + char* content = new char[length]; + bytes_read = recv(client_socket, content, length, 0); + if (bytes_read > 0) { + // 解析JSON内容(假设前端发送的是JSON格式) + // 这里需要添加JSON解析逻辑来提取IP和端口 + // 示例:{"ip":"192.168.1.1","port":8080} + // 可以使用第三方库如nlohmann/json来解析JSON + + // 假设解析后得到ip和port + // ConnectToGateway(ip, port); + + delete[] content; + } + } + } + + // 发送HTTP响应 + std::string response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"; + send(client_socket, response.c_str(), response.size(), 0); + close(client_socket); +} + +// 启动HTTP服务器的函数 +void StartHttpServer() { + int http_server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (http_server_socket < 0) { + perror("Could not create HTTP server socket"); + return; + } + + int opt = 1; + if (setsockopt(http_server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { + perror("Setsockopt failed"); + close(http_server_socket); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(HTTP_SERVER_PORT); + + if (bind(http_server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Bind failed"); + close(http_server_socket); + return; + } + + if (listen(http_server_socket, 5) < 0) { + perror("Listen failed"); + close(http_server_socket); + return; + } + + std::cout << "HTTP server listening on port " << HTTP_SERVER_PORT << std::endl; + + while (!exit_flag) { + int client_socket = accept(http_server_socket, NULL, NULL); + if (client_socket < 0) { + perror("Accept failed"); + continue; + } + + threads.emplace_back(HandleHttpRequest, client_socket); + } + + close(http_server_socket); +} + +int main() { + // threads.emplace_back(StartHttpServer); + + // std::string command, ip; + // int port; + // bool in_connect_mode = false; + // std::cout << "Enter command (connect/exit): "; + // while (true) { + + // if (in_connect_mode) + // { + // std::cout << "continue connect or not ? (y or n) "; + // std::cin >> command; + // if(command=="y"){ + // std::cout << "Enter the gateway IP address: "; + // std::cin >> ip; + // std::cout << "Enter the gateway port: "; + // std::cin >> port; + // ConnectToGateway(ip, port); + // } + // else { + // in_connect_mode = false; // 退出连接模式 + // std::cout << "Exiting connect mode." << std::endl; + // } + // } + // else{ + // std::cout << "Enter command (connect/exit): "; + // std::cin>>command; + // if (command == "connect") { + // if (in_connect_mode) { + // std::cout << "Already in connect mode." << std::endl; + // continue; + // } + // in_connect_mode = true; // 进入连接模式 + // std::cout << "Enter the gateway IP address: "; + // std::cin >> ip; + // std::cout << "Enter the gateway port: "; + // std::cin >> port; + // ConnectToGateway(ip, port); + // } else if (command == "exit") { + // exit_flag = true; // 设置退出标志 + // std::cout << "Exiting program." << std::endl; + // // 关闭所有套接字 + // for (auto& sock_pair : sockets) { + // shutdown(sock_pair.first, SHUT_RDWR); // 关闭套接字的发送和接收 + // close(sock_pair.first); + // } + // sockets.clear(); + // std::cout << "sockets.clear" << std::endl; + // // 等待所有线程结束 + // for (auto& thread : threads) { + // if (thread.joinable()) { + // thread.join(); + // } + // } + // threads.clear(); + // break; // 退出主循环 + // } else { + // std::cout << "Unknown command" << std::endl; + // } + // } + // } + // std::cout<<"out"<> command; + + if (command == "connect") { + if (in_connect_mode) { + std::cout << "Already in connect mode." << std::endl; + continue; + } + in_connect_mode = true; + std::cout << "Enter the gateway IP address: "; + std::cin >> ip; + std::cout << "Enter the gateway port: "; + std::cin >> port; + ConnectToGateway(ip, port); + } else if (command == "exit") { + break; // 接收到退出命令,退出主循环 + } else { + std::cout << "Unknown command" << std::endl; + } + } + + // 设置退出标志 + exit_flag = true; + + // 强制结束HTTP服务器线程 + if (http_server_thread.joinable()) { + http_server_thread.join(); + } + + // 关闭所有套接字 + for (auto& sock_pair : sockets) { + shutdown(sock_pair.first, SHUT_RDWR); + close(sock_pair.first); + } + sockets.clear(); + + // 等待所有工作线程结束 + for (auto& thread : threads) { + if (thread.joinable()) { + thread.join(); + } + } + threads.clear(); + + std::cout << "Exited program." << std::endl; + return 0; + +} \ No newline at end of file diff --git a/src/controller-v1.1/epollip/connector_database.cpp b/src/controller-v1.1/epollip/connector_database.cpp new file mode 100644 index 0000000..d0768cc --- /dev/null +++ b/src/controller-v1.1/epollip/connector_database.cpp @@ -0,0 +1,31 @@ +#include +#include + +int main() { + MYSQL *conn; + conn = mysql_init(NULL); + + if (conn == NULL) { + std::cerr << "MySQL init failed" << std::endl; + return 1; + } + + const char *server = "localhost"; + const char *user = "rtsw"; + const char *password = "123456"; + const char *database = "nginxdb"; + unsigned int port = 3306; // 使用云数据库提供的端口 + + if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) { + std::cerr << "Connection error: " << mysql_error(conn) << std::endl; + mysql_close(conn); + return 1; + } + + std::cout << "Successfully connected to the database" << std::endl; + + // ... 执行数据库操作 ... + + mysql_close(conn); + return 0; +} \ No newline at end of file diff --git a/src/controller-v1.1/epollip/connector_gateway.cpp b/src/controller-v1.1/epollip/connector_gateway.cpp new file mode 100644 index 0000000..b6ba4d7 --- /dev/null +++ b/src/controller-v1.1/epollip/connector_gateway.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include +#include + +#define PORT 8080 +#define MAX_CLIENTS 5 +#define BUFFER_SIZE 1024 + +// 声明 handle_client 函数 +void handle_client(int client_socket); + +int main() { + int server_fd, new_socket; + struct sockaddr_in server_addr, client_addr; + socklen_t client_len = sizeof(client_addr); + char buffer[BUFFER_SIZE]; + int opt = 1; + + // 创建套接字 + server_fd = socket(AF_INET, SOCK_STREAM, 0); + if (server_fd < 0) { + perror("socket creation failed"); + exit(EXIT_FAILURE); + } + + // 设置选项,允许重用地址 + if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { + perror("setsockopt failed"); + exit(EXIT_FAILURE); + } + + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(PORT); + + // 绑定 + if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("bind failed"); + exit(EXIT_FAILURE); + } + + // 监听 + if (listen(server_fd, MAX_CLIENTS) < 0) { + perror("listen failed"); + exit(EXIT_FAILURE); + } + + std::cout << "Server listening on port " << PORT << std::endl; + + while (true) { + client_len = sizeof(client_addr); + new_socket = accept(server_fd, (struct sockaddr *)&client_addr, &client_len); + if (new_socket < 0) { + perror("accept failed"); + exit(EXIT_FAILURE); + } + + std::cout << "New client connected from " << inet_ntoa(client_addr.sin_addr) << " port " << ntohs(client_addr.sin_port) << std::endl; + + // 创建并启动线程处理客户端 + std::thread(handle_client, new_socket).detach(); + } + + close(server_fd); + return 0; +} + +// 定义 handle_client 函数 +void handle_client(int client_socket) { + while (true) { + char buffer[BUFFER_SIZE]; + memset(buffer, 0, BUFFER_SIZE); // 清空缓冲区 + int nbytes = read(client_socket, buffer, BUFFER_SIZE); + if (nbytes <= 0) { + std::cout << "Client disconnected." << std::endl; + close(client_socket); + break; + } + std::cout << "Received message from client: " << buffer << std::endl; + // 这里可以添加更多的处理逻辑 + } +} \ No newline at end of file diff --git a/src/controller-v1.1/epollip/db_processer/basic_db.cpp b/src/controller-v1.1/epollip/db_processer/basic_db.cpp new file mode 100644 index 0000000..0abc9ed --- /dev/null +++ b/src/controller-v1.1/epollip/db_processer/basic_db.cpp @@ -0,0 +1,66 @@ +#include +#include + +int main() { + MYSQL *conn; + conn = mysql_init(NULL); + + if (conn == NULL) { + std::cerr << "MySQL init failed" << std::endl; + return 1; + } + + const char *server = "localhost"; + const char *user = "rtsw"; + const char *password = "123456"; + const char *database = "nginxdb"; + unsigned int port = 3306; + + if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) { + std::cerr << "Connection error: " << mysql_error(conn) << std::endl; + mysql_close(conn); + return 1; + } + + std::cout << "Successfully connected to the database" << std::endl; + + // 插入数据 + const char *insert_query = "INSERT INTO users (username, email) VALUES ('newuser', 'newuser@example.com')"; + if (mysql_query(conn, insert_query)) { + std::cerr << "Insert error: " << mysql_error(conn) << std::endl; + } else { + std::cout << "Insert successful" << std::endl; + } + + // 查询数据 + const char *select_query = "SELECT * FROM users"; + MYSQL_RES *result = mysql_store_result(conn); + if (result) { + MYSQL_ROW row; + while ((row = mysql_fetch_row(result))) { + std::cout << "id: " << row[0] << ", username: " << row[1] << ", email: " << row[2] << std::endl; + } + mysql_free_result(result); + } else { + std::cerr << "Select error: " << mysql_error(conn) << std::endl; + } + + // 更新数据 + const char *update_query = "UPDATE users SET email = 'newuser_updated@example.com' WHERE username = 'newuser'"; + if (mysql_query(conn, update_query)) { + std::cerr << "Update error: " << mysql_error(conn) << std::endl; + } else { + std::cout << "Update successful" << std::endl; + } + + // 删除数据 + const char *delete_query = "DELETE FROM users WHERE username = 'newuser'"; + if (mysql_query(conn, delete_query)) { + std::cerr << "Delete error: " << mysql_error(conn) << std::endl; + } else { + std::cout << "Delete successful" << std::endl; + } + + mysql_close(conn); + return 0; +} \ No newline at end of file diff --git a/src/controller-v1.1/epollip/interface/connector_controller.cpp b/src/controller-v1.1/epollip/interface/connector_controller.cpp new file mode 100644 index 0000000..2c93baf --- /dev/null +++ b/src/controller-v1.1/epollip/interface/connector_controller.cpp @@ -0,0 +1,280 @@ +#include "connector_controller.h" + +#define BUFFER_SIZE 1024 +#define HTTP_SERVER_PORT 8080 + +std::vector threads; // 存储所有连接线程 +std::map sockets; // 存储套接字 +std::mutex sockets_mutex; // 用于同步访问 sockets +std::atomic exit_flag(false); // 原子退出标志 + + +// 维护连接的函数 +void KeepConnection(int sock) { + char buffer[BUFFER_SIZE]; + int nbytes; + while (!exit_flag) { + nbytes = recv(sock, buffer, BUFFER_SIZE, 0); + if (nbytes <= 0) { + std::cout << "Gateway disconnected" << std::endl; + break; + } + std::cout << "Received from gateway (Socket " << sock << "): " << buffer << std::endl; + } + + { + std::lock_guard lock(sockets_mutex); + sockets.erase(sock); // 从集合中移除套接字 + } + close(sock); +} +// 连接到网关的函数 +void ConnectToGateway(const std::string& ip, int port) { + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror("Could not create socket"); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + if (inet_pton(AF_INET, ip.c_str(), &server_addr.sin_addr) <= 0) { + perror("Invalid address"); + close(sock); + return; + } + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Connection to the server failed"); + close(sock); + return; + } + + { + std::lock_guard lock(sockets_mutex); + sockets[sock] = 1; // 将套接字添加到集合中 + } + + std::cout << "Connected to gateway at " << ip << ":" << port << " (Socket " << sock << ")" << std::endl; + + // 在新线程中维护连接 + threads.emplace_back(KeepConnection, sock); +} + +// 处理HTTP请求的函数 +void HandleHttpRequest(int client_socket) { + char buffer[BUFFER_SIZE]; + int bytes_read = recv(client_socket, buffer, sizeof(buffer), 0); + if (bytes_read <= 0) { + close(client_socket); + return; + } + + // 简单解析HTTP请求 + std::string http_request(buffer, bytes_read); + std::string ip; + int port; + + // 寻找IP和端口号 + auto host_pos = http_request.find("Host: "); + if (host_pos != std::string::npos) { + auto start = host_pos + std::string("Host: ").size(); + ip = http_request.substr(start, http_request.find("\r\n", start) - start); + } + + auto content_length_pos = http_request.find("Content-Length:"); + if (content_length_pos != std::string::npos) { + auto start = content_length_pos + std::string("Content-Length: ").size(); + int length = std::stoi(http_request.substr(start, http_request.find("\r\n", start) - start)); + if (length > 0) { + char* content = new char[length]; + bytes_read = recv(client_socket, content, length, 0); + if (bytes_read > 0) { + // 解析JSON内容(假设前端发送的是JSON格式) + // 这里需要添加JSON解析逻辑来提取IP和端口 + // 示例:{"ip":"192.168.1.1","port":8080} + // 可以使用第三方库如nlohmann/json来解析JSON + + // 假设解析后得到ip和port + // ConnectToGateway(ip, port); + + delete[] content; + } + } + } + + // 发送HTTP响应 + std::string response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"; + send(client_socket, response.c_str(), response.size(), 0); + close(client_socket); +} + +// 启动HTTP服务器的函数 +void StartHttpServer() { + int http_server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (http_server_socket < 0) { + perror("Could not create HTTP server socket"); + return; + } + + int opt = 1; + if (setsockopt(http_server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { + perror("Setsockopt failed"); + close(http_server_socket); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(HTTP_SERVER_PORT); + + if (bind(http_server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Bind failed"); + close(http_server_socket); + return; + } + + if (listen(http_server_socket, 5) < 0) { + perror("Listen failed"); + close(http_server_socket); + return; + } + + std::cout << "HTTP server listening on port " << HTTP_SERVER_PORT << std::endl; + + while (!exit_flag) { + int client_socket = accept(http_server_socket, NULL, NULL); + if (client_socket < 0) { + perror("Accept failed"); + continue; + } + + threads.emplace_back(HandleHttpRequest, client_socket); + } + + close(http_server_socket); +} + +/* +int main() { + // threads.emplace_back(StartHttpServer); + + // std::string command, ip; + // int port; + // bool in_connect_mode = false; + // std::cout << "Enter command (connect/exit): "; + // while (true) { + + // if (in_connect_mode) + // { + // std::cout << "continue connect or not ? (y or n) "; + // std::cin >> command; + // if(command=="y"){ + // std::cout << "Enter the gateway IP address: "; + // std::cin >> ip; + // std::cout << "Enter the gateway port: "; + // std::cin >> port; + // ConnectToGateway(ip, port); + // } + // else { + // in_connect_mode = false; // 退出连接模式 + // std::cout << "Exiting connect mode." << std::endl; + // } + // } + // else{ + // std::cout << "Enter command (connect/exit): "; + // std::cin>>command; + // if (command == "connect") { + // if (in_connect_mode) { + // std::cout << "Already in connect mode." << std::endl; + // continue; + // } + // in_connect_mode = true; // 进入连接模式 + // std::cout << "Enter the gateway IP address: "; + // std::cin >> ip; + // std::cout << "Enter the gateway port: "; + // std::cin >> port; + // ConnectToGateway(ip, port); + // } else if (command == "exit") { + // exit_flag = true; // 设置退出标志 + // std::cout << "Exiting program." << std::endl; + // // 关闭所有套接字 + // for (auto& sock_pair : sockets) { + // shutdown(sock_pair.first, SHUT_RDWR); // 关闭套接字的发送和接收 + // close(sock_pair.first); + // } + // sockets.clear(); + // std::cout << "sockets.clear" << std::endl; + // // 等待所有线程结束 + // for (auto& thread : threads) { + // if (thread.joinable()) { + // thread.join(); + // } + // } + // threads.clear(); + // break; // 退出主循环 + // } else { + // std::cout << "Unknown command" << std::endl; + // } + // } + // } + // std::cout<<"out"<> command; + + if (command == "connect") { + if (in_connect_mode) { + std::cout << "Already in connect mode." << std::endl; + continue; + } + in_connect_mode = true; + std::cout << "Enter the gateway IP address: "; + std::cin >> ip; + std::cout << "Enter the gateway port: "; + std::cin >> port; + ConnectToGateway(ip, port); + } else if (command == "exit") { + break; // 接收到退出命令,退出主循环 + } else { + std::cout << "Unknown command" << std::endl; + } + } + + // 设置退出标志 + exit_flag = true; + + // 强制结束HTTP服务器线程 + if (http_server_thread.joinable()) { + http_server_thread.join(); + } + + // 关闭所有套接字 + for (auto& sock_pair : sockets) { + shutdown(sock_pair.first, SHUT_RDWR); + close(sock_pair.first); + } + sockets.clear(); + + // 等待所有工作线程结束 + for (auto& thread : threads) { + if (thread.joinable()) { + thread.join(); + } + } + threads.clear(); + + std::cout << "Exited program." << std::endl; + return 0; + +} +*/ \ No newline at end of file diff --git a/src/controller-v1.1/epollip/interface/connector_controller.h b/src/controller-v1.1/epollip/interface/connector_controller.h new file mode 100644 index 0000000..c45ca15 --- /dev/null +++ b/src/controller-v1.1/epollip/interface/connector_controller.h @@ -0,0 +1,22 @@ +#ifndef CONNECTOR_CONTROLLER_H +#define CONNECTOR_CONTROLLER_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// 函数声明 +void StartHttpServer(); +void ConnectToGateway(const std::string& ip, int port); +void KeepConnection(int sock); +void HandleHttpRequest(int client_socket); + +#endif // CONNECTOR_CONTROLLER_H \ No newline at end of file diff --git a/src/controller-v1.1/epollip/interface/connector_database.cpp b/src/controller-v1.1/epollip/interface/connector_database.cpp new file mode 100644 index 0000000..212bfd5 --- /dev/null +++ b/src/controller-v1.1/epollip/interface/connector_database.cpp @@ -0,0 +1,68 @@ +/* + 这个文件可以只负责链接数据库,其他的都不管 + 把数据库的句柄传给主程序 +*/ + +/* +#include +#include + +int main() { + MYSQL *conn; + conn = mysql_init(NULL); + + if (conn == NULL) { + std::cerr << "MySQL init failed" << std::endl; + return 1; + } + + const char *server = "localhost"; + const char *user = "rtsw"; + const char *password = "123456"; + const char *database = "nginxdb"; + unsigned int port = 3306; // 使用云数据库提供的端口 + + if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) { + std::cerr << "Connection error: " << mysql_error(conn) << std::endl; + mysql_close(conn); + return 1; + } + + std::cout << "Successfully connected to the database" << std::endl; + + // ... 执行数据库操作 ... + + mysql_close(conn); + return 0; +} +*/ + +#include "connector_database.h" + +// DatabaseOperation函数定义 +void DatabaseOperation() { + MYSQL *conn = mysql_init(NULL); + + if (conn == NULL) { + std::cerr << "MySQL init failed" << std::endl; + return; + } + + const char *server = "localhost"; + const char *user = "rtsw"; + const char *password = "123456"; + const char *database = "nginxdb"; + unsigned int port = 3306; + + if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) { + std::cerr << "Connection error: " << mysql_error(conn) << std::endl; + mysql_close(conn); + return; + } + + std::cout << "Successfully connected to the database" << std::endl; + + // ... 执行数据库操作 ... + + mysql_close(conn); +} \ No newline at end of file diff --git a/src/controller-v1.1/epollip/interface/connector_database.h b/src/controller-v1.1/epollip/interface/connector_database.h new file mode 100644 index 0000000..83a2cf6 --- /dev/null +++ b/src/controller-v1.1/epollip/interface/connector_database.h @@ -0,0 +1,10 @@ +#ifndef CONNECTOR_DATABASE_H +#define CONNECTOR_DATABASE_H + +#include +#include + +// 函数声明 +void DatabaseOperation(); + +#endif // CONNECTOR_DATABASE_H \ No newline at end of file diff --git a/src/controller-v1.1/epollip/interface/main.cpp b/src/controller-v1.1/epollip/interface/main.cpp new file mode 100644 index 0000000..e469900 --- /dev/null +++ b/src/controller-v1.1/epollip/interface/main.cpp @@ -0,0 +1,35 @@ +#include +#include +#include "connector_controller.h" +#include "connector_database.h" + +int main() { + std::string command; + bool running = true; + + while (running) { + std::cout << "Enter command (start_http_server/connect_gateway/database_operation/exit): "; + std::cin >> command; + + if (command == "start_http_server") { + StartHttpServer(); + } else if (command == "connect_gateway") { + std::string ip; + int port; + std::cout << "Enter the gateway IP address: "<>ip; // 使用getline以获取包含空格的IP地址 + std::cout << "Enter the gateway port: "<> port; + ConnectToGateway(ip, port); + } else if (command == "database_operation") { + DatabaseOperation(); + } else if (command == "exit") { + running = false; + } else { + std::cout << "Unknown command" << std::endl; + } + } + + std::cout << "Exiting program." << std::endl; + return 0; +} \ No newline at end of file diff --git a/src/controller-v1.1/epollip/interface/main_program b/src/controller-v1.1/epollip/interface/main_program new file mode 100644 index 0000000..71bad27 Binary files /dev/null and b/src/controller-v1.1/epollip/interface/main_program differ diff --git a/src/controller-v1.1/epollip/new_client.cpp b/src/controller-v1.1/epollip/new_client.cpp new file mode 100644 index 0000000..78f05f7 --- /dev/null +++ b/src/controller-v1.1/epollip/new_client.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFFER_SIZE 1024 + +std::vector threads; // 存储所有连接线程 +std::map sockets; // 存储套接字 +std::mutex sockets_mutex; // 用于同步访问 sockets +std::atomic exit_flag(false); // 原子退出标志 + + +// 维护连接的函数 +void KeepConnection(int sock) { + char buffer[BUFFER_SIZE]; + int nbytes; + while (!exit_flag) { + nbytes = recv(sock, buffer, BUFFER_SIZE, 0); + if (nbytes <= 0) { + std::cout << "Gateway disconnected" << std::endl; + break; + } + std::cout << "Received from gateway (Socket " << sock << "): " << buffer << std::endl; + } + + { + std::lock_guard lock(sockets_mutex); + sockets.erase(sock); // 从集合中移除套接字 + } + close(sock); +} +// 连接到网关的函数 +void ConnectToGateway(const std::string& ip, int port) { + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror("Could not create socket"); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + if (inet_pton(AF_INET, ip.c_str(), &server_addr.sin_addr) <= 0) { + perror("Invalid address"); + close(sock); + return; + } + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Connection to the server failed"); + close(sock); + return; + } + + { + std::lock_guard lock(sockets_mutex); + sockets[sock] = 1; // 将套接字添加到集合中 + } + + std::cout << "Connected to gateway at " << ip << ":" << port << " (Socket " << sock << ")" << std::endl; + + // 在新线程中维护连接 + threads.emplace_back(KeepConnection, sock); +} + + + +int main() { + std::string command, ip; + int port; + bool in_connect_mode = false; + + while (true) { + std::cout << "Enter command (connect/exitconnect/exit): "; + std::cin >> command; + + if (command == "connect") { + if (in_connect_mode) { + std::cout << "Already in connect mode." << std::endl; + continue; + } + in_connect_mode = true; // 进入连接模式 + std::cout << "Enter the gateway IP address: "; + std::cin >> ip; + std::cout << "Enter the gateway port: "; + std::cin >> port; + ConnectToGateway(ip, port); + } else if (command == "exitconnect") { + if (!in_connect_mode) { + std::cout << "Not in connect mode." << std::endl; + continue; + } + in_connect_mode = false; // 退出连接模式 + std::cout << "Exiting connect mode." << std::endl; + } else if (command == "exit") { + exit_flag = true; // 设置退出标志 + std::cout << "Exiting program." << std::endl; + // 关闭所有套接字 + for (auto& sock_pair : sockets) { + shutdown(sock_pair.first, SHUT_RDWR); // 关闭套接字的发送和接收 + close(sock_pair.first); + } + sockets.clear(); + // 等待所有线程结束 + for (auto& thread : threads) { + if (thread.joinable()) { + thread.join(); + } + } + threads.clear(); + break; // 退出主循环 + } else { + std::cout << "Unknown command" << std::endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/src/controller/connector_controller.cpp b/src/controller/connector_controller.cpp new file mode 100644 index 0000000..6b0d54c --- /dev/null +++ b/src/controller/connector_controller.cpp @@ -0,0 +1,288 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFFER_SIZE 1024 +#define HTTP_SERVER_PORT 8080 + +std::vector threads; // 存储所有连接线程 +std::map sockets; // 存储套接字 +std::mutex sockets_mutex; // 用于同步访问 sockets +std::atomic exit_flag(false); // 原子退出标志 + + +// 维护连接的函数 +void KeepConnection(int sock) { + char buffer[BUFFER_SIZE]; + int nbytes; + while (!exit_flag) { + nbytes = recv(sock, buffer, BUFFER_SIZE, 0); + if (nbytes <= 0) { + std::cout << "Gateway disconnected" << std::endl; + break; + } + std::cout << "Received from gateway (Socket " << sock << "): " << buffer << std::endl; + } + + { + std::lock_guard lock(sockets_mutex); + sockets.erase(sock); // 从集合中移除套接字 + } + close(sock); +} +// 连接到网关的函数 +void ConnectToGateway(const std::string& ip, int port) { + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror("Could not create socket"); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + if (inet_pton(AF_INET, ip.c_str(), &server_addr.sin_addr) <= 0) { + perror("Invalid address"); + close(sock); + return; + } + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Connection to the server failed"); + close(sock); + return; + } + + { + std::lock_guard lock(sockets_mutex); + sockets[sock] = 1; // 将套接字添加到集合中 + } + + std::cout << "Connected to gateway at " << ip << ":" << port << " (Socket " << sock << ")" << std::endl; + + // 在新线程中维护连接 + threads.emplace_back(KeepConnection, sock); +} + +// 处理HTTP请求的函数 +void HandleHttpRequest(int client_socket) { + char buffer[BUFFER_SIZE]; + int bytes_read = recv(client_socket, buffer, sizeof(buffer), 0); + if (bytes_read <= 0) { + close(client_socket); + return; + } + + // 简单解析HTTP请求 + std::string http_request(buffer, bytes_read); + std::string ip; + int port; + + // 寻找IP和端口号 + auto host_pos = http_request.find("Host: "); + if (host_pos != std::string::npos) { + auto start = host_pos + std::string("Host: ").size(); + ip = http_request.substr(start, http_request.find("\r\n", start) - start); + } + + auto content_length_pos = http_request.find("Content-Length:"); + if (content_length_pos != std::string::npos) { + auto start = content_length_pos + std::string("Content-Length: ").size(); + int length = std::stoi(http_request.substr(start, http_request.find("\r\n", start) - start)); + if (length > 0) { + char* content = new char[length]; + bytes_read = recv(client_socket, content, length, 0); + if (bytes_read > 0) { + // 解析JSON内容(假设前端发送的是JSON格式) + // 这里需要添加JSON解析逻辑来提取IP和端口 + // 示例:{"ip":"192.168.1.1","port":8080} + // 可以使用第三方库如nlohmann/json来解析JSON + + // 假设解析后得到ip和port + // ConnectToGateway(ip, port); + + delete[] content; + } + } + } + + // 发送HTTP响应 + std::string response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"; + send(client_socket, response.c_str(), response.size(), 0); + close(client_socket); +} + +// 启动HTTP服务器的函数 +void StartHttpServer() { + int http_server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (http_server_socket < 0) { + perror("Could not create HTTP server socket"); + return; + } + + int opt = 1; + if (setsockopt(http_server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { + perror("Setsockopt failed"); + close(http_server_socket); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(HTTP_SERVER_PORT); + + if (bind(http_server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Bind failed"); + close(http_server_socket); + return; + } + + if (listen(http_server_socket, 5) < 0) { + perror("Listen failed"); + close(http_server_socket); + return; + } + + std::cout << "HTTP server listening on port " << HTTP_SERVER_PORT << std::endl; + + while (!exit_flag) { + int client_socket = accept(http_server_socket, NULL, NULL); + if (client_socket < 0) { + perror("Accept failed"); + continue; + } + + threads.emplace_back(HandleHttpRequest, client_socket); + } + + close(http_server_socket); +} + +int main() { + // threads.emplace_back(StartHttpServer); + + // std::string command, ip; + // int port; + // bool in_connect_mode = false; + // std::cout << "Enter command (connect/exit): "; + // while (true) { + + // if (in_connect_mode) + // { + // std::cout << "continue connect or not ? (y or n) "; + // std::cin >> command; + // if(command=="y"){ + // std::cout << "Enter the gateway IP address: "; + // std::cin >> ip; + // std::cout << "Enter the gateway port: "; + // std::cin >> port; + // ConnectToGateway(ip, port); + // } + // else { + // in_connect_mode = false; // 退出连接模式 + // std::cout << "Exiting connect mode." << std::endl; + // } + // } + // else{ + // std::cout << "Enter command (connect/exit): "; + // std::cin>>command; + // if (command == "connect") { + // if (in_connect_mode) { + // std::cout << "Already in connect mode." << std::endl; + // continue; + // } + // in_connect_mode = true; // 进入连接模式 + // std::cout << "Enter the gateway IP address: "; + // std::cin >> ip; + // std::cout << "Enter the gateway port: "; + // std::cin >> port; + // ConnectToGateway(ip, port); + // } else if (command == "exit") { + // exit_flag = true; // 设置退出标志 + // std::cout << "Exiting program." << std::endl; + // // 关闭所有套接字 + // for (auto& sock_pair : sockets) { + // shutdown(sock_pair.first, SHUT_RDWR); // 关闭套接字的发送和接收 + // close(sock_pair.first); + // } + // sockets.clear(); + // std::cout << "sockets.clear" << std::endl; + // // 等待所有线程结束 + // for (auto& thread : threads) { + // if (thread.joinable()) { + // thread.join(); + // } + // } + // threads.clear(); + // break; // 退出主循环 + // } else { + // std::cout << "Unknown command" << std::endl; + // } + // } + // } + // std::cout<<"out"<> command; + + if (command == "connect") { + if (in_connect_mode) { + std::cout << "Already in connect mode." << std::endl; + continue; + } + in_connect_mode = true; + std::cout << "Enter the gateway IP address: "; + std::cin >> ip; + std::cout << "Enter the gateway port: "; + std::cin >> port; + ConnectToGateway(ip, port); + } else if (command == "exit") { + break; // 接收到退出命令,退出主循环 + } else { + std::cout << "Unknown command" << std::endl; + } + } + + // 设置退出标志 + exit_flag = true; + + // 强制结束HTTP服务器线程 + if (http_server_thread.joinable()) { + http_server_thread.join(); + } + + // 关闭所有套接字 + for (auto& sock_pair : sockets) { + shutdown(sock_pair.first, SHUT_RDWR); + close(sock_pair.first); + } + sockets.clear(); + + // 等待所有工作线程结束 + for (auto& thread : threads) { + if (thread.joinable()) { + thread.join(); + } + } + threads.clear(); + + std::cout << "Exited program." << std::endl; + return 0; + +} \ No newline at end of file diff --git a/src/controller/connector_database.cpp b/src/controller/connector_database.cpp new file mode 100644 index 0000000..d0768cc --- /dev/null +++ b/src/controller/connector_database.cpp @@ -0,0 +1,31 @@ +#include +#include + +int main() { + MYSQL *conn; + conn = mysql_init(NULL); + + if (conn == NULL) { + std::cerr << "MySQL init failed" << std::endl; + return 1; + } + + const char *server = "localhost"; + const char *user = "rtsw"; + const char *password = "123456"; + const char *database = "nginxdb"; + unsigned int port = 3306; // 使用云数据库提供的端口 + + if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) { + std::cerr << "Connection error: " << mysql_error(conn) << std::endl; + mysql_close(conn); + return 1; + } + + std::cout << "Successfully connected to the database" << std::endl; + + // ... 执行数据库操作 ... + + mysql_close(conn); + return 0; +} \ No newline at end of file diff --git a/src/controller/connector_gateway.cpp b/src/controller/connector_gateway.cpp new file mode 100644 index 0000000..b6ba4d7 --- /dev/null +++ b/src/controller/connector_gateway.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include +#include + +#define PORT 8080 +#define MAX_CLIENTS 5 +#define BUFFER_SIZE 1024 + +// 声明 handle_client 函数 +void handle_client(int client_socket); + +int main() { + int server_fd, new_socket; + struct sockaddr_in server_addr, client_addr; + socklen_t client_len = sizeof(client_addr); + char buffer[BUFFER_SIZE]; + int opt = 1; + + // 创建套接字 + server_fd = socket(AF_INET, SOCK_STREAM, 0); + if (server_fd < 0) { + perror("socket creation failed"); + exit(EXIT_FAILURE); + } + + // 设置选项,允许重用地址 + if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { + perror("setsockopt failed"); + exit(EXIT_FAILURE); + } + + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(PORT); + + // 绑定 + if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("bind failed"); + exit(EXIT_FAILURE); + } + + // 监听 + if (listen(server_fd, MAX_CLIENTS) < 0) { + perror("listen failed"); + exit(EXIT_FAILURE); + } + + std::cout << "Server listening on port " << PORT << std::endl; + + while (true) { + client_len = sizeof(client_addr); + new_socket = accept(server_fd, (struct sockaddr *)&client_addr, &client_len); + if (new_socket < 0) { + perror("accept failed"); + exit(EXIT_FAILURE); + } + + std::cout << "New client connected from " << inet_ntoa(client_addr.sin_addr) << " port " << ntohs(client_addr.sin_port) << std::endl; + + // 创建并启动线程处理客户端 + std::thread(handle_client, new_socket).detach(); + } + + close(server_fd); + return 0; +} + +// 定义 handle_client 函数 +void handle_client(int client_socket) { + while (true) { + char buffer[BUFFER_SIZE]; + memset(buffer, 0, BUFFER_SIZE); // 清空缓冲区 + int nbytes = read(client_socket, buffer, BUFFER_SIZE); + if (nbytes <= 0) { + std::cout << "Client disconnected." << std::endl; + close(client_socket); + break; + } + std::cout << "Received message from client: " << buffer << std::endl; + // 这里可以添加更多的处理逻辑 + } +} \ No newline at end of file diff --git a/src/controller/db_processer/basic_db.cpp b/src/controller/db_processer/basic_db.cpp new file mode 100644 index 0000000..0abc9ed --- /dev/null +++ b/src/controller/db_processer/basic_db.cpp @@ -0,0 +1,66 @@ +#include +#include + +int main() { + MYSQL *conn; + conn = mysql_init(NULL); + + if (conn == NULL) { + std::cerr << "MySQL init failed" << std::endl; + return 1; + } + + const char *server = "localhost"; + const char *user = "rtsw"; + const char *password = "123456"; + const char *database = "nginxdb"; + unsigned int port = 3306; + + if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) { + std::cerr << "Connection error: " << mysql_error(conn) << std::endl; + mysql_close(conn); + return 1; + } + + std::cout << "Successfully connected to the database" << std::endl; + + // 插入数据 + const char *insert_query = "INSERT INTO users (username, email) VALUES ('newuser', 'newuser@example.com')"; + if (mysql_query(conn, insert_query)) { + std::cerr << "Insert error: " << mysql_error(conn) << std::endl; + } else { + std::cout << "Insert successful" << std::endl; + } + + // 查询数据 + const char *select_query = "SELECT * FROM users"; + MYSQL_RES *result = mysql_store_result(conn); + if (result) { + MYSQL_ROW row; + while ((row = mysql_fetch_row(result))) { + std::cout << "id: " << row[0] << ", username: " << row[1] << ", email: " << row[2] << std::endl; + } + mysql_free_result(result); + } else { + std::cerr << "Select error: " << mysql_error(conn) << std::endl; + } + + // 更新数据 + const char *update_query = "UPDATE users SET email = 'newuser_updated@example.com' WHERE username = 'newuser'"; + if (mysql_query(conn, update_query)) { + std::cerr << "Update error: " << mysql_error(conn) << std::endl; + } else { + std::cout << "Update successful" << std::endl; + } + + // 删除数据 + const char *delete_query = "DELETE FROM users WHERE username = 'newuser'"; + if (mysql_query(conn, delete_query)) { + std::cerr << "Delete error: " << mysql_error(conn) << std::endl; + } else { + std::cout << "Delete successful" << std::endl; + } + + mysql_close(conn); + return 0; +} \ No newline at end of file diff --git a/src/controller/interface/connector_controller.cpp b/src/controller/interface/connector_controller.cpp new file mode 100644 index 0000000..2c93baf --- /dev/null +++ b/src/controller/interface/connector_controller.cpp @@ -0,0 +1,280 @@ +#include "connector_controller.h" + +#define BUFFER_SIZE 1024 +#define HTTP_SERVER_PORT 8080 + +std::vector threads; // 存储所有连接线程 +std::map sockets; // 存储套接字 +std::mutex sockets_mutex; // 用于同步访问 sockets +std::atomic exit_flag(false); // 原子退出标志 + + +// 维护连接的函数 +void KeepConnection(int sock) { + char buffer[BUFFER_SIZE]; + int nbytes; + while (!exit_flag) { + nbytes = recv(sock, buffer, BUFFER_SIZE, 0); + if (nbytes <= 0) { + std::cout << "Gateway disconnected" << std::endl; + break; + } + std::cout << "Received from gateway (Socket " << sock << "): " << buffer << std::endl; + } + + { + std::lock_guard lock(sockets_mutex); + sockets.erase(sock); // 从集合中移除套接字 + } + close(sock); +} +// 连接到网关的函数 +void ConnectToGateway(const std::string& ip, int port) { + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror("Could not create socket"); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + if (inet_pton(AF_INET, ip.c_str(), &server_addr.sin_addr) <= 0) { + perror("Invalid address"); + close(sock); + return; + } + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Connection to the server failed"); + close(sock); + return; + } + + { + std::lock_guard lock(sockets_mutex); + sockets[sock] = 1; // 将套接字添加到集合中 + } + + std::cout << "Connected to gateway at " << ip << ":" << port << " (Socket " << sock << ")" << std::endl; + + // 在新线程中维护连接 + threads.emplace_back(KeepConnection, sock); +} + +// 处理HTTP请求的函数 +void HandleHttpRequest(int client_socket) { + char buffer[BUFFER_SIZE]; + int bytes_read = recv(client_socket, buffer, sizeof(buffer), 0); + if (bytes_read <= 0) { + close(client_socket); + return; + } + + // 简单解析HTTP请求 + std::string http_request(buffer, bytes_read); + std::string ip; + int port; + + // 寻找IP和端口号 + auto host_pos = http_request.find("Host: "); + if (host_pos != std::string::npos) { + auto start = host_pos + std::string("Host: ").size(); + ip = http_request.substr(start, http_request.find("\r\n", start) - start); + } + + auto content_length_pos = http_request.find("Content-Length:"); + if (content_length_pos != std::string::npos) { + auto start = content_length_pos + std::string("Content-Length: ").size(); + int length = std::stoi(http_request.substr(start, http_request.find("\r\n", start) - start)); + if (length > 0) { + char* content = new char[length]; + bytes_read = recv(client_socket, content, length, 0); + if (bytes_read > 0) { + // 解析JSON内容(假设前端发送的是JSON格式) + // 这里需要添加JSON解析逻辑来提取IP和端口 + // 示例:{"ip":"192.168.1.1","port":8080} + // 可以使用第三方库如nlohmann/json来解析JSON + + // 假设解析后得到ip和port + // ConnectToGateway(ip, port); + + delete[] content; + } + } + } + + // 发送HTTP响应 + std::string response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"; + send(client_socket, response.c_str(), response.size(), 0); + close(client_socket); +} + +// 启动HTTP服务器的函数 +void StartHttpServer() { + int http_server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (http_server_socket < 0) { + perror("Could not create HTTP server socket"); + return; + } + + int opt = 1; + if (setsockopt(http_server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { + perror("Setsockopt failed"); + close(http_server_socket); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(HTTP_SERVER_PORT); + + if (bind(http_server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Bind failed"); + close(http_server_socket); + return; + } + + if (listen(http_server_socket, 5) < 0) { + perror("Listen failed"); + close(http_server_socket); + return; + } + + std::cout << "HTTP server listening on port " << HTTP_SERVER_PORT << std::endl; + + while (!exit_flag) { + int client_socket = accept(http_server_socket, NULL, NULL); + if (client_socket < 0) { + perror("Accept failed"); + continue; + } + + threads.emplace_back(HandleHttpRequest, client_socket); + } + + close(http_server_socket); +} + +/* +int main() { + // threads.emplace_back(StartHttpServer); + + // std::string command, ip; + // int port; + // bool in_connect_mode = false; + // std::cout << "Enter command (connect/exit): "; + // while (true) { + + // if (in_connect_mode) + // { + // std::cout << "continue connect or not ? (y or n) "; + // std::cin >> command; + // if(command=="y"){ + // std::cout << "Enter the gateway IP address: "; + // std::cin >> ip; + // std::cout << "Enter the gateway port: "; + // std::cin >> port; + // ConnectToGateway(ip, port); + // } + // else { + // in_connect_mode = false; // 退出连接模式 + // std::cout << "Exiting connect mode." << std::endl; + // } + // } + // else{ + // std::cout << "Enter command (connect/exit): "; + // std::cin>>command; + // if (command == "connect") { + // if (in_connect_mode) { + // std::cout << "Already in connect mode." << std::endl; + // continue; + // } + // in_connect_mode = true; // 进入连接模式 + // std::cout << "Enter the gateway IP address: "; + // std::cin >> ip; + // std::cout << "Enter the gateway port: "; + // std::cin >> port; + // ConnectToGateway(ip, port); + // } else if (command == "exit") { + // exit_flag = true; // 设置退出标志 + // std::cout << "Exiting program." << std::endl; + // // 关闭所有套接字 + // for (auto& sock_pair : sockets) { + // shutdown(sock_pair.first, SHUT_RDWR); // 关闭套接字的发送和接收 + // close(sock_pair.first); + // } + // sockets.clear(); + // std::cout << "sockets.clear" << std::endl; + // // 等待所有线程结束 + // for (auto& thread : threads) { + // if (thread.joinable()) { + // thread.join(); + // } + // } + // threads.clear(); + // break; // 退出主循环 + // } else { + // std::cout << "Unknown command" << std::endl; + // } + // } + // } + // std::cout<<"out"<> command; + + if (command == "connect") { + if (in_connect_mode) { + std::cout << "Already in connect mode." << std::endl; + continue; + } + in_connect_mode = true; + std::cout << "Enter the gateway IP address: "; + std::cin >> ip; + std::cout << "Enter the gateway port: "; + std::cin >> port; + ConnectToGateway(ip, port); + } else if (command == "exit") { + break; // 接收到退出命令,退出主循环 + } else { + std::cout << "Unknown command" << std::endl; + } + } + + // 设置退出标志 + exit_flag = true; + + // 强制结束HTTP服务器线程 + if (http_server_thread.joinable()) { + http_server_thread.join(); + } + + // 关闭所有套接字 + for (auto& sock_pair : sockets) { + shutdown(sock_pair.first, SHUT_RDWR); + close(sock_pair.first); + } + sockets.clear(); + + // 等待所有工作线程结束 + for (auto& thread : threads) { + if (thread.joinable()) { + thread.join(); + } + } + threads.clear(); + + std::cout << "Exited program." << std::endl; + return 0; + +} +*/ \ No newline at end of file diff --git a/src/controller/interface/connector_controller.h b/src/controller/interface/connector_controller.h new file mode 100644 index 0000000..c45ca15 --- /dev/null +++ b/src/controller/interface/connector_controller.h @@ -0,0 +1,22 @@ +#ifndef CONNECTOR_CONTROLLER_H +#define CONNECTOR_CONTROLLER_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// 函数声明 +void StartHttpServer(); +void ConnectToGateway(const std::string& ip, int port); +void KeepConnection(int sock); +void HandleHttpRequest(int client_socket); + +#endif // CONNECTOR_CONTROLLER_H \ No newline at end of file diff --git a/src/controller/interface/connector_database.cpp b/src/controller/interface/connector_database.cpp new file mode 100644 index 0000000..212bfd5 --- /dev/null +++ b/src/controller/interface/connector_database.cpp @@ -0,0 +1,68 @@ +/* + 这个文件可以只负责链接数据库,其他的都不管 + 把数据库的句柄传给主程序 +*/ + +/* +#include +#include + +int main() { + MYSQL *conn; + conn = mysql_init(NULL); + + if (conn == NULL) { + std::cerr << "MySQL init failed" << std::endl; + return 1; + } + + const char *server = "localhost"; + const char *user = "rtsw"; + const char *password = "123456"; + const char *database = "nginxdb"; + unsigned int port = 3306; // 使用云数据库提供的端口 + + if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) { + std::cerr << "Connection error: " << mysql_error(conn) << std::endl; + mysql_close(conn); + return 1; + } + + std::cout << "Successfully connected to the database" << std::endl; + + // ... 执行数据库操作 ... + + mysql_close(conn); + return 0; +} +*/ + +#include "connector_database.h" + +// DatabaseOperation函数定义 +void DatabaseOperation() { + MYSQL *conn = mysql_init(NULL); + + if (conn == NULL) { + std::cerr << "MySQL init failed" << std::endl; + return; + } + + const char *server = "localhost"; + const char *user = "rtsw"; + const char *password = "123456"; + const char *database = "nginxdb"; + unsigned int port = 3306; + + if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) { + std::cerr << "Connection error: " << mysql_error(conn) << std::endl; + mysql_close(conn); + return; + } + + std::cout << "Successfully connected to the database" << std::endl; + + // ... 执行数据库操作 ... + + mysql_close(conn); +} \ No newline at end of file diff --git a/src/controller/interface/connector_database.h b/src/controller/interface/connector_database.h new file mode 100644 index 0000000..83a2cf6 --- /dev/null +++ b/src/controller/interface/connector_database.h @@ -0,0 +1,10 @@ +#ifndef CONNECTOR_DATABASE_H +#define CONNECTOR_DATABASE_H + +#include +#include + +// 函数声明 +void DatabaseOperation(); + +#endif // CONNECTOR_DATABASE_H \ No newline at end of file diff --git a/src/controller/interface/main.cpp b/src/controller/interface/main.cpp new file mode 100644 index 0000000..e469900 --- /dev/null +++ b/src/controller/interface/main.cpp @@ -0,0 +1,35 @@ +#include +#include +#include "connector_controller.h" +#include "connector_database.h" + +int main() { + std::string command; + bool running = true; + + while (running) { + std::cout << "Enter command (start_http_server/connect_gateway/database_operation/exit): "; + std::cin >> command; + + if (command == "start_http_server") { + StartHttpServer(); + } else if (command == "connect_gateway") { + std::string ip; + int port; + std::cout << "Enter the gateway IP address: "<>ip; // 使用getline以获取包含空格的IP地址 + std::cout << "Enter the gateway port: "<> port; + ConnectToGateway(ip, port); + } else if (command == "database_operation") { + DatabaseOperation(); + } else if (command == "exit") { + running = false; + } else { + std::cout << "Unknown command" << std::endl; + } + } + + std::cout << "Exiting program." << std::endl; + return 0; +} \ No newline at end of file diff --git a/src/controller/interface/main_program b/src/controller/interface/main_program new file mode 100644 index 0000000..71bad27 Binary files /dev/null and b/src/controller/interface/main_program differ diff --git a/src/controller/new_client.cpp b/src/controller/new_client.cpp new file mode 100644 index 0000000..78f05f7 --- /dev/null +++ b/src/controller/new_client.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFFER_SIZE 1024 + +std::vector threads; // 存储所有连接线程 +std::map sockets; // 存储套接字 +std::mutex sockets_mutex; // 用于同步访问 sockets +std::atomic exit_flag(false); // 原子退出标志 + + +// 维护连接的函数 +void KeepConnection(int sock) { + char buffer[BUFFER_SIZE]; + int nbytes; + while (!exit_flag) { + nbytes = recv(sock, buffer, BUFFER_SIZE, 0); + if (nbytes <= 0) { + std::cout << "Gateway disconnected" << std::endl; + break; + } + std::cout << "Received from gateway (Socket " << sock << "): " << buffer << std::endl; + } + + { + std::lock_guard lock(sockets_mutex); + sockets.erase(sock); // 从集合中移除套接字 + } + close(sock); +} +// 连接到网关的函数 +void ConnectToGateway(const std::string& ip, int port) { + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror("Could not create socket"); + return; + } + + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + if (inet_pton(AF_INET, ip.c_str(), &server_addr.sin_addr) <= 0) { + perror("Invalid address"); + close(sock); + return; + } + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + perror("Connection to the server failed"); + close(sock); + return; + } + + { + std::lock_guard lock(sockets_mutex); + sockets[sock] = 1; // 将套接字添加到集合中 + } + + std::cout << "Connected to gateway at " << ip << ":" << port << " (Socket " << sock << ")" << std::endl; + + // 在新线程中维护连接 + threads.emplace_back(KeepConnection, sock); +} + + + +int main() { + std::string command, ip; + int port; + bool in_connect_mode = false; + + while (true) { + std::cout << "Enter command (connect/exitconnect/exit): "; + std::cin >> command; + + if (command == "connect") { + if (in_connect_mode) { + std::cout << "Already in connect mode." << std::endl; + continue; + } + in_connect_mode = true; // 进入连接模式 + std::cout << "Enter the gateway IP address: "; + std::cin >> ip; + std::cout << "Enter the gateway port: "; + std::cin >> port; + ConnectToGateway(ip, port); + } else if (command == "exitconnect") { + if (!in_connect_mode) { + std::cout << "Not in connect mode." << std::endl; + continue; + } + in_connect_mode = false; // 退出连接模式 + std::cout << "Exiting connect mode." << std::endl; + } else if (command == "exit") { + exit_flag = true; // 设置退出标志 + std::cout << "Exiting program." << std::endl; + // 关闭所有套接字 + for (auto& sock_pair : sockets) { + shutdown(sock_pair.first, SHUT_RDWR); // 关闭套接字的发送和接收 + close(sock_pair.first); + } + sockets.clear(); + // 等待所有线程结束 + for (auto& thread : threads) { + if (thread.joinable()) { + thread.join(); + } + } + threads.clear(); + break; // 退出主循环 + } else { + std::cout << "Unknown command" << std::endl; + } + } + + return 0; +} \ No newline at end of file