github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/docs/examples/example-includeos-unik-service/service.cpp (about)

     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 <os>
    19  #include <net/inet4>
    20  
    21  constexpr int port {8080};
    22  
    23  void Service::start(const std::string&) {
    24    using namespace std::string_literals;
    25  
    26    printf("here we go!\n");
    27    printf("OS::ready_ is %d\n", OS::ready_);
    28    printf("here are my env vars: %s\n", environ);
    29  
    30    char** env = (char**) malloc(sizeof(char*)*2);
    31    env[0] = "foo";
    32    env[1] = "bar";
    33  
    34    printf("env[0]: %s\nenv[1]: %s\nenv[2]: %s\n", env[0], env[1], env[2]);
    35  
    36    auto& server = net::Inet4::stack().tcp().bind(port);
    37    server.on_connect([] (auto conn) {
    38      conn->on_read(1024, [conn] (auto buf, size_t n) {
    39        auto response {"My first unikernel!\n"s};
    40        conn->write(response);
    41        conn->close();
    42      });
    43    });
    44  }