<commit_before>package main

import "golang.org/x/net/websocket"
import "log"
import "net/http"
import "encoding/json"
import "time"
import "math/rand"
import "github.com/scgolang/osc"
import "net"
import "io"

type Params []string

type Message struct {
	Address string
	Params  Params
}

func (m *Message) Clone() *Message {
	p := make(Params, len(m.Params))
	for i,v := range m.Params {
		p[i] = v
	}
	return &Message{
		Address: m.Address,
		Params: p,
	}
}

var toServer = make(chan *Message, 10)
var toClient = make(chan *Message, 10)

var clients = make(map[string]*websocket.Conn)

var oscClient = connect("127.0.0.1", 4559)
var oscServer = listen("127.0.0.1", 4560)

func connect(address string, port int) *osc.UDPConn {
	ip := net.ParseIP(address)
	if ip == nil {
		panic("Unable to parse IP.")
	}
	addr := &net.UDPAddr{ip, port, ""}
	conn, err := osc.DialUDP("udp", nil, addr)
	if err != nil {
		panic(err)
	}
	return conn
}

func listen(address string, port int) *osc.UDPConn {
	ip := net.ParseIP(address)
	if ip == nil {
		panic("Unable to parse IP.")
	}
	addr := &net.UDPAddr{ip, port, ""}
	conn, err := osc.ListenUDP("udp", addr)
	if err != nil {
		panic(err)
	}
	return conn
}

func sendToClient() {
	for {
		msg := <-toClient
		if msg.Address == "" {
			log.Print("Dropping message. No address.")
			continue
		}
		if len(msg.Params) == 0 {
			log.Print("Dropping message. No client id.")
			continue
		}
		clientId := msg.Params[0]
		ws, ok := clients[clientId]
		if !ok {
			log.Print("Dropping message. Unknown client id: " + clientId)
			continue
		}
		msg.Params = msg.Params[1:]
		msgJson, err := json.Marshal(msg)
		if err != nil {
			log.Print("Error marshalling message to client.")
			log.Print(err)
			continue
		}
		if _, err = ws.Write([]byte(msgJson)); err != nil {
			log.Print("Error writing message to client websocket.")
			log.Print(err)
			continue
		}
	}
}

func sendToServer() {
	for {
		msg := <-toServer
		if msg.Address == "" {
			log.Print("Dropping message. No address.")
			continue
		}
		oscMessage, err := osc.NewMessage(msg.Address)
		if err != nil {
			log.Print("Error create new OSC message to server.")
			log.Print(err)
			continue
		}
		for _, p := range msg.Params {
			oscMessage.WriteString(p)
		}
		err = oscClient.Send(oscMessage)
		if err != nil {
			log.Print("Error sending OSC message to server.")
			log.Print(err)
			continue
		}
	}
}

func websocketHandler(ws *websocket.Conn) {
	clientId := randStringRunes(16)
	log.Print("Connected client: " + clientId)
	clients[clientId] = ws
	for {
		msgJson := make([]byte, 512)
		n, err := ws.Read(msgJson)
		if err != nil {
			if err == io.EOF {
				log.Print("Disconnected client: " + clientId)
				return
			}
			log.Print("Error reading from client websocket.")
			log.Print(err)
			continue
		}
		log.Print("Received from client: ", string(msgJson[:n]))
		msg := &Message{}
		err = json.Unmarshal(msgJson[:n], msg)
		if err != nil {
			log.Print("Error unmarshalling message from client.")
			log.Print(err)
			continue
		}
		params := make([]string, 1)
		params[0] = clientId
		for _, p := range msg.Params {
			params = append(params, p)
		}
		msg.Params = params
		toServer <- msg
		log.Print("Forwarded to server: ", msg)
	}
}

func oscHandler(oscMsg *osc.Message) error {<commit_msg>Increase osc-bridge websocket buffer size.<commit_after>package main

import "golang.org/x/net/websocket"
import "log"
import "net/http"
import "encoding/json"
import "time"
import "math/rand"
import "github.com/scgolang/osc"
import "net"
import "io"

type Params []string

type Message struct {
	Address string
	Params  Params
}

func (m *Message) Clone() *Message {
	p := make(Params, len(m.Params))
	for i,v := range m.Params {
		p[i] = v
	}
	return &Message{
		Address: m.Address,
		Params: p,
	}
}

var toServer = make(chan *Message, 10)
var toClient = make(chan *Message, 10)

var clients = make(map[string]*websocket.Conn)

var oscClient = connect("127.0.0.1", 4559)
var oscServer = listen("127.0.0.1", 4560)

func connect(address string, port int) *osc.UDPConn {
	ip := net.ParseIP(address)
	if ip == nil {
		panic("Unable to parse IP.")
	}
	addr := &net.UDPAddr{ip, port, ""}
	conn, err := osc.DialUDP("udp", nil, addr)
	if err != nil {
		panic(err)
	}
	return conn
}

func listen(address string, port int) *osc.UDPConn {
	ip := net.ParseIP(address)
	if ip == nil {
		panic("Unable to parse IP.")
	}
	addr := &net.UDPAddr{ip, port, ""}
	conn, err := osc.ListenUDP("udp", addr)
	if err != nil {
		panic(err)
	}
	return conn
}

func sendToClient() {
	for {
		msg := <-toClient
		if msg.Address == "" {
			log.Print("Dropping message. No address.")
			continue
		}
		if len(msg.Params) == 0 {
			log.Print("Dropping message. No client id.")
			continue
		}
		clientId := msg.Params[0]
		ws, ok := clients[clientId]
		if !ok {
			log.Print("Dropping message. Unknown client id: " + clientId)
			continue
		}
		msg.Params = msg.Params[1:]
		msgJson, err := json.Marshal(msg)
		if err != nil {
			log.Print("Error marshalling message to client.")
			log.Print(err)
			continue
		}
		if _, err = ws.Write([]byte(msgJson)); err != nil {
			log.Print("Error writing message to client websocket.")
			log.Print(err)
			continue
		}
	}
}

func sendToServer() {
	for {
		msg := <-toServer
		if msg.Address == "" {
			log.Print("Dropping message. No address.")
			continue
		}
		oscMessage, err := osc.NewMessage(msg.Address)
		if err != nil {
			log.Print("Error create new OSC message to server.")
			log.Print(err)
			continue
		}
		for _, p := range msg.Params {
			oscMessage.WriteString(p)
		}
		err = oscClient.Send(oscMessage)
		if err != nil {
			log.Print("Error sending OSC message to server.")
			log.Print(err)
			continue
		}
	}
}

func websocketHandler(ws *websocket.Conn) {
	clientId := randStringRunes(16)
	log.Print("Connected client: " + clientId)
	clients[clientId] = ws
	for {
		msgJson := make([]byte, 2000)
		n, err := ws.Read(msgJson)
		if err != nil {
			if err == io.EOF {
				log.Print("Disconnected client: " + clientId)
				return
			}
			log.Print("Error reading from client websocket.")
			log.Print(err)
			continue
		}
		log.Print("Received from client: ", string(msgJson[:n]))
		msg := &Message{}
		err = json.Unmarshal(msgJson[:n], msg)
		if err != nil {
			log.Print("Error unmarshalling message from client.")
			log.Print(err)
			continue
		}
		params := make([]string, 1)
		params[0] = clientId
		for _, p := range msg.Params {
			params = append(params, p)
		}
		msg.Params = params
		toServer <- msg
		log.Print("Forwarded to server: ", msg)
	}
}

func oscHandler(oscMsg *osc.Message) error {
{'fraction_non_alphanumeric': 0.15328907664453834, 'fraction_numerical': 0.010410380205190103, 'mean_word_length': 3.324200913242009, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '6846589', 'n_tokens_mistral': 2719, 'n_tokens_neox': 2423, 'n_words': 630}