<commit_before>/*
 * Copyright (c) 2015-2016 Fotonic
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *       http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *       http://www.opensource.org/licenses/apache2.0.php
 *
 * You can redistribute this code under either of these licenses.
 * For more information; see http://www.arrowhead.eu/licensing
 */

/**
 * @file
 * @brief       Arrowhead CoAP implementation
 *
 * @todo split coap.cpp into file per class
 *
 * @author      <NAME> <<EMAIL>>
 */

#include "arrowhead/config.h"

#include <stdexcept> // for std::runtime_error
#include <new> // for std::bad_alloc

#if ARROWHEAD_USE_LIBCOAP
#include "arrowhead/coap.hpp"

namespace Arrowhead {

void CoAPResource::register_handler(unsigned char method, coap_method_handler_t handler)
{
    coap_register_handler(res, method, handler);
}

void CoAPResource::initialize()
{
    const unsigned char *urip = reinterpret_cast<const unsigned char *>(uri.c_str());
    int flags = 0;
    res = coap_resource_init(urip, uri.length(), flags);
    if (res == 0) {
        // Failed to allocate memory for resource descriptor
        throw std::bad_alloc();
    }
}

/* CoAPContext ********************** */

CoAPContext::CoAPContext(unsigned short port)
{
    coap_address_init(&listen_addr);
    listen_addr.addr.sin6.sin6_family = AF_INET6;
    listen_addr.addr.sin6.sin6_addr   = IN6ADDR_ANY_INIT;
    listen_addr.addr.sin6.sin6_port   = htons(port);
    ctx = coap_new_context(&listen_addr);
    if (ctx == 0) {
        // Failed to allocate a new context struct
        throw std::bad_alloc();
    }
}

CoAPContext::CoAPContext(const struct sockaddr_in6& sin6)
{
    coap_address_init(&listen_addr);
    listen_addr.addr.sin6 = sin6;
    ctx = coap_new_context(&listen_addr);
    if (ctx == 0) {
        // Failed to allocate a new context struct
        throw std::bad_alloc();
    }
}

CoAPContext::~CoAPContext()
{
    // Clean up context
    coap_free_context(ctx);
}

void CoAPContext::add_resource(coap_resource_t *res)
{
    coap_add_resource(ctx, res);
}

void CoAPContext::add_resource(CoAPResource& res)
{
    coap_add_resource(ctx, res.resource_ptr());
}

void CoAPContext::run_forever()
{
    using namespace std;
    fd_set readfds;
    while (1) {
        FD_ZERO(&readfds);
        FD_SET(ctx->sockfd, &readfds);
        int result = select(FD_SETSIZE, &readfds, 0, 0, 0);
        if (result < 0) /* socket error */
        {
            throw std::runtime_error("socket read error");
        }
        else if (result > 0 && FD_ISSET(ctx->sockfd, &readfds))
        {
            coap_read(ctx);
        }
    }
}

void CoAPContext::run_once()
{
    using namespace std;
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(ctx->sockfd, &readfds);
    int result = select(FD_SETSIZE, &readfds, 0, 0, 0);
    if (result < 0) /* socket error */
    {
        throw std::runtime_error("socket read error");
    }
    else if (result > 0 && FD_ISSET(ctx->sockfd, &readfds))
    {
        coap_read(ctx);
    }
}

} /* namespace Arrowhead */
#endif<commit_msg>coap: Return error message for select<commit_after>/*
 * Copyright (c) 2015-2016 Fotonic
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *       http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *       http://www.opensource.org/licenses/apache2.0.php
 *
 * You can redistribute this code under either of these licenses.
 * For more information; see http://www.arrowhead.eu/licensing
 */

/**
 * @file
 * @brief       Arrowhead CoAP implementation
 *
 * @todo split coap.cpp into file per class
 *
 * @author      <NAME> <<EMAIL>>
 */

#include "arrowhead/config.h"

#include <stdexcept> // for std::runtime_error
#include <new> // for std::bad_alloc
#include <cstring> // for strerror
#include <errno.h> // for errno

#if ARROWHEAD_USE_LIBCOAP
#include "arrowhead/coap.hpp"

namespace Arrowhead {

void CoAPResource::register_handler(unsigned char method, coap_method_handler_t handler)
{
    coap_register_handler(res, method, handler);
}

void CoAPResource::initialize()
{
    const unsigned char *urip = reinterpret_cast<const unsigned char *>(uri.c_str());
    int flags = 0;
    res = coap_resource_init(urip, uri.length(), flags);
    if (res == 0) {
        // Failed to allocate memory for resource descriptor
        throw std::bad_alloc();
    }
}

/* CoAPContext ********************** */

CoAPContext::CoAPContext(unsigned short port)
{
    coap_address_init(&listen_addr);
    listen_addr.addr.sin6.sin6_family = AF_INET6;
    listen_addr.addr.sin6.sin6_addr   = IN6ADDR_ANY_INIT;
    listen_addr.addr.sin6.sin6_port   = htons(port);
    ctx = coap_new_context(&listen_addr);
    if (ctx == 0) {
        // Failed to allocate a new context struct
        throw std::bad_alloc();
    }
}

CoAPContext::CoAPContext(const struct sockaddr_in6& sin6)
{
    coap_address_init(&listen_addr);
    listen_addr.addr.sin6 = sin6;
    ctx = coap_new_context(&listen_addr);
    if (ctx == 0) {
        // Failed to allocate a new context struct
        throw std::bad_alloc();
    }
}

CoAPContext::~CoAPContext()
{
    // Clean up context
    coap_free_context(ctx);
}

void CoAPContext::add_resource(coap_resource_t *res)
{
    coap_add_resource(ctx, res);
}

void CoAPContext::add_resource(CoAPResource& res)
{
    coap_add_resource(ctx, res.resource_ptr());
}

void CoAPContext::run_forever()
{
    using namespace std;
    fd_set readfds;
    while (1) {
        FD_ZERO(&readfds);
        FD_SET(ctx->sockfd, &readfds);
        int result = select(FD_SETSIZE, &readfds, 0, 0, 0);
        if (result < 0)
        {
            std::string err(strerror(errno));
            throw std::runtime_error("select: " + err);
        }
        else if (result > 0 && FD_ISSET(ctx->sockfd, &readfds))
        {
            coap_read(ctx);
        }
    }
}

void CoAPContext::run_once()
{
    using namespace std;
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(ctx->sockfd, &readfds);
    int result = select(FD_SETSIZE, &readfds, 0, 0, 0);
    if (result < 0)
    {
        std::string err(strerror(errno));
        throw std::runtime_error("select: " + err);
    }
    else if (result > 0 && FD_ISSET(ctx->sockfd, &readfds))
    {
        coap_read(ctx);
    }
}

} /* namespace Arrowhead */
#endif
{'fraction_non_alphanumeric': 0.12910832719233603, 'fraction_numerical': 0.013264554163596167, 'mean_word_length': 3.075675675675676, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2449828', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2277, 'n_words': 542}