-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainTrain.cpp
197 lines (168 loc) · 5.07 KB
/
MainTrain.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* run2.cpp
*
* Created on: 8 ����� 2019
* Author: Eli
*/
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <thread>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <ctime>
#include <fcntl.h>
#include "Server.h"
using namespace std;
inline void writeStr(string input,int serverFD){
write(serverFD,input.c_str(),input.length());
write(serverFD,"\n",1);
}
string readStr(int serverFD){
string serverInput="";
char c=0;
read(serverFD,&c,sizeof(char));
while(c!='\n'){
serverInput+=c;
read(serverFD,&c,sizeof(char));
}
return serverInput;
}
void readMenu(ofstream& out, int serverFD){
bool done=false;
while(!done){
// read string line
string serverInput = readStr(serverFD);
if(serverInput=="6.exit")
done=true;
out<<serverInput<<endl;
}
}
int initClient(int port)throw (const char*){
int serverFD, n;
struct sockaddr_in serv_addr;
struct hostent *server;
serverFD = socket(AF_INET, SOCK_STREAM, 0);
if (serverFD < 0)
throw "socket problem";
server = gethostbyname("localhost");
if(server==NULL)
throw "no such host";
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(port);
if (connect(serverFD,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
throw "connection problem";
return serverFD;
}
void clientSide1(int port,string outputFile)throw (const char*){
int serverFD = initClient(port);
ofstream out(outputFile);
readMenu(out, serverFD);
out.close();
string input="6";
writeStr(input,serverFD);
close(serverFD);
cout<<"end of clt 1"<<endl;
}
void clientSide2(int port,string outputFile) throw (const char*){
int serverFD = initClient(port);
ofstream out(outputFile);
ifstream in("input.txt");
string input="";
try {
while (input != "6") {
/*if (input == "0.95")
cout << "PROBLEM HERE" << endl;*/
readMenu(out, serverFD);
getline(in, input);
//writeStr(input,serverFD);
write(serverFD, input.c_str(), input.length());
write(serverFD, "\n", 1);
if (input == "1") {
out << readStr(serverFD) << endl; // please upload...
while (input != "done") {
getline(in, input);
writeStr(input, serverFD);
}
out << readStr(serverFD) << endl; // Upload complete
out << readStr(serverFD) << endl; // please upload...
input = "";
while (input != "done") {
getline(in, input);
writeStr(input, serverFD);
}
out << readStr(serverFD) << endl; // Upload complete
}
if (input == "3") {
out << readStr(serverFD) << endl; // Anomaly... complete
}
if (input == "5") {
out << readStr(serverFD) << endl; // please upload...
while (input != "done" && input != "done\n") {
getline(in, input);
writeStr(input, serverFD);
}
out << readStr(serverFD) << endl; // Upload complete
out << readStr(serverFD) << endl; // TPR
out << readStr(serverFD) << endl; // FPR
}
}
} catch (std::invalid_argument &x) {
cout << x.what() << endl;
} catch (...) {
cout << ":<" << endl;
}
in.close();
out.close();
close(serverFD);
cout<<"end of clt 2"<<endl;
}
size_t check(string outputFile,string expectedOutputFile){
ifstream st(outputFile);
ifstream ex(expectedOutputFile);
size_t i=0;
string lst,lex;
while(!ex.eof()){
getline(st,lst);
getline(ex,lex);
if(lst.compare(lex)!=0)
i++;
}
st.close();
ex.close();
return i;
}
int main(){
srand (time(NULL));
int port=5000+ rand() % 1000;
string outputFile1="output_menu";
string outputFile2="output";
int x=rand() % 1000;
outputFile1+=to_string(x);
outputFile1+=".txt";
outputFile2+=to_string(x);
outputFile2+=".txt";
pid_t server_pid;
try{
AnomalyDetectionHandler adh;
Server server(port);
// starts server on a new process, and return it's pid.
server_pid = server.start(adh); // runs on its own thread
// let's run 2 clients
clientSide1(port,outputFile1);
clientSide2(port,outputFile2);
server.stop(); // joins the svr's thread
}catch(const char* s){
cout<<s<<endl;
}
/* size_t mistakes = check(outputFile1,"expected_output_menu.txt");
mistakes += check(outputFile2,"expected_output.txt");
if(mistakes>0)
cout<<"you have "<<mistakes<<" mistakes in your output (-"<<(mistakes*2)<<")"<<endl;*/
//wait(&server_pid);
cout<<"done"<<endl;
return 0;
}