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 f167b64..5583ea3 100644 --- a/cmd/Client.go +++ b/cmd/Client.go @@ -1,7 +1,11 @@ package cmd // code.bitsetter.de/fun/gosl/cmd import ( + "encoding/gob" "fmt" + "net" + "os" + "strconv" "github.com/spf13/cobra" @@ -9,17 +13,37 @@ import ( ) 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 ...") - data.TestNC() -} -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 df5d698..4279f88 100644 --- a/cmd/Server.go +++ b/cmd/Server.go @@ -1,13 +1,14 @@ package cmd // code.bitsetter.de/fun/gosl/cmd import ( + "encoding/gob" "fmt" "log" "net" + "sort" + "time" "github.com/spf13/cobra" - - "code.bitsetter.de/fun/gosl/data" ) var cmdServer = &cobra.Command{ @@ -17,32 +18,67 @@ var cmdServer = &cobra.Command{ // Run: } -func handleConn(conn *net.TCPConn) { - log.Println("Got a connection!") - conn.Close() +type goslClient struct { + con *net.TCPConn + id int + w int + h int +} +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 163334a..a0af029 100644 --- a/data/Screen.go +++ b/data/Screen.go @@ -13,7 +13,7 @@ type Screen struct { data []rune } -func TestNC() { +func TestNC() (int, int) { win, err := goncurses.Init() if err != nil { log.Fatal(err) @@ -23,5 +23,5 @@ func TestNC() { h, w := win.MaxYX() goncurses.End() fmt.Printf("screen: %d x %d\n", w, h) - + return w, h }