This commit is contained in:
tkarrass 2016-02-05 16:20:48 +01:00
commit 6a4fcb25c8
6 changed files with 83 additions and 21 deletions

BIN
.gitignore vendored

Binary file not shown.

View File

@ -1,22 +1,49 @@
package cmd // code.bitsetter.de/fun/gosl/cmd package cmd // code.bitsetter.de/fun/gosl/cmd
import ( import (
"encoding/gob"
"fmt" "fmt"
"net"
"os"
"strconv"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"code.bitsetter.de/fun/gosl/data"
) )
var cmdClient = &cobra.Command{ var cmdClient = &cobra.Command{
Use: "client", Use: "client <IP> <NUMBER>",
Short: "Runs Gosl as a client", Short: "Runs Gosl as a client",
Long: "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) { func runClient(cmd *cobra.Command, args []string) {
fmt.Println("running client ...") fmt.Println("running client ...")
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() { register(con, id)
cmdClient.Run = runClient
} }

View File

@ -6,6 +6,10 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const SERVERPORT int = 8989
type handshake struct{ ID, W, H int }
var cmdGosl = &cobra.Command{ var cmdGosl = &cobra.Command{
Use: "gosl [command]", Use: "gosl [command]",
Short: "Run a gosl instance", Short: "Run a gosl instance",

View File

@ -5,10 +5,10 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"sort"
"time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"code.bitsetter.de/fun/gosl/data"
) )
var cmdServer = &cobra.Command{ var cmdServer = &cobra.Command{
@ -22,36 +22,67 @@ gosl.json for configuration (Port/Adress, Level)
// Run: // Run:
} }
func handleConn(conn *net.TCPConn) { type goslClient struct {
log.Println("Got a connection from: ", conn.RemoteAddr().String()) con *net.TCPConn
id int
w int
h int
}
gobd := gob.NewDecoder(conn) var TotalWidth int = 0
var h data.Handshake var clients map[int]goslClient = make(map[int]goslClient)
gobd.Decode(&h) var clientKeys []int = make([]int, 100)
log.Println(h)
conn.Close() 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) { func runServer(cmd *cobra.Command, args []string) {
fmt.Println("running server ...") 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
panic("Could not open Listener") panic("Could not open Listener")
} }
defer listener.Close() defer listener.Close()
go serveClients()
for { for {
conn, err := listener.AcceptTCP() conn, err := listener.AcceptTCP()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
panic("Listener could not accept connection!") panic("Listener could not accept connection!")
} }
go handleConn(conn) go handleConn(conn)
} }
data.TestNC()
} }
func init() { func init() {

View File

@ -13,15 +13,15 @@ type Screen struct {
data []rune data []rune
} }
func TestNC() { func TestNC() (int, int) {
win, err := goncurses.Init() win, err := goncurses.Init()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
os.Exit(1) os.Exit(1)
} }
win.Clear() win.Clear()
w, h := win.MaxYX() h, w := win.MaxYX()
goncurses.End() goncurses.End()
fmt.Printf("screen: %d x %d", w, h) fmt.Printf("screen: %d x %d\n", w, h)
return w, h
} }

BIN
gosl

Binary file not shown.