<commit_before>/*
 * crofbase.cpp
 *
 *  Created on: 05.08.2014
 *      Author: andreas
 */

#include "cbasebox.hpp"
#include <assert.h>
#include "cunixenv.hpp"

using namespace basebox;

/*static*/ bool cbasebox::keep_on_running = true;

/*static*/ int cbasebox::run(int argc, char **argv) {
  rofl::cunixenv env_parser(argc, argv);

  /* update defaults */
  env_parser.add_option(
      rofl::coption(true, REQUIRED_ARGUMENT, 'p', "port", "set port", "6653"));

  // command line arguments
  env_parser.parse_args();

  /*
   * extract help flag
   */
  if (env_parser.is_arg_set("help")) {
    std::cout << env_parser.get_usage((char *)"baseboxd");
    exit(0);
  }

  /*
   * extract debug level
   */
  int rofl_debug = 0, core_debug = 0;
  if (env_parser.is_arg_set("debug")) {
    core_debug = rofl_debug = atoi(env_parser.get_arg("debug").c_str());
  }

  rofcore::logging::set_debug_level(core_debug);

  rofl::openflow::cofhello_elem_versionbitmap versionbitmap;
  versionbitmap.add_ofp_version(rofl::openflow13::OFP_VERSION);
  rofcore::logging::notice << "[baseboxd][main] using OpenFlow version-bitmap:"
                           << std::endl
                           << versionbitmap;
  basebox::cbasebox &box = basebox::cbasebox::get_instance(versionbitmap);

  std::stringstream portno;
  if (env_parser.is_arg_set("port")) {
    portno << env_parser.get_arg("port").c_str();
  } else {
    portno << (int)6653;
  }

  rofl::csockaddr baddr(AF_INET, std::string("0.0.0.0"),
                        atoi(portno.str().c_str()));
  box.dpt_sock_listen(baddr);

  // start netlink
  (void)rofcore::cnetlink::get_instance();

  while (keep_on_running) {
    try {
      // Launch main I/O loop
      struct timespec ts;
      ts.tv_sec = 5;
      ts.tv_nsec = 0;
      pselect(0, NULL, NULL, NULL, &ts, NULL);

    } catch (std::exception &e) {
      std::cerr << "exception caught, what: " << e.what() << std::endl;
    }
  }

  return EXIT_SUCCESS;
}

/*static*/
void cbasebox::stop() {
  cbasebox::keep_on_running = false;
  //	thread.stop();
}

void cbasebox::handle_dpt_open(rofl::crofdpt &dpt) {

  if (rofl::openflow13::OFP_VERSION < dpt.get_version()) {
    rofcore::logging::error
        << "[cbasebox][handle_dpt_open] datapath "
        << "attached with invalid OpenFlow protocol version: "
        << (int)dpt.get_version() << std::endl;
    return;
  }

  if (sa) {
    delete sa;
    sa = NULL;
  }

#ifdef DEBUG
  dpt.set_conn(rofl::cauxid(0))
      .set_trace(true)
      .set_journal()
      .log_on_stderr(true)
      .set_max_entries(64);
  dpt.set_conn(rofl::cauxid(0))
      .set_tcp_journal()
      .log_on_stderr(true)
      .set_max_entries(16);
#endif

  rofcore::logging::debug << "[cbasebox][handle_dpt_open] dpid: "
                          << dpt.get_dpid().str() << std::endl;
  rofcore::logging::debug << "[cbasebox][handle_dpt_open] dpt: " << dpt
                          << std::endl;

  dpt.send_features_request(rofl::cauxid(0));
  dpt.send_desc_stats_request(rofl::cauxid(0), 0);
  dpt.send_port_desc_stats_request(rofl::cauxid(0), 0);

  // todo timeout?
}

void cbasebox::handle_dpt_close(const rofl::cdptid &dptid) {

  rofcore::logging::debug << "[cbasebox][handle_dpt_close] dpid: " << dpid.str()
                          << std::endl;

  // FIXME implemente close
  if (sa) {
    sa->handle_dpt_close(dptid);
  }
}

void cbasebox::handle_conn_terminated(rofl::crofdpt &dpt,
                                      const rofl::cauxid &auxid) {
  assert(0);
}

void cbasebox::handle_conn_refused(rofl::crofdpt &dpt,
                                   const rofl::cauxid &auxid) {
  assert(0);
}

void cbasebox::handle_conn_failed(rofl::crofdpt &dpt,
                                  const rofl::cauxid &auxid) {
  assert(0);
}

void cbasebox::handle_conn_negotiation_failed(rofl::crofdpt &dpt,
                                              const rofl::cauxid &auxid) {
  assert(0);
}

void cbasebox::handle_conn_congestion_occured(rofl::crofdpt &dpt,
                                              const rofl::cauxid &auxid) {
  rofcore::logging::crit << "[cbasebox][" << __FUNCTION__
                         << "]: XXX not implemented" << std::endl;
}

void cbasebox::handle_conn_congestion_solved(rofl::crofdpt &dpt,
                                             const rofl::cauxid &auxid) {
  rofcore::logging::crit << "[cbasebox][" << __FUNCTION__
                         << "]: XXX not implemented" << std::endl;
}

void cbasebox::handle_features_reply(
    rofl::crofdpt &dpt, const rofl::cauxid &auxid,
    rofl::openflow::cofmsg_features_reply &msg) {
  rofcore::logging::debug << "[cbasebox][handle_features_reply] dpid: "
                          << dpt.get_dpid().str() << std::endl
                          << msg;
}
<commit_msg>remove asserts, since they throw for c++<commit_after>/*
 * crofbase.cpp
 *
 *  Created on: 05.08.2014
 *      Author: andreas
 */

#include "cbasebox.hpp"
#include <assert.h>
#include "cunixenv.hpp"

using namespace basebox;

/*static*/ bool cbasebox::keep_on_running = true;

/*static*/ int cbasebox::run(int argc, char **argv) {
  rofl::cunixenv env_parser(argc, argv);

  /* update defaults */
  env_parser.add_option(
      rofl::coption(true, REQUIRED_ARGUMENT, 'p', "port", "set port", "6653"));

  // command line arguments
  env_parser.parse_args();

  /*
   * extract help flag
   */
  if (env_parser.is_arg_set("help")) {
    std::cout << env_parser.get_usage((char *)"baseboxd");
    exit(0);
  }

  /*
   * extract debug level
   */
  int rofl_debug = 0, core_debug = 0;
  if (env_parser.is_arg_set("debug")) {
    core_debug = rofl_debug = atoi(env_parser.get_arg("debug").c_str());
  }

  rofcore::logging::set_debug_level(core_debug);

  rofl::openflow::cofhello_elem_versionbitmap versionbitmap;
  versionbitmap.add_ofp_version(rofl::openflow13::OFP_VERSION);
  rofcore::logging::notice << "[baseboxd][main] using OpenFlow version-bitmap:"
                           << std::endl
                           << versionbitmap;
  basebox::cbasebox &box = basebox::cbasebox::get_instance(versionbitmap);

  std::stringstream portno;
  if (env_parser.is_arg_set("port")) {
    portno << env_parser.get_arg("port").c_str();
  } else {
    portno << (int)6653;
  }

  rofl::csockaddr baddr(AF_INET, std::string("0.0.0.0"),
                        atoi(portno.str().c_str()));
  box.dpt_sock_listen(baddr);

  // start netlink
  (void)rofcore::cnetlink::get_instance();

  while (keep_on_running) {
    try {
      // Launch main I/O loop
      struct timespec ts;
      ts.tv_sec = 5;
      ts.tv_nsec = 0;
      pselect(0, NULL, NULL, NULL, &ts, NULL);

    } catch (std::exception &e) {
      std::cerr << "exception caught, what: " << e.what() << std::endl;
    }
  }

  return EXIT_SUCCESS;
}

/*static*/
void cbasebox::stop() {
  cbasebox::keep_on_running = false;
  //	thread.stop();
}

void cbasebox::handle_dpt_open(rofl::crofdpt &dpt) {

  if (rofl::openflow13::OFP_VERSION < dpt.get_version()) {
    rofcore::logging::error
        << "[cbasebox][handle_dpt_open] datapath "
        << "attached with invalid OpenFlow protocol version: "
        << (int)dpt.get_version() << std::endl;
    return;
  }

  if (sa) {
    delete sa;
    sa = NULL;
  }

#ifdef DEBUG
  dpt.set_conn(rofl::cauxid(0))
      .set_trace(true)
      .set_journal()
      .log_on_stderr(true)
      .set_max_entries(64);
  dpt.set_conn(rofl::cauxid(0))
      .set_tcp_journal()
      .log_on_stderr(true)
      .set_max_entries(16);
#endif

  rofcore::logging::debug << "[cbasebox][handle_dpt_open] dpid: "
                          << dpt.get_dpid().str() << std::endl;
  rofcore::logging::debug << "[cbasebox][handle_dpt_open] dpt: " << dpt
                          << std::endl;

  dpt.send_features_request(rofl::cauxid(0));
  dpt.send_desc_stats_request(rofl::cauxid(0), 0);
  dpt.send_port_desc_stats_request(rofl::cauxid(0), 0);

  // todo timeout?
}

void cbasebox::handle_dpt_close(const rofl::cdptid &dptid) {

  rofcore::logging::debug << "[cbasebox][handle_dpt_close] dpid: " << dpid.str()
                          << std::endl;

  // FIXME implemente close
  if (sa) {
    sa->handle_dpt_close(dptid);
  }
}

void cbasebox::handle_conn_terminated(rofl::crofdpt &dpt,
                                      const rofl::cauxid &auxid) {
  rofcore::logging::crit << "[cbasebox][" << __FUNCTION__
                         << "]: XXX not implemented" << std::endl;
}

void cbasebox::handle_conn_refused(rofl::crofdpt &dpt,
                                   const rofl::cauxid &auxid) {
  rofcore::logging::crit << "[cbasebox][" << __FUNCTION__
                         << "]: XXX not implemented" << std::endl;
}

void cbasebox::handle_conn_failed(rofl::crofdpt &dpt,
                                  const rofl::cauxid &auxid) {
  rofcore::logging::crit << "[cbasebox][" << __FUNCTION__
                         << "]: XXX not implemented" << std::endl;
}

void cbasebox::handle_conn_negotiation_failed(rofl::crofdpt &dpt,
                                              const rofl::cauxid &auxid) {
  rofcore::logging::crit << "[cbasebox][" << __FUNCTION__
                         << "]: XXX not implemented" << std::endl;
}

void cbasebox::handle_conn_congestion_occured(rofl::crofdpt &dpt,
                                              const rofl::cauxid &auxid) {
  rofcore::logging::crit << "[cbasebox][" << __FUNCTION__
                         << "]: XXX not implemented" << std::endl;
}

void cbasebox::handle_conn_congestion_solved(rofl::crofdpt &dpt,
                                             const rofl::cauxid &auxid) {
  rofcore::logging::crit << "[cbasebox][" << __FUNCTION__
                         << "]: XXX not implemented" << std::endl;
}

void cbasebox::handle_features_reply(
    rofl::crofdpt &dpt, const rofl::cauxid &auxid,
    rofl::openflow::cofmsg_features_reply &msg) {
  rofcore::logging::debug << "[cbasebox][handle_features_reply] dpid: "
                          << dpt.get_dpid().str() << std::endl
                          << msg;
}

{'fraction_non_alphanumeric': 0.15547177299335252, 'fraction_numerical': 0.008532592519099116, 'mean_word_length': 2.682864450127877, 'pattern_counts': {'":': 0, '<': 175, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4979452', 'n_tokens_mistral': 3652, 'n_tokens_neox': 3562, 'n_words': 570}