Skip to content

Commit

Permalink
Define constants for duplicated error messages to improve code mainta…
Browse files Browse the repository at this point in the history
…inability
  • Loading branch information
kerwenwwer committed Apr 9, 2024
1 parent b93cb64 commit 6b75df2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
19 changes: 11 additions & 8 deletions cmd/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
common "github.com/kerwenwwer/xdp-gossip/common"
)

const errMsgInvalidRequestMethod = "Invalid request method"
const errMsgErrorWritingResponse = "Error writing response"

/*
* HTTP server for XDP Gossip control plane.
*/
Expand All @@ -19,7 +22,7 @@ import (
func (nl *NodeList) ListNodeHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
http.Error(w, errMsgInvalidRequestMethod, http.StatusMethodNotAllowed)
return
}

Expand All @@ -42,7 +45,7 @@ func (nl *NodeList) ListNodeHandler() http.HandlerFunc {
func (nl *NodeList) StopNodeHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
http.Error(w, errMsgInvalidRequestMethod, http.StatusMethodNotAllowed)
return
}

Expand All @@ -52,7 +55,7 @@ func (nl *NodeList) StopNodeHandler() http.HandlerFunc {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("Node stopped successfully.\n"))
if err != nil {
log.Println("Error writing response")
log.Println(errMsgErrorWritingResponse)
return
}
}
Expand All @@ -62,7 +65,7 @@ func (nl *NodeList) StopNodeHandler() http.HandlerFunc {
func (nl *NodeList) GetMetadataHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
http.Error(w, errMsgInvalidRequestMethod, http.StatusMethodNotAllowed)
return
}

Expand All @@ -86,7 +89,7 @@ func (nl *NodeList) GetMetadataHandler() http.HandlerFunc {
func (nl *NodeList) PublishHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
http.Error(w, errMsgInvalidRequestMethod, http.StatusMethodNotAllowed)
return
}

Expand All @@ -104,7 +107,7 @@ func (nl *NodeList) PublishHandler() http.HandlerFunc {
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte("[Control]: Data published successfully.\n"))
if err != nil {
log.Println("Error writing response")
log.Println(errMsgErrorWritingResponse)
return
}
}
Expand All @@ -114,7 +117,7 @@ func (nl *NodeList) PublishHandler() http.HandlerFunc {
func (nl *NodeList) SetNodeHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
http.Error(w, errMsgInvalidRequestMethod, http.StatusMethodNotAllowed)
return
}

Expand All @@ -140,7 +143,7 @@ func (nl *NodeList) SetNodeHandler() http.HandlerFunc {
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte("Node list updated successfully.\n"))
if err != nil {
log.Println("Error writing response")
log.Println(errMsgErrorWritingResponse)
return
}
}
Expand Down
16 changes: 9 additions & 7 deletions cmd/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
common "github.com/kerwenwwer/xdp-gossip/common"
)

const errMsgControlErrorPrefix = "[Control Error]:"

// New initializes the local node list
func (nodeList *NodeList) New(localNode common.Node) {

Expand Down Expand Up @@ -71,7 +73,7 @@ func (nodeList *NodeList) Join() {

// If the local node list of this node has not been initialized
if len(nodeList.localNode.Addr) == 0 {
nodeList.println("[Control Error]:", "New() a nodeList before Join().")
nodeList.println(errMsgControlErrorPrefix, "New() a nodeList before Join().")
// Directly return
return
}
Expand All @@ -96,7 +98,7 @@ func (nodeList *NodeList) Stop() {

// If the local node list of this node has not been initialized
if len(nodeList.localNode.Addr) == 0 {
nodeList.println("[Control Error]:", "New() a nodeList before Stop().")
nodeList.println(errMsgControlErrorPrefix, "New() a nodeList before Stop().")
// Return directly
return
}
Expand All @@ -110,7 +112,7 @@ func (nodeList *NodeList) Start() {

// If the local node list of this node has not been initialized
if len(nodeList.localNode.Addr) == 0 {
nodeList.println("[Control Error]:", "New() a nodeList before Start().")
nodeList.println(errMsgControlErrorPrefix, "New() a nodeList before Start().")
// Return directly
return
}
Expand All @@ -131,7 +133,7 @@ func (nodeList *NodeList) Set(node common.Node) {

// If the local node list of this node has not been initialized
if len(nodeList.localNode.Addr) == 0 {
nodeList.println("[Control Error]:", "New() a nodeList before Set().")
nodeList.println(errMsgControlErrorPrefix, "New() a nodeList before Set().")
// Return directly
return
}
Expand All @@ -155,7 +157,7 @@ func (nodeList *NodeList) Get() []common.Node {

// If the local node list of this node has not been initialized
if len(nodeList.localNode.Addr) == 0 {
nodeList.println("[Control Error]:", "New() a nodeList before Get().")
nodeList.println(errMsgControlErrorPrefix, "New() a nodeList before Get().")
// Return directly
return nil
}
Expand All @@ -180,7 +182,7 @@ func (nodeList *NodeList) Publish(newMetadata []byte) {

//Return if the node's local node list has not been initialized
if len(nodeList.localNode.Addr) == 0 {
nodeList.println("[Control Error]:", "New() a nodeList before Publish().")
nodeList.println(errMsgControlErrorPrefix, "New() a nodeList before Publish().")
return
}

Expand Down Expand Up @@ -225,7 +227,7 @@ func (nodeList *NodeList) Read() []byte {

// If the local node list of this node has not been initialized
if len(nodeList.localNode.Addr) == 0 {
nodeList.println("[Control Error]:", "New() a nodeList before Read().")
nodeList.println(errMsgControlErrorPrefix, "New() a nodeList before Read().")
// Directly return
return nil
}
Expand Down
18 changes: 10 additions & 8 deletions cmd/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ import (
"net"
)

const errMsgUDPErrorPrefix = "[UDP Error]:"

// udpWrite send udp data
func udpWrite(nodeList *NodeList, addr string, port int, data []byte) {
socket, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: net.ParseIP(addr),
Port: port,
})
if err != nil {
nodeList.println("[UDP Error]:", err)
nodeList.println(errMsgUDPErrorPrefix, err)
return
}

_, err = socket.Write(data) // socket write syscall
if err != nil {
nodeList.println("[UDP Error]:", err)
nodeList.println(errMsgUDPErrorPrefix, err)
return
}

defer func(socket *net.UDPConn) {
err = socket.Close()
if err != nil {
nodeList.println("[UDP Error]:", err)
nodeList.println(errMsgUDPErrorPrefix, err)
}
}(socket)
}
Expand All @@ -37,18 +39,18 @@ func udpListen(nodeList *NodeList, mq chan []byte) {

udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", nodeList.ListenAddr, nodeList.localNode.Port))
if err != nil {
nodeList.println("[UDP Error]:", err)
nodeList.println(errMsgUDPErrorPrefix, err)
return
}
conn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
nodeList.println("[UDP Error]:", err)
nodeList.println(errMsgUDPErrorPrefix, err)
return
}
defer func(conn *net.UDPConn) {
err = conn.Close()
if err != nil {
nodeList.println("[UDP Error]:", err)
nodeList.println(errMsgUDPErrorPrefix, err)
}
}(conn)

Expand All @@ -59,12 +61,12 @@ func udpListen(nodeList *NodeList, mq chan []byte) {
// listen for UDP packets to the port
n, _, err := conn.ReadFromUDP(bs)
if err != nil {
nodeList.println("[UDP Error]:", err)
nodeList.println(errMsgUDPErrorPrefix, err)
continue
}

if n >= nodeList.Size {
nodeList.println("[UDP Error]:", fmt.Sprintf("received data size (%v) exceeds the limit (%v)", n, nodeList.Size))
nodeList.println(errMsgUDPErrorPrefix, fmt.Sprintf("received data size (%v) exceeds the limit (%v)", n, nodeList.Size))
continue
}

Expand Down

0 comments on commit 6b75df2

Please sign in to comment.