diffapp.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:4k
源码类别:

通讯编程

开发平台:

Visual C++

  1. //
  2. // diffapp.cc     : Base Class for Diffusion Apps and Filters
  3. // author         : Fabio Silva and Padma Haldar
  4. //
  5. // Copyright (C) 2000-2002 by the University of Southern California
  6. // $Id: diffapp.cc,v 1.11 2005/09/13 04:53:49 tomh Exp $
  7. //
  8. // This program is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU General Public License,
  10. // version 2, as published by the Free Software Foundation.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License along
  18. // with this program; if not, write to the Free Software Foundation, Inc.,
  19. // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  20. //
  21. // Linking this file statically or dynamically with other modules is making
  22. // a combined work based on this file.  Thus, the terms and conditions of
  23. // the GNU General Public License cover the whole combination.
  24. //
  25. // In addition, as a special exception, the copyright holders of this file
  26. // give you permission to combine this file with free software programs or
  27. // libraries that are released under the GNU LGPL and with code included in
  28. // the standard release of ns-2 under the Apache 2.0 license or under
  29. // otherwise-compatible licenses with advertising requirements (or modified
  30. // versions of such code, with unchanged license).  You may copy and
  31. // distribute such a system following the terms of the GNU GPL for this
  32. // file and the licenses of the other code concerned, provided that you
  33. // include the source code of that other code when and as the GNU GPL
  34. // requires distribution of source code.
  35. //
  36. // Note that people who make modified versions of this file are not
  37. // obligated to grant this special exception for their modified versions;
  38. // it is their choice whether to do so.  The GNU General Public License
  39. // gives permission to release a modified version without this exception;
  40. // this exception also makes it possible to release a modified version
  41. // which carries forward this exception.
  42. //
  43. #include "diffapp.hh"
  44. #ifdef NS_DIFFUSION
  45. int DiffApp::command(int argc, const char*const* argv) {
  46.   if (argc == 2) {
  47.     if (strcmp(argv[1], "start") == 0) {
  48.       start();
  49.       return TCL_OK;
  50.     }
  51.   }
  52.   if (argc == 3) {
  53.     if (strcmp(argv[1], "dr") == 0) {
  54.       DiffAppAgent *agent;
  55.       agent = (DiffAppAgent *)TclObject::lookup(argv[2]);
  56.       dr_ = agent->dr();
  57.       return TCL_OK;
  58.     }
  59.   }
  60.   return Application::command(argc, argv);
  61. }
  62. #else
  63. void DiffApp::usage(char *s){
  64.   DiffPrint(DEBUG_ALWAYS, "Usage: %s [-d debug] [-p port] [-f file] [-h]nn", s);
  65.   DiffPrint(DEBUG_ALWAYS, "t-d - Sets debug level (0-10)n");
  66.   DiffPrint(DEBUG_ALWAYS, "t-p - Uses port 'port' to talk to diffusionn");
  67.   DiffPrint(DEBUG_ALWAYS, "t-f - Specifies a config filen");
  68.   DiffPrint(DEBUG_ALWAYS, "t-h - Prints this informationn");
  69.   DiffPrint(DEBUG_ALWAYS, "n");
  70.   exit(0);
  71. }
  72. void DiffApp::parseCommandLine(int argc, char **argv)
  73. {
  74.   u_int16_t diff_port = DEFAULT_DIFFUSION_PORT;
  75.   int debug_level;
  76.   int opt;
  77.   config_file_ = NULL;
  78.   opterr = 0;
  79.   while (1){
  80.     opt = getopt(argc, argv, "f:hd:p:");
  81.     switch (opt){
  82.     case 'p':
  83.       diff_port = (u_int16_t) atoi(optarg);
  84.       if ((diff_port < 1024) || (diff_port >= 65535)){
  85. DiffPrint(DEBUG_ALWAYS, "Error: Diffusion port must be between 1024 and 65535 !n");
  86. exit(-1);
  87.       }
  88.       break;
  89.     case 'h':
  90.       usage(argv[0]);
  91.       break;
  92.     case 'd':
  93.       debug_level = atoi(optarg);
  94.       if (debug_level < 1 || debug_level > 10){
  95. DiffPrint(DEBUG_ALWAYS, "Error: Debug level outside range or missing !n");
  96. usage(argv[0]);
  97.       }
  98.       global_debug_level = debug_level;
  99.       break;
  100.     case 'f':
  101.       if (!strncasecmp(optarg, "-", 1)){
  102. DiffPrint(DEBUG_ALWAYS, "Error: Parameter missing !n");
  103. usage(argv[0]);
  104.       }
  105.       config_file_ = strdup(optarg);
  106.       break;
  107.     case '?':
  108.       DiffPrint(DEBUG_ALWAYS,
  109. "Error: %c isn't a valid option or its parameter is missing !n", optopt);
  110.       usage(argv[0]);
  111.       break;
  112.     case ':':
  113.       DiffPrint(DEBUG_ALWAYS, "Parameter missing !n");
  114.       usage(argv[0]);
  115.       break;
  116.     }
  117.     if (opt == -1)
  118.       break;
  119.   }
  120.   diffusion_port_ = diff_port;
  121. }
  122. #endif // NS_DIFFUSION