From 5eae7b3019b58fd8a99c4d9811060fec0bc8df14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Pi=C4=85tkowski?= Date: Tue, 29 Nov 2022 21:15:39 +0100 Subject: [PATCH] scanner fix, minor changes --- lib/midi | 1 + server/src/scaner.go | 10 +++++++--- server/src/server.go | 38 ++++++++++++++++++++++++++++++++++++-- server/src/timesync.go | 7 ------- 4 files changed, 44 insertions(+), 12 deletions(-) create mode 160000 lib/midi diff --git a/lib/midi b/lib/midi new file mode 160000 index 0000000..f42b663 --- /dev/null +++ b/lib/midi @@ -0,0 +1 @@ +Subproject commit f42b663f0d08fc629c7deb26cd32ee06fba76d83 diff --git a/server/src/scaner.go b/server/src/scaner.go index d80766c..15bb013 100644 --- a/server/src/scaner.go +++ b/server/src/scaner.go @@ -6,8 +6,8 @@ import ( "time" ) -func scan() string { - information := "" +func scan() []string { + var information []string ifaces, _ := net.Interfaces() for _, i := range ifaces { addrs, _ := i.Addrs() @@ -19,11 +19,15 @@ func scan() string { ipv4[3]++ for i := 1; i < 256; i++ { _, dialErr := net.DialTimeout("tcp", ipv4.String()+":8081", time.Duration(1)*time.Second) + // _, dialErr := net.DialTimeout("tcp", "192.168.0.100:8081", time.Duration(1)*time.Second) if dialErr != nil { fmt.Println("Cannot connect to " + ipv4.String()) + // fmt.Println("Cannot connect to " + "192.168.0.100:8081") } else { fmt.Println("Response from " + ipv4.String()) - information = fmt.Sprintf("%s\n%s", information, ipv4) + // fmt.Println("Response from " + "192.168.0.100:8081") + information = append(information, ipv4.String()) + // information = append(information, "192.168.0.100:8081") } ipv4[3]++ } diff --git a/server/src/server.go b/server/src/server.go index fb2d9a5..880488f 100644 --- a/server/src/server.go +++ b/server/src/server.go @@ -2,11 +2,18 @@ package main import ( "bufio" + "fmt" "log" "net" "os" ) +func scanError(scanResult []string, conn net.Conn) { + if scanResult == nil { + conn.Write([]byte("Empty scan result, run 'scan' first.\n")) + } +} + func main() { // HTTP part @@ -31,18 +38,45 @@ func main() { go func(c net.Conn) { s := bufio.NewScanner(c) conn.Write([]byte("> ")) + var scanResult []string for s.Scan() { resp := s.Text() if resp == "scan" { conn.Write([]byte("Scanning...\n")) - conn.Write([]byte(scan() + "\n")) + scanResult = scan() + conn.Write([]byte("Scanning done!\n")) + conn.Write([]byte("> ")) + fmt.Println(len(scanResult)) continue } if resp == "time" { conn.Write([]byte(showTime().String() + "\n")) + conn.Write([]byte("> ")) + continue + } + if resp == "hosts" { + scanError(scanResult, conn) + for _, host := range scanResult { + conn.Write([]byte(host + "\n")) + + } + conn.Write([]byte("> ")) + continue } if resp == "timesync" { - conn.Write([]byte(showMonoTime().String() + "\n")) + scanError(scanResult, conn) + for _, host := range scanResult { + if host == "" { + fmt.Print("No host") + } + fmt.Println(host) + //conn.Write([]byte(host + "\n")) + _, err := net.Dial("tcp", host) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + } } if resp == "quit" { c.Close() diff --git a/server/src/timesync.go b/server/src/timesync.go index 0db5500..ef6ad3d 100644 --- a/server/src/timesync.go +++ b/server/src/timesync.go @@ -8,10 +8,3 @@ func showTime() time.Time { time := time.Now() return time } - -func showMonoTime() time.Duration { - start := time.Now() - t := time.Now() - elapsed := t.Sub(start) - return elapsed -}