Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 1 | #include "slp.hpp" |
| 2 | #include "slp_meta.hpp" |
| 3 | #include "slp_server.hpp" |
| 4 | #include "sock_channel.hpp" |
| 5 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 6 | #include <algorithm> |
| 7 | #include <iomanip> |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 8 | |
| 9 | /* Call Back for the sd event loop */ |
| 10 | static int requestHandler(sd_event_source* es, int fd, uint32_t revents, |
| 11 | void* userdata) |
| 12 | { |
| 13 | int rc = slp::SUCCESS; |
| 14 | timeval tv{slp::TIMEOUT, 0}; |
| 15 | udpsocket::Channel channel(fd, tv); |
| 16 | std::vector<uint8_t> recvBuff; |
| 17 | slp::Message req; |
| 18 | std::vector<uint8_t> resp; |
| 19 | // Read the packet |
| 20 | std::tie(rc, recvBuff) = channel.read(); |
| 21 | |
| 22 | if (rc < 0) |
| 23 | { |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 24 | std::cerr << "SLP Error in Read : " << std::hex << rc << "\n"; |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 25 | return rc; |
| 26 | } |
| 27 | |
| 28 | switch (recvBuff[0]) |
| 29 | { |
| 30 | case slp::VERSION_2: |
| 31 | { |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 32 | // Parse the buffer and construct the req object |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 33 | std::tie(rc, req) = slp::parser::parseBuffer(recvBuff); |
| 34 | if (!rc) |
| 35 | { |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 36 | // Passing the req object to handler to serve it |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 37 | std::tie(rc, resp) = slp::handler::processRequest(req); |
| 38 | } |
| 39 | break; |
| 40 | } |
| 41 | default: |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 42 | std::cout << "SLP Unsupported Request Version=" << (int)recvBuff[0] |
| 43 | << "\n"; |
Ratan Gupta | 0d3e9e3 | 2017-02-10 22:27:12 +0530 | [diff] [blame] | 44 | |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 45 | rc = static_cast<uint8_t>(slp::Error::VER_NOT_SUPPORTED); |
| 46 | break; |
| 47 | } |
| 48 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 49 | // if there was error during Parsing of request |
| 50 | // or processing of request then handle the error. |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 51 | if (rc) |
| 52 | { |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 53 | resp = slp::handler::processError(req, rc); |
| 54 | } |
| 55 | |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 56 | channel.write(resp); |
| 57 | return slp::SUCCESS; |
| 58 | } |
| 59 | |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 60 | int main(int argc, char* argv[]) |
| 61 | { |
| 62 | slp::udp::Server svr(slp::PORT, requestHandler); |
| 63 | return svr.run(); |
| 64 | } |