This commit is contained in:
tkarrass 2016-02-06 01:48:29 +01:00
parent 65334c1779
commit bba1177326
6 changed files with 48 additions and 23 deletions

View File

@ -13,15 +13,15 @@ import (
)
var cmdClient = &cobra.Command{
Use: "client <IP> <NUMBER>",
Use: "client <NUMBER>",
Short: "Runs Gosl as a client",
Long: "Runs Gosl as a client",
Run: runClient,
//Run: runClient,
}
//func init() {
// cmdClient.Run = runClient
//}
var (
ServerHost string
)
func register(con net.Conn, id int) error {
w, h := data.TestNC()
@ -33,17 +33,24 @@ func register(con net.Conn, id int) error {
func runClient(cmd *cobra.Command, args []string) {
fmt.Println("running client ...")
if len(args) < 2 {
fmt.Println("Params: <IP> <NUMBER>")
if len(args) < 1 {
fmt.Println("Params: <NUMBER>")
os.Exit(1)
}
server := args[0]
id, _ := strconv.Atoi(args[1])
id, _ := strconv.Atoi(args[0])
con, err := net.Dial("tcp", server+":"+strconv.Itoa(SERVERPORT))
con, err := net.Dial("tcp", ServerHost+":"+strconv.Itoa(ServerPort))
if err != nil {
fmt.Println("connect error:", err)
}
register(con, id)
}
func init() {
CmdGosl.AddCommand(cmdClient)
cmdClient.Run = runClient
cmdClient.Flags().StringVarP(&ServerHost, "host", "H", "127.0.0.1", "Connect to this server")
cmdClient.Flags().IntVarP(&ServerPort, "port", "p", 8090, "Connect to server on this port")
}

View File

@ -6,11 +6,9 @@ import (
"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]",
Short: "Run a gosl instance",
Long: `Run a gosl server or client.
@ -20,9 +18,7 @@ For further instructions run 'gosl help [command]'
}
func Execute() {
cmdGosl.AddCommand(cmdServer)
cmdGosl.AddCommand(cmdClient)
if err := cmdGosl.Execute(); err != nil {
if err := CmdGosl.Execute(); err != nil {
os.Exit(-1)
}
}

View File

@ -2,7 +2,6 @@ package cmd // code.bitsetter.de/fun/gosl/cmd
import (
"encoding/gob"
"fmt"
"log"
"net"
"sort"
@ -29,6 +28,10 @@ type goslClient struct {
h int
}
var (
LevelFile string
ServerPort int
)
var TotalWidth int = 0
var clients map[int]goslClient = make(map[int]goslClient)
var clientKeys []int = make([]int, 100)
@ -52,7 +55,7 @@ func serveClients() {
for _, k := range clientKeys {
id, client := k, clients[k]
if id > 0 {
fmt.Println("ID:", id, "Client:", client)
log.Println("ID:", id, "Client:", client)
}
}
time.Sleep(time.Second)
@ -60,8 +63,8 @@ func serveClients() {
}
func runServer(cmd *cobra.Command, args []string) {
fmt.Println("running server ...")
listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: SERVERPORT})
log.Println("running server on port", ServerPort)
listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: ServerPort})
if err != nil {
log.Fatal(err)
panic("Could not open Listener")
@ -79,5 +82,9 @@ func runServer(cmd *cobra.Command, args []string) {
}
func init() {
CmdGosl.AddCommand(cmdServer)
cmdServer.Run = runServer
cmdServer.Flags().StringVarP(&LevelFile, "level", "l", "level.lvl", "Use specific levelfile")
cmdServer.Flags().IntVarP(&ServerPort, "port", "p", 8090, "Run server on this port")
}

14
data/level.go Normal file
View File

@ -0,0 +1,14 @@
package data // code.bitsetter.de/fun/gosl/data
type Layer struct {
data []byte
w, h int
}
type Level struct {
Layers map[int]*Layer
}
func (lvl *Level) AddLayer(z int, l *Layer) {
lvl.Layers[z] = l
}

View File

@ -5,7 +5,7 @@ import (
"log"
"os"
"github.com/rthornton128/goncurses"
nc "github.com/rthornton128/goncurses"
)
type Screen struct {
@ -14,14 +14,15 @@ type Screen struct {
}
func TestNC() (int, int) {
win, err := goncurses.Init()
win, err := nc.Init()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
win.Clear()
h, w := win.MaxYX()
goncurses.End()
nc.End()
fmt.Printf("screen: %d x %d\n", w, h)
return w, h
}

0
memo Normal file
View File