blob: 13da385cafe180d8589b0a65a2a8e924ac91f328 [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 {
25 std::cerr << "E> SLP Error in Read : " << std::hex << rc << "\n";
26 return rc;
27 }
28
29 switch (recvBuff[0])
30 {
31 case slp::VERSION_2:
32 {
33 std::cout << "SLP Request" << "\n";
34 //print the buffer
35 std::for_each(recvBuff.begin(), recvBuff.end(),
36 [](uint8_t & ch)
37 {
38 std::cout << std::hex << std::setfill('0')
39 << std::setw(2) << (int)ch << ' ' ;
40 });
41 std::cout << "\n";
42 //Parse the buffer and construct the req object
43 std::tie(rc, req) = slp::parser::parseBuffer(recvBuff);
44 if (!rc)
45 {
46 //Passing the req object to handler to serve it
47 std::tie(rc, resp) = slp::handler::processRequest(req);
48 }
49 break;
50 }
51 default:
52 rc = static_cast<uint8_t>(slp::Error::VER_NOT_SUPPORTED);
53 break;
54 }
55
56 //if there was error during Parsing of request
57 //or processing of request then handle the error.
58 if (rc)
59 {
60 std::cerr << "E> SLP Error rc=" << rc << "\n";
61 resp = slp::handler::processError(req, rc);
62 }
63
64 //print and send the response
65 std::cout << "SLP Response" << "\n";
66 std::for_each(resp.begin(), resp.end(),
67 [](uint8_t & ch)
68 {
69 std::cout << std::hex << std::setfill('0')
70 << std::setw(2) << (int)ch << ' ' ;
71 });
72
73 channel.write(resp);
74 return slp::SUCCESS;
75}
76
77
78int main(int argc, char* argv[])
79{
80 slp::udp::Server svr(slp::PORT, requestHandler);
81 return svr.run();
82}