aboutsummaryrefslogtreecommitdiff
path: root/src/server/server.cc
blob: ab1db9efc400dbe4cfac73b13e4a957b573a3c1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: BSD-3-Clause

#include <cstring>
#include <string>
#include <cerrno>
#include <algorithm>
#include <memory>
#include <utility>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>

#include "../voltronic/exceptions.h"
#include "../p18/exceptions.h"
#include "../voltronic/time.h"
#include "../logging.h"
//#include "hexdump/hexdump.h"
#include "server.h"
#include "connection.h"
#include "signal.h"

namespace server {

Server::Server(std::shared_ptr<voltronic::Device> device)
    : sock_(0)
    , port_(0)
    , cacheTimeout_(CACHE_TIMEOUT)
    , verbose_(false)
    , device_(std::move(device)) {
    client_.setDevice(device_);
}

void Server::setVerbose(bool verbose) {
    verbose_ = verbose;
    device_->setVerbose(verbose);
}

void Server::setCacheTimeout(u64 timeout) {
    cacheTimeout_ = timeout;
}

Server::~Server() {
    if (sock_ > 0)
        close(sock_);
}

void Server::start(std::string& host, int port) {
    host_ = host;
    port_ = port;

    sock_ = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_ == -1)
        throw ServerError("failed to create socket");

    struct linger sl = {0};
    sl.l_onoff = 1;
    sl.l_linger = 0;
    if (setsockopt(sock_, SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)) == -1)
        throw ServerError("setsockopt(linger): " + std::string(strerror(errno)));

    int flag = 1;
    if (setsockopt(sock_, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) == -1)
        throw ServerError("setsockopt(reuseaddr): " + std::string(strerror(errno)));

    struct sockaddr_in serv_addr = {0};
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(host_.c_str());
    serv_addr.sin_port = htons(port_);
    memset(serv_addr.sin_zero, 0, sizeof(serv_addr.sin_zero));

    if (bind(sock_, (struct sockaddr*)&serv_addr, sizeof(serv_addr)))
        throw ServerError("bind: " + std::string(strerror(errno)));

    if (listen(sock_, 50))
        throw ServerError("start: " + std::string(strerror(errno)));

    while (!shutdownCaught) {
        struct sockaddr_in addr = {0};
        socklen_t addr_size = sizeof(addr);

        if (verbose_)
            mylog << "waiting for client..";

        int sock = accept(sock_, (struct sockaddr*)&addr, &addr_size);
        if (sock == -1)
            continue;

        auto conn = new Connection(sock, addr, this);
        addConnection(conn);
    }
}

void Server::addConnection(Connection *conn) {
    if (verbose_)
        myerr << "adding " << conn->ipv4();
    LockGuard lock(threads_mutex_);
    connections_.emplace_back(conn);
}

void Server::removeConnection(Connection *conn) {
    if (verbose_)
        myerr << "removing " << conn->ipv4();
    LockGuard lock(threads_mutex_);
    connections_.erase(std::remove(connections_.begin(), connections_.end(), conn), connections_.end());
}

size_t Server::getConnectionsCount() const {
    return connections_.size();
}

std::shared_ptr<p18::response_type::BaseResponse> Server::executeCommand(p18::CommandType commandType, std::vector<std::string>& arguments) {
    LockGuard lock(client_mutex_);

    auto it = cache_.find(commandType);
    if (it != cache_.end()) {
        auto cr = it->second;
        if (voltronic::timestamp() - cr.time <= cacheTimeout_) {
            return cr.response;
        }

        cache_.erase(it);
    }

    try {
        auto response = client_.execute(commandType, arguments);
        CachedResponse cr{voltronic::timestamp(), response};
        cache_[commandType] = cr;
        return response;
    }
    catch (voltronic::DeviceError& e) {
        throw std::runtime_error("device error: " + std::string(e.what()));
    }
    catch (voltronic::TimeoutError& e) {
        throw std::runtime_error("timeout: " + std::string(e.what()));
    }
    catch (voltronic::InvalidDataError& e) {
        throw std::runtime_error("data is invalid: " + std::string(e.what()));
    }
    catch (p18::InvalidResponseError& e) {
        throw std::runtime_error("response is invalid: " + std::string(e.what()));
    }
}


}