ft_irc 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Server.hpp
[詳解]
1
8#ifndef SERVER_HPP
9#define SERVER_HPP
10
11#include <map>
12#include <poll.h>
13#include <string>
14#include <vector>
15
16// #include <cstdlib> // for getenv
17// #include <iostream>
18
19// DEBUG_MODE環境変数が "1" または "true" の場合にのみログを出力
20// #define DEBUG_LOG(message)
21// do {
22// const char* debug_env = std::getenv("DEBUG_MODE");
23// if (debug_env && (std::string(debug_env) == "1" || std::string(debug_env) == "true")) {
24// std::cerr << "[DEBUG] " << __FILE__ << ":" << __LINE__ << " " << message << std::endl;
25// }
26// } while (false)
27// マクロは各行末に '/' を入れて1行としてコードに認識させる。
28
29// Define PING/PONG timeouts based on build type (moved to Server.cpp)
30
31#define UNREGISTERED_CLIENT_TIMEOUT 30 // Seconds to disconnect unregistered clients
32
33class Client;
34class Channel;
35class CommandManager;
36
37// Declaration of the global running flag
38extern volatile bool g_running;
39
49class Server {
50 private:
51 // Private constructor for Singleton
57 Server(int port, const std::string &password);
62 Server(const Server &other);
68 Server &operator=(const Server &other);
69
70 // Attributes
71 int _port;
72 std::string _password;
73 int _server_fd;
74 std::string _serverName;
78 CommandManager *_commandManager;
81 static Server *_instance;
83 // Private methods for internal logic
88 void setupSocket();
89
95 void mainLoop();
96
101 void handleClientData(int fd);
102
107 void handleClientWrite(int fd);
108
112 void checkClientTimeouts();
113
114 public:
115 // Destructor
119 ~Server();
120
121 // Singleton accessor
128 static Server *getInstance(int port = -1, const std::string &password = "");
129
134 void run();
135
136 // --- Testability Additions ---
142 static void resetInstance();
149 void addTestClient(Client *client);
150 // -----------------------------
151
152 // Client management
158 void acceptNewClient();
159
164 void removeClient(int fd);
165
171 Client *getClient(int fd);
172
178 Client *getClientByNickname(const std::string &nickname);
179
180 // Channel management
185 void addChannel(Channel *channel);
186
192 Channel *getChannel(const std::string &name);
193
198 void removeChannel(const std::string &name);
199
205
210 const std::map<int, Client *> &getClients() const;
211
212 // Getters
217 const std::string &getPassword() const;
218
223 const std::string &getServerName() const;
224};
225
226#endif
volatile bool g_running
Definition main.cpp:13
チャンネルのメンバーと状態を管理するクラス。
Definition Channel.hpp:25
Represents an IRC client connected to the server.
Definition Client.hpp:25
Manages the registration, parsing, and execution of IRC commands.
Implements the core IRC server functionality as a Singleton.
Definition Server.hpp:49
Channel * getChannel(const std::string &name)
Retrieves a Channel object by its name.
Definition Server.cpp:406
Client * getClientByNickname(const std::string &nickname)
Retrieves a Client object by its nickname.
Definition Server.cpp:391
const std::map< int, Client * > & getClients() const
Gets a constant reference to the map of clients managed by the server.
Definition Server.cpp:424
const std::map< std::string, Channel * > & getChannels() const
Gets a constant reference to the map of channels managed by the server.
Definition Server.cpp:422
~Server()
Destroys the Server object, cleaning up all clients, channels, and the command manager.
Definition Server.cpp:77
static Server * getInstance(int port=-1, const std::string &password="")
Gets the single instance of the Server (Singleton pattern).
Definition Server.cpp:63
void removeChannel(const std::string &name)
Removes a channel from the server, deleting the Channel object.
Definition Server.cpp:414
const std::string & getPassword() const
Gets the server's password.
Definition Server.cpp:379
void removeClient(int fd)
Removes a client from the server, closing its socket and freeing resources.
Definition Server.cpp:290
void acceptNewClient()
Accepts a new incoming client connection.
Definition Server.cpp:262
const std::string & getServerName() const
Gets the server's name.
Definition Server.cpp:381
void addChannel(Channel *channel)
Adds a new channel to the server.
Definition Server.cpp:400
void addTestClient(Client *client)
Adds a client directly to the server's management for testing purposes.
Definition Server.cpp:48
static void resetInstance()
Resets the singleton instance of the Server for testing purposes.
Definition Server.cpp:43
Client * getClient(int fd)
Retrieves a Client object by its file descriptor.
Definition Server.cpp:383
void run()
Starts the IRC server, setting up the socket and entering the main event loop.
Definition Server.cpp:101