<commit_before>/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * network.cpp
 * Implementation of the Network class
 * Copyright (C) 2005  <NAME>
 */

#include "network.h"
#include <llad/logger.h>

#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>


/*
 * Create a new Network object
 */
Network::Network() {
  m_sd = 0;

}

/*
 * Clean up handlers
 *
 *
 */
Network::~Network() {
  unsigned int i;
  vector<Listener*>::iterator it;

  for (i = 0; i < m_rhandlers_vect.size(); i++) {
    delete m_rhandlers_vect[i];
  }

  for (i = 0; i < m_whandlers_vect.size(); i++) {
    delete m_whandlers_vect[i];
  }

  m_rhandlers_vect.clear();
  m_whandlers_vect.clear();
}


/*
 * Initialise this network object
 *
 *
 */
int Network::init() {
  struct sockaddr_in servaddr;

  m_sd = socket(AF_INET, SOCK_DGRAM, 0);

  if (m_sd < 0) {
    Logger::instance()->log(Logger::CRIT, "Failed to create socket %s", strerror(errno));
    goto e_socket;
  }

  // setup
  memset(&servaddr, 0x00, sizeof(servaddr) );
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(LLAD_PORT);
  inet_aton(LLAD_ADDR, &servaddr.sin_addr);

  if( bind(m_sd, (struct sockaddr *) &servaddr, sizeof(servaddr) ) == -1 ) {
    Logger::instance()->log(Logger::CRIT, "Bind error %s", strerror(errno));
    goto e_bind;
  }

  return 0;

e_bind:
  close(m_sd);
e_socket:
  return -1;
}




/*
 * Register a file descriptor
 *
 * @param  fd    the file descriptor to register
 * @param  dir    LLA_FD_RD (read) or LLA_FD_WR (write)
 * @param  fh    the function to invoke
 * @param  data  data passed to the handler
 *
 * @return 0 on sucess, -1 on failure
 */
int Network::register_fd(int fd, Network::Direction dir, FDListener *listener, FDManager *manager ) {
  Listener *list;

  list = new Listener(fd, listener, manager);

  // add to handlers
  if( dir == Network::READ ) {
    m_rhandlers_vect.push_back(list);
  } else {
    m_whandlers_vect.push_back(list);
  }
  Logger::instance()->log(Logger::INFO, "Registered fd %d", fd);

  return 0;

}


/*
 * Unregister a file descriptor
 *
 * @param  fh  the file descriptor to unregister
 * @return 0 on sucess, -1 on failure
 */
int Network::unregister_fd(int fd, Network::Direction dir) {
  vector<Listener*>::iterator it;

  if( dir == Network::READ ) {
    for (it = m_rhandlers_vect.begin(); it != m_rhandlers_vect.end(); ++it) {
      if((*it)->m_fd == fd) {
        delete *it;
        m_rhandlers_vect.erase(it);
        Logger::instance()->log(Logger::INFO, "Unregistered fd %d", fd);
        break;
      }
    }
  } else {
    for (it = m_whandlers_vect.begin(); it != m_whandlers_vect.end(); ++it) {
      if((*it)->m_fd == fd) {
        delete *it;
        m_rhandlers_vect.erase(it);
        Logger::instance()->log(Logger::INFO, "Unregistered fd %d!!\n", fd);
        break;
      }
    }

  }
  return 0;
}


/*
 * Register a timeout function
 *
 * @param seconds    the delay between function calls
 */
int Network::register_timeout(int ms, TimeoutListener *listener, bool recur,
                              bool free) {
  event_t event;
  event.listener = listener;
  event.interval = recur ? ms : 0;
  event.free = free;

  gettimeofday(&event.next, NULL);
  event.next.tv_sec += ms / 1000;
  event.next.tv_usec += 1000 * (ms % 1000);

  m_event_cbs.push(event);
  printf("registed %d\n", ms);
  return 1;
}

#define max(a,b) a>b?a:b
/*
 *
 *
 * @return -1 on error, 0 on timeout or interrupt, else the number of bytes read
 */
int Network::read(lla_msg *msg) {
  int maxsd, ret;
  unsigned int i;
  fd_set r_fds, w_fds;
  struct timeval tv;
  struct timeval now;

  while(1) {
    FD_ZERO(&r_fds);
    FD_ZERO(&w_fds);
<commit_msg> * remove a extra printf<commit_after>/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * network.cpp
 * Implementation of the Network class
 * Copyright (C) 2005  <NAME>
 */

#include "network.h"
#include <llad/logger.h>

#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>


/*
 * Create a new Network object
 */
Network::Network() {
  m_sd = 0;

}

/*
 * Clean up handlers
 *
 *
 */
Network::~Network() {
  unsigned int i;
  vector<Listener*>::iterator it;

  for (i = 0; i < m_rhandlers_vect.size(); i++) {
    delete m_rhandlers_vect[i];
  }

  for (i = 0; i < m_whandlers_vect.size(); i++) {
    delete m_whandlers_vect[i];
  }

  m_rhandlers_vect.clear();
  m_whandlers_vect.clear();
}


/*
 * Initialise this network object
 *
 *
 */
int Network::init() {
  struct sockaddr_in servaddr;

  m_sd = socket(AF_INET, SOCK_DGRAM, 0);

  if (m_sd < 0) {
    Logger::instance()->log(Logger::CRIT, "Failed to create socket %s", strerror(errno));
    goto e_socket;
  }

  // setup
  memset(&servaddr, 0x00, sizeof(servaddr) );
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(LLAD_PORT);
  inet_aton(LLAD_ADDR, &servaddr.sin_addr);

  if( bind(m_sd, (struct sockaddr *) &servaddr, sizeof(servaddr) ) == -1 ) {
    Logger::instance()->log(Logger::CRIT, "Bind error %s", strerror(errno));
    goto e_bind;
  }

  return 0;

e_bind:
  close(m_sd);
e_socket:
  return -1;
}




/*
 * Register a file descriptor
 *
 * @param  fd    the file descriptor to register
 * @param  dir    LLA_FD_RD (read) or LLA_FD_WR (write)
 * @param  fh    the function to invoke
 * @param  data  data passed to the handler
 *
 * @return 0 on sucess, -1 on failure
 */
int Network::register_fd(int fd, Network::Direction dir, FDListener *listener, FDManager *manager ) {
  Listener *list;

  list = new Listener(fd, listener, manager);

  // add to handlers
  if( dir == Network::READ ) {
    m_rhandlers_vect.push_back(list);
  } else {
    m_whandlers_vect.push_back(list);
  }
  Logger::instance()->log(Logger::INFO, "Registered fd %d", fd);

  return 0;

}


/*
 * Unregister a file descriptor
 *
 * @param  fh  the file descriptor to unregister
 * @return 0 on sucess, -1 on failure
 */
int Network::unregister_fd(int fd, Network::Direction dir) {
  vector<Listener*>::iterator it;

  if( dir == Network::READ ) {
    for (it = m_rhandlers_vect.begin(); it != m_rhandlers_vect.end(); ++it) {
      if((*it)->m_fd == fd) {
        delete *it;
        m_rhandlers_vect.erase(it);
        Logger::instance()->log(Logger::INFO, "Unregistered fd %d", fd);
        break;
      }
    }
  } else {
    for (it = m_whandlers_vect.begin(); it != m_whandlers_vect.end(); ++it) {
      if((*it)->m_fd == fd) {
        delete *it;
        m_rhandlers_vect.erase(it);
        Logger::instance()->log(Logger::INFO, "Unregistered fd %d!!\n", fd);
        break;
      }
    }

  }
  return 0;
}


/*
 * Register a timeout function
 *
 * @param seconds    the delay between function calls
 */
int Network::register_timeout(int ms, TimeoutListener *listener, bool recur,
                              bool free) {
  event_t event;
  event.listener = listener;
  event.interval = recur ? ms : 0;
  event.free = free;

  gettimeofday(&event.next, NULL);
  event.next.tv_sec += ms / 1000;
  event.next.tv_usec += 1000 * (ms % 1000);

  m_event_cbs.push(event);
  return 1;
}

#define max(a,b) a>b?a:b
/*
 *
 *
 * @return -1 on error, 0 on timeout or interrupt, else the number of bytes read
 */
int Network::read(lla_msg *msg) {
  int maxsd, ret;
  unsigned int i;
  fd_set r_fds, w_fds;
  struct timeval tv;
  struct timeval now;

  while(1) {
    FD_ZERO(&r_fds);
    FD_ZERO(&w_fds);

{'fraction_non_alphanumeric': 0.1361413647477217, 'fraction_numerical': 0.011780395643476329, 'mean_word_length': 3.2051401869158878, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1305755', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3161, 'n_words': 915}