apps/tca_admin/tca_admin.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2005-2006 University of Waterloo
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifdef HAVE_CONFIG_H
       
    18 #  include <dtn-config.h>
       
    19 #endif
       
    20 
       
    21 #include <stdio.h>
       
    22 #include <unistd.h>
       
    23 #include <errno.h>
       
    24 #include <strings.h>
       
    25 #include <stdlib.h>
       
    26 #include <sys/time.h>
       
    27 
       
    28 #include <string>
       
    29 #include <oasys/debug/Log.h>
       
    30 
       
    31 #include "dtn_api.h"
       
    32 #include "TcaController.h"
       
    33 
       
    34 static const int debug = 1;
       
    35 
       
    36 static const int MAX_TTL = 604800;      // 604800 seconds == 1 week
       
    37 
       
    38 // parsed from command line args
       
    39 static const char* progname;
       
    40 static std::string node_type = "mobile";
       
    41 static bool tidy = false;           // discard pending bundles on startup
       
    42 static std::string link_id;         // published link id of node
       
    43 static std::string ask_addr;        // clayer address to send ask to
       
    44 static std::string adv_string;      // published adv string
       
    45 static int registry_ttl = MAX_TTL;  // registry entry ttl
       
    46 static int control_ttl = MAX_TTL;   // control bundle ttl
       
    47 
       
    48 
       
    49 static TcaController::Role  role = TcaController::TCA_MOBILE;
       
    50 
       
    51 
       
    52 void
       
    53 print_usage()
       
    54 {
       
    55     fprintf(stderr, "usage: %s [opts]\n"
       
    56             "options:\n"
       
    57             " -h help\n"
       
    58             " -n <node_type: mobile | router | gateway> (default = mobile)\n"
       
    59             " -l <link_addr> local contact addr (required for gateway only)\n"
       
    60             " -a <link_addr> send ask on startup\n"
       
    61             " -d <adv_string> string to send in response to ASK\n"
       
    62             " -r <time in seconds> TTL for Registry entries (default = 604800)\n"
       
    63             " -e <time in seconds> control bundle expiration time (default = 604800)\n"
       
    64             " -t tidy (discard pending bundles on startup)\n",
       
    65            progname);
       
    66     fprintf(stderr, "usage: %s [-h] -t <node_type: mobile | router |"
       
    67             " gateway>\n", progname);
       
    68     exit(1);
       
    69 }
       
    70 
       
    71 
       
    72 void
       
    73 parse_options(int argc, const char **argv)
       
    74 {
       
    75     bool done = false;
       
    76     int c;
       
    77 
       
    78     progname = argv[0];
       
    79 
       
    80     while (!done)
       
    81     {
       
    82         c = getopt(argc, (char **) argv, "htn:l:a:d:r:e:");
       
    83 
       
    84         switch (c)
       
    85         {
       
    86         case 'h':
       
    87             print_usage();
       
    88             exit(0);
       
    89             break;
       
    90         case 't':
       
    91             tidy = true;
       
    92             break;
       
    93         case 'n':
       
    94             {
       
    95                 node_type = optarg;
       
    96                 if (node_type == "mobile")
       
    97                     role = TcaController::TCA_MOBILE;
       
    98                 else if (node_type == "router")
       
    99                     role = TcaController::TCA_ROUTER;
       
   100                 else if (node_type == "gateway")
       
   101                     role = TcaController::TCA_GATEWAY;
       
   102                 else
       
   103                     fprintf(stderr, "unknown node type '%s'\n",
       
   104                             node_type.c_str());
       
   105             }
       
   106             break;
       
   107         case 'l':
       
   108             link_id = optarg;
       
   109             break;
       
   110         case 'a':
       
   111             ask_addr = optarg;
       
   112             // TODO: some syntax checking would be a nice idea
       
   113             break;
       
   114         case 'd':
       
   115             adv_string = optarg;
       
   116             // TODO: some syntax checking would be a nice idea
       
   117             break;
       
   118         case 'r':
       
   119             {
       
   120                 int n = atoi(optarg);
       
   121                 if (n<=0 || n >MAX_TTL)
       
   122                 {
       
   123                     fprintf(stderr, "registry TTL out of range (1..%d)\n",
       
   124                             MAX_TTL);
       
   125                     registry_ttl = MAX_TTL;
       
   126                 }
       
   127                 else
       
   128                 {
       
   129                     registry_ttl = n;
       
   130                 }
       
   131             }
       
   132             break;
       
   133         case 'e':
       
   134              {
       
   135                 int n = atoi(optarg);
       
   136                 if (n<=0 || n >MAX_TTL)
       
   137                 {
       
   138                     fprintf(stderr, "control bundle TTL out of range (1..%d)\n",
       
   139                             MAX_TTL);
       
   140                     control_ttl = MAX_TTL;
       
   141                 }
       
   142                 else
       
   143                 {
       
   144                     control_ttl = n;
       
   145                 }
       
   146             }
       
   147             break;
       
   148         case -1:
       
   149             done = true;
       
   150             break;
       
   151         default:
       
   152             print_usage();
       
   153             break;
       
   154         }
       
   155     }
       
   156 
       
   157     if (optind < argc)
       
   158     {
       
   159         fprintf(stderr, "unsupported argument '%s'\n", argv[optind]);
       
   160         exit(1);
       
   161     }
       
   162 
       
   163     // echo args
       
   164     printf("using options:\n");
       
   165     printf("    node_type = '%s'\n", node_type.c_str());
       
   166     printf("    link_id = '%s'\n", link_id.c_str());
       
   167     printf("    ask_addr = '%s'\n", ask_addr.c_str());
       
   168     printf("    adv_string = '%s'\n", adv_string.c_str());
       
   169     printf("    registry_ttl = %d\n", registry_ttl);
       
   170     printf("    control_ttl = %d\n", control_ttl);
       
   171     if (tidy) printf("    tidy = true\n");
       
   172     else printf("    tidy = false\n");
       
   173 
       
   174 }
       
   175 
       
   176 
       
   177 // main
       
   178 int
       
   179 main(int argc, const char** argv)
       
   180 {
       
   181     oasys::Log::init("-", oasys::LOG_NOTICE, "", "~/.tcadebug");
       
   182     log_notice_p("/tca/admin", "tca_admin starting up");
       
   183     
       
   184     parse_options(argc, argv);
       
   185 
       
   186     TcaController controller(role, link_id, ask_addr, adv_string,
       
   187                         registry_ttl, control_ttl);
       
   188 
       
   189     if (!controller.init(tidy))
       
   190     {
       
   191         exit(1);
       
   192     }
       
   193 
       
   194     controller.run();
       
   195 
       
   196     return 0;
       
   197 }
       
   198 
       
   199 
       
   200