blob: 4e1dbe87df37001540db52b110ebdf994fd32f9b [file] [log] [blame]
Ratan Gupta309ac442016-12-13 20:40:06 +05301#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 */
11static 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 Gupta0d3e9e32017-02-10 22:27:12 +053025 std::cerr << "SLP Error in Read : " << std::hex << rc << "\n";
Ratan Gupta309ac442016-12-13 20:40:06 +053026 return rc;
27 }
28
29 switch (recvBuff[0])
30 {
31 case slp::VERSION_2:
32 {
Ratan Gupta309ac442016-12-13 20:40:06 +053033 //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 Gupta0d3e9e32017-02-10 22:27:12 +053043 std::cout << "SLP Unsupported Request Version="
44 << (int)recvBuff[0] <<"\n";
45
Ratan Gupta309ac442016-12-13 20:40:06 +053046 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 Gupta0d3e9e32017-02-10 22:27:12 +053054 std::cerr << "SLP Error rc=" << rc << "\n";
Ratan Gupta309ac442016-12-13 20:40:06 +053055 resp = slp::handler::processError(req, rc);
56 }
57
Ratan Gupta309ac442016-12-13 20:40:06 +053058 channel.write(resp);
59 return slp::SUCCESS;
60}
61
62
63int main(int argc, char* argv[])
64{
65 slp::udp::Server svr(slp::PORT, requestHandler);
66 return svr.run();
67}