Skip to content

Commit

Permalink
#267 Restructured server architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed May 13, 2023
1 parent fc5adf2 commit 8ad33a2
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 256 deletions.
30 changes: 9 additions & 21 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"os"
"sync"

"github.com/joho/godotenv"
)
Expand All @@ -16,11 +15,6 @@ type Config struct {
EntryMap string
}

var (
config *Config
once sync.Once
)

func getEnvWithFallBack(envName, fallback string) (env string) {
env = os.Getenv(envName)
if env == "" {
Expand All @@ -29,20 +23,14 @@ func getEnvWithFallBack(envName, fallback string) (env string) {
return
}

func Init() {
func NewConfiFromEnv() Config {
godotenv.Load(".env")
once.Do(func() {
config = &Config{
ServerHost: getEnvWithFallBack("POKETE_SERVER_HOST", "localhost"),
ServerPort: getEnvWithFallBack("POKETE_SERVER_PORT", "9988"),
APIPort: getEnvWithFallBack("POKETE_API_PORT", "9989"),
ServerType: getEnvWithFallBack("POKETE_SERVER_TYPE", "tcp"),
ClientVersion: getEnvWithFallBack("POKETE_SERVER_CLIENT_VERSION", "0.9.1"),
EntryMap: getEnvWithFallBack("POKETE_SERVER_CLIENT_ENTRYMAP", "playmap_1"),
}
})
}

func Get() Config {
return *config
return Config{
ServerHost: getEnvWithFallBack("POKETE_SERVER_HOST", "localhost"),
ServerPort: getEnvWithFallBack("POKETE_SERVER_PORT", "9988"),
APIPort: getEnvWithFallBack("POKETE_API_PORT", "9989"),
ServerType: getEnvWithFallBack("POKETE_SERVER_TYPE", "tcp"),
ClientVersion: getEnvWithFallBack("POKETE_SERVER_CLIENT_VERSION", "0.9.1"),
EntryMap: getEnvWithFallBack("POKETE_SERVER_CLIENT_ENTRYMAP", "playmap_1"),
}
}
106 changes: 6 additions & 100 deletions server/main.go
Original file line number Diff line number Diff line change
@@ -1,110 +1,16 @@
package main

import (
"encoding/json"
"github.com/lxgr-linux/pokete/server/config"
"github.com/lxgr-linux/pokete/server/responses"
"github.com/lxgr-linux/pokete/server/status"
"github.com/lxgr-linux/pokete/server/user_repository"
"github.com/lxgr-linux/pokete/server/config"
"github.com/lxgr-linux/pokete/server/server"
"log"
"net"
"os"

"github.com/lxgr-linux/pokete/server/map_repository"
"github.com/lxgr-linux/pokete/server/requests"
)

func main() {
config.Init()
user_repository.Init()
err := map_repository.Read()
if err != nil {
panic(err.Error())
}
log.Print("Server Running...")
server, err := net.Listen(config.Get().ServerType, config.Get().ServerHost+":"+config.Get().ServerPort)
if err != nil {
log.Print("Error listening:", err.Error())
os.Exit(1)
}
defer server.Close()
go status.HandleRequests()
log.Print("Listening on " + config.Get().ServerHost + ":" + config.Get().ServerPort)
log.Print("Waiting for client...")
for {
connection, err := server.Accept()
if err != nil {
log.Print("Error accepting: ", err.Error())
os.Exit(1)
}
log.Print("client connected")
go processClient(connection)
}
}

func unmarshallRequest[T requests.RequestBody](res []byte, genericResponseObject *requests.Request[requests.RequestBody]) error {
responseObject := requests.Request[T]{}
err := json.Unmarshal(res, &responseObject)
if err != nil {
return err
}

genericResponseObject.Body = responseObject.Body

return nil
}

func handleRequests(res []byte, connection *net.Conn) error {
genericResponseObject := requests.Request[requests.RequestBody]{}
err := json.Unmarshal(res, &genericResponseObject)

switch genericResponseObject.Type {
case requests.RequestType_POSITION_UPDATE:
err = unmarshallRequest[requests.RequestPosition](res, &genericResponseObject)
if err != nil {
return err
}
case requests.RequestType_HANDSHAKE:
err = unmarshallRequest[requests.RequestHandshake](res, &genericResponseObject)
if err != nil {
return err
}
}

log.Printf("%#v\n", genericResponseObject)
err = genericResponseObject.Body.Handle(connection)
if err != nil {
return err
}
return nil
}

func removeUser(connection *net.Conn) error {
thisUser, err := user_repository.GetByConn(connection)
err = user_repository.RemoveByConn(connection)
for _, user := range user_repository.GetAllUsers() {
err = responses.WriteUserRemovedResponse(user.Conn, thisUser.Name)
}
err = (*connection).Close()
return err
}

func processClient(connection net.Conn) {
for {
buffer := make([]byte, 1024)
mLen, err := connection.Read(buffer)
if err != nil {
log.Print("Error reading:", err)
break
}
err = handleRequests(buffer[:mLen], &connection)
if err != nil {
log.Print("Error handeling:", err)
break
}
}
err := removeUser(&connection)
pokete, err := server.NewServer(config.NewConfiFromEnv())
if err != nil {
log.Print("Error closing:", err)
log.Fatal(err)
}

pokete.Start()
}
76 changes: 36 additions & 40 deletions server/map_repository/map_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,64 @@ package map_repository
import (
"encoding/json"
"os"
"sync"
)

var (
obmaps *Obmaps
maps *Maps
npcs *NPCs
trainers *Trainers
once sync.Once
)
type MapRepo struct {
obmaps *Obmaps
maps *Maps
npcs *NPCs
trainers *Trainers
}

func readFile[T any](fileName string) (temp T, err error) {
content, err := os.ReadFile(fileName)
if err != nil {
return
}
func (m MapRepo)GetObmaps() Obmaps {
return *m.obmaps
}

err = json.Unmarshal(content, &temp)
func (m MapRepo)GetMaps() Maps {
return *m.maps
}

func (m MapRepo)GetNPCs() NPCs {
return *m.npcs
}

return
func (m MapRepo)GetTrainers() Trainers {
return *m.trainers
}

func Read() error {
func NewMapRepo() (mapRepo MapRepo, err error) {
tempObmaps, err := readFile[Obmaps]("res/map_data.json")
if err != nil {
return err
return
}
tempMaps, err := readFile[Maps]("res/maps.json")
if err != nil {
return err
return
}
tempNPCs, err := readFile[NPCs]("res/npcs.json")
if err != nil {
return err
return
}
tempTrainers, err := readFile[Trainers]("res/trainers.json")
if err != nil {
return err
return
}

once.Do(func() {
obmaps = &tempObmaps
maps = &tempMaps
npcs = &tempNPCs
trainers = &tempTrainers
})

return nil
return MapRepo {
obmaps: &tempObmaps,
maps: &tempMaps,
npcs: &tempNPCs,
trainers: &tempTrainers,
}, nil
}

func GetObmaps() Obmaps {
return *obmaps
}

func GetMaps() Maps {
return *maps
}
func readFile[T any](fileName string) (temp T, err error) {
content, err := os.ReadFile(fileName)
if err != nil {
return
}

func GetNPCs() NPCs {
return *npcs
}
err = json.Unmarshal(content, &temp)

func GetTrainers() Trainers {
return *trainers
return
}
13 changes: 13 additions & 0 deletions server/provider/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package provider

import (
"github.com/lxgr-linux/pokete/server/config"
"github.com/lxgr-linux/pokete/server/map_repository"
"github.com/lxgr-linux/pokete/server/user_repository"
)

type Provider struct {
Config config.Config
MapRepo map_repository.MapRepo
UserRepo user_repository.UserRepo
}
37 changes: 37 additions & 0 deletions server/requests/handler/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package handler

import (
"encoding/json"

"github.com/lxgr-linux/pokete/server/requests"
)

func unmarshallRequest[T requests.RequestBody](res []byte, genericResponseObject *requests.Request[requests.RequestBody]) error {
responseObject := requests.Request[T]{}
err := json.Unmarshal(res, &responseObject)
if err != nil {
return err
}

genericResponseObject.Body = responseObject.Body

return nil
}

func Handle(res []byte) (genericResponseObject requests.Request[requests.RequestBody], err error) {
err = json.Unmarshal(res, &genericResponseObject)

switch genericResponseObject.Type {
case requests.RequestType_POSITION_UPDATE:
err = unmarshallRequest[requests.RequestPosition](res, &genericResponseObject)
if err != nil {
return
}
case requests.RequestType_HANDSHAKE:
err = unmarshallRequest[requests.RequestHandshake](res, &genericResponseObject)
if err != nil {
return
}
}
return
}
Loading

0 comments on commit 8ad33a2

Please sign in to comment.