From 8e2f4e3db81530df1ea3fde92b858a9b7aa6b219 Mon Sep 17 00:00:00 2001 From: Mike Holloway Date: Sat, 26 Jun 2021 17:07:48 -0400 Subject: [PATCH] On branch timer Changes to be committed: modified: main.go Adding use of time package directly modified: timer/timer.go Simplifying this away. --- main.go | 21 ++++++++++++++++++--- timer/timer.go | 28 +++++++++++++++++++++------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 22b714e..1d67f1b 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,8 @@ package main import ( -"./timer" +//"./timer" +"time" "fmt" "strings" "regexp" @@ -10,6 +11,8 @@ import ( ) +var tcp_data string = read_tcp_file("/proc/net/tcp") + func main() { // How to split on space-delimited fields, then colon-delimited fields? // How to turn this byte into a string? @@ -18,8 +21,20 @@ func main() { // and then converted byte by byte (2 char at a time) //fmt.Println(convert_address(reverse_string(reverse_bytes("0100007F")))) - timer.New() - tcp_rows(read_tcp_file("/proc/net/tcp")) +// tensec := timer.New(10, "Second") +// onemin := timer.New(1, "Minute") +// tensec.Reset( + tensec := time.NewTimer(10 * time.Second) + onemin := time.NewTimer(1 * time.Minute) +// if !tensec.Stop() { +// fmt.Println("Done waiting 10 seconds!") +// } + fmt.Println("Waiting 10 seconds...") + <-tensec.C + tcp_rows(tcp_data) + fmt.Println("Waiting 1 minute...") + <-onemin.C + tcp_rows(tcp_data) } func check(e error) { diff --git a/timer/timer.go b/timer/timer.go index fa1390a..6c4c677 100644 --- a/timer/timer.go +++ b/timer/timer.go @@ -6,16 +6,30 @@ import ( //"reflect" ) -func New() { +func New(duration_int int, unit string) *time.Timer { - timer_10s := time.NewTimer(10 * time.Second) + start := time.NewTimer(0) + duration := time.Duration(duration_int) +// fmt.Println(reflect.TypeOf(duration)) +// timer_10s := time.NewTimer(10 * time.Second) // timer_1m := time.NewTimer(1 * time.Minute) - fmt.Println("Waiting 10 seconds...") - <-timer_10s.C - - - + fmt.Println("Duration: ", duration_int, "Unit: ", unit, ".") + if unit == "Second" { + start = time.NewTimer(duration * time.Second) + } else if unit == "Minute" { + start = time.NewTimer(duration * time.Minute) + } else { + fmt.Println("Unit not recognized.") + } + fmt.Println("Waiting ", duration_int, " ", unit, ".") + if start.Stop() { + <-start.C + fmt.Println("Done waiting ", duration_int, " ", unit, "!") + } + return start // for elapsed < ten_seconds { //} // for } + +func Reset