merged
This commit is contained in:
commit
6a4fcb25c8
BIN
.gitignore
vendored
BIN
.gitignore
vendored
Binary file not shown.
|
@ -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 <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 ...")
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user