Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	CMakeLists.txt
  • Loading branch information
tomery11 committed Jan 16, 2019
2 parents 0525dab + 436c4be commit 2a9158a
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 14 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ add_executable(fligtSimProj2 main.cpp ISearcher.h ISearchable.h CacheManager.cpp
MyTestClientHandler.h MySerialServer.cpp MyParallelServer.cpp MyParallelServer.h
PQueueSearcher.h Searcher.h Server.h Solver.h State.h
State.cpp StatePriority.h StringReverser.h StringReverser.cpp Matrix.h Matrix.cpp DepthFirstSearch.h
BreadthFirstSearch.h MatrixSearchClient.h MatrixSearchClient.cpp Client.h BestFirstSearch.h AStar.h
MyClientHandler.cpp MyClientHandler.h)
BreadthFirstSearch.h MatrixSearchClient.h MatrixSearchClient.cpp Client.h BestFirstSearch.h)
4 changes: 2 additions & 2 deletions DepthFirstSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include <stack>
#include "Searcher.h"
template <class T>

class DepthFirstSearch {
template <class T>
class DepthFirstSearch : public Searcher<State<T>>{
private:
stack<State<T>*> openList1;
stack<State<T>*> openList2;
Expand Down
1 change: 1 addition & 0 deletions ISearchable.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ISearchable {
virtual State<T>* getInitialState()=0;
virtual bool isGoal(State<T>*)=0;
virtual set<State<T>*> getAllPossibleStates(State<T>*)=0;
virtual string getStrRepresentation() = 0;
virtual ~ISearchable()= default;
};

Expand Down
18 changes: 13 additions & 5 deletions Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <vector>
#include "MatrixSearchClient.h"
#include "MyParallelServer.h"
#include "MyClientHandler.h"
#include "SolverToSearcherAdapter.h"
#include "Matrix.h"
using namespace std;

//template class CacheManager<string,string>;
Expand All @@ -39,12 +42,17 @@ int Main::main(int argc, char *argv[]) {//todo no new
CacheManager<string,string> *cacheManager = &fcm;
cout << "Main after cache" << endl;
//create solver
StringReverser stringRev;
Solver<string, string> *solver = &stringRev;
//StringReverser stringRev;
//Solver<string, string> *solver = &stringRev;

SolverToSearcherAdapter<Matrix, string> solver1 = SolverToSearcherAdapter<Matrix, string>();
Solver<Matrix, string> *solver = &solver1;

//SolverToSearcherAdapter(ISearcher *searcher)

//create client handler
MyTestClientHandler testClient(solver, cacheManager);
ClientHandler *clientHandler = &testClient;
MyClientHandler client(solver, cacheManager);
ClientHandler *clientHandler = &client;


//get message
Expand All @@ -68,7 +76,7 @@ int Main::main(int argc, char *argv[]) {//todo no new
MatrixSearchClient searcherMatrix;
searcherMatrix.open("127.0.0.1" ,port, &message);
sleep(1);
//myServer->stop();
myServer->stop();
return 0;
} catch (const char *exception) {
printf("%s",exception);
Expand Down
15 changes: 15 additions & 0 deletions MatrixCacheAdapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Created by t on 1/16/19.
//

#include "MatrixCacheAdapter.h"

MatrixCacheAdapter::MatrixCacheAdapter(FileCacheManager *fileCacheManager1) {
this->fileCacheManager = fileCacheManager1;
}

bool MatrixCacheAdapter::hasSolutionForProblem(Matrix problem) {
this->fileCacheManager->hasSolutionForProblem(problem.)
}
string MatrixCacheAdapter::getSolutionForProblem(Matrix problem);
void MatrixCacheAdapter::save(Matrix problem, string solution);
23 changes: 23 additions & 0 deletions MatrixCacheAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by t on 1/16/19.
//

#ifndef FLIGTSIMPROJ2_MATRIXCACHEADAPTER_H
#define FLIGTSIMPROJ2_MATRIXCACHEADAPTER_H

#include "FileCacheManager.h"
#include "CacheManager.h"
#include "Matrix.h"
using namespace std;

class MatrixCacheAdapter : public CacheManager<Matrix, string> {
FileCacheManager *fileCacheManager;
public:
MatrixCacheAdapter(FileCacheManager *fileCacheManager1);
virtual bool hasSolutionForProblem(Matrix problem);
virtual string getSolutionForProblem(Matrix problem);
virtual void save(Matrix problem, string solution);
};


#endif //FLIGTSIMPROJ2_MATRIXCACHEADAPTER_H
3 changes: 3 additions & 0 deletions MyClientHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include <vector>
#include "Utils.h"
#include <algorithm>
#include <iostream>

#define BUFFER_LENGTH 512
using namespace std;

//talk with a client. client write lines format: num,num,num,num\n, then row,col\n row,col\n, end\n.
//then client handler return the solution: values {Up, Down, Left, Right} with ',' between them.
void MyClientHandler::handleClient(int socket) {
cout << "start jandle client" << endl;
//get a problem from client
char inputBuffer[BUFFER_LENGTH + 1];
char outputBuffer[BUFFER_LENGTH + 1];
Expand Down
16 changes: 13 additions & 3 deletions MyParallelServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using namespace std;
//little server threads: run in a thread, talk to clients.
void *clientTalkThreadFunc(void *talkingData) {
try {
cout << "little thread" << endl;
//TalkingData is of type TalkingData
TalkingData *talkingData1;
talkingData1 = (TalkingData *) talkingData;
Expand Down Expand Up @@ -51,7 +52,7 @@ int MyParallelServer::listenAccept(int time, struct ServerData *serverData1) {
//in timeout end all the threads
if (errno == EWOULDBLOCK) {
cout << "timeout!" << endl;
serverData1->parallelServer->stop();
serverData1->parallelServer->setDoneListening(true);
} else { //in other error throw an exception
throw "accept failed";
}
Expand All @@ -78,14 +79,15 @@ void* parallelServerThreadFunc(void *serverData) {
talking1->newSocket = newSocket;
talking1->clientHandler = serverData1->clientHandler;
//create a thread to talk with
int rc = pthread_create(&threadID, nullptr, parallelServerThreadFunc, (void *)&talking1);
int rc = pthread_create(&threadID, nullptr, clientTalkThreadFunc, (void *)&talking1);
if (rc) {
throw "unable to create thread";
}
threads.push_back(threadID);
serverData1->parallelServer->setDoneListening(true);//todo delete this
//talk with the other clients: client after client serially. 1 second max to wait, end if timeout.
//cout << *serverData1->setStop << endl;
while (!(*serverData1->setStop)) {
while (!(serverData1->parallelServer->getDoneListening())) {
//listen for 1 second. set the new socket number for the thread
newSocket = serverData1->parallelServer->listenAccept(1, serverData1);
auto *talking = new TalkingData;
Expand Down Expand Up @@ -213,3 +215,11 @@ void MyParallelServer::stopThreads(vector<pthread_t> *threads, vector<TalkingDat
}
cout << "after free all but one thread" << endl;
}

void MyParallelServer::setDoneListening(bool done) {
this->doneListening = done;
}

bool MyParallelServer::getDoneListening() {
return this->doneListening;
}
3 changes: 3 additions & 0 deletions MyParallelServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ class MyParallelServer : public Server {
pthread_t threadID;
//vector<pthread_t> threads;
bool setStop = false;
bool doneListening = false;
int socketDescriptor;
struct ServerData *serverData;
public:
void stopThreads(vector<pthread_t> *threads, vector<TalkingData*> *talkingStructs);
int listenAccept(int time, struct ServerData *serverData1);
virtual void open(int port, ClientHandler *clientHandler);
virtual void stop();
void setDoneListening(bool done);
bool getDoneListening();

};

Expand Down
1 change: 1 addition & 0 deletions Solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ template<class Problem, class Solution> class Solver {
public:
virtual Solution solve(Problem problem) = 0;
virtual string ProblemToString(Problem problem) = 0;
//void setProblem(Problem *problem) = 0;
};
#endif //FLIGTSIMPROJ2_SOLVER_H
20 changes: 18 additions & 2 deletions SolverToSearcherAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class SolverToSearcherAdapter : public Solver<Problem, Solution> {
ISearcher *searcher;
ISearchable<Problem> *searchable;
public:
explicit SolverToSearcherAdapter(ISearcher *searcher, ISearchable<Problem> *searchable);
explicit SolverToSearcherAdapter(ISearcher *searcher);
SolverToSearcherAdapter();
//void setSearchable(ISearchable<Problem> *searchable);
virtual Solution solve(Problem problem);
virtual string ProblemToString(Problem problem);
};

template<class Problem, class Solution>
SolverToSearcherAdapter<Problem, Solution>::SolverToSearcherAdapter(ISearcher *searcher, ISearchable<Problem> *searchable) {
SolverToSearcherAdapter<Problem, Solution>::SolverToSearcherAdapter(ISearcher *searcher) {
this->searcher = searcher;
this->searchable = searchable;
}
Expand All @@ -36,4 +38,18 @@ string SolverToSearcherAdapter<Problem, Solution>::ProblemToString(Problem probl
return std::__cxx11::string();
}

/*
template<class Problem, class Solution>
void SolverToSearcherAdapter<Problem, Solution>::setSearchable(ISearchable<Problem> *searchable) {
this->searchable = searchable;
}
template<class Problem, class Solution>
void SolverToSearcherAdapter<Problem, Solution>::setSearcher(ISearcher *searcher) {
this->searcher = searcher;
}*/

template<class Problem, class Solution>
SolverToSearcherAdapter<Problem, Solution>::SolverToSearcherAdapter() {}

#endif //FLIGTSIMPROJ2_SOLVERTOSEARCHERADAPTER_H

0 comments on commit 2a9158a

Please sign in to comment.