コード例 #1
0
ファイル: mailer.c プロジェクト: ddugovic/nngs
int mail_spool(char *nbuff, const char *to, const char *subj, const char *text, const char *fname)
{
  char buff[MAX_LINE_SIZE];
  FILE *fp;

  if (!to) return -1;
  mail_tempnam(nbuff);
  fp = fopen(nbuff, "w");
  if (!fp) {
    Logit("Mail_spool(%s) : failed to open: %d(%s)"
    , buff, errno, strerror(errno));
    return -1;
  }
  fprintf(fp, "t %s\n", to);
  if (subj) fprintf(fp, "s %s\n", subj);
  fprintf(fp, "\n");
  if (text) fprintf(fp, "%s\n", text);
  if (fname) {
    FILE *in;
    in = fopen(fname, "r");
    if (!in) {
      Logit("Mail_spool(%s) : failed to open \"%s\": %d(%s)"
      , buff, fname, errno, strerror(errno));
      fclose(fp); return -2; }
    while (fgets(buff, sizeof buff, in)) fputs(buff,fp);
    fclose(in);
  }
  fclose(fp);
  return mail_child(nbuff);
}
コード例 #2
0
ファイル: mailer.c プロジェクト: ddugovic/nngs
static int mail_one(const char *spool)
{
  FILE *fp, *pipo;
  char buff[MAX_LINE_SIZE];
  char *to=NULL;
  char *subj=NULL;
  int rc;

  fp = fopen(spool, "r");
  if (!fp) {
    Logit("Mail_one(%s) : failed to open: %d(%s)"
    , spool, errno, strerror(errno));
    return -1;
  }

  while(fgets(buff, sizeof buff, fp)) {
    switch(buff[0]) {
    case 't': to = mystrdup(buff+2); break;
    case 's': subj = mystrdup(buff+2); break;
    case '\0': case '\n': case ' ': goto body;
    default: continue;
  }}
body:

  if (!to) {
    Logit("Mail_one(%s) : no \"to:\"", spool);
    fclose(fp);
    if (subj) free(subj);
    return -2;}

  to[strlen(to)-1] = 0;
  if (subj) subj[strlen(subj)-1] = 0;

  if (memcmp(conffile.mail_program, "SMTP", 4)) {
    if (subj) sprintf(buff, "%s -s \"%s\" %s", conffile.mail_program, subj, to);
    else sprintf(buff, "%s %s", conffile.mail_program, to);
    if (conffile.debug_mailer) Logit("Mail_one(%s) : opening pipe: \"%s\""
       , spool, buff);
    pipo = popen(buff, "w");
    fprintf(pipo, "From: %s\n", conffile.server_email);
    if (conffile.smtp_reply_to) fprintf(pipo, "Reply-To: %s\n", conffile.smtp_reply_to);
    fprintf(pipo, "X-NNGS-SMTP: %s\n", "No, Sir!" );
    fprintf(pipo, "\n");
    while(fgets(buff, sizeof buff, fp)) fputs(buff, pipo);
    fclose(pipo);
    rc = 0;
  } else {
    rc =  smtp_mail(fp, to, subj);
    if (conffile.debug_mailer) Logit("Smtp_mail(to=%s,Subj=%s) returned %d"
       , to, subj, rc);
  }

  if (rc >= 0) unlink(spool);
  fclose(fp);
  if (to) free(to);
  if (subj) free(subj);
  return (rc>=0) ? 0: -1;
}
コード例 #3
0
ファイル: nngsmain.c プロジェクト: henrychoi/realgo
static void TerminateServer(int sig)
{
  fprintf(stderr, "Got signal %d\n", sig);
  Logit("Got signal %d", sig);
  TerminateCleanup();
  net_closeAll();
  main_exit(1);
}
コード例 #4
0
ファイル: mailer.c プロジェクト: ddugovic/nngs
static int child_perror(const char *msg)
{
int err;

err = smtp_err;
if (!msg) msg = "Message";
Logit("Child_perror; %s : %d(%s)", msg, err, strerror(err));
return -1;
}
コード例 #5
0
ファイル: nngsmain.c プロジェクト: henrychoi/realgo
static int all_the_internets(void)
{
int port;
int pos, len, rc, cnt;

  for(pos=cnt=0;conffile.server_ports[pos];pos += len	) {
    rc = sscanf(conffile.server_ports+pos, "%d%n", &port, &len );
    if (rc < 0) break;
    if (rc < 1) { len=1; continue; }
    if (net_init(port, conffile.want_udp_port > 0 ? 1 : 0)) {
      Logit("Init failed on port %d.", port);
      continue;
      }
    Logit("Initialized on port %d.", port);
    cnt++;
  }
return cnt;
}
コード例 #6
0
ファイル: nngsmain.c プロジェクト: henrychoi/realgo
static void reapchild(int sig)
{
  int pid, status;
  struct rusage usag;
  fprintf(stderr, "Got signal %d\n", sig);
  pid = wait3(&status, WNOHANG, &usag);
  Logit("Reaped Child %d", pid);
	/* Obsolete: reinstall signal handler. Won't harm */
  signal(SIGCHLD, reapchild);
}
コード例 #7
0
ファイル: mailer.c プロジェクト: ddugovic/nngs
static int mail_child(const char *spool)
{
  int rc, pid;

  if (conffile.want_mail_child > 0) {
    pid = fork();
    if (pid) {
      if (conffile.debug_mailer) Logit("Mail_child() =: %d", pid);
      return pid;
    }
    rc = mail_one(spool);
    if (conffile.debug_mailer) Logit("Mail_one() =: %d", rc);
    exit(0);
    return -1;
  } else {
    rc = mail_one(spool);
    if (conffile.debug_mailer) Logit("Mail_one() =: %d", rc);
    return rc;
  }
}
コード例 #8
0
ファイル: mailer.c プロジェクト: ddugovic/nngs
static void mail_tempnam(char *buff)
{
  time_t now = globclock.time;
  static time_t then = 0;
  static int seq=0;
  char deet[40];
  int siz;

  if (now != then) seq = 0;
  sprintf(deet, time2str_file((time_t *) &now));
  siz = xyfilename(buff, FILENAME_SPOOL_sd, deet, seq);
  if (conffile.debug_mailer) Logit("Mail_tempnam() := [%d]%s", siz, buff);
  then = now; seq++;
  return;
}
コード例 #9
0
ファイル: nngsmain.c プロジェクト: henrychoi/realgo
static void read_ban_ip_list(void)
{
  FILE *fp;
  int rc,cnt=0;
  char buff[100], bots[40], tops[40];
  unsigned bot, top;

  fp = xyfopen(FILENAME_LIST_BAN, "r");
  if (!fp) return;
  while(fgets(buff, sizeof buff, fp)) {
    rc = sscanf(buff, "%x %x", &bot, &top);
    if (rc < 2) {
      rc = sscanf(buff, "%s %s", bots, tops);
      if (rc < 2) continue;
#if 0
#include <netinet/in.h>
#include <arpa/inet.h>
      bot = inet_addr(bots);
      if ((int)bot == -1) continue;
      top = inet_addr(tops);
      if ((int)top == -1) continue;
      bot = ntohl(bot); /* could use asc2ipaddr(bots, &bot); */
      top = ntohl(top);
#else
      if (asc2ipaddr(bots, &bot)) continue;
      if (asc2ipaddr(tops, &top)) continue;
#endif
      }
    cnt += range_add(bot,top);
  }
  fclose(fp);
  Logit("Ipban: Read %d ranges from %s", cnt, filename() );
  bot = top = 0xf7000001;
  cnt = range_remove(bot,top);
  Logit("Ipban: Avoided %d ranges %x-%x", cnt, bot, top );
}
コード例 #10
0
ファイル: nngsmain.c プロジェクト: henrychoi/realgo
static void BrokenPipe(int sig)
{
  static time_t lasttime=0;
  static unsigned count=0;

	/* Obsolete: reinstall signal handler. Won't harm */
  signal(SIGPIPE, BrokenPipe);
  count++;
  if (globclock.time - lasttime > 10) {
    Logit("Got %u Broken Pipes in %d seconds: sig %d\n"
	, count, globclock.time - lasttime, sig);
    lasttime=globclock.time;
    count=0;
    }
}
コード例 #11
0
ファイル: gbdt.cpp プロジェクト: qiyiping/gbdt
void GBDT::LogLossProcess(DataVector *d, size_t samples, int i) {
#ifdef USE_OPENMP
#pragma omp parallel for
#endif
  for (size_t j = 0; j < samples; ++j) {
    ValueType p = Predict(*(*d)[j], i);
    (*d)[j]->target =
        static_cast<ValueType>(LogitLossGradient((*d)[j]->label, p));
  }

  if (g_conf.debug) {
    Auc auc;
    DataVector::iterator iter = d->begin();
    for ( ; iter != d->end(); ++iter) {
      ValueType p = Logit(Predict(**iter, i));
      auc.Add(p, (*iter)->label);
    }
    std::cout << "auc: " << auc.CalculateAuc() << std::endl;
  }
}
コード例 #12
0
ファイル: nngsmain.c プロジェクト: henrychoi/realgo
int main(int argc, char *argv[])
{
  FILE *fp;

#if USING_DMALLOC
  dmalloc_debug(1);
#endif

	/* start the clock (which is used by the Logit fnc) */
  (void) refetch_ticker();
  GetArgs(argc, argv);
  if (conf_file_read(confname)) {
    Logit("Failed to read config file \"%s\"", confname);
    strcpy(confname, "./nngs.cnf");
    conf_file_write(confname);
    Logit("Created \"%s\"", confname);
  }
  Logit("Starting %s (%s %s) From: %s"
  , conffile.version_string, conffile.compile_date, conffile.compile_time, confname);
  if (daemonise()) {
    Logit("Failed to daemonise, giving up");
    main_exit(1);
  }
  conf_file_write("written.cnf");
  signal(SIGTERM, TerminateServer);
  signal(SIGINT, TerminateServer);
#if 0
  signal(SIGPIPE, SIG_IGN);
#else
  signal(SIGPIPE, BrokenPipe);
#endif
  signal(SIGCHLD, reapchild);
  mink_init();
  startuptime = time(NULL);
  srand(startuptime);
  read_ban_ip_list();
  if (!all_the_internets() ) {
    fprintf(stderr, "Network initialize failed on ports %s.\n"
    , conffile.server_ports);
    main_exit(1);
  }
  player_high = 0;
  game_high = 0;
  bytes_sent = 0;
  bytes_received = 0;

#ifdef SGI
  /*mallopt(100, 1);*/  /* Turn on malloc(3X) debugging (Irix only) */
#endif
  command_init();
  EmoteInit(conffile.emotes_file);
  help_init();
  /*Logit("commands_init()");*/
  commands_init();
  /*Logit("channel_init()");*/
  channel_init();
  /*Logit("player_array_init()");*/
  player_array_init();
  player_init();
  ladder_init(NUM_LADDERS);
  Ladder9 = ladder_new(LADDERSIZE);
  Ladder19 = ladder_new(LADDERSIZE);

  completed_games = 0;
  num_logins = num_logouts = new_players = 0;

  num_9 = 0;
  fp = xyfopen(FILENAME_LADDER9, "r");
  if (fp) {
    num_9 = ladder_load(fp, Ladder9);
    Logit("%d players loaded from file %s", num_9, filename() );
    fclose(fp);
  }

  num_19 = 0;
  fp = xyfopen(FILENAME_LADDER19, "r");
  if (fp) {
    num_19 = ladder_load(fp, Ladder19);
    Logit("%d players loaded from file %s", num_19, filename() );
    fclose(fp);
  }

  /* mink_init();*/
  if (conffile.admin_name) create_admin_account(conffile.admin_name );
  Logit("Server up and running.");
  main_event_loop();
  Logit("Closing down.");
  net_closeAll();
  main_exit(0);
  return 0;
}
コード例 #13
0
ファイル: gbdt.cpp プロジェクト: litaoshao/gbdt
void GBDT::Fit(DataVector *d) {
  delete[] trees;
  trees = new RegressionTree[g_conf.iterations];

  size_t samples = d->size();
  if (g_conf.data_sample_ratio < 1) {
    samples = static_cast<size_t>(d->size() * g_conf.data_sample_ratio);
  }

  Init(*d, d->size());

  for (size_t i = 0; i < g_conf.iterations; ++i) {
    std::cout  << "iteration: " << i << std::endl;

    if (samples < d->size()) {
#ifndef USE_OPENMP
      std::random_shuffle(d->begin(), d->end());
#else
      __gnu_parallel::random_shuffle(d->begin(), d->end());
#endif
    }

    if (g_conf.loss == SQUARED_ERROR) {
      for (size_t j = 0; j < samples; ++j) {
        ValueType p = Predict(*(*d)[j], i);
        (*d)[j]->target = (*d)[j]->label - p;
      }

      if (g_conf.debug) {
        double s = 0;
        double c = 0;
        DataVector::iterator iter = d->begin();
        for ( ; iter != d->end(); ++iter) {
          ValueType p = Predict(**iter, i);
          s += Squared((*iter)->label - p) * (*iter)->weight;
          c += (*iter)->weight;
        }
        std::cout << "rmse: " << std::sqrt(s / c) << std::endl;
      }
    } else if (g_conf.loss == LOG_LIKELIHOOD) {
      for (size_t j = 0; j < samples; ++j) {
        ValueType p = Predict(*(*d)[j], i);
        (*d)[j]->target =
            static_cast<ValueType>(LogitLossGradient((*d)[j]->label, p));
      }

      if (g_conf.debug) {
        Auc auc;
        DataVector::iterator iter = d->begin();
        for ( ; iter != d->end(); ++iter) {
          ValueType p = Logit(Predict(**iter, i));
          auc.Add(p, (*iter)->label);
        }
        std::cout << "auc: " << auc.CalculateAuc() << std::endl;
      }
    }

    trees[i].Fit(d, samples);
  }


  // Calculate gain
  delete[] gain;
  gain = new double[g_conf.number_of_feature];

  for (size_t i = 0; i < g_conf.number_of_feature; ++i) {
    gain[i] = 0.0;
  }

  for (size_t j = 0; j < iterations; ++j) {
    double *g = trees[j].GetGain();
    for (size_t i = 0; i < g_conf.number_of_feature; ++i) {
      gain[i] += g[i];
    }
  }
}