cloud-control-panel/socketWIT.go

123 lines
2.8 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 {
fmt.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 {
fmt.Println("SOCKET WRITE FAILED with err=", err, a)
connCurrent = nil
return
}
}
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)
go shell.NonBlockingReader(newreader, 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')
text = strings.TrimSpace(text) // this is like perl chomp
log.Println("LINE WAS:", text)
log.Println("LINE WAS:", text)
log.Println("LINE WAS:", text)
if (text == "ADD2") {
add2()
continue
}
// send the command over the socket socket
a, err := conn.Write([]byte(string(text + "\n")))
if (err != nil) {
fmt.Println("SOCKET WRITE FAILED 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")
}
}