-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgunrock.cpp
179 lines (154 loc) · 4.45 KB
/
gunrock.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <fcntl.h>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <sstream>
#include <deque>
#include "ClientError.h"
#include "HTTPRequest.h"
#include "HTTPResponse.h"
#include "HttpService.h"
#include "HttpUtils.h"
#include "FileService.h"
#include "DistributedFileSystemService.h"
#include "MySocket.h"
#include "MyServerSocket.h"
#include "dthread.h"
using namespace std;
int PORT = 8080;
int THREAD_POOL_SIZE = 1;
int BUFFER_SIZE = 1;
string BASEDIR = "ds3";
string SCHEDALG = "FIFO";
string LOGFILE = "/dev/null";
string DISKFILE = "disk.img";
vector<HttpService *> services;
HttpService *find_service(HTTPRequest *request) {
// find a service that is registered for this path prefix
for (unsigned int idx = 0; idx < services.size(); idx++) {
if (request->getPath().find(services[idx]->pathPrefix()) == 0) {
return services[idx];
}
}
return NULL;
}
void invoke_service_method(HttpService *service, HTTPRequest *request, HTTPResponse *response) {
stringstream payload;
try {
// invoke the service if we found one
if (service == NULL) {
// not found status
response->setStatus(404);
} else if (request->isHead()) {
service->head(request, response);
} else if (request->isGet()) {
service->get(request, response);
} else if (request->isPut()) {
service->put(request, response);
} else if (request->isPost()) {
service->post(request, response);
} else if (request->isDelete()) {
service->del(request, response);
} else if (request->isMove()) {
service->move(request, response);
} else {
// The server doesn't know about this method
response->setStatus(501);
}
} catch (ClientError &ce) {
response->setStatus(ce.status_code);
} catch (...) {
// reset the response object and return an error
response->setBody("");
response->setStatus(500);
}
}
void handle_request(MySocket *client) {
HTTPRequest *request = new HTTPRequest(client, PORT);
HTTPResponse *response = new HTTPResponse();
stringstream payload;
// read in the request
bool readResult = false;
try {
payload << "client: " << (void *) client;
sync_print("read_request_enter", payload.str());
readResult = request->readRequest();
sync_print("read_request_return", payload.str());
} catch (...) {
// swallow it
}
if (!readResult) {
// there was a problem reading in the request, bail
delete response;
delete request;
sync_print("read_request_error", payload.str());
return;
}
HttpService *service = find_service(request);
invoke_service_method(service, request, response);
// send data back to the client and clean up
payload.str(""); payload.clear();
payload << " RESPONSE " << response->getStatus() << " client: " << (void *) client;
sync_print("write_response", payload.str());
cout << payload.str() << endl;
client->write(response->response());
delete response;
delete request;
payload.str(""); payload.clear();
payload << " client: " << (void *) client;
sync_print("close_connection", payload.str());
client->close();
delete client;
}
int main(int argc, char *argv[]) {
signal(SIGPIPE, SIG_IGN);
int option;
while ((option = getopt(argc, argv, "d:p:t:b:s:l:i:")) != -1) {
switch (option) {
case 'd':
BASEDIR = string(optarg);
break;
case 'p':
PORT = atoi(optarg);
break;
case 't':
THREAD_POOL_SIZE = atoi(optarg);
break;
case 'b':
BUFFER_SIZE = atoi(optarg);
break;
case 's':
SCHEDALG = string(optarg);
break;
case 'l':
LOGFILE = string(optarg);
break;
case 'i':
DISKFILE = string(optarg);
break;
default:
cerr<< "usage: " << argv[0] << " [-p port] [-t threads] [-b buffers] [-i diskFile]" << endl;
exit(1);
}
}
set_log_file(LOGFILE);
cout << "Lisening on port " << PORT << endl;
sync_print("init", "");
MyServerSocket *server = new MyServerSocket(PORT);
MySocket *client;
// The order that you push services dictates the search order
// for path prefix matching
services.push_back(new DistributedFileSystemService(DISKFILE));
services.push_back(new FileService(BASEDIR));
while(true) {
sync_print("waiting_to_accept", "");
client = server->accept();
sync_print("client_accepted", "");
handle_request(client);
}
}