-
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.
add a basic http server to host the client app, add README and launch…
….json for vscode debugging
- Loading branch information
Showing
10 changed files
with
570 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "(msvc) Launch", | ||
"type": "cppvsdbg", | ||
"request": "launch", | ||
// Resolved by CMake Tools: | ||
"program": "${command:cmake.launchTargetPath}", | ||
"args": [], | ||
"stopAtEntry": false, | ||
"cwd": "${workspaceFolder}", | ||
"environment": [ | ||
{ | ||
// add the directory where our target was built to the PATHs | ||
// it gets resolved by CMake Tools: | ||
"name": "PATH", | ||
"value": "${env:PATH}:${command:cmake.getLaunchTargetDirectory}" | ||
} | ||
], | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
} |
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,42 @@ | ||
# Gate Control | ||
|
||
Gate Control is a client-server application that allows control of an Arduino microcontroller over the local network. | ||
|
||
## Building | ||
|
||
There are two ways of getting built server binaries: manual build and release build. | ||
|
||
### Manual build | ||
|
||
First, clone the repo to your local machine: | ||
|
||
```sh | ||
$ git clone https://github.com/catink123/gate-control | ||
$ cd gate-control | ||
``` | ||
|
||
Then, run CMake (version >= 3.14) in the repo's root directory with one of the provided presets: | ||
|
||
```sh | ||
$ cmake --build --preset <build-preset> | ||
``` | ||
|
||
Substitute `<build-preset>` with the chosen preset. Available presets can be obtained using the following command: | ||
|
||
```sh | ||
$ cmake --list-presets | ||
``` | ||
|
||
After CMake successfully builds the server, the binaries should be located in the `out/<build-preset>` directory. | ||
|
||
### Release build | ||
|
||
You can obtain prebuilt binaries from the [Releases tab](https://github.com/catink123/gate-control/releases). | ||
|
||
## Architecture | ||
|
||
The server acts as a bridge between a user on the network and the USB-connected Arduino microcontroller. | ||
|
||
The server hosts a client application, available at it's address. A user can enter the server's address in the web browser and access the remote control panel for the microcontroller. | ||
|
||
Arduino microcontroller acts as a gate control, or in other words it holds information about the physical state of the gate and allows to change that state (raise or lower the gate). |
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,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Gate Control</title> | ||
</head> | ||
<body> | ||
<h1>Test Client Page</h1> | ||
</body> | ||
</html> |
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,16 @@ | ||
#ifndef COMMON_HPP | ||
#define COMMON_HPP | ||
|
||
#include <boost/beast/core.hpp> | ||
#include <boost/beast/http.hpp> | ||
#include <boost/asio/dispatch.hpp> | ||
#include <boost/asio/strand.hpp> | ||
#include <boost/config.hpp> | ||
#include <iostream> | ||
|
||
namespace beast = boost::beast; | ||
namespace http = beast::http; | ||
namespace net = boost::beast::net; | ||
using tcp = net::ip::tcp; | ||
|
||
#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,66 @@ | ||
#include "http_listener.hpp" | ||
|
||
http_listener::http_listener( | ||
net::io_context& ioc, | ||
tcp::endpoint endpoint, | ||
const std::shared_ptr<const std::string>& doc_root | ||
) : ioc(ioc), | ||
acceptor(net::make_strand(ioc)), | ||
doc_root(doc_root) | ||
{ | ||
beast::error_code ec; | ||
|
||
acceptor.open(endpoint.protocol(), ec); | ||
if (ec) { | ||
std::cerr << "Couldn't open acceptor: " << ec.message() << std::endl; | ||
return; | ||
} | ||
|
||
acceptor.set_option(net::socket_base::reuse_address(true), ec); | ||
if (ec) { | ||
std::cerr << "Couldn't set reuse_address: " << ec.message() << std::endl; | ||
return; | ||
} | ||
|
||
acceptor.bind(endpoint, ec); | ||
if (ec) { | ||
std::cerr << "Couldn't bind to endpoint: " << ec.message() << std::endl; | ||
return; | ||
} | ||
|
||
acceptor.listen( | ||
net::socket_base::max_listen_connections, ec | ||
); | ||
if (ec) { | ||
std::cerr << "Couldn't start listening: " << ec.message() << std::endl; | ||
return; | ||
} | ||
} | ||
|
||
void http_listener::run() { | ||
do_accept(); | ||
} | ||
|
||
void http_listener::do_accept() { | ||
acceptor.async_accept( | ||
net::make_strand(ioc), | ||
beast::bind_front_handler( | ||
&http_listener::on_accept, | ||
shared_from_this() | ||
) | ||
); | ||
} | ||
|
||
void http_listener::on_accept(beast::error_code ec, tcp::socket socket) { | ||
if (ec) { | ||
std::cerr << "Couldn't accept incoming connection: " << ec.message() << std::endl; | ||
return; | ||
} | ||
|
||
std::make_shared<http_session>( | ||
std::move(socket), | ||
doc_root | ||
)->run(); | ||
|
||
do_accept(); | ||
} |
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,29 @@ | ||
#ifndef HTTP_LISTENER_HPP | ||
#define HTTP_LISTENER_HPP | ||
|
||
#include <nlohmann/json.hpp> | ||
#include <memory> | ||
#include <string> | ||
#include "common.hpp" | ||
#include "http_session.hpp" | ||
|
||
class http_listener : public std::enable_shared_from_this<http_listener> { | ||
net::io_context& ioc; | ||
tcp::acceptor acceptor; | ||
std::shared_ptr<const std::string> doc_root; | ||
|
||
public: | ||
http_listener( | ||
net::io_context& ioc, | ||
tcp::endpoint endpoint, | ||
const std::shared_ptr<const std::string>& doc_root | ||
); | ||
|
||
void run(); | ||
|
||
private: | ||
void do_accept(); | ||
void on_accept(beast::error_code ec, tcp::socket socket); | ||
}; | ||
|
||
#endif |
Oops, something went wrong.