From 4ae05792033f3deb96e5cbefb258c3b68f74b032 Mon Sep 17 00:00:00 2001 From: yarintz33 Date: Fri, 31 Dec 2021 03:43:06 -0800 Subject: [PATCH] kibalt? 30/12 13:50 --- CLI.cpp | 2 +- CMakeLists.txt | 4 +- MainTrain.cpp | 210 +++++++++++++++++++++++++++++++++++-------------- Server.cpp | 73 +++++++++++++++++ Server.h | 70 +++++++++++++++++ commands.h | 13 +-- 6 files changed, 306 insertions(+), 66 deletions(-) create mode 100644 Server.cpp create mode 100644 Server.h diff --git a/CLI.cpp b/CLI.cpp index 4930f7e..710edcd 100644 --- a/CLI.cpp +++ b/CLI.cpp @@ -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)); diff --git a/CMakeLists.txt b/CMakeLists.txt index 7228a52..588b397 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/MainTrain.cpp b/MainTrain.cpp index 3bb7fa8..293d1b9 100644 --- a/MainTrain.cpp +++ b/MainTrain.cpp @@ -7,85 +7,181 @@ #include #include -#include "commands.h" -#include "CLI.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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<>*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"<0) + cout<<"you have "<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() { +} diff --git a/Server.h b/Server.h new file mode 100644 index 0000000..5994890 --- /dev/null +++ b/Server.h @@ -0,0 +1,70 @@ +/* + * Server.h + * + * Created on: Dec 13, 2020 + * Author: Eli + */ + +#ifndef SERVER_H_ +#define SERVER_H_ +#include "CLI.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + +// edit your ClientHandler interface here: +class ClientHandler{ +public: + virtual void handle(int clientID)=0; +}; + +// you can add helper classes + +class socketIO:public DefaultIO{ + int clientID; +public: + socketIO(int clientID):clientID(clientID){} + virtual string read(); + virtual void write(string text); + virtual void write(float f); + virtual void read(float* f); +}; + + +// edit your AnomalyDetectionHandler class here +class AnomalyDetectionHandler:public ClientHandler{ +public: + virtual void handle(int clientID){ + socketIO socketIO(clientID); + CLI cli(&socketIO); + cli.start(); + } +}; + +class Server { + thread* trd; + int socNum; + sockaddr_in server; + sockaddr_in client; + volatile bool hasStopped; + +public: + Server(int port) throw (const char*); + virtual ~Server(); + void start(ClientHandler& cltHandler)throw(const char*); + void stopServer(); +}; + +#endif /* SERVER_H_ */ \ No newline at end of file diff --git a/commands.h b/commands.h index 7138feb..da9b6ea 100644 --- a/commands.h +++ b/commands.h @@ -5,7 +5,6 @@ #include #include - #include #include #include "HybridAnomalyDetector.h" @@ -71,7 +70,7 @@ class Upload:public Command{ void makeFile(string name){ std::ofstream myFile(name); string line = this->dio->read(); - while (line.compare("done") != 0) { + while (line.compare("done\n") != 0) { myFile<dio->read(); @@ -89,16 +88,16 @@ class Correlation:public Command{ float number = std::stof(this->dio->read()); while (number >= 1 || number <= 0){ this->dio->write("please choose a value between 0 and 1.\n"); - float number = std::stof(this->dio->read()); + float number = stof(this->dio->read()); } d->THRESHOLD = number; } }; -class HybridAlgo:public Command{ +class detect:public Command{ public: - HybridAlgo(DefaultIO* dio):Command(dio){} + detect(DefaultIO* dio):Command(dio){} void execute(data *d) { TimeSeries tsTrain = TimeSeries("anomalyTrain.csv"); TimeSeries tsTest = TimeSeries("anomalyTest.csv"); @@ -111,6 +110,8 @@ class HybridAlgo:public Command{ if(ar.size() == 0){ return; } + + // saving the sequences of anomalies into vector "anomalysequences" in struct "data" Anomalyseq anomalyseq; anomalyseq.start = ar.at(0).timeStep; anomalyseq.end = anomalyseq.start; @@ -171,7 +172,7 @@ class UploadAnom:public Command{ dio->write("Please upload your local anomalies file.\n"); string s=""; float TP=0,sum=0,P=0; - while((s=dio->read())!="done"){ + while((s=dio->read())!="done\n"){ P++; size_t t=0; while(s[t] != ','){