<commit_before>package routes

import (
	"fmt"
	"log"
	"net/http"
	"strings"

	"github.com/gorilla/websocket"
	"github.com/jijeshmohan/mtgrid/models"
)

type NodeMsg struct {
	Status  string
	Message string
}

var upgradeWs = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

func Node(w http.ResponseWriter, r *http.Request) {
	ws, err := upgradeWs.Upgrade(w, r, nil)
	if _, ok := err.(websocket.HandshakeError); ok {
		log.Println("Not a websocket handshake")
		http.Error(w, "Not a websocket handshake", 400)
		return
	} else if err != nil {
		log.Println(err)
		return
	}
	nodeMsg := NodeMsg{}
	ws.ReadJSON(&nodeMsg)

	if err = processClient(nodeMsg); err != nil {
		log.Println(err)
		ws.Close()
		return
	}
	for {
		messageType, p, err := ws.ReadMessage()
		ws.WriteMessage(messageType, p)
		if err != nil {
			log.Println(err)
			return
		}
	}
}

func processClient(msg NodeMsg) error {
	if strings.ToLower(msg.Status) != "connected" {
		return fmt.Errorf("Invalid protocol from client")
	}
	device, err := models.GetDeviceWithName(strings.ToLower(msg.Message))
	if err != nil || device == nil {
		log.Println(err)
		return fmt.Errorf("Unable to find device : %s", msg.Message)
	}
	err = models.UpdateDeviceStatus(device, "Connected")
	return err
}<commit_msg>update status when the connection closes<commit_after>package routes

import (
	"fmt"
	"log"
	"net/http"
	"strings"

	"github.com/gorilla/websocket"
	"github.com/jijeshmohan/mtgrid/models"
)

type NodeMsg struct {
	Status  string
	Message string
}

var upgradeWs = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

func Node(w http.ResponseWriter, r *http.Request) {
	ws, err := upgradeWs.Upgrade(w, r, nil)
	if _, ok := err.(websocket.HandshakeError); ok {
		http.Error(w, "Not a websocket handshake", 400)
		return
	} else if err != nil {
		log.Println(err)
		return
	}
	nodeMsg := NodeMsg{}
	ws.ReadJSON(&nodeMsg)

	if err = processClient(nodeMsg); err != nil {
		log.Println(err)
		ws.Close()
		return
	}

	defer updateStatus(nodeMsg.Message, "Disconnected")
	for {
		messageType, p, err := ws.ReadMessage()
		ws.WriteMessage(messageType, p)
		if err != nil {
			log.Println("ERROR :", err)
			return
		}
	}
}

func updateStatus(devicename string, status string) error {
	device, err := models.GetDeviceWithName(strings.ToLower(devicename))
	if err != nil || device == nil {
		log.Println(err)
		return fmt.Errorf("Unable to find device : %s", devicename)
	}
	err = models.UpdateDeviceStatus(device, status)
	return err
}

func processClient(msg NodeMsg) error {
	if strings.ToLower(msg.Status) != "connected" {
		return fmt.Errorf("Invalid protocol from client")
	}

	err := updateStatus(msg.Message, "Connected")
	return err
}
{'fraction_non_alphanumeric': 0.13482361159622774, 'fraction_numerical': 0.007684247293049249, 'mean_word_length': 4.132616487455197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7009030', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1031, 'n_words': 248}