forked from simwir/P7-kode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request simwir#57 from simwir/order-generation-service
Order Generation
- Loading branch information
Showing
14 changed files
with
635 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.