tca_admin.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * University of Waterloo Open Source License
00008  * 
00009  * Copyright (c) 2005 University of Waterloo. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  *  
00022  *   Neither the name of the University of Waterloo nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00030  * OF WATERLOO OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00031  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00032  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00033  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00034  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00035  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00036  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <stdio.h>
00040 #include <unistd.h>
00041 #include <errno.h>
00042 #include <strings.h>
00043 #include <stdlib.h>
00044 #include <sys/time.h>
00045 
00046 #include <string>
00047 #include <oasys/debug/Log.h>
00048 
00049 #include "dtn_api.h"
00050 #include "TcaController.h"
00051 
00052 static const int debug = 1;
00053 
00054 static const int MAX_TTL = 604800;      // 604800 seconds == 1 week
00055 
00056 // parsed from command line args
00057 static const char* progname;
00058 static std::string node_type = "mobile";
00059 static bool tidy = false;           // discard pending bundles on startup
00060 static std::string link_id;         // published link id of node
00061 static std::string ask_addr;        // clayer address to send ask to
00062 static std::string adv_string;      // published adv string
00063 static int registry_ttl = MAX_TTL;  // registry entry ttl
00064 static int control_ttl = MAX_TTL;   // control bundle ttl
00065 
00066 
00067 static TcaController::Role  role = TcaController::TCA_MOBILE;
00068 
00069 
00070 void
00071 print_usage()
00072 {
00073     fprintf(stderr, "usage: %s [opts]\n"
00074             "options:\n"
00075             " -h help\n"
00076             " -n <node_type: mobile | router | gateway> (default = mobile)\n"
00077             " -l <link_addr> local contact addr (required for gateway only)\n"
00078             " -a <link_addr> send ask on startup\n"
00079             " -d <adv_string> string to send in response to ASK\n"
00080             " -r <time in seconds> TTL for Registry entries (default = 604800)\n"
00081             " -e <time in seconds> control bundle expiration time (default = 604800)\n"
00082             " -t tidy (discard pending bundles on startup)\n",
00083            progname);
00084     fprintf(stderr, "usage: %s [-h] -t <node_type: mobile | router |"
00085             " gateway>\n", progname);
00086     exit(1);
00087 }
00088 
00089 
00090 void
00091 parse_options(int argc, const char **argv)
00092 {
00093     bool done = false;
00094     int c;
00095 
00096     progname = argv[0];
00097 
00098     while (!done)
00099     {
00100         c = getopt(argc, (char **) argv, "htn:l:a:d:r:e:");
00101 
00102         switch (c)
00103         {
00104         case 'h':
00105             print_usage();
00106             exit(0);
00107             break;
00108         case 't':
00109             tidy = true;
00110             break;
00111         case 'n':
00112             {
00113                 node_type = optarg;
00114                 if (node_type == "mobile")
00115                     role = TcaController::TCA_MOBILE;
00116                 else if (node_type == "router")
00117                     role = TcaController::TCA_ROUTER;
00118                 else if (node_type == "gateway")
00119                     role = TcaController::TCA_GATEWAY;
00120                 else
00121                     fprintf(stderr, "unknown node type '%s'\n",
00122                             node_type.c_str());
00123             }
00124             break;
00125         case 'l':
00126             link_id = optarg;
00127             break;
00128         case 'a':
00129             ask_addr = optarg;
00130             // TODO: some syntax checking would be a nice idea
00131             break;
00132         case 'd':
00133             adv_string = optarg;
00134             // TODO: some syntax checking would be a nice idea
00135             break;
00136         case 'r':
00137             {
00138                 int n = atoi(optarg);
00139                 if (n<=0 || n >MAX_TTL)
00140                 {
00141                     fprintf(stderr, "registry TTL out of range (1..%d)\n",
00142                             MAX_TTL);
00143                     registry_ttl = MAX_TTL;
00144                 }
00145                 else
00146                 {
00147                     registry_ttl = n;
00148                 }
00149             }
00150             break;
00151         case 'e':
00152              {
00153                 int n = atoi(optarg);
00154                 if (n<=0 || n >MAX_TTL)
00155                 {
00156                     fprintf(stderr, "control bundle TTL out of range (1..%d)\n",
00157                             MAX_TTL);
00158                     control_ttl = MAX_TTL;
00159                 }
00160                 else
00161                 {
00162                     control_ttl = n;
00163                 }
00164             }
00165             break;
00166         case -1:
00167             done = true;
00168             break;
00169         default:
00170             print_usage();
00171             break;
00172         }
00173     }
00174 
00175     if (optind < argc)
00176     {
00177         fprintf(stderr, "unsupported argument '%s'\n", argv[optind]);
00178         exit(1);
00179     }
00180 
00181     // echo args
00182     printf("using options:\n");
00183     printf("    node_type = '%s'\n", node_type.c_str());
00184     printf("    link_id = '%s'\n", link_id.c_str());
00185     printf("    ask_addr = '%s'\n", ask_addr.c_str());
00186     printf("    adv_string = '%s'\n", adv_string.c_str());
00187     printf("    registry_ttl = %d\n", registry_ttl);
00188     printf("    control_ttl = %d\n", control_ttl);
00189     if (tidy) printf("    tidy = true\n");
00190     else printf("    tidy = false\n");
00191 
00192 }
00193 
00194 
00195 // main
00196 int
00197 main(int argc, const char** argv)
00198 {
00199     oasys::Log::init("-", oasys::LOG_NOTICE, "", "~/.tcadebug");
00200     log_notice("/tca/admin", "tca_admin starting up");
00201     
00202     parse_options(argc, argv);
00203 
00204     TcaController controller(role, link_id, ask_addr, adv_string,
00205                         registry_ttl, control_ttl);
00206 
00207     if (!controller.init(tidy))
00208     {
00209         exit(1);
00210     }
00211 
00212     controller.run();
00213 
00214     return 0;
00215 }
00216 
00217 
00218 

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1