Removed global variables

This commit is contained in:
Maciej Krzyżanowski 2024-03-21 23:54:13 +01:00
parent 15a92f508d
commit 7793298474

74
main.go
View File

@ -11,6 +11,11 @@ import (
"time" "time"
) )
type ServerContext struct {
peersList []Peer
peersListLock sync.RWMutex
}
type Peer struct { type Peer struct {
id int id int
conn net.Conn conn net.Conn
@ -41,20 +46,6 @@ const (
listPeersRID = 2 listPeersRID = 2
) )
var peersList []Peer
var peersListLock sync.RWMutex
var idCounter int = 0
var idCounterLock sync.Mutex
func genNewID() int {
var newid int
idCounterLock.Lock()
idCounter++
newid = idCounter
idCounterLock.Unlock()
return newid
}
func peerSliceIndexOf(s []Peer, id int) int { func peerSliceIndexOf(s []Peer, id int) int {
i := 0 i := 0
var p Peer var p Peer
@ -71,15 +62,15 @@ func peerSliceRemove(s *[]Peer, i int) {
*s = (*s)[:len(*s)-1] *s = (*s)[:len(*s)-1]
} }
func handleDisconnection(id int) { func handleDisconnection(srvCtx *ServerContext, id int) {
peersListLock.Lock() srvCtx.peersListLock.Lock()
p := peersList[peerSliceIndexOf(peersList, id)] p := srvCtx.peersList[peerSliceIndexOf(srvCtx.peersList, id)]
log.Printf("[Server] %s disconnected\n", p.conn.RemoteAddr()) log.Printf("[Server] %s disconnected\n", p.conn.RemoteAddr())
peerSliceRemove(&peersList, peerSliceIndexOf(peersList, id)) peerSliceRemove(&srvCtx.peersList, peerSliceIndexOf(srvCtx.peersList, id))
peersListLock.Unlock() srvCtx.peersListLock.Unlock()
} }
func handlePeer(p Peer) { func handlePeer(srvCtx *ServerContext, p Peer) {
br := bufio.NewReader(p.conn) br := bufio.NewReader(p.conn)
bw := bufio.NewWriter(p.conn) bw := bufio.NewWriter(p.conn)
@ -87,7 +78,7 @@ func handlePeer(p Peer) {
reqBytes, err := br.ReadBytes('\n') reqBytes, err := br.ReadBytes('\n')
if err == io.EOF { if err == io.EOF {
handleDisconnection(p.id) handleDisconnection(srvCtx, p.id)
break break
} else if err != nil { } else if err != nil {
log.Println(err) log.Println(err)
@ -105,9 +96,9 @@ func handlePeer(p Peer) {
var resBytes []byte var resBytes []byte
if operationCode == echoRID { if operationCode == echoRID {
resBytes, err = handleEcho(reqJsonBytes) resBytes, err = handleEcho(srvCtx, reqJsonBytes)
} else if operationCode == listPeersRID { } else if operationCode == listPeersRID {
resBytes, err = handleListPeers(reqJsonBytes) resBytes, err = handleListPeers(srvCtx, reqJsonBytes)
} }
if err != nil { if err != nil {
@ -131,7 +122,7 @@ func handlePeer(p Peer) {
} }
} }
func handleEcho(reqBytes []byte) (resBytes []byte, err error) { func handleEcho(_ *ServerContext, reqBytes []byte) (resBytes []byte, err error) {
var echoReq EchoRequest var echoReq EchoRequest
err = json.Unmarshal(reqBytes, &echoReq) err = json.Unmarshal(reqBytes, &echoReq)
@ -149,7 +140,7 @@ func handleEcho(reqBytes []byte) (resBytes []byte, err error) {
return resBytes, nil return resBytes, nil
} }
func handleListPeers(reqBytes []byte) (resBytes []byte, err error) { func handleListPeers(srvCtx *ServerContext, reqBytes []byte) (resBytes []byte, err error) {
// For the sake of conciseness -> currently unmarshalling empty slice to empty struct // For the sake of conciseness -> currently unmarshalling empty slice to empty struct
var listPeersReq ListPeersRequest var listPeersReq ListPeersRequest
err = json.Unmarshal(reqBytes, &listPeersReq) err = json.Unmarshal(reqBytes, &listPeersReq)
@ -158,10 +149,10 @@ func handleListPeers(reqBytes []byte) (resBytes []byte, err error) {
return nil, err return nil, err
} }
peersListLock.RLock() srvCtx.peersListLock.RLock()
peersFreeze := make([]Peer, len(peersList)) peersFreeze := make([]Peer, len(srvCtx.peersList))
copy(peersFreeze, peersList) copy(peersFreeze, srvCtx.peersList)
peersListLock.RUnlock() srvCtx.peersListLock.RUnlock()
listPeersRes := ListPeersResponse{make([]PeerInfo, 0)} listPeersRes := ListPeersResponse{make([]PeerInfo, 0)}
for _, peer := range peersFreeze { for _, peer := range peersFreeze {
@ -180,29 +171,29 @@ func handleListPeers(reqBytes []byte) (resBytes []byte, err error) {
return resBytes, nil return resBytes, nil
} }
func printConnectedPeers() { func printConnectedPeers(srvCtx *ServerContext) {
peersListLock.RLock() srvCtx.peersListLock.RLock()
log.Println("[Server] Displaying all connections:") log.Println("[Server] Displaying all connections:")
for _, p := range peersList { for _, p := range srvCtx.peersList {
log.Printf("[Server] ID#%d: %s\n", p.id, p.conn.RemoteAddr()) log.Printf("[Server] ID#%d: %s\n", p.id, p.conn.RemoteAddr())
} }
peersListLock.RUnlock() srvCtx.peersListLock.RUnlock()
} }
func runServer() { func runServer() {
idCounter := 0
srvCtx := &ServerContext{peersList: make([]Peer, 0)}
ln, err := net.Listen("tcp", ":8080") ln, err := net.Listen("tcp", ":8080")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
peersList = make([]Peer, 0)
go func() { go func() {
for { for {
printConnectedPeers() printConnectedPeers(srvCtx)
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
} }
}() }()
@ -216,11 +207,12 @@ func runServer() {
} }
log.Printf("[Server] client connected %s\n", c.RemoteAddr()) log.Printf("[Server] client connected %s\n", c.RemoteAddr())
peersListLock.Lock() idCounter++
np := Peer{genNewID(), c} newPeer := Peer{idCounter, c}
peersList = append(peersList, np) srvCtx.peersListLock.Lock()
peersListLock.Unlock() srvCtx.peersList = append(srvCtx.peersList, newPeer)
go handlePeer(np) srvCtx.peersListLock.Unlock()
go handlePeer(srvCtx, newPeer)
} }
} }