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