Пример #1
0
int main(int argc, char *argv[])
{
	enum engine_id engine = E_UCT;
	struct time_info ti_default = { .period = TT_NULL };
	char *testfile = NULL;
	char *gtp_port = NULL;
	char *log_port = NULL;
	int gtp_sock = -1;
	char *chatfile = NULL;
	char *fbookfile = NULL;
	char *ruleset = NULL;

	seed = time(NULL) ^ getpid();

	int opt;
	while ((opt = getopt(argc, argv, "c:e:d:Df:g:l:r:s:t:u:")) != -1) {
		switch (opt) {
			case 'c':
				chatfile = strdup(optarg);
				break;
			case 'e':
				if (!strcasecmp(optarg, "random")) {
					engine = E_RANDOM;
				} else if (!strcasecmp(optarg, "replay")) {
					engine = E_REPLAY;
				} else if (!strcasecmp(optarg, "montecarlo")) {
					engine = E_MONTECARLO;
				} else if (!strcasecmp(optarg, "uct")) {
					engine = E_UCT;
				} else if (!strcasecmp(optarg, "distributed")) {
					engine = E_DISTRIBUTED;
				} else if (!strcasecmp(optarg, "patternscan")) {
					engine = E_PATTERNSCAN;
				} else if (!strcasecmp(optarg, "patternplay")) {
					engine = E_PATTERNPLAY;
				} else if (!strcasecmp(optarg, "joseki")) {
					engine = E_JOSEKI;
				} else {
					fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
					exit(1);
				}
				break;
			case 'd':
				debug_level = atoi(optarg);
				break;
			case 'D':
				debug_boardprint = false;
				break;
			case 'f':
				fbookfile = strdup(optarg);
				break;
			case 'g':
				gtp_port = strdup(optarg);
				break;
			case 'l':
				log_port = strdup(optarg);
				break;
			case 'r':
				ruleset = strdup(optarg);
				break;
			case 's':
				seed = atoi(optarg);
				break;
			case 't':
				/* Time settings to follow; if specified,
				 * GTP time information is ignored. Useful
				 * e.g. when you want to force your bot to
				 * play weaker while giving the opponent
				 * reasonable time to play, or force play
				 * by number of simulations in timed games. */
				/* Please see timeinfo.h:time_parse()
				 * description for syntax details. */
				if (!time_parse(&ti_default, optarg)) {
					fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
					exit(1);
				}
				ti_default.ignore_gtp = true;
				assert(ti_default.period != TT_NULL);
				break;
			case 'u':
				testfile = strdup(optarg);
				break;
			default: /* '?' */
				usage(argv[0]);
				exit(1);
		}
	}

	if (log_port)
		open_log_port(log_port);

	fast_srandom(seed);
	if (DEBUGL(0))
		fprintf(stderr, "Random seed: %d\n", seed);

	struct board *b = board_init(fbookfile);
	if (ruleset) {
		if (!board_set_rules(b, ruleset)) {
			fprintf(stderr, "Unknown ruleset: %s\n", ruleset);
			exit(1);
		}
	}

	struct time_info ti[S_MAX];
	ti[S_BLACK] = ti_default;
	ti[S_WHITE] = ti_default;

	chat_init(chatfile);

	char *e_arg = NULL;
	if (optind < argc)
		e_arg = argv[optind];
	struct engine *e = init_engine(engine, e_arg, b);

	if (testfile) {
		unittest(testfile);
		return 0;
	}

	if (gtp_port) {
		open_gtp_connection(&gtp_sock, gtp_port);
	}

	for (;;) {
		char buf[4096];
		while (fgets(buf, 4096, stdin)) {
			if (DEBUGL(1))
				fprintf(stderr, "IN: %s", buf);

			enum parse_code c = gtp_parse(b, e, ti, buf);
			if (c == P_ENGINE_RESET) {
				ti[S_BLACK] = ti_default;
				ti[S_WHITE] = ti_default;
				if (!e->keep_on_clear) {
					b->es = NULL;
					done_engine(e);
					e = init_engine(engine, e_arg, b);
				}
			} else if (c == P_UNKNOWN_COMMAND && gtp_port) {
				/* The gtp command is a weak identity check,
				 * close the connection with a wrong peer. */
				break;
			}
		}
		if (!gtp_port) break;
		open_gtp_connection(&gtp_sock, gtp_port);
	}
	done_engine(e);
	chat_done();
	free(testfile);
	free(gtp_port);
	free(log_port);
	free(chatfile);
	free(fbookfile);
	free(ruleset);
	return 0;
}
Пример #2
0
	/*! Find a session time field by tag from an xml entity.
	  \param from the xml entity to search
	  \param tag the tag to find
	  \param timeonly if true, only use the time part
	  \return Tickval::ticks time or errorticks if not found */
	Tickval::ticks get_time_field(const XmlElement *from, const std::string& tag, bool timeonly=false) const
	{
		std::string time_str;
		return from && from->GetAttr(tag, time_str) && time_str.size() == 8 ? time_parse(time_str.c_str(), 8, timeonly)
																								  : Tickval::errorticks;
	}
Пример #3
0
unsigned long pi_rrd_handler_rrdcreate (plugin_user_t * user, buffer_t * output, void *dummy,
					unsigned int argc, unsigned char **argv)
{
  rrd_ctxt_t *rrd = NULL;
  unsigned long period, i;
  unsigned char *ds = NULL, *s, *e;


  if (argc < 4) {
    bf_printf (output, _("Usage: %s <name> <filename> <period>\n"
			 "          [DS:ds-name:DST:dst arguments]\n"
			 "          [RRA:CF:cf arguments]\n"
			 " <name>: name of the rrd entry\n"
			 " <filename>: filename to save data in\n"
			 " <period>: how often to sample data (suggestion: 300s)\n"
			 "  for the other arguments please consult the rrdtool manual\n"), argv[0]);
    return 0;
  }

  rrd = pi_rrd_find (argv[1]);
  if (rrd) {
    bf_printf (output, _("RRD %s already exists."), argv[1]);
    return 0;
  }

  period = time_parse (argv[3]);
  if (!period) {
    bf_printf (output, _("%s: %s is not a valid time string\n"), argv[0], argv[3]);
    return 0;
  }

  rrd = pi_rrd_create (argv[1], argv[2], period);
  if (!rrd)
    return 0;

  for (i = 4; i < argc; i++) {
    if (!strncmp (argv[i], "DS", 2)) {
      ds = strdup (argv[i]);
      if (!ds)
	goto error;
      /* extract stat name */
      s = strchr (ds, ':');
      e = NULL;
      if (s)
	e = strchr (s + 1, ':');
      if (!s || !e) {
	bf_printf (output, _("DS entry has bad format: %s\n"), ds);
	free (ds);
	goto error;
      }
      s++;
      *e = 0;

      /* replace all . with _ */
      e = strchr (argv[i], ':');
      for (e++; *e != ':'; e++)
	if (*e == '.')
	  *e = '_';

      /* create DS */
      pi_rrd_datapoint_create (output, &rrd->points, s, argv[i]);
      free (ds);
      rrd->npoints++;
    } else if (!strncmp (argv[i], "RRA", 3)) {
      pi_rrd_datapoint_create (output, &rrd->rras, NULL, argv[i]);
      rrd->nrras++;
    } else
      continue;

  }

  if (pi_rrd_start (rrd)) {
    bf_printf (output, _("RRD create FAILED: %s\n"), rrd_get_error ());
    goto error;
  }

  bf_printf (output, _("RRD %s created.\n"), rrd->name);

  return 0;

error:
  if (rrd)
    pi_rrd_delete (rrd);

  return 0;
}