-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
64 lines (59 loc) · 2.08 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// main.cpp
#include <iostream>
#include <string>
#include <memory>
#include "./ServerFactory.hpp"
#include "./HTTPServer.hpp"
int main(int argc, char *argv[])
{
if (argc < 4 || argc > 6)
{
std::cerr << "Usage: " << argv[0] << " <server_type> <cache_type> <port> [num_threads] [cache_size]" << std::endl;
std::cerr << "Server types: Threadpool, Semaphore" << std::endl;
std::cerr << "Cache types: LRUCache, LFUCache" << std::endl;
std::cerr << "Port: Any valid port number (e.g., 8080)" << std::endl;
std::cerr << "Num threads: (Optional) Number of threads for the server" << std::endl;
std::cerr << "Cache size: (Optional) Maximum number of elements in the cache" << std::endl;
return 1;
}
std::string serverType = argv[1];
std::string cacheType = argv[2];
int port, numThreads = 25, cacheSize = 100; // Default values
try
{
port = std::stoi(argv[3]);
if (argc > 4)
numThreads = std::stoi(argv[4]);
if (argc > 5)
cacheSize = std::stoi(argv[5]);
}
catch (const std::invalid_argument &e)
{
std::cerr << "Error: Invalid argument. Please provide valid numbers for port, num_threads, and cache_size." << std::endl;
return 1;
}
catch (const std::out_of_range &e)
{
std::cerr << "Error: Number out of range for port, num_threads, or cache_size." << std::endl;
return 1;
}
try
{
std::unique_ptr<HTTPServer> server = ServerFactory::createHTTPServer(serverType, cacheType, numThreads, cacheSize);
std::cout << "Starting HTTP proxy server on port " << port << std::endl;
std::cout << "Number of threads: " << numThreads << std::endl;
std::cout << "Cache size: " << cacheSize << std::endl;
server->start(port);
}
catch (const std::runtime_error &e)
{
std::cerr << "Runtime error: " << e.what() << std::endl;
return 1;
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}