[HICN-626] Return output from libhicnlight
[hicn.git] / apps / http-proxy / main.cc
1 /*
2  * Copyright (c) 2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <hicn/http-proxy/http_proxy.h>
17
18 using namespace transport;
19
20 int usage(char* program) {
21   std::cerr << "USAGE: " << program << "\n"
22             << "[HTTP_PREFIX] -a [SERVER_IP_ADDRESS] "
23                "-p [SERVER_PORT] -c [CACHE_SIZE] -m [MTU] -l [DEFAULT_LIFETIME "
24                "(seconds)] -P [FIRST_IPv6_WORD_HEX] -M (enable manifest)"
25             << std::endl;
26   return -1;
27 }
28
29 struct Params : HTTPProxy::ClientParams, HTTPProxy::ServerParams {
30   void printParams() override {
31     if (client) {
32       HTTPProxy::ClientParams::printParams();
33     } else if (server) {
34       HTTPProxy::ServerParams::printParams();
35     } else {
36       throw std::runtime_error(
37           "Proxy configured as client and server at the same time.");
38     }
39
40     std::cout << "\t"
41               << "N Threads: " << n_thread << std::endl;
42   }
43
44   HTTPProxy* instantiateProxyAsValue() {
45     if (client) {
46       HTTPProxy::ClientParams* p = dynamic_cast<HTTPProxy::ClientParams*>(this);
47       return new transport::HTTPProxy(*p, n_thread);
48     } else if (server) {
49       HTTPProxy::ServerParams* p = dynamic_cast<HTTPProxy::ServerParams*>(this);
50       return new transport::HTTPProxy(*p, n_thread);
51     } else {
52       throw std::runtime_error(
53           "Proxy configured as client and server at the same time.");
54     }
55   }
56
57   bool client = false;
58   bool server = false;
59   std::uint16_t n_thread = 1;
60 };
61
62 int main(int argc, char** argv) {
63   Params params;
64
65   params.prefix = "http://hicn-http-proxy";
66   params.origin_address = "127.0.0.1";
67   params.origin_port = "80";
68   params.cache_size = "50000";
69   params.mtu = "1500";
70   params.first_ipv6_word = "b001";
71   params.content_lifetime = "7200;";  // seconds
72   params.manifest = false;
73   params.tcp_listen_port = 8080;
74
75   int opt;
76   while ((opt = getopt(argc, argv, "CSa:p:c:m:P:l:ML:t:")) != -1) {
77     switch (opt) {
78       case 'C':
79         if (params.server) {
80           std::cerr << "Cannot be both client and server (both -C anc -S "
81                        "options specified.)."
82                     << std::endl;
83           return usage(argv[0]);
84         }
85         params.client = true;
86         break;
87       case 'S':
88         if (params.client) {
89           std::cerr << "Cannot be both client and server (both -C anc -S "
90                        "options specified.)."
91                     << std::endl;
92           return usage(argv[0]);
93         }
94         params.server = true;
95         break;
96       case 'a':
97         params.origin_address = optarg;
98         break;
99       case 'p':
100         params.origin_port = optarg;
101         break;
102       case 'c':
103         params.cache_size = optarg;
104         break;
105       case 'm':
106         params.mtu = optarg;
107         break;
108       case 'P':
109         params.first_ipv6_word = optarg;
110         break;
111       case 'l':
112         params.content_lifetime = optarg;
113         break;
114       case 'L':
115         params.tcp_listen_port = std::stoul(optarg);
116         break;
117       case 'M':
118         params.manifest = true;
119         break;
120       case 't':
121         params.n_thread = std::stoul(optarg);
122         break;
123       case 'h':
124       default:
125         return usage(argv[0]);
126         break;
127     }
128   }
129
130   if (argv[optind] == 0) {
131     std::cerr << "Using default prefix " << params.prefix << std::endl;
132   } else {
133     params.prefix = argv[optind];
134   }
135
136   params.printParams();
137   auto proxy = params.instantiateProxyAsValue();
138   proxy->run();
139   delete proxy;
140
141   return 0;
142 }