Skip to content

Commit

Permalink
kibalt? 30/12 13:50
Browse files Browse the repository at this point in the history
  • Loading branch information
yarintz33 committed Dec 31, 2021
1 parent 105156a commit 4ae0579
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 66 deletions.
2 changes: 1 addition & 1 deletion CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CLI::CLI(DefaultIO* dio) {
this->dio = dio;
this->theCommands.push_back(new Upload(this->dio));
this->theCommands.push_back(new Correlation(this->dio));
this->theCommands.push_back(new HybridAlgo(this->dio));
this->theCommands.push_back(new detect(this->dio));
this->theCommands.push_back(new printAnomaly(this->dio));
this->theCommands.push_back(new UploadAnom(this->dio));
this->theCommands.push_back(new Exit(this->dio));
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.20)
project(ass1)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_CXX_FLAGS -pthread)
add_executable(ass1 anomaly_detection_util.cpp anomaly_detection_util.h AnomalyDetector.h
SimpleAnomalyDetector.cpp SimpleAnomalyDetector.h timeseries.h timeseries.cpp minCircle.cpp minCircle.h
HybridAnomalyDetector.h HybridAnomalyDetector.cpp CLI.cpp CLI.h commands.h expectedOutput.txt input.txt MainTrain.cpp)
HybridAnomalyDetector.h HybridAnomalyDetector.cpp CLI.cpp CLI.h commands.h expectedOutput.txt input.txt MainTrain.cpp Server.h Server.cpp)


210 changes: 153 additions & 57 deletions MainTrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,181 @@

#include <iostream>
#include <fstream>
#include "commands.h"
#include "CLI.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "Server.h"




using namespace std;

void writeStr(string input,int serverFD){
write(serverFD,input.c_str(),input.length());
write(serverFD,"\n",1);
}

class STDtest:public DefaultIO{
ifstream in;
ofstream out;
public:
STDtest(string inputFile,string outputFile){
in.open(inputFile);
out.open(outputFile);
}
virtual string read(){
string s;
in>>s;
return s;
}
virtual void write(string text){
out<<text;
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;
}

virtual void write(float f){
out<<f;
void readMenue(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;
}
}

virtual void read(float* f){
in>>*f;
}
int initClient(int port)throw (const char*){
int serverFD, n;
struct sockaddr_in serv_addr;
struct hostent *server;

void close(){
if(in.is_open())
in.close();
if(out.is_open())
out.close();
}
~STDtest(){
close();
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);
readMenue(out,serverFD);
out.close();
string input="6";
writeStr(input,serverFD);
close(serverFD);
cout<<"end of client 1"<<endl;
}


void clientSide2(int port,string outputFile)throw (const char*){

int serverFD = initClient(port);

ofstream out(outputFile);
ifstream in("input.txt");
string input="";
while(input!="6"){
readMenue(out,serverFD);
getline(in,input);
writeStr(input,serverFD);
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"){
getline(in,input);
writeStr(input,serverFD);
}
out<<readStr(serverFD)<<endl; // Upload complete
out<<readStr(serverFD)<<endl; // TPR
out<<readStr(serverFD)<<endl; // FPR
}
}
};
in.close();
out.close();

close(serverFD);
cout<<"end of client 2"<<endl;
}

void check(string outputFile,string expectedOutputFile){
size_t chk[]={31,62,63,74,75,86,87,98,99,110,111};
size_t check(string outputFile,string expectedOutputFile){
ifstream st(outputFile);
ifstream ex(expectedOutputFile);
size_t i=1,j=0;
size_t i=0;
string lst,lex;
while(!st.eof() && !ex.eof()){
while(!ex.eof()){
getline(st,lst);

getline(ex,lex);
//cout<<lst<<endl;

if(i<13 && lst.compare(lex)!=0){ // 12
cout<<"line "<<i<<" expected: "<<lex<<" you got "<<lst<<endl;
cout<<"wrong output (-1)"<<endl;
}else
if(j<11 && i==chk[j]){
if(lst.compare(lex)!=0){ // 88
cout<<"line "<<i<<" expected: "<<lex<<" you got "<<lst<<endl;
cout<<"wrong output (-8)"<<endl;
}
j++;
}
i++;
if(lst.compare(lex)!=0)
i++;
}
if(j<11)
cout<<"wrong output size (-"<<(11-j)*8<<")"<<endl;
st.close();
ex.close();
return i;
}

//small test

int main(){
STDtest std("input.txt","output.txt");
CLI cli(&std);
cli.start();
std.close();
check("output.txt","expectedOutput.txt");

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";

try{
AnomalyDetectionHandler adh;
Server server(port);
server.start(adh); // runs on its own thread
// let's run 2 clients
clientSide1(port,outputFile1);
clientSide2(port,outputFile2);
server.stopServer(); // joins the server'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;

cout<<"done"<<endl;
return 0;
}
73 changes: 73 additions & 0 deletions Server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

#include "Server.h"

string socketIO::read(){
char c=0;
string s = "";
while(c != '\n'){
recv(clientID,&c,sizeof(char),0);
s += c;
}
return s;
}
void socketIO::write(string text){
const char* txt=text.c_str();
send(clientID,txt,strlen(txt),0);
}

void socketIO::write(float f){
ostringstream oss;
oss <<f;
string str(oss.str());
write(str);
}

void socketIO::read(float* f){
}


Server::Server(int port)throw (const char*) {
socNum = socket(AF_INET,SOCK_STREAM,0);
if(socNum < 0)
throw "Error! socket failed";
hasStopped = false;
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);

if(bind(socNum,(struct sockaddr*)&server, sizeof(server))<0)
throw "bind failure";

if(listen(socNum, 3)<0)
throw "listen failure";
}

void sigHandler(int sigNum){
cout<<"sidH"<<endl;
}

void Server::start(ClientHandler& cltHandler)throw(const char*){
this->trd = new thread([&cltHandler,this](){
signal(SIGALRM,sigHandler);
while(this->hasStopped == false){
socklen_t clientSize = sizeof(this->client);
alarm(1);
int acceptedClient = accept(this->socNum,(struct sockaddr*)&this->client,&clientSize);
if(acceptedClient>0){
cltHandler.handle(acceptedClient);
close(acceptedClient);
}
alarm(0);

}
close(this->socNum);
});
}

void Server::stopServer(){
this->hasStopped=true;
this->trd->join();
}

Server::~Server() {
}
Loading

0 comments on commit 4ae0579

Please sign in to comment.