main.cpp

00001
00002 /***************************************************************************
00003  *  main.cpp - Fawkes RefBox Repeater
00004  *
00005  *  Created: Wed Apr 09 09:46:29 2008
00006  *  Copyright  2006-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022
00023 #include <utils/system/argparser.h>
00024
00025 #include "refbox_state_sender.h"
00026 #include "refbox_state_writer.h"
00027 #include "msl2007.h"
00028 #include "msl2008.h"
00029 #include "spl.h"
00030
00031 #include <vector>
00032 #include <string>
00033 #include <cstdlib>
00034 #include <cstdio>
00035 #include <cstring>
00036
00037 using namespace fawkes;
00038
00039 void
00040 print_usage(const char *program_name)
00041 {
00042   printf("Usage: %s [-d] -l league -t team -g goal_color [hosts]\n"
00043          "  -d             Turn on debug mode (prints to stdout)\n"
00044          "  -b             Use blackboard writer instead of world info sender\n"
00045          "  -l league      Define league, may be one of\n"
00046          "                 midsize, msl2007, msl2008, spl\n"
00047          "  -t team        Our team, either cyan or magenta\n"
00048          "  -g goal_color  Our goal color, either blue or yellow\n"
00049          "  -p port        UDP port to send to (default 2806)\n"
00050          "  -m addr        Multicast address to send to (default 224.16.0.1)\n"
00051          "  -k key         Encryption key (default AllemaniACs)\n"
00052          "  -i iv          Encryption initialization vector (default AllemaniACs)\n"
00053          " hosts           The hosts of the robots; only when -b is used\n",
00054          program_name);
00055 }
00056 
00057 /** Config tool main.
00058  * @param argc argument count
00059  * @param argv arguments
00060  */
00061 int
00062 main(int argc, char **argv)
00063 {
00064   ArgumentParser argp(argc, argv, "hdbl:t:g:p:m:k:i:");
00065
00066   if ( argp.has_arg("h") ) {
00067     print_usage(argv[0]);
00068     exit(0);
00069   }
00070
00071   if ( ! argp.has_arg("l") ) {
00072     printf("You must give a league name.\n\n");
00073     print_usage(argv[0]);
00074     exit(1);
00075   }
00076
00077   if ( ! argp.has_arg("t") ) {
00078     printf("You must give our team color.\n\n");
00079     print_usage(argv[0]);
00080     exit(2);
00081   }
00082
00083   if ( ! argp.has_arg("g") ) {
00084     printf("You must give our goal color.\n\n");
00085     print_usage(argv[0]);
00086     exit(3);
00087   }
00088
00089   worldinfo_gamestate_team_t our_team;
00090   worldinfo_gamestate_goalcolor_t our_goal;
00091   const char *addr = "224.16.0.1";
00092   const char *key  = "AllemaniACs";
00093   const char *iv   = "AllemaniACs";
00094   unsigned short int port = 2806;
00095
00096   if ( strcmp(argp.arg("t"), "cyan") == 0 ) {
00097     our_team = TEAM_CYAN;
00098   } else if ( strcmp(argp.arg("t"), "magenta") == 0 ) {
00099     our_team = TEAM_MAGENTA;
00100   } else {
00101     printf("Invalid team '%s', must be one of 'cyan' and 'magenta'.\n\n", argp.arg("t"));
00102     print_usage(argv[0]);
00103     exit(4);
00104   }
00105
00106   if ( strcmp(argp.arg("g"), "blue") == 0 ) {
00107     our_goal = GOAL_BLUE;
00108   } else if ( strcmp(argp.arg("g"), "yellow") == 0 ) {
00109     our_goal = GOAL_YELLOW;
00110   } else {
00111     printf("Invalid goal '%s', must be one of 'blue' and 'yellow'.\n\n", argp.arg("g"));
00112     print_usage(argv[0]);
00113     exit(5);
00114   }
00115
00116   if ( argp.has_arg("m") ) {
00117     addr = argp.arg("m");
00118   }
00119
00120   if ( argp.has_arg("k") ) {
00121     key = argp.arg("k");
00122   }
00123
00124   if ( argp.has_arg("i") ) {
00125     iv = argp.arg("i");
00126   }
00127
00128   if ( argp.has_arg("p") ) {
00129     port = atoi(argp.arg("p"));
00130   }
00131
00132   printf("Sending to: %s:%u\n"
00133          "Key: %s  IV: %s\n", addr, port, key, iv);
00134
00135   RefBoxStateSender *rss;
00136   if ( argp.has_arg("b") ) {
00137     std::vector<const char*> items = argp.items();
00138     std::vector<std::string> hosts(items.begin(), items.end());
00139     rss = new RefBoxStateBBWriter(hosts, argp.has_arg("d"));
00140   } else {
00141     rss = new RefBoxStateSender(addr, port, key, iv, argp.has_arg("d"));
00142   }
00143   rss->set_team_goal(our_team, our_goal);
00144
00145   printf("League: %s\n", argp.arg("l"));
00146   if ( strcmp(argp.arg("l"), "msl2007") == 0 || strcmp(argp.arg("l"), "midsize") == 0 ) {
00147     MidsizeRefBoxRepeater mrr(*rss, "127.0.0.1", 28097);
00148     mrr.run();
00149   } else if ( strcmp(argp.arg("l"), "msl2008") == 0 ) {
00150     Msl2008RefBoxRepeater m8rr(*rss, "230.0.0.1", 30000);
00151     m8rr.run();
00152   } else if ( strcmp(argp.arg("l"), "spl") == 0 ) {
00153     SplRefBoxRepeater nrr(*rss, "255.255.255.0", 3838, our_team, our_goal);
00154     nrr.run();
00155   } else {
00156     printf("Invalid league name given.\n\n");
00157     print_usage(argv[0]);
00158     exit(2);
00159   }
00160
00161   return 0;
00162 }