implement handshake and list of connected clients on the server

This commit is contained in:
Lennart Buhl 2016-01-30 02:04:13 +01:00
parent a9e3afffa9
commit d17f64b489
5 changed files with 80 additions and 16 deletions

BIN
.gitignore vendored

Binary file not shown.

View File

@ -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 <IP> <NUMBER>",
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()
if len(args) < 2 {
fmt.Println("Params: <IP> <NUMBER>")
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)
}
func init() {
cmdClient.Run = runClient
register(con, id)
}

View File

@ -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",

View File

@ -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() {

View File

@ -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
}