Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New routes created for UDP and TCP #20

Merged
merged 1 commit into from
Jan 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
{
"files.associations": {
"vector": "cpp"
"vector": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"hash_map": "cpp",
"strstream": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp"
}
}
6 changes: 4 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ set(
core/db
core/discovery
core/state
io/adaptor
io/adaptor/tcp
io/adaptor/udp
utils/thing
utils/port_map
)

set(SRC_PATHS "")
Expand All @@ -20,7 +22,7 @@ foreach(DIR ${SOURCE_DIR})
)
# iterativly append src path variable
aux_source_directory(
{$DIR}/src
${DIR}/src
SRC_PATHS
)
endforeach(DIR)
Expand Down
1 change: 0 additions & 1 deletion src/io/adaptor/http/inc/http_routes.h

This file was deleted.

1 change: 0 additions & 1 deletion src/io/adaptor/http/src/http_routes.cpp

This file was deleted.

23 changes: 23 additions & 0 deletions src/io/adaptor/tcp/inc/http.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef LATTICE_HUB_IO_ADAPTOR_HTTP
#define LATTICE_HUB_IO_ADAPTOR_HTTP

// std lib
#include <string>

// lib
#include "crow_all.h"

namespace io::adaptor {
class Http;
}

class io::adaptor::Http {
std::string api_output;
crow::SimpleApp app;

public:
Http();
void listen();
};

#endif
18 changes: 18 additions & 0 deletions src/io/adaptor/tcp/src/http.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// implementation file
#include "http.h"

io::adaptor::Http::Http() {}

void io::adaptor::Http::listen() {
CROW_ROUTE(app, "/")([]() { return "Lattice deployed"; });

CROW_ROUTE(app, "/attach").methods("PUT"_method)([&](const crow::request &req) {
return api_output.c_str();
});

CROW_ROUTE(app, "/devices")
.methods("GET"_method)(
[&](const crow::request &req) { return api_output.c_str(); });

app.port(8080).multithreaded().run();
}
28 changes: 28 additions & 0 deletions src/io/adaptor/udp/inc/udp_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef LATTICE_HUB_IO_ADAPTOR_UDP_INTERFACE
#define LATTICE_HUB_IO_ADAPTOR_UDP_INTERFACE

// libs
#include <boost/array.hpp>
#include <boost/asio.hpp>

namespace io::adaptor::udp_interface {
using boost::asio::ip::udp;

class UdpServer;
} // namespace io::adaptor::udp_interface

class io::adaptor::udp_interface::UdpServer {
udp::socket socket_;
udp::endpoint remote_endpoint_;
boost::array<char, 1> recv_buffer_;

void start_receive();
void handle_receive(const boost::system::error_code &, std::size_t);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments about method above it and add meaningful parameter names.

void handle_send(boost::shared_ptr<std::string>,
const boost::system::error_code &, std::size_t);

public:
UdpServer(boost::asio::io_service &, unsigned short);
};

#endif
43 changes: 43 additions & 0 deletions src/io/adaptor/udp/src/udp_interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// std lib
#include <iostream>
#include <thread>

// lib
#include <boost/bind.hpp>

// implementation file
#include "udp_interface.h"

#define UDP_PORT 13000

io::adaptor::udp_interface::UdpServer::UdpServer(
boost::asio::io_service &io_service, unsigned short port)
: socket_(io_service, udp::endpoint(udp::v4(), port)) {
start_receive();
}

void io::adaptor::udp_interface::UdpServer::start_receive() {
socket_.async_receive_from(
boost::asio::buffer(recv_buffer_), remote_endpoint_,
boost::bind(&io::adaptor::udp_interface::UdpServer::handle_receive, this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

void io::adaptor::udp_interface::UdpServer::handle_receive(
const boost::system::error_code &error, std::size_t bytes_transferred) {
if (!error || error == boost::asio::error::message_size) {
boost::shared_ptr<std::string> message(new std::string("Test string"));

socket_.async_send_to(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments.

boost::asio::buffer(*message), remote_endpoint_,
boost::bind(&io::adaptor::udp_interface::UdpServer::handle_send, this, message,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));

start_receive();
}
}

void io::adaptor::udp_interface::UdpServer::handle_send(
boost::shared_ptr<std::string> message,
const boost::system::error_code &error, std::size_t bytes_transferred) {}
42 changes: 20 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@
#include <string>

// boost imports
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

// lib
#include "crow_all.h"

// proto
#include "thing_buffer.pb.h"
#include "user.pb.h"

// internal module
#include "http.h"
#include "port_map.h"
#include "udp_interface.h"

using namespace std;
using namespace boost::uuids;

void demo_user() {
shared_ptr<lattice_hub::user::User> user1 = make_shared<lattice_hub::user::User>();
shared_ptr<lattice_hub::user::User> user1 =
make_shared<lattice_hub::user::User>();
// Generate random boost uuid, convert it to string-16 bytes and store.
user1->set_id(boost::lexical_cast<string>(random_generator()()).c_str());
user1->set_name("Lattic_hub_dummy_user");
Expand All @@ -30,24 +34,18 @@ void demo_user() {
cout << user1->DebugString() << endl;
}

int main()
{

std::string api_output;
crow::SimpleApp app;

CROW_ROUTE(app, "/")([](){
return "Lattice deployed";
});
demo_user();

CROW_ROUTE(app, "/add").methods("PUT"_method)([&](const crow::request &req){
return api_output.c_str();
});
CROW_ROUTE(app, "/devices").methods("GET"_method)([&](const crow::request &req){
return api_output.c_str();
});
int main() {
demo_user();

// io::adaptor::Http http_listener;
// http_listener.listen();

app.port(8080).multithreaded().run();
try {
boost::asio::io_service io_service;
io::adaptor::udp_interface::UdpServer udpServer(io_service,
PortMap::UDP_PORT);
io_service.run();
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
}
}
11 changes: 11 additions & 0 deletions src/utils/port_map/inc/port_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef LATTICE_HUB_UTILS_PORT_MAP
#define LATTICE_HUB_UTILS_PORT_MAP

#include <string>

class PortMap{
public:
const static unsigned short UDP_PORT = 1300;
};

#endif