cloud-control-panel/eventChannel.go

205 lines
5.7 KiB
Go
Raw Normal View History

package main
import "fmt"
import "log"
import "net"
import "os"
import "io"
import "bytes"
import "strconv"
import "image/color"
import "runtime"
import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf"
import "git.wit.com/wit/gui"
var mychannel chan *pb.Event
var currentMessage *pb.Event
var windowsTextVeryLightGrey = color.RGBA{10, 10, 10, 10}
var windowsTextBlack = color.RGBA{200, 200, 200, 200}
var windowsTextWhite = color.RGBA{255, 255, 255, 255}
var windowsTextYellow = color.RGBA{200, 200, 0, 200}
var windowsTextRed = color.RGBA{200, 0, 0, 200}
var windowsTextBlue = color.RGBA{0, 100, 200, 100}
var windowsBgGrey = color.RGBA{100, 100, 100, 142}
var windowsBgBlack = color.RGBA{55, 55, 55, 255}
var linuxTextBlue = color.RGBA{0, 100, 200, 100}
var linuxTextBlack = color.RGBA{0, 0, 0, 0}
var linuxTextYellow = color.RGBA{200, 200, 0, 200}
var linuxBgMauve = color.RGBA{72, 0, 0, 142}
var linuxBgBlack = color.RGBA{55, 55, 55, 255}
var linuxBgWhite = color.RGBA{0, 0, 0, 0}
func initChannel() {
mychannel = make(chan *pb.Event)
}
// func processEvents(mychannel chan *pb.Event) {
func processEvents() {
for {
currentMessage = <-mychannel
log.Println("processEvents() START on channel recieved a message = ", currentMessage)
if (currentMessage.Type == pb.Event_DEMO) {
log.Println("processEvents() do Event DEMO")
// figure out how to interact with the GUI here
// go ui.Main(gui.SetupDemoUI)
} else {
gui.Data.State = "bmath done"
/*
count := countVMS(message)
mh := addVmsTab(count)
ReadReceivedData(message, mh)
*/
}
log.Println("processEvents() END on channel")
}
}
func countVMS(data *pb.Event) int {
vms := data.GetVms()
log.Println("NUMBER OF vms=", len(vms))
// os.Exit(-1)
return len(vms)
}
func ReadReceivedData(data *pb.Event, mh *gui.TableData) {
msgItems := data.GetResults()
log.Println("ReadReceivedData() Event msgItems=", msgItems)
for _, item := range msgItems {
log.Println(item)
}
nets := data.GetNetworks()
log.Println("ReadReceivedData() Event networks=", nets)
for _, item := range nets {
log.Println(item)
}
vms := data.GetVms()
log.Println("ReadReceivedData() Event vms=", vms)
for row, item := range vms {
defaultTextColor := linuxTextBlue
defaultTextYellow := linuxTextYellow
defaultTextBlack := linuxTextBlack
if runtime.GOOS == "windows" {
defaultTextColor = windowsTextRed
defaultTextBlack = windowsTextBlack
}
// if (row % 2) == 1 {
if (row == 1) {
mh.Rows[row].HumanData[0].Color = color.RGBA{100, 100, 100, 142} // makes the background of each other row grey
} else if (row == 2) {
mh.Rows[row].HumanData[0].Color = color.RGBA{0, 0, 0, 0} // makes the background of each other row grey
} else if (row == 3) {
mh.Rows[row].HumanData[0].Color = color.RGBA{255, 255, 255, 255} // makes the background of each other row grey
} else if (row == 4) {
mh.Rows[row].HumanData[0].Color = color.RGBA{255, 255, 255, 0} // makes the background of each other row grey
} else if (row == 5) {
mh.Rows[row].HumanData[0].Color = color.RGBA{55, 55, 55, 255} // makes the background of each other row grey
} else {
mh.Rows[row].HumanData[0].Color = linuxBgMauve
}
mh.Rows[row].HumanData[1].Text = item.Name
mh.Rows[row].HumanData[1].Color = defaultTextColor
mh.Rows[row].HumanData[2].Text = item.Hostname
mh.Rows[row].HumanData[2].Color = defaultTextYellow
mh.Rows[row].HumanData[3].Text = fmt.Sprintf("%d",item.Cpus)
mh.Rows[row].HumanData[3].Color = defaultTextColor
mh.Rows[row].HumanData[4].Text = fmt.Sprintf("%d",item.Memory)
mh.Rows[row].HumanData[4].Color = defaultTextBlack
mh.Rows[row].HumanData[5].Text = fmt.Sprintf("%d",item.Disk)
mh.Rows[row].HumanData[5].Color = defaultTextBlack
mh.Rows[row].HumanData[6].Text = fmt.Sprintf("Details %s",item.Name)
log.Println("\titem=", item)
log.Println("\titem.Name=", item.Name)
log.Println("\tRows[row]=", mh.Rows[row])
log.Println("\tRows[row]=", row)
log.Println("\tRows[row].Name=", mh.Rows[row].Name)
log.Println(item)
row += 1
}
/*
log.Println("ReadReceivedData() mh rows")
for row := 0; row < mh.RowCount; row++ {
log.Println("\trow=", row)
log.Println("\tRows[row]=", mh.Rows[row])
}
*/
}
func addSampleEvent() {
msg := pb.CreateSampleEvent()
msg.Name = "generated in addSampleEvent()"
addEvent(msg)
}
// Marshal & Unmarshal just to see if it ever fails
func addEvent(msg *pb.Event) {
data, err := proto.Marshal(msg)
if (err != nil) {
log.Printf("addEvent() something fucked up happened in Marshal")
}
pdata := new(pb.Event)
err = proto.Unmarshal(data, pdata)
if (err != nil) {
log.Printf("addEvent() something fucked up happened in Unmarshal")
}
mychannel <- pdata
}
func sendDataToDest() {
msg := pb.CreateSampleEvent()
msg.Name = "from dnssecsockettest()"
data, err := proto.Marshal(msg)
if (err != nil) {
log.Printf("something fucked up happened")
}
conn, err := net.Dial("tcp", "localhost:8080")
if err != nil {
log.Println("Fatal error: %s", err.Error())
return
}
n, err := conn.Write(data)
if err != nil {
// fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
log.Println("Fatal error: %s", err.Error())
return
}
pb.DumpEventData(msg)
log.Println("Sent " + strconv.Itoa(n) + " bytes")
// time.Sleep(3 * 1000 * 1000 * 1000)
conn.Close()
}
func handleProtoClient(conn net.Conn) {
fmt.Println("Connected!")
defer conn.Close()
var buf bytes.Buffer
_, err := io.Copy(&buf, conn)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
return
}
pdata := pb.CreateSampleEvent()
err = proto.Unmarshal(buf.Bytes(), pdata)
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
return
}
mychannel <- pdata
}