Skip to content

Commit

Permalink
Merge pull request simwir#57 from simwir/order-generation-service
Browse files Browse the repository at this point in the history
Order Generation
  • Loading branch information
simwir authored Dec 4, 2019
2 parents 9b5a886 + 0d8f230 commit 4bea71d
Show file tree
Hide file tree
Showing 14 changed files with 635 additions and 17 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ project(p7)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED)
set(CMAKE_CXX_FLAGS "-g -fno-omit-frame-pointer -m32")
set(CMAKE_CXX_FLAGS "-g -fno-omit-frame-pointer -Wall")

include(ExternalProject)

Expand All @@ -44,6 +44,7 @@ include_directories(
${PROJECT_SOURCE_DIR}/scheduling/include
${PROJECT_SOURCE_DIR}/util/include
${PROJECT_SOURCE_DIR}/config/include
${PROJECT_SOURCE_DIR}/order/include
)

# Libraries
Expand All @@ -55,6 +56,7 @@ add_subdirectory(tcp)
add_subdirectory(config)
add_subdirectory(scheduling)
add_subdirectory(robot)
add_subdirectory(order)

# Executables
add_subdirectory(tests)
Expand Down
18 changes: 18 additions & 0 deletions config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# Copyright 2019 Anders Madsen, Emil Jørgensen Njor, Emil Stenderup Bækdahl, Frederik Baymler
# Mathiesen, Nikolaj Jensen Ulrik, Simon Mejlby Virenfeldt
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
# OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

include_directories(${PROJECT_SOURCE_DIR}/config/include)

file(GLOB SOURCES "src/*.cpp")
Expand Down
34 changes: 34 additions & 0 deletions order/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2019 Anders Madsen, Emil Jørgensen Njor, Emil Stenderup Bækdahl, Frederik Baymler
# Mathiesen, Nikolaj Jensen Ulrik, Simon Mejlby Virenfeldt
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
# OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

include_directories(${PROJECT_SOURCE_DIR}/order/include)

file(GLOB SOURCES "src/*.cpp")
list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/order_generation_service.cpp)

add_library(order ${SOURCES})
target_link_libraries(order
jsoncpp_lib
pthread
tcp
)

add_executable(order_generation_service src/order_generation_service.cpp)
target_link_libraries(order_generation_service
order
)
41 changes: 41 additions & 0 deletions order/include/order/generation_service.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*Copyright 2019 Anders Madsen, Emil Jørgensen Njor, Emil Stenderup Bækdahl, Frederik Baymler
*Mathiesen, Nikolaj Jensen Ulrik, Simon Mejlby Virenfeldt
*
*Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
*associated documentation files (the "Software"), to deal in the Software without restriction,
*including without limitation the rights to use, copy, modify, merge, publish, distribute,
*sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all copies or
*substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
*NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
*DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
*OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef ORDER_GENERATION_SERVICE_HPP
#define ORDER_GENERATION_SERVICE_HPP

#include "order/generator.hpp"
#include "tcp/connection.hpp"
#include "tcp/server.hpp"
#include <memory>

namespace order {
class GenerationService {
tcp::Server server;
std::shared_ptr<Generator> generator;
void parse_message(std::shared_ptr<tcp::Connection> connection);

public:
GenerationService(int port, std::shared_ptr<Generator> generator);
void start();
int get_port();
};
} // namespace order

#endif
35 changes: 35 additions & 0 deletions order/include/order/generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*Copyright 2019 Anders Madsen, Emil Jørgensen Njor, Emil Stenderup Bækdahl, Frederik Baymler
*Mathiesen, Nikolaj Jensen Ulrik, Simon Mejlby Virenfeldt
*
*Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
*associated documentation files (the "Software"), to deal in the Software without restriction,
*including without limitation the rights to use, copy, modify, merge, publish, distribute,
*sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all copies or
*substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
*NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
*DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
*OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef ORDER_GENERATOR_HPP
#define ORDER_GENERATOR_HPP

#include "order/order.hpp"
#include <vector>

namespace order {
class Generator {
public:
virtual ~Generator() = default;
virtual Order generate_order() const = 0;
virtual std::vector<Order> generate_n_orders(int n) const = 0;
};
} // namespace order

#endif
37 changes: 37 additions & 0 deletions order/include/order/order.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*Copyright 2019 Anders Madsen, Emil Jørgensen Njor, Emil Stenderup Bækdahl, Frederik Baymler
*Mathiesen, Nikolaj Jensen Ulrik, Simon Mejlby Virenfeldt
*
*Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
*associated documentation files (the "Software"), to deal in the Software without restriction,
*including without limitation the rights to use, copy, modify, merge, publish, distribute,
*sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all copies or
*substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
*NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
*DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
*OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef ORDER_ORDER_HPP
#define ORDER_ORDER_HPP

#include "util/json.hpp"
#include <string>
#include <vector>

namespace order {
struct Order {
std::vector<int> stations;

Json::Value to_json() const;
static Order from_json(const Json::Value &json);
static Order from_json(const std::string &json);
};
} // namespace order

#endif
49 changes: 49 additions & 0 deletions order/include/order/random_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*Copyright 2019 Anders Madsen, Emil Jørgensen Njor, Emil Stenderup Bækdahl, Frederik Baymler
*Mathiesen, Nikolaj Jensen Ulrik, Simon Mejlby Virenfeldt
*
*Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
*associated documentation files (the "Software"), to deal in the Software without restriction,
*including without limitation the rights to use, copy, modify, merge, publish, distribute,
*sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all copies or
*substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
*NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
*DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
*OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef ORDER_RANDOM_GENERATOR
#define ORDER_RANDOM_GENERATOR

#include "order/generator.hpp"
#include <ctime>
#include <vector>

namespace order {
class RandomGenerator : public Generator {
const std::vector<int> stations;
const int min_size;
const int max_size;
const int seed;

public:
RandomGenerator(std::vector<int> stations) : RandomGenerator(stations, time(0)){};
RandomGenerator(std::vector<int> stations, unsigned seed)
: RandomGenerator(stations, seed, 0){};
RandomGenerator(std::vector<int> stations, unsigned seed, unsigned min_size)
: RandomGenerator(stations, seed, min_size, stations.size()){};
RandomGenerator(std::vector<int> stations, unsigned seed, unsigned min_size, unsigned max_size);
int get_seed() const;
int get_min_size() const;
int get_max_size() const;
Order generate_order() const;
std::vector<Order> generate_n_orders(int n) const;
};
} // namespace order

#endif
61 changes: 61 additions & 0 deletions order/src/generation_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*Copyright 2019 Anders Madsen, Emil Jørgensen Njor, Emil Stenderup Bækdahl, Frederik Baymler
*Mathiesen, Nikolaj Jensen Ulrik, Simon Mejlby Virenfeldt
*
*Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
*associated documentation files (the "Software"), to deal in the Software without restriction,
*including without limitation the rights to use, copy, modify, merge, publish, distribute,
*sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all copies or
*substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
*NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
*DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
*OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "order/generation_service.hpp"
#include <iostream>
#include <memory>
#include <thread>

namespace order {
GenerationService::GenerationService(int port, std::shared_ptr<Generator> generator)
: server(port), generator(generator){};

int GenerationService::get_port()
{
return server.get_port();
}

void GenerationService::parse_message(std::shared_ptr<tcp::Connection> connection)
{
std::string message = connection->receive_blocking();

if (message == "get_order") {
Order order = generator->generate_order();
connection->send(order.to_json().toStyledString());
}
else {
std::cerr << "Message not understood: " << message;
connection->send("Message not understood: " + message);
}
}

void GenerationService::start()
{
while (true) {
try {
std::shared_ptr<tcp::Connection> connection = server.accept();
std::thread thread{&GenerationService::parse_message, this, connection};
thread.detach();
}
catch (tcp::AcceptException &error) {
std::cerr << error.what() << std::endl;
}
}
}
} // namespace order
54 changes: 54 additions & 0 deletions order/src/order.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*Copyright 2019 Anders Madsen, Emil Jørgensen Njor, Emil Stenderup Bækdahl, Frederik Baymler
*Mathiesen, Nikolaj Jensen Ulrik, Simon Mejlby Virenfeldt
*
*Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
*associated documentation files (the "Software"), to deal in the Software without restriction,
*including without limitation the rights to use, copy, modify, merge, publish, distribute,
*sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all copies or
*substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
*NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
*DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
*OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "order/order.hpp"
#include <sstream>

namespace order {
Json::Value Order::to_json() const
{
Json::Value json{Json::arrayValue};

for (int station : stations) {
json.append(Json::Value{station});
}

return json;
}

Order Order::from_json(const std::string &json)
{
std::stringstream ss{json};
Json::Value root{Json::arrayValue};
ss >> root;

return Order::from_json(root);
}

Order Order::from_json(const Json::Value &json)
{
std::vector<int> stations;

for (auto station : json) {
stations.push_back(station.asInt());
}

return Order{stations};
}
} // namespace order
Loading

0 comments on commit 4bea71d

Please sign in to comment.