SNode.C
Loading...
Searching...
No Matches
Controller.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "express/Controller.h"
43
44#include "core/EventLoop.h"
45#include "express/Request.h"
46#include "express/Response.h"
47#include "express/RootRoute.h"
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51#endif /* DOXYGEN_SHOULD_SKIP_THIS */
52
53namespace express {
54
55 Controller::Controller(const std::shared_ptr<web::http::server::Request>& request,
56 const std::shared_ptr<web::http::server::Response>& response)
57 : request(std::make_shared<Request>(request))
58 , response(std::make_shared<Response>(response))
60 }
61
62 Controller::Controller(const Controller& controller)
63 : request(controller.request)
64 , response(controller.response)
65 , rootRoute(controller.rootRoute)
66 , lastRoute(controller.lastRoute)
67 , currentRoute(controller.currentRoute)
68 , lastTick(controller.lastTick)
69 , flags(controller.flags) {
70 }
71
72 Controller& Controller::operator=(const Controller& controller) noexcept {
73 if (this != &controller) {
74 request = controller.request;
75 response = controller.response;
76 rootRoute = controller.rootRoute;
77 lastRoute = controller.lastRoute;
78 currentRoute = controller.currentRoute;
79 lastTick = controller.lastTick;
80 flags = controller.flags;
81 }
82
83 return *this;
84 }
85
86 void Controller::setRootRoute(RootRoute* rootRoute) {
87 this->rootRoute = rootRoute;
88 }
89
90 void Controller::setCurrentRoute(Route* currentRoute) {
91 this->currentRoute = currentRoute;
92 }
93
94 const std::shared_ptr<Request>& Controller::getRequest() {
95 return request;
96 }
97
98 const std::shared_ptr<Response>& Controller::getResponse() {
99 return response;
100 }
101
102 int Controller::getFlags() const {
103 return flags;
104 }
105
106 void Controller::next(const std::string& how) const {
107 flags = NEXT;
108
109 if (how == "route") {
110 flags |= NEXT_ROUTE;
111 } else if (how == "router") {
113 }
114
116
117 if (lastTick != core::EventLoop::getTickCounter()) { // If asynchronous next() start traversing of route-tree
118 rootRoute->dispatch(*const_cast<express::Controller*>(this));
119 }
120 }
121
123 const bool breakDispatching = lastRoute == currentRoute && (flags & Controller::NEXT_ROUTER) != 0;
124
125 if (breakDispatching) {
127 }
128
129 return breakDispatching;
130 }
131
132 bool Controller::dispatchNext(const std::string& parentMountPath) {
133 bool dispatched = false;
134
135 if (((flags & Controller::NEXT) != 0)) {
136 if (lastRoute == currentRoute) {
138 if ((flags & Controller::NEXT_ROUTE) != 0) {
140 } else if ((flags & Controller::NEXT_ROUTER) == 0) {
141 dispatched = currentRoute->dispatchNext(*this, parentMountPath);
142 }
143 } else { // ? Optimization: Dispatch only parent route matched path
144 dispatched = currentRoute->dispatchNext(*this, parentMountPath);
145 }
146 }
147
148 return dispatched;
149 }
150
151 bool Controller::setStrictRouting(bool strictRouting) {
152 const bool oldStrictRouting = this->strictRouting;
153
154 this->strictRouting = strictRouting;
155
156 return oldStrictRouting;
157 }
158
160 return strictRouting;
161 }
162
163} // namespace express
static unsigned long getTickCounter()
Definition EventLoop.cpp:85
unsigned long lastTick
Definition Controller.h:103
void next(const std::string &how) const
const std::shared_ptr< Request > & getRequest()
RootRoute * rootRoute
Definition Controller.h:96
bool setStrictRouting(bool strictRouting)
Controller(const std::shared_ptr< web::http::server::Request > &request, const std::shared_ptr< web::http::server::Response > &response)
bool getStrictRouting() const
std::shared_ptr< Response > response
Definition Controller.h:94
Controller(const Controller &controller)
void setCurrentRoute(Route *currentRoute)
void setRootRoute(RootRoute *rootRoute)
std::shared_ptr< Request > request
Definition Controller.h:93
bool dispatchNext(const std::string &parentMountPath)
Controller & operator=(const Controller &controller) noexcept
const std::shared_ptr< Response > & getResponse()
void dispatch(Controller &controller)
bool dispatchNext(Controller &controller, const std::string &parentMountPath)
Definition Route.cpp:110