|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2015 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#include <cmath> // rand() |
| 19 | +#include <sstream> |
| 20 | + |
| 21 | +#include <os> |
| 22 | +#include <net/inet4> |
| 23 | +#include <timers> |
| 24 | +#include <net/http/request.hpp> |
| 25 | +#include <net/http/response.hpp> |
| 26 | +#include "liu.hpp" |
| 27 | + |
| 28 | +using namespace std::chrono; |
| 29 | +http::Response handle_request(const http::Request& req); |
| 30 | + |
| 31 | +void Service::start() |
| 32 | +{ |
| 33 | + // Get the first IP stack |
| 34 | + auto& inet = net::Super_stack::get<net::IP4>(0); |
| 35 | + |
| 36 | + // Print some useful netstats every 30 secs |
| 37 | + Timers::periodic(5s, 30s, |
| 38 | + [&inet] (uint32_t) { |
| 39 | + printf("<Service> TCP STATUS:\n%s\n", inet.tcp().status().c_str()); |
| 40 | + }); |
| 41 | + |
| 42 | + // Set up a TCP server on port 80 |
| 43 | + auto& server = inet.tcp().listen(80); |
| 44 | + |
| 45 | + // Add a TCP connection handler - here a hardcoded HTTP-service |
| 46 | + server.on_connect( |
| 47 | + [] (auto conn) |
| 48 | + { |
| 49 | + printf("<Service> @on_connect: Connection %s successfully established.\n", |
| 50 | + conn->remote().to_string().c_str()); |
| 51 | + // read async with a buffer size of 1024 bytes |
| 52 | + // define what to do when data is read |
| 53 | + conn->on_read(1024, |
| 54 | + [conn] (auto buf, size_t n) |
| 55 | + { |
| 56 | + printf("<Service> @on_read: %lu bytes received.\n", n); |
| 57 | + try |
| 58 | + { |
| 59 | + std::string data{(const char*)buf.get(), n}; |
| 60 | + // try to parse the request |
| 61 | + http::Request req{data}; |
| 62 | + |
| 63 | + // handle the request, getting a matching response |
| 64 | + auto res = handle_request(req); |
| 65 | + |
| 66 | + printf("<Service> Responding with %u %s.\n", |
| 67 | + res.status_code(), http::code_description(res.status_code()).to_string().c_str()); |
| 68 | + |
| 69 | + conn->write(res); |
| 70 | + } |
| 71 | + catch(const std::exception& e) |
| 72 | + { |
| 73 | + printf("<Service> Unable to parse request:\n%s\n", e.what()); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + if (liu::LiveUpdate::is_resumable() == false) |
| 79 | + { |
| 80 | + setup_liveupdate_server(inet, 666, nullptr); |
| 81 | + } |
| 82 | + else { |
| 83 | + printf("System live updated!\n"); |
| 84 | + } |
| 85 | +} |
0 commit comments