<commit_before>package client

import (
	"encoding/json"
	"log"
	"net/http"

	"github.com/caelifer/webtime/service"
	"github.com/gorilla/websocket"
)

// Public package variable to contol Websocket buffers
var WSUpgrader = &websocket.Upgrader{ReadBufferSize: 256, WriteBufferSize: 256}

type WSClient struct {
	service service.Service
}

func (cl *WSClient) log(msg string, args ...interface{}) {
	m := "[CLIENT] " + msg
	log.Printf(m, args...)
}

func NewWSClient(srv service.Service) *WSClient {
	return &WSClient{service: srv}
}

func (cl *WSClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Get remote address
	peer := r.RemoteAddr

	// Create websocket
	ws, err := WSUpgrader.Upgrade(w, r, nil)
	if err != nil {
		cl.log("(%s) - Failed to create WebSocket, exiting\n", peer)
		return // Need to better handle errors
	}

	// Connection object
	c := &connection{
		send: make(chan []byte, 256),
		ws:   ws,
	}

	// Init service

	// Make quit channel
	quit := make(chan bool)

	// Get service channel
	servchan := cl.service.Service(quit)

	cl.log("(%s) - initialized serivce\n", peer)

	// Loop in the separate goroutine
	go func() {
		for {
			select {
			case ts := <-servchan:
				cl.log("(%s) - sending update [%v]\n", peer, ts)
				func(ts string) {
					msg, err := json.Marshal(&timeUpdate{ts})
					if err != nil {
						cl.log("Bad encoding for %q [%v]\n", ts, err)
						return
					}
					c.send <- []byte(msg)
				}(ts.(string))

			case <-quit:
				cl.log("(%s)- got quit signal\n", peer)
				close(quit)
				return
			}
		}
	}()

	go c.writer()
	c.reader()

	// Clean-up
	quit <- true
}

type timeUpdate struct {
	Time string `json:"time"`
}

type connection struct {
	send chan []byte
	ws   *websocket.Conn
}

func (c *connection) reader() {
	for {
		_, _, err := c.ws.ReadMessage()
		if err != nil {
			break
		}
		// log.Println("[From WS]", msg)
	}
	c.ws.Close()
}

func (c *connection) writer() {
	for msg := range c.send {
		// log.Println("[To WS]", string(msg))
		err := c.ws.WriteMessage(websocket.TextMessage, msg)
		if err != nil {
			log.Println(err)
			break
		}
	}
	c.ws.Close()
}<commit_msg>Minor code refactoring<commit_after>package client

import (
	"encoding/json"
	"log"
	"net/http"

	"github.com/caelifer/webtime/service"
	"github.com/gorilla/websocket"
)

// Public package variable to contol Websocket buffers
var WSUpgrader = &websocket.Upgrader{ReadBufferSize: 256, WriteBufferSize: 256}

type WSClient struct {
	service service.Service
}

func (cl *WSClient) log(msg string, args ...interface{}) {
	m := "[CLIENT] " + msg
	log.Printf(m, args...)
}

func NewWSClient(srv service.Service) *WSClient {
	return &WSClient{service: srv}
}

func (cl *WSClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Get remote address
	peer := r.RemoteAddr

	// Create websocket
	ws, err := WSUpgrader.Upgrade(w, r, nil)
	if err != nil {
		cl.log("(%s) - Failed to create WebSocket, exiting\n", peer)
		return // Need to better handle errors
	}

	// Connection object
	c := &connection{
		send: make(chan []byte, 256),
		ws:   ws,
	}

	// Init service

	// Make quit channel
	quit := make(chan bool)

	// Get service channel
	servchan := cl.service.Service(quit)

	cl.log("(%s) - initialized serivce\n", peer)

	// Loop in the separate goroutine
	go func() {
		for {
			select {
			case update := <-servchan:
				ts := update.(string) // Cast to the correct type
				cl.log("(%s) - sending update [%v]\n", peer, ts)

				msg, err := json.Marshal(&timeUpdate{ts})
				if err != nil {
					cl.log("Bad encoding for %q [%v]\n", ts, err)
					return
				}
				c.send <- []byte(msg)

			case <-quit:
				cl.log("(%s)- got quit signal\n", peer)
				close(quit)
				return
			}
		}
	}()

	// Enable by-directional communication via WS
	go c.write()

	// Reader will return when WS is closed on the remote client-side
	c.read()

	// Clean-up
	quit <- true
}

type connection struct {
	send chan []byte
	ws   *websocket.Conn
}

func (c *connection) read() {
	for {
		_, _, err := c.ws.ReadMessage()
		if err != nil {
			// Close on error
			break
		}
	}
	c.ws.Close()
}

func (c *connection) write() {
	for msg := range c.send {
		err := c.ws.WriteMessage(websocket.TextMessage, msg)
		if err != nil {
			log.Println(err)
			break
		}
	}
	c.ws.Close()
}

// Helper type to allow for fast JSON serialization
type timeUpdate struct {
	Time string `json:"time"`
}
{'fraction_non_alphanumeric': 0.15671133318192143, 'fraction_numerical': 0.004088121735180559, 'mean_word_length': 2.9747292418772564, 'pattern_counts': {'":': 0, '<': 11, '<?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': '941521', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1596, 'n_words': 443}