diff --git a/.gitignore b/.gitignore index ed9349b..543ffdb 100644 Binary files a/.gitignore and b/.gitignore differ diff --git a/cmd/Client.go b/cmd/Client.go index 0916c68..5583ea3 100644 --- a/cmd/Client.go +++ b/cmd/Client.go @@ -1,22 +1,49 @@ package cmd // code.bitsetter.de/fun/gosl/cmd import ( + "encoding/gob" "fmt" + "net" + "os" + "strconv" "github.com/spf13/cobra" + + "code.bitsetter.de/fun/gosl/data" ) var cmdClient = &cobra.Command{ - Use: "client", + Use: "client ", Short: "Runs Gosl as a client", Long: "Runs Gosl as a client", - // Run: + Run: runClient, +} + +//func init() { +// cmdClient.Run = runClient +//} + +func register(con net.Conn, id int) error { + w, h := data.TestNC() + enc := gob.NewEncoder(con) // Encoder + err := enc.Encode(handshake{ID: id, H: h, W: w}) + return err } func runClient(cmd *cobra.Command, args []string) { fmt.Println("running client ...") -} -func init() { - cmdClient.Run = runClient + if len(args) < 2 { + fmt.Println("Params: ") + os.Exit(1) + } + server := args[0] + id, _ := strconv.Atoi(args[1]) + + con, err := net.Dial("tcp", server+":"+strconv.Itoa(SERVERPORT)) + if err != nil { + fmt.Println("connect error:", err) + } + + register(con, id) } diff --git a/cmd/Cmd.go b/cmd/Cmd.go index b63a08b..e28f82e 100644 --- a/cmd/Cmd.go +++ b/cmd/Cmd.go @@ -6,6 +6,10 @@ import ( "github.com/spf13/cobra" ) +const SERVERPORT int = 8989 + +type handshake struct{ ID, W, H int } + var cmdGosl = &cobra.Command{ Use: "gosl [command]", Short: "Run a gosl instance", diff --git a/cmd/Server.go b/cmd/Server.go index 23a031e..57e9ba3 100644 --- a/cmd/Server.go +++ b/cmd/Server.go @@ -5,10 +5,10 @@ import ( "fmt" "log" "net" + "sort" + "time" "github.com/spf13/cobra" - - "code.bitsetter.de/fun/gosl/data" ) var cmdServer = &cobra.Command{ @@ -22,36 +22,67 @@ gosl.json for configuration (Port/Adress, Level) // Run: } -func handleConn(conn *net.TCPConn) { - log.Println("Got a connection from: ", conn.RemoteAddr().String()) +type goslClient struct { + con *net.TCPConn + id int + w int + h int +} - gobd := gob.NewDecoder(conn) - var h data.Handshake - gobd.Decode(&h) - log.Println(h) - conn.Close() +var TotalWidth int = 0 +var clients map[int]goslClient = make(map[int]goslClient) +var clientKeys []int = make([]int, 100) + +func handleConn(conn *net.TCPConn) { + var hs handshake + + log.Println("Got a connection!") + + // handshake + dec := gob.NewDecoder(conn) // Decoder + dec.Decode(&hs) + log.Println("Got client! ID:", hs.ID, "dimensions:", hs.W, hs.H) + clientKeys = append(clientKeys, hs.ID) + sort.Ints(clientKeys) + clients[hs.ID] = goslClient{con: conn, id: hs.ID, w: hs.W, h: hs.H} + TotalWidth += hs.W + + // conn.Close() +} + +func serveClients() { + for { // while true + + for _, k := range clientKeys { + id, client := k, clients[k] + if id > 0 { + fmt.Println("ID:", id, "Client:", client) + } + } + time.Sleep(time.Second) + + } } func runServer(cmd *cobra.Command, args []string) { fmt.Println("running server ...") - listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: 8989}) + listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: SERVERPORT}) if err != nil { log.Fatal(err) panic("Could not open Listener") } defer listener.Close() + go serveClients() for { conn, err := listener.AcceptTCP() if err != nil { log.Fatal(err) panic("Listener could not accept connection!") } - go handleConn(conn) } - data.TestNC() } func init() { diff --git a/data/Screen.go b/data/Screen.go index 5479060..a0af029 100644 --- a/data/Screen.go +++ b/data/Screen.go @@ -13,15 +13,15 @@ type Screen struct { data []rune } -func TestNC() { +func TestNC() (int, int) { win, err := goncurses.Init() if err != nil { log.Fatal(err) os.Exit(1) } win.Clear() - w, h := win.MaxYX() + h, w := win.MaxYX() goncurses.End() - fmt.Printf("screen: %d x %d", w, h) - + fmt.Printf("screen: %d x %d\n", w, h) + return w, h } diff --git a/gosl b/gosl index 9e83a8e..96823ef 100755 Binary files a/gosl and b/gosl differ