diff --git a/cmd/control.go b/cmd/control.go index 69bbc0e..c56fed9 100644 --- a/cmd/control.go +++ b/cmd/control.go @@ -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. */ @@ -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 } @@ -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 } @@ -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 } } @@ -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 } @@ -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 } @@ -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 } } @@ -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 } @@ -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 } } diff --git a/cmd/opt.go b/cmd/opt.go index 76465c4..8b1c46e 100644 --- a/cmd/opt.go +++ b/cmd/opt.go @@ -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) { @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/cmd/udp.go b/cmd/udp.go index 731f4d1..c6b33b3 100644 --- a/cmd/udp.go +++ b/cmd/udp.go @@ -7,6 +7,8 @@ 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{ @@ -14,20 +16,20 @@ func udpWrite(nodeList *NodeList, addr string, port int, data []byte) { 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) } @@ -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) @@ -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 }