tools/dtnd-control
changeset 0 2b3e5ec03512
child 29 00dfdf113d87
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 #!/usr/bin/tclsh
       
     2 
       
     3 set port 5050
       
     4 
       
     5 proc usage {} {
       
     6     puts stderr "dtnd-control \[-port port\] stop|check|logrotate|bundle_stats|daemon_stats"
       
     7     puts "|reset_stats|route_dump|link_dump|bundle_list|registration_list|gettimeofday"
       
     8     puts "|\[-id id]\ bundle_info"
       
     9     puts "|\[-id id]\ bundle_del"
       
    10     puts "|\[-id id]\ bundle_dump"
       
    11     puts "|\[-id id]\ bundle_expire"
       
    12     exit 1
       
    13 }
       
    14 
       
    15 proc shift {listVar} {
       
    16     upvar $listVar l
       
    17     
       
    18     set ret [lindex $l 0]
       
    19     set l [lrange $l 1 end]
       
    20     return $ret
       
    21 }
       
    22 
       
    23 after 30000 {exit 1}
       
    24 
       
    25 while {[llength $argv] > 1} {
       
    26     set arg [shift argv]
       
    27 
       
    28     if {[string index $arg 0] != "-"} {
       
    29 	break
       
    30     }
       
    31 	
       
    32     switch -- $arg {
       
    33 	-port   { set port [shift argv] } -id { set id [shift argv] }
       
    34 	default {
       
    35 	    puts stderr "unknown argument $arg"
       
    36 	    usage
       
    37 	}
       
    38     }
       
    39 
       
    40 }
       
    41 
       
    42 set operation [shift argv]
       
    43 
       
    44 switch -- $operation {
       
    45     stop - check - status - logrotate - daemon_stats - reset_stats - route_dump - link_dump - bundle_stats - bundle_list - bundle_info - bundle_del - bundle_expire - bundle_dump - registration_list - gettimeofday {}
       
    46     default {
       
    47 	puts "unknown operation $operation"
       
    48 	usage
       
    49     }
       
    50 }
       
    51 
       
    52 if {[llength $argv] != 0} {
       
    53     puts "extra arguments after operation"
       
    54     usage
       
    55 }
       
    56 
       
    57 if [catch {
       
    58     set sock [socket localhost $port]
       
    59 
       
    60 } err] {
       
    61     if {$operation == "stop"} {
       
    62         puts "(already stopped)"
       
    63         exit 0
       
    64     }
       
    65     puts stderr "dtnd-control: cannot connect to localhost port $port"
       
    66     exit 1
       
    67 }
       
    68 
       
    69 puts $sock "tell_encode"
       
    70 flush $sock
       
    71 set ret1 [gets $sock]; # the prompt
       
    72 set ret2 [gets $sock]; # the command response
       
    73 
       
    74 if {$operation == "check" || $operation == "status"} {
       
    75     set cmd "bundle daemon_status"
       
    76     
       
    77 } elseif {$operation == "stats" || $operation == "bundle_stats"} {
       
    78     set cmd "bundle stats"
       
    79 
       
    80 } elseif {$operation == "daemon_stats"} {
       
    81     set cmd "bundle daemon_stats"
       
    82 
       
    83 } elseif {$operation == "reset_stats"} {
       
    84     set cmd "bundle reset_stats"
       
    85 
       
    86 } elseif {$operation == "route_dump"} {
       
    87     set cmd "route dump"
       
    88 
       
    89 } elseif {$operation == "link_dump"} {
       
    90     set cmd "link dump"
       
    91 
       
    92 } elseif {$operation == "bundle_list"} {
       
    93     set cmd "bundle list"
       
    94 
       
    95 } elseif {$operation == "bundle_info"} {
       
    96     set cmd "bundle info $id"
       
    97 
       
    98 } elseif {$operation == "bundle_del"} {
       
    99     set cmd "bundle del $id"
       
   100 
       
   101 } elseif {$operation == "bundle_expire"} {
       
   102     set cmd "bundle expire $id"
       
   103 
       
   104 } elseif {$operation == "bundle_dump"} {
       
   105     set cmd "bundle dump $id"
       
   106 
       
   107 } elseif {$operation == "registration_list"} {
       
   108     set cmd "registration list"
       
   109 
       
   110 } elseif {$operation == "gettimeofday"} {
       
   111     set cmd "gettimeofday"
       
   112 
       
   113 
       
   114 } elseif {$operation == "stop"} {
       
   115     set cmd "shutdown"
       
   116     
       
   117 } elseif {$operation == "logrotate"} {
       
   118     set cmd "log rotate"
       
   119 
       
   120 } else {
       
   121     error "bogus op $operation"
       
   122 }
       
   123 
       
   124 puts $sock $cmd
       
   125 flush $sock
       
   126 
       
   127 set cmd_response [gets $sock]
       
   128 if {($cmd_response == "") || [eof $sock]} {
       
   129     puts stderr "error getting response"
       
   130     exit 1
       
   131 }
       
   132 
       
   133 set cmd_error [string index $cmd_response 0]
       
   134 set result    [string range $cmd_response 2 end]
       
   135 regsub -all -- {\\n} $result "\n" result
       
   136 
       
   137 if {$cmd_error == 0} {
       
   138     puts $result
       
   139 
       
   140     # if the daemon is stopping, wait until the socket closes before
       
   141     # exiting the script so we know it's actually dead
       
   142     if {$operation == "stop"} {
       
   143         catch {gets $sock}
       
   144         after 1000
       
   145     }
       
   146     
       
   147     exit 0
       
   148 } else {
       
   149     puts "error running command '$cmd':"
       
   150     puts $result
       
   151     exit 1
       
   152 }
       
   153 
       
   154 exit 0