-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
112 lines (92 loc) · 2.53 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
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
/* import libraries */
/*************************************/
#include <stdio.h>
#include <string>
#include <Request.h>
#include "HttpClient.h"
#include <Parser.h>
/* main function */
/*************************************/
void connectionStatus(int status){
if(status == -1){
cout << "Connecting Disconnected\n";
}
}
int applyCommands(vector<string> userCommands, HttpClient client){
vector<Request> reqs;
int status;
for(string command : userCommands) {
// parse user command
Request requestObj = Parser::parseInputCommand(command);
if(requestObj.getMethod() == GET){
reqs.push_back(requestObj);
}else{
if(!reqs.empty()) {
status = client.sendGETRequests(reqs);
connectionStatus(status);
cout << "Done getting\n";
}
status = client.sendPOSTRequest(requestObj);
connectionStatus(status);
cout << "Done posting\n";
}
}
if(!reqs.empty()){
status = client.sendGETRequests(reqs);
connectionStatus(status);
cout << "Done getting\n";
}
client.closeConnection();
return 0;
}
static void* runThread(void *p){
int port = 8000;
char *serverAddress = "127.0.0.1";
vector<string> userCommands = {"GET s.jpeg 127.0.0.1"};
string dataDirectory = ".";
HttpClient client(dataDirectory);
client.connectionInit(serverAddress, port);
applyCommands(userCommands, client);
return NULL;
}
int testMain(){
int sz = 1;
pthread_t ts[sz];
for(int i = 0 ; i < sz ; i++){
pthread_create(&ts[i],NULL, runThread, NULL);
usleep(100);
}
void *status;
for(int i = 0 ; i < sz ; i++){
pthread_join(ts[i], &status);
free(status);
}
return 0;
}
int main(int argc, char *argv[])
{
// input shape ./client server-ip port-number file-name
// where to store data on client module
/*
// reading args
int port = atoi(argv[2]);
char *serverAddress = argv[1];
char *fileName = argv[3];
vector<string> userCommands = {};
string command;
ifstream myfile (fileName);
if(myfile.is_open()){
while ( getline (myfile,command) )
{
userCommands.push_back(command);
cout << command << endl;
}
myfile.close();
}
string dataDirectory = ".";
HttpClient client(dataDirectory);
client.connectionInit(serverAddress, port);
applyCommands(userCommands, client);
*/
testMain();
}