Debian Codegraph @

Release/samples/CasaLens/casalens.cpp

raw
1 /***
2 * Copyright (C) Microsoft. All rights reserved.
3 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4 *
5 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6 *
7 * casalens.cpp: Listener code: Given a location/postal code, the listener queries different services
8 * for weather, things to do: events, movie and pictures and returns it to the client.
9 *
10 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
11 ****/
12
13 #include "stdafx.h"
14
15 #include "casalens.h"
16
17 using namespace web;
18 using namespace http;
19 using namespace utility;
20 using namespace http::experimental::listener;
21
22 const utility::string_t casalens_creds::events_url = U("http://api.eventful.com/json/events/search?...&location=");
23 const utility::string_t casalens_creds::movies_url = U("http://data.tmsapi.com/v1/movies/showings?");
24 const utility::string_t casalens_creds::images_url =
25 U("https://api.datamarket.azure.com/Bing/Search/Image?$format=json");
26 const utility::string_t casalens_creds::bmaps_url = U("http://dev.virtualearth.net/REST/v1/Locations");
27 const utility::string_t casalens_creds::gmaps_url = U("http://maps.googleapis.com/maps/api/geocode/json");
28 const utility::string_t casalens_creds::weather_url = U("http://api.openweathermap.org/data/2.1/find/name?q=");
29
30 // FILL IN THE API KEYS FOR THE DIFFERENT SERVICES HERE.
31 // Refer Readme.txt for details on how to obtain the key for the services.
32 const utility::string_t casalens_creds::events_keyname = U("app_key");
33 const utility::string_t casalens_creds::events_key;
34 const utility::string_t casalens_creds::movies_keyname = U("api_key");
35 const utility::string_t casalens_creds::movies_key;
36 const utility::string_t casalens_creds::images_keyname = U("username");
37 const utility::string_t casalens_creds::images_key;
38 const utility::string_t casalens_creds::bmaps_keyname = U("key");
39 const utility::string_t casalens_creds::bmaps_key;
40
41 const utility::string_t CasaLens::events_json_key = U("events");
42 const utility::string_t CasaLens::movies_json_key = U("movies");
43 const utility::string_t CasaLens::weather_json_key = U("weather");
44 const utility::string_t CasaLens::images_json_key = U("images");
45 const utility::string_t CasaLens::error_json_key = U("error");
46
47 CasaLens::CasaLens(utility::string_t url) : m_listener(url)
48 {
49 m_listener.support(methods::GET, std::bind(&CasaLens::handle_get, this, std::placeholders::_1));
50 m_listener.support(methods::POST, std::bind(&CasaLens::handle_post, this, std::placeholders::_1));
51
52 m_htmlcontentmap[U("/")] = std::make_tuple(U("AppCode.html"), U("text/html"));
53 m_htmlcontentmap[U("/js/default.js")] = std::make_tuple(U("js/default.js"), U("application/javascript"));
54 m_htmlcontentmap[U("/css/default.css")] = std::make_tuple(U("css/default.css"), U("text/css"));
55 m_htmlcontentmap[U("/image/logo.png")] = std::make_tuple(U("image/logo.png"), U("application/octet-stream"));
56 m_htmlcontentmap[U("/image/bing-logo.jpg")] =
57 std::make_tuple(U("image/bing-logo.jpg"), U("application/octet-stream"));
58 m_htmlcontentmap[U("/image/wall.jpg")] = std::make_tuple(U("image/wall.jpg"), U("application/octet-stream"));
59 }
60
61 void CasaLens::handle_error(pplx::task<void>& t)
62 {
63 try
64 {
65 t.get();
66 }
67 catch (...)
68 {
69 // Ignore the error, Log it if a logger is available
70 }
71 }
72
73 pplx::task<void> CasaLens::open()
74 {
75 return m_listener.open().then([](pplx::task<void> t) { handle_error(t); });
76 }
77
78 pplx::task<void> CasaLens::close()
79 {
80 return m_listener.close().then([](pplx::task<void> t) { handle_error(t); });
81 }
82
83 // Handler to process HTTP::GET requests.
84 // Replies to the request with data.
85 void CasaLens::handle_get(http_request message)
86 {
87 auto path = message.relative_uri().path();
88 auto content_data = m_htmlcontentmap.find(path);
89 if (content_data == m_htmlcontentmap.end())
90 {
91 message.reply(status_codes::NotFound, U("Path not found")).then([](pplx::task<void> t) { handle_error(t); });
92 return;
93 }
94
95 auto file_name = std::get<0>(content_data->second);
96 auto content_type = std::get<1>(content_data->second);
97 concurrency::streams::fstream::open_istream(file_name, std::ios::in)
98 .then([=](concurrency::streams::istream is) {
99 message.reply(status_codes::OK, is, content_type).then([](pplx::task<void> t) { handle_error(t); });
100 })
101 .then([=](pplx::task<void> t) {
102 try
103 {
104 t.get();
105 }
106 catch (...)
107 {
108 // opening the file (open_istream) failed.
109 // Reply with an error.
110 message.reply(status_codes::InternalError).then([](pplx::task<void> t) { handle_error(t); });
111 }
112 });
113 }
114
115 // Respond to HTTP::POST messages
116 // Post data will contain the postal code or location string.
117 // Aggregate location data from different services and reply to the POST request.
118 void CasaLens::handle_post(http_request message)
119 {
120 auto path = message.relative_uri().path();
121 if (0 == path.compare(U("/")))
122 {
123 message.extract_string()
124 .then([=](const utility::string_t& location) { get_data(message, location); })
125 .then([](pplx::task<void> t) { handle_error(t); });
126 }
127 else
128 {
129 message.reply(status_codes::NotFound, U("Path not found")).then([](pplx::task<void> t) { handle_error(t); });
130 }
131 }
132
133 #ifdef _WIN32
134 int wmain(int argc, wchar_t* args[])
135 #else
136 int main(int argc, char* args[])
137 #endif
138 {
139 if (argc != 2)
140 {
141 wprintf(U("Usage: casalens.exe port\n"));
142 return -1;
143 }
144
145 std::wstring address = U("http://localhost:");
146 address.append(args[1]);
147
148 CasaLens listener(address);
149 listener.open().wait();
150
151 std::wcout << utility::string_t(U("Listening for requests at: ")) << address << std::endl;
152
153 std::string line;
154 std::wcout << U("Hit Enter to close the listener.");
155 std::getline(std::cin, line);
156
157 listener.close().wait();
158
159 return 0;
160 }