package main import "log" import "os" import "time" import "net" import "fmt" import "bufio" import "github.com/svent/go-nbreader" import "git.wit.com/jcarr/dnssecsocket" // import "github.com/gookit/config" // import "git.wit.com/wit/gui" // import "github.com/davecgh/go-spew/spew" // reminder to use this for JSON // https://github.com/tidwall/gjson // value := gjson.Get(json, "name.last") // println(value.String()) // value := gjson.Get(json, friends.#[last=="Murphy"].first) // use mergo to merge structs // import "github.com/imdario/mergo" // mergo.Merge(&dest, src) // always sorted slice (new project) // https://github.com/yaa110/sslice const ( CONN_HOST = "v000185.testing.com.customers.wprod.wit.com" CONN_PORT = "3333" CONN_PROTO = "tcp" ) func socketToWIT(myhostname string) { remoteAddr := CONN_HOST + ":" + CONN_PORT log.Println("remoteAddr:", remoteAddr) localTCPAddr := dnssecsocket.ResolveIPv6hostname(myhostname) remoteTCPAddr, err := net.ResolveTCPAddr("tcp", remoteAddr) // connect to this socket conn, err := net.DialTCP(CONN_PROTO, localTCPAddr, remoteTCPAddr) if err != nil { log.Println(CONN_HOST, CONN_PORT, CONN_PROTO) log.Println(err) log.Println("socket connect failed. Not sure what to do here. os.Exit() ?") return } defer conn.Close() log.Println("socket connected to", CONN_PROTO, CONN_HOST, CONN_PORT) tmp, err := conn.Write([]byte(string(myhostname + "\n"))) log.Println("conn.Write:", tmp, err) connStdin, _ := conn.File() go nonBlockingReader(connStdin, os.Stdout) // read in input from stdin reader := bufio.NewReader(os.Stdin) for { // read in the command to send to our server fmt.Print("Text to send: ") text, _ := reader.ReadString('\n') // send the command over the socket socket a, err := conn.Write([]byte(string(text + "\n"))) // a, err := connStdin.Write([]byte(string(text + "\n"))) if (err != nil) { fmt.Println("SOCKET DIED with err=", err, a) return } } } func retrySocket() { for { log.Println("Sleep for 3 seconds, then try to establish connection to WIT") time.Sleep(3 * 1000 * 1000 * 1000) // for now, loop until I figure out what to actually do socketToWIT("librem15.lab.wit.com") } } // pass in two file handles (1 read, 1 write) func nonBlockingReader(readFileHandle *os.File, writeFileHandle *os.File) { // create a nonblocking GO reader newreader := bufio.NewReader(readFileHandle) nbr := nbreader.NewNBReader(newreader, 1024) for { defer readFileHandle.Close() defer writeFileHandle.Close() totalCount := 0 for { oneByte := make([]byte, 1024) count, err := nbr.Read(oneByte) if (err != nil) { log.Println("count, err =", count, err) return } totalCount += count if (count == 0) { time.Sleep(20 * time.Millisecond) // only check the buffer 500 times a second if (totalCount != 0) { log.Println("totalCount = ", totalCount) totalCount = 0 } } writeFileHandle.Write([]byte(string(oneByte))) } } }