Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ before_install:
- sudo python ./serverAPI.py &

script:
- ./linters/run.sh --local
- mkdir build
- cd build
- cmake ../
Expand Down
11 changes: 11 additions & 0 deletions Attraction
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
1 1 1 Почтампт #0
2 24 78 Музей_Пушкина #Пушкин#Дворец
3 5 13 Красная_площадь #Красная_площадь
4 5 10 Пушкинский_музей #История#Красная_площадь
5 4 8 Грановитая_палата #История#Красная_площадь#Искуство#шапка
6 5 11 Сабор_василия_блаженного #Красная_площадь#История#Храмы
7 56 65 ВДНХ #Выставка#Прогулка
8 58 65 Фонтан_дружбы_народов #Фонтан#История
9 54 63 Музей_космонавтики #Музеи#История#Космос
10 60 65 Каменный_цветок #Фонтан#История

16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ include_directories(include)

add_library(algorithm STATIC src/algorithm.cpp)

add_library(queu STATIC src/queu.cpp)
add_library(queue STATIC src/queue.cpp)

add_library(server STATIC src/server.cpp)

#find_package (SQLite3)
#if (SQLITE3_FOUND)
# include_directories(${SQLITE3_INCLUDE_DIRS})
# target_link_libraries (new ${SQLITE3_LIBRARIES})
#endif (SQLITE3_FOUND)

add_library(worker STATIC src/worker.cpp)

add_executable(test test/test.cpp src/worker.cpp)
target_link_libraries(test gtest)
include_directories(/usr/include)
link_directories(/usr/lib)
target_link_libraries(worker gtest gmock new.cpp)

add_executable(test test/test.cpp src/worker.cpp src/algorithm.cpp)
target_link_libraries(test gtest gmock)
42 changes: 42 additions & 0 deletions include/Data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#ifndef INCLUDE_DATA_HPP_
#define INCLUDE_DATA_HPP_

static const unsigned MIN_IN_HOUR = 60;

typedef std::pair<double, double> Point;
typedef std::vector<Point> Points;
typedef std::vector<std::string> Filters;

struct DataIn {
Filters FilterList;
unsigned TimeLimit;
unsigned MaxDots;
Point StartPoint;
Point EndPoint;
int UserID;
DataIn() {}
DataIn(::Filters _f, unsigned _t, unsigned _md, Point _sp, Point _ep,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

значения должны передаваться в функцию по значению
именовать переменный через нижнее подчеркивание - плохой стиль. Такое именование используется в системных библиотеках для приватных членов

int _u)
: FilterList(_f),
TimeLimit(_t),
MaxDots(_md),
StartPoint(_sp),
EndPoint(_ep),
UserID(_u) {}
};

struct DataOut {
Points RoutePoints;
unsigned MaxTime;
int UserID;
DataOut() {}
DataOut(const Points&& _p, unsigned _t, int _u)
: RoutePoints(_p), MaxTime(_t), UserID(_u) {}
};

#endif // INCLUDE_DATA_HPP_
51 changes: 50 additions & 1 deletion include/algorithm.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,57 @@
/*
* Copyright 2019 <Copyright Owner>
* Copyright 2019 <Alex>
*/

#ifndef INCLUDE_ALGORITHM_HPP_
#define INCLUDE_ALGORITHM_HPP_

#include <utility>
#include <vector>
#include <unordered_set>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <assert.h>

class Algorithm {

public:
typedef std::size_t dotId;
typedef std::pair<dotId, dotId> edge;
typedef std::size_t weight;

private:
typedef boost::adjacency_list <boost::listS, boost::vecS, boost::directedS,
boost::no_property,boost::property<boost::edge_weight_t, size_t> > graph_t;
typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_descriptor;

public:
Algorithm()= delete;
explicit Algorithm(const std::vector<edge> &edgeArr, const std::vector<weight> &weightArr);
~Algorithm();
Algorithm(const Algorithm&) = delete;
Algorithm(const Algorithm&&) = delete;
Algorithm& operator=(const Algorithm&) = delete;
Algorithm& operator=(const Algorithm&&) = delete;

std::vector<dotId> CalcRoute(const dotId &A, const dotId &B);

std::pair<std::vector<Algorithm::dotId>, size_t>
getRoute(dotId from, const size_t &pointsCount,
const size_t &time, const size_t &maxPlacesCount);



// delete this from algo and use from WorkerAPI
long int getWeightIndex(const size_t &pointsCount, const size_t &from, const size_t &to);

private:
void MakeGraph();
edge * edgeArr;
size_t edgeSize;
weight * weightArr;
graph_t myGraph;
const size_t MAX_PLACES = 200; // контроль (максимальное кол-во мест в пути)
};

#endif // INCLUDE_ALGORITHM_HPP_
8 changes: 0 additions & 8 deletions include/queu.hpp

This file was deleted.

59 changes: 59 additions & 0 deletions include/queue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 <Alex>
*/

#ifndef INCLUDE_QUEUE_HPP_
#define INCLUDE_QUEUE_HPP_


#include <queue>
#include <mutex>
#include <memory>

template <class T>
class GQueue {
public:
GQueue() = delete;
explicit GQueue(const T &element404);
~GQueue();
void push(const T &element);
T popIfNotEmpty();

GQueue(const GQueue&) = delete;
GQueue(const GQueue&&) = delete;
GQueue& operator=(const GQueue&) = delete;
GQueue& operator=(const GQueue&&) = delete;

private:
T element404;
std::unique_ptr<std::queue<T>> queue;
std::mutex GQueueMutex;
};

template <class T>
GQueue<T>::~GQueue() = default;

template <class T>
GQueue<T>::GQueue(const T &element404) {
queue.reset(new std::queue<T>);
this->element404 = element404;
}

template <class T>
void GQueue<T>::push(const T &element) {
const std::lock_guard<std::mutex> lock(GQueueMutex);
queue->push(element);
}

template <class T>
T GQueue<T>::popIfNotEmpty() {
const std::lock_guard<std::mutex> lock(GQueueMutex);
T answer = this->element404;
if (!queue->empty()) {
answer = queue->front();
queue->pop();
}
return answer;
}

#endif // INCLUDE_QUEU_HPP_
105 changes: 64 additions & 41 deletions include/worker.hpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,77 @@
/*
* Copyright 2019 <Copyright Alex>
*/
#include <string>
#include <utility>
#include <vector>
#include <map>
//#include "sqlite-autoconf-3300100/sqlite3.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "queue.hpp"
#include "algorithm.hpp"
#include <thread>
#include "Data.hpp"
#include <fstream>

#ifndef INCLUDE_WORKER_HPP_
#define INCLUDE_WORKER_HPP_

#include <vector>
#include <list>
#include <string>
#include <sstream>
#include <boost/beast.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>


struct Point
struct Limit
{
double X;
double Y;
double Point[4];
int Number_of_dots;
int Time;
};

namespace http = boost::beast::http;

class Worker {

public: // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! PRIVATE AFTER TESTS !!!!!!!!!!!!!!!!!!!!!!

// this typedef get from Algorithm before merge we don't know about Algo
typedef std::size_t dotId;
typedef std::pair<dotId, dotId> edge;
typedef std::size_t weight;

// храним в этих векторах данные, которые получим из API
std::vector<edge> edges;
std::vector<weight> weightArr;
struct Line {
int id;
std::string Name;
double x;
double y;
std::vector<std::string>Tags;
Line(int ID, std::string n, double X, double Y, std::vector<std::string>T): id(ID), Name(std::move(n)), x(X), y(Y), Tags(std::move(T)){};
Line(): id(0), Name("name"), x(0), y(0), Tags({}){};
};

const size_t MAX_POINT_COUNT = 500;
const size_t MIN_POINT_COUNT = 3;
class Table {
public:
std::ifstream file;
std::vector<Line> table;
public:
void SelectTag(std::vector<std::string> values, std::vector<Point>& res);
void SelectAll(std::vector<Point>& res);
Table(std::string filename);
virtual ~Table();
};

class Worker
{
private:
DataIn request;
Table DB;
GQueue<DataIn> &In;
GQueue<DataOut> &Out;
bool Stop;
std::vector<std::pair<size_t,size_t>> edges; // вектор из 2 точек
std::vector<size_t> weightArr; // вес ребра
std::thread WProces;
DataIn GetFromQueueIn();
void SendToQueueOut(const DataOut &value);
void GetDotsFromDB(const DataIn &value, std::vector<Point> &points);
void GetRibsFromAPI(const std::vector<Point> &points);
void getWeightFromPythonAPI(const std::string &jsonPoints, std::string &answer);
std::string createJsonForSending(const std::vector<Point> &points);
void setJsonAnswerInClass(const std::string &answer, const size_t &pointCount);

void GetRoutefromAlgorithm(const std::vector<Algorithm::edge> edge, const std::vector<size_t> weight,
std::pair<std::vector<Algorithm::dotId >, size_t> &res, size_t num_dots, DataIn value);
void FinalPoints(std::vector<Point> &points, const std::pair<std::vector<Algorithm::dotId >, size_t> &res);
void WorkerProcess();
public:

static long int getWeightIndex(const size_t &pointsCount, const size_t &from, const size_t &to);

Worker(GQueue<DataIn> &in, GQueue<DataOut> &out, std::string DBName);
~Worker();
void GetRest(int timeToSleep);
void Kill();
FRIEND_TEST(Get, Get_from_queu);
FRIEND_TEST(Send, Send_to_queu);
FRIEND_TEST(Get_dots, Get_dots_from_DB);
FRIEND_TEST(Get_dots2, Get_dots_from_DB2);
FRIEND_TEST(Get_dots3, Get_dots_from_DB3);
FRIEND_TEST(Get_dots4, NumberOfDots);
FRIEND_TEST(Get_dots5, NumberOfDots);
};
#endif // INCLUDE_WORKER_HPP_

Loading