Split Client struct into different structs
This commit is contained in:
parent
7db2633351
commit
a152708dcc
|
@ -5,21 +5,33 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
//"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
ClientBasicInfo
|
||||||
|
Conn net.Conn
|
||||||
|
IsOnline bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientBasicInfo struct {
|
||||||
Username string
|
Username string
|
||||||
UID string
|
UID string
|
||||||
GID string
|
GID string
|
||||||
OperatingSystem string
|
OperatingSystem string
|
||||||
Hostname string
|
Hostname string
|
||||||
Conn net.Conn
|
PublicIP string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Instructions struct {
|
type Instructions struct {
|
||||||
|
IsHeartbeat bool
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
Message string
|
Message string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,38 +73,71 @@ func Server() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleConn(conn net.Conn) {
|
func handleConn(conn net.Conn) {
|
||||||
client, err := getOS(conn)
|
client, err := getClient(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error happened receiving OS information: %v\n", err)
|
log.Printf("Error happened receiving OS information: %v\n", err)
|
||||||
}
|
}
|
||||||
client.Conn = conn
|
fmt.Printf("Got info from new user:\nUsername: %v\nUID: %v\nGID: %v\nHostname: %v\nOS: %v\n", client.ClientBasicInfo.Username,
|
||||||
fmt.Printf("Got info from new user:\nUsername: %v\nUID: %v\nGID: %v\nHostname: %v\nOS: %v\n", client.Username,
|
client.ClientBasicInfo.UID, client.ClientBasicInfo.GID, client.ClientBasicInfo.Hostname, client.ClientBasicInfo.OperatingSystem)
|
||||||
client.UID, client.GID, client.Hostname, client.OperatingSystem)
|
|
||||||
time.Sleep(time.Second * 7)
|
go Heartbeat(*client, conn)
|
||||||
fmt.Println("7 seconds have passed, sending message!")
|
|
||||||
time.Sleep(time.Millisecond * 500)
|
|
||||||
newMessage := Instructions{
|
|
||||||
Message: "Hello world",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Heartbeat(client Client, conn net.Conn) {
|
||||||
for {
|
for {
|
||||||
time.Sleep(time.Second)
|
inst := Instructions{
|
||||||
client.Instruct(newMessage)
|
IsHeartbeat: true,
|
||||||
|
Message: "PING",
|
||||||
|
}
|
||||||
|
client.Instruct(inst)
|
||||||
|
log.Printf("Pinging %v...\n", client.ClientBasicInfo.PublicIP)
|
||||||
|
|
||||||
|
resp, err := ServerMessageReceiver(conn)
|
||||||
|
if err == nil && resp.Message == "PONG" {
|
||||||
|
fmt.Printf("Client %v is online!\n", client.ClientBasicInfo.PublicIP)
|
||||||
|
client.IsOnline = true
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Client %v is offline :(\n", client.ClientBasicInfo.PublicIP)
|
||||||
|
client.IsOnline = false
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// for i, v := range clientList {
|
time.Sleep(time.Second * 10)
|
||||||
// fmt.Println(i, v)
|
}
|
||||||
//
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getOS(conn net.Conn) (*Client, error) {
|
func ServerMessageReceiver(conn net.Conn) (Response, error) {
|
||||||
dec := gob.NewDecoder(conn)
|
dec := gob.NewDecoder(conn)
|
||||||
c := new(Client)
|
c := new(Response)
|
||||||
err := dec.Decode(&c)
|
err := dec.Decode(c)
|
||||||
|
if err != nil {
|
||||||
|
return Response{}, err
|
||||||
|
}
|
||||||
|
return *c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getClient(conn net.Conn) (*Client, error) {
|
||||||
|
dec := gob.NewDecoder(conn)
|
||||||
|
basicC := new(ClientBasicInfo)
|
||||||
|
err := dec.Decode(&basicC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bad := Client{}
|
bad := Client{}
|
||||||
return &bad, err
|
return &bad, err
|
||||||
}
|
}
|
||||||
clientList = append(clientList, c)
|
newC := new(Client)
|
||||||
return c, nil
|
newC.ClientBasicInfo = *basicC
|
||||||
|
newC.Conn = conn
|
||||||
|
newC.IsOnline = true
|
||||||
|
|
||||||
|
// for _, v := range clientList {
|
||||||
|
// sameClient := reflect.DeepEqual(c, v.ClientInfo)
|
||||||
|
// if sameClient == true {
|
||||||
|
// fmt.Println("CLIENT ALREADY PRESENT, SETTING TO ONLINE")
|
||||||
|
// c.IsOnline = true
|
||||||
|
// return c, nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//fmt.Println("CLIENT NOT PRESENT, ADDING TO LIST")
|
||||||
|
clientList = append(clientList, newC)
|
||||||
|
return newC, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue