Skip to content

Commit de7f95b

Browse files
authored
Merge pull request #22 from snandasena/dev
Improved
2 parents f830d30 + 004226c commit de7f95b

File tree

8 files changed

+248
-1
lines changed

8 files changed

+248
-1
lines changed

app/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ add_subdirectory(common)
77
add_subdirectory(interfaces)
88
add_subdirectory(parsers)
99
add_subdirectory(configs)
10-
add_subdirectory(auth)
10+
add_subdirectory(auth)
11+
add_subdirectory(servers)

app/servers/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
project(Servers)
2+
3+
4+
find_package(Boost COMPONENTS system REQUIRED)
5+
6+
include_directories(${Boost_INCLUDE_DIRS})
7+
8+
set(HEADERS
9+
${PROJECT_SOURCE_DIR}/interfaces/services/ITaxServiceFactory.h
10+
${PROJECT_SOURCE_DIR}/interfaces/services/ITaxService.h
11+
${PROJECT_SOURCE_DIR}/include/servers/ReportSession.h
12+
)
13+
14+
set(SOURCES
15+
${PROJECT_SOURCE_DIR}/src/ReportSession.cpp
16+
${PROJECT_SOURCE_DIR}/include/servers/Server.h
17+
${PROJECT_SOURCE_DIR}/src/Server.cpp
18+
)
19+
20+
add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
21+
22+
set(COMON_LINK_LIBS
23+
${PROJECT_NAME}
24+
${Boost_SYSTEM_LIBRARY}
25+
Common
26+
Interfaces
27+
pthread
28+
)
29+
30+
if (WIN32)
31+
target_link_libraries(${COMON_LINK_LIBS} ws2_32)
32+
else ()
33+
target_link_libraries(${COMON_LINK_LIBS})
34+
endif (WIN32)
35+
36+
target_include_directories(${PROJECT_NAME} PUBLIC
37+
${PROJECT_SOURCE_DIR}/include
38+
${PROJECT_SOURCE_DIR}/interfaces
39+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_REPORTSESSION_H
6+
#define DESIGN_PATTERNS_REPORTSESSION_H
7+
8+
#include <boost/asio.hpp>
9+
#include <memory>
10+
#include <optional>
11+
12+
#include "auth/IAuthentication.h"
13+
#include "parsers/ICredentialsParser.h"
14+
#include "services/ITaxServiceFactory.h"
15+
16+
using boost::asio::ip::tcp;
17+
18+
namespace types
19+
{
20+
struct User;
21+
}
22+
23+
namespace servers
24+
{
25+
class ReportSession
26+
{
27+
public:
28+
29+
ReportSession(tcp::socket, const auth::IAuthentication &, const parsers::ICredentialsParser&,
30+
const services::ITaxServiceFactory &);
31+
32+
void start();
33+
34+
void stop();
35+
36+
private:
37+
38+
std::optional<types::User> loginUser();
39+
40+
static constexpr auto MAX_LENGTH = 1024;
41+
42+
tcp::socket sock;
43+
char data[MAX_LENGTH];
44+
45+
const auth::IAuthentication &authManager;
46+
const parsers::ICredentialsParser &parser;
47+
const services::ITaxServiceFactory &taxServiceFactory;
48+
49+
};
50+
}
51+
52+
53+
#endif //DESIGN_PATTERNS_REPORTSESSION_H
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_SERVER_H
6+
#define DESIGN_PATTERNS_SERVER_H
7+
8+
#endif //DESIGN_PATTERNS_SERVER_H
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_ITAXSERVICE_H
6+
#define DESIGN_PATTERNS_ITAXSERVICE_H
7+
8+
#include <string>
9+
#include <string_view>
10+
11+
#include "types/ReportFormat.h"
12+
13+
namespace services
14+
{
15+
using ReportStatus = std::string;
16+
17+
class ITaxService
18+
{
19+
public:
20+
21+
virtual ~ITaxService() = default;
22+
23+
virtual ReportStatus onReportRequest(const std::string &) = 0;
24+
};
25+
}
26+
#endif //DESIGN_PATTERNS_ITAXSERVICE_H
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_ITAXSERVICEFACTORY_H
6+
#define DESIGN_PATTERNS_ITAXSERVICEFACTORY_H
7+
8+
#include <memory>
9+
#include "ITaxService.h"
10+
#include "types/ReportFormat.h"
11+
#include "types/User.h"
12+
13+
14+
namespace services
15+
{
16+
class ITaxServiceFactory
17+
{
18+
public:
19+
20+
virtual ~ITaxServiceFactory() = default;
21+
22+
virtual std::unique_ptr<ITaxService> create(const types::User &) const = 0;
23+
};
24+
}
25+
26+
#endif //DESIGN_PATTERNS_ITAXSERVICEFACTORY_H

app/servers/src/ReportSession.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//
4+
5+
6+
#include "servers/ReportSession.h"
7+
8+
#include <iostream>
9+
#include <stdexcept>
10+
11+
#include "constants/Constants.h"
12+
#include "types/User.h"
13+
14+
namespace
15+
{
16+
std::size_t read(tcp::socket &sock, char *buff, const std::size_t n)
17+
{
18+
boost::system::error_code error;
19+
const auto readLength = sock.read_some(boost::asio::buffer(buff, n), error);
20+
std::cout << "Received " << readLength << " bytes\n";
21+
22+
if (error == boost::asio::error::eof)
23+
{
24+
return 0;
25+
}
26+
else if (error)
27+
{
28+
throw boost::system::system_error{error};
29+
}
30+
return readLength;
31+
}
32+
}
33+
34+
35+
namespace servers
36+
{
37+
ReportSession::ReportSession(tcp::socket sk, const auth::IAuthentication &aManager,
38+
const parsers::ICredentialsParser &pser,
39+
const services::ITaxServiceFactory &tSF) : sock{std::move(sk)}, authManager{aManager},
40+
parser{pser}, taxServiceFactory{tSF}
41+
{}
42+
43+
std::optional<types::User> ReportSession::loginUser()
44+
{
45+
if (const auto length = read(sock, data, MAX_LENGTH))
46+
{
47+
const auto user = parser.parseCredentials({data, length});
48+
if (user && authManager.authenticate(user->login, user->password))
49+
{
50+
return user;
51+
}
52+
}
53+
return std::nullopt;
54+
}
55+
56+
void ReportSession::start() try
57+
{
58+
if (const auto user = loginUser())
59+
{
60+
boost::asio::write(sock, boost::asio::buffer(constants::OK.data(), constants::OK.size()));
61+
62+
for (auto taxService = taxServiceFactory.create(*user);;)
63+
{
64+
if (const auto length = read(sock, data, MAX_LENGTH))
65+
{
66+
const auto response = taxService->onReportRequest({data, length});
67+
boost::asio::write(sock, boost::asio::buffer(response.data(), response.size()));
68+
}
69+
else
70+
{
71+
stop();
72+
}
73+
}
74+
}
75+
else
76+
{
77+
boost::asio::write(sock, boost::asio::buffer(constants::NOK.data(), constants::NOK.size()));
78+
stop();
79+
std::cerr << __FILE__ << " Failed to login a user for tax reporting\n";
80+
}
81+
}catch (const std::exception &e)
82+
{
83+
std::cerr<<__FILE__<<' '<< __LINE__ <<' '<<e.what()<<'\n';
84+
}
85+
86+
87+
void ReportSession::stop()
88+
{
89+
90+
}
91+
}

app/servers/src/Server.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//

0 commit comments

Comments
 (0)