cloud-control-panel/socketWIT.go

108 lines
2.4 KiB
Go

package main
import "log"
import "os"
import "time"
import "net"
import "fmt"
import "bufio"
// import "strings"
import "git.wit.com/wit/shell"
import "git.wit.com/jcarr/dnssecsocket"
// import "github.com/svent/go-nbreader"
// import "github.com/gookit/config"
// import "github.com/davecgh/go-spew/spew"
var socketAlive bool = false
var connCurrent *net.TCPConn
const (
CONN_HOST = "v000185.testing.com.customers.wprod.wit.com"
CONN_PORT = "3333"
CONN_PROTO = "tcp"
)
func writelnToSocket(aline string) {
if connCurrent == nil {
log.Println("SOCKET IS NOT CONNECTED")
return
}
// a, err := connCurrent.Write([]byte(string(text + "\n")))
a, err := connCurrent.Write([]byte(string(aline + "\n")))
if err != nil {
log.Println("SOCKET WRITE FAILED with err=", err, a)
connCurrent = nil
return
}
}
func writeBytesToSocket(data []byte) {
if (connCurrent == nil) {
log.Println("WIT Socket is currently down (connCurrent == nil)")
return
}
a, err := connCurrent.Write(data)
log.Println("WIT Socket Write() returned:", a, err)
}
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() ?")
connCurrent = nil
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)
if (err != nil) {
fmt.Println("SOCKET DIED with err=", err)
connCurrent = nil
return
}
connStdin, err := conn.File()
if (err != nil) {
fmt.Println("SOCKET File() failed with err=", err)
connCurrent = nil
return
}
// set the socketAlive flag to true since the socket appears to be alive
socketAlive = true
connCurrent = conn
newreader := bufio.NewReader(connStdin)
shell.NonBlockingReader(newreader, os.Stdout)
}
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")
}
}