Guest User

Casa_srv.cpp

a guest
Jun 3rd, 2016
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <cpprest/http_listener.h>
  2. #include <cpprest/json.h>
  3.  
  4. using namespace web;
  5. using namespace web::http;
  6. using namespace web::http::experimental::listener;
  7.  
  8. #include <iostream>
  9. #include <map>
  10. #include <set>
  11. #include <string>
  12. using namespace std;
  13.  
  14. #define TRACE(msg)            wcout << msg
  15. #define TRACE_ACTION(a, k, v) wcout << a << L" (" << k << L", " << v << L")\n"
  16.  
  17. map<utility::string_t, utility::string_t> dictionary;
  18. typedef std::vector<std::pair<utility::string_t, json::value>> field_map;
  19. /* handlers implementation */
  20.  
  21. void handle_get(http_request request)
  22. {
  23.     TRACE(L"\nhandle GET\n");
  24.  
  25.     field_map answer;
  26.  
  27.     for (auto const & p : dictionary)
  28.     {
  29.         answer.push_back(make_pair(p.first, json::value(p.second)));
  30.     }
  31.  
  32.     request.reply(status_codes::OK, json::value::object(answer));
  33. }
  34.  
  35. void handle_request(http_request request,
  36.     function<void(json::value &, field_map &)> action)
  37. {
  38.     field_map answer;
  39.  
  40.     request
  41.         .extract_json()
  42.         .then([&answer, &action](pplx::task<json::value> task) {
  43.         try
  44.         {
  45.             auto & jvalue = task.get();
  46.  
  47.             if (!jvalue.is_null())
  48.             {
  49.                 action(jvalue, answer);
  50.             }
  51.         }
  52.         catch (http_exception const & e)
  53.         {
  54.             wcout << e.what() << endl;
  55.         }
  56.     })
  57.         .wait();
  58.  
  59.     request.reply(status_codes::OK, json::value::object(answer));
  60. }
  61.  
  62. void handle_post(http_request request)
  63. {
  64.     TRACE("\nhandle POST\n");
  65.  
  66.     handle_request(
  67.         request,
  68.         [](json::value & jvalue, field_map & answer)
  69.     {
  70.         for (auto const & e : jvalue.as_array())
  71.         {
  72.             if (e.is_string())
  73.             {
  74.                 auto key = e.as_string();
  75.  
  76.                 auto pos = dictionary.find(key);
  77.                 if (pos == dictionary.end())
  78.                 {
  79.                     answer.push_back(make_pair(key, json::value(L"<nil>")));
  80.                 }
  81.                 else
  82.                 {
  83.                     answer.push_back(make_pair(pos->first, json::value(pos->second)));
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     );
  89. }
  90.  
  91. void handle_put(http_request request)
  92. {
  93.     TRACE("\nhandle PUT\n");
  94.  
  95.     handle_request(
  96.         request,
  97.         [](json::value & jvalue, field_map & answer)
  98.     {
  99.         for (auto const & e : jvalue.as_object())
  100.         {
  101.             if (e.second.is_string())
  102.             {
  103.                 auto key = e.first;
  104.                 auto value = e.second.as_string();
  105.  
  106.                 if (dictionary.find(key) == dictionary.end())
  107.                 {
  108.                     TRACE_ACTION(L"added", key, value);
  109.                     answer.push_back(make_pair(key, json::value(L"<put>")));
  110.                 }
  111.                 else
  112.                 {
  113.                     TRACE_ACTION(L"updated", key, value);
  114.                     answer.push_back(make_pair(key, json::value(L"<updated>")));
  115.                 }
  116.  
  117.                 dictionary[key] = value;
  118.             }
  119.         }
  120.     }
  121.     );
  122. }
  123.  
  124. void handle_del(http_request request)
  125. {
  126.     TRACE("\nhandle DEL\n");
  127.  
  128.     handle_request(
  129.         request,
  130.         [](json::value & jvalue, field_map & answer)
  131.     {
  132.         set<utility::string_t> keys;
  133.         for (auto const & e : jvalue.as_array())
  134.         {
  135.             if (e.is_string())
  136.             {
  137.                 auto key = e.as_string();
  138.  
  139.                 auto pos = dictionary.find(key);
  140.                 if (pos == dictionary.end())
  141.                 {
  142.                     answer.push_back(make_pair(key, json::value(L"<failed>")));
  143.                 }
  144.                 else
  145.                 {
  146.                     TRACE_ACTION(L"deleted", pos->first, pos->second);
  147.                     answer.push_back(make_pair(key, json::value(L"<deleted>")));
  148.                     keys.insert(key);
  149.                 }
  150.             }
  151.         }
  152.  
  153.         for (auto const & key : keys)
  154.             dictionary.erase(key);
  155.     }
  156.     );
  157. }
  158.  
  159. int main()
  160. {
  161.     http_listener listener(L"http://localhost/restdemo");
  162.  
  163.     listener.support(methods::GET, handle_get);
  164.     listener.support(methods::POST, handle_post);
  165.     listener.support(methods::PUT, handle_put);
  166.     listener.support(methods::DEL, handle_del);
  167.  
  168.     try
  169.     {
  170.         listener
  171.             .open()
  172.             .then([&listener]() {TRACE(L"\nstarting to listen\n"); })
  173.             .wait();
  174.  
  175.         while (true);
  176.     }
  177.     catch (exception const & e)
  178.     {
  179.         wcout << e.what() << endl;
  180.     }
  181.  
  182.     return 0;
  183. }
Add Comment
Please, Sign In to add comment