<commit_before>package quibit

import (
	"fmt"
)

var peerList map[string]*Peer
var quit chan bool

// Initialize the Quibit Service
// Frames from the network will be sent to recvChan, and includes the sending peer
// Frames for the network should be sent to sendChan, and include the receiving peer
// New Peers for connecting should be sent to peerChan.
// A local server will be started on the port specified by "port"
// If an error is returned, than neither the server or mux has been started.
func Initialize(log chan string, recvChan, sendChan chan Frame, peerChan chan Peer, port uint16) error {
	var err error

	err = initServer(recvChan, peerChan, fmt.Sprintf(":%d", port))
	if err != nil {
		return err
	}

	peerList = make(map[string]*Peer)
	quit = make(chan bool)

	go mux(recvChan, sendChan, peerChan, quit, log)
	return nil
}

// Cleanup the Quibit Service
// End the mux and server routines, and Disconnect from all peers.
func Cleanup() {
	quit <- true
}

func KillPeer(p string) {
	peer, ok := peerList[p]
	if ok {
		peer.Disconnect()
		delete(peerList, p)
	}
}

func GetPeer(p string) *Peer {
	peer, ok := peerList[p]
	if ok {
		return peer
	} else {
		return nil
	}
}

func mux(recvChan, sendChan chan Frame, peerChan chan Peer, quit chan bool, log chan string) {
	var frame Frame
	var peer Peer
	var err error

	for {
		select {
		case frame = <-sendChan:
			// Received frame to send to peer(s)
			if frame.Peer == "" {
				// Send to all peers
				for key, p := range peerList {
					err = p.sendFrame(frame)
					if err != nil {
						if err.Error() != QuibitError(eHEADER).Error() {
							// Disconnect from Peer
							p.Disconnect()
							delete(peerList, key)
						}
						// Malformed header, break out of for loop
						fmt.Println("Malformed header in frame!")
						break
					}
				}
			} else {
				// Send to one peer
				p, ok := peerList[frame.Peer]
				if ok {
					err = p.sendFrame(frame)<commit_msg>Even more debugging<commit_after>package quibit

import (
	"fmt"
)

var peerList map[string]*Peer
var quit chan bool

// Initialize the Quibit Service
// Frames from the network will be sent to recvChan, and includes the sending peer
// Frames for the network should be sent to sendChan, and include the receiving peer
// New Peers for connecting should be sent to peerChan.
// A local server will be started on the port specified by "port"
// If an error is returned, than neither the server or mux has been started.
func Initialize(log chan string, recvChan, sendChan chan Frame, peerChan chan Peer, port uint16) error {
	var err error

	err = initServer(recvChan, peerChan, fmt.Sprintf(":%d", port))
	if err != nil {
		return err
	}

	peerList = make(map[string]*Peer)
	quit = make(chan bool)

	go mux(recvChan, sendChan, peerChan, quit, log)
	return nil
}

// Cleanup the Quibit Service
// End the mux and server routines, and Disconnect from all peers.
func Cleanup() {
	quit <- true
}

func KillPeer(p string) {
	peer, ok := peerList[p]
	if ok {
		peer.Disconnect()
		delete(peerList, p)
	}
}

func GetPeer(p string) *Peer {
	peer, ok := peerList[p]
	if ok {
		return peer
	} else {
		return nil
	}
}

func mux(recvChan, sendChan chan Frame, peerChan chan Peer, quit chan bool, log chan string) {
	var frame Frame
	var peer Peer
	var err error

	for {
		select {
		case frame = <-sendChan:
			// Received frame to send to peer(s)
			if frame.Peer == "" {
				fmt.Println("Broadcasting message!")
				// Send to all peers
				for key, p := range peerList {
					fmt.Println("Sending Message to Peer: ", key)
					err = p.sendFrame(frame)
					if err != nil {
						if err.Error() != QuibitError(eHEADER).Error() {
							// Disconnect from Peer
							p.Disconnect()
							delete(peerList, key)
						}
						// Malformed header, break out of for loop
						fmt.Println("Malformed header in frame!")
						break
					}
				}
			} else {
				// Send to one peer
				p, ok := peerList[frame.Peer]
				if ok {
					err = p.sendFrame(frame)
{'fraction_non_alphanumeric': 0.10060210737581535, 'fraction_numerical': 0.0010035122930255895, 'mean_word_length': 2.9397233201581026, 'pattern_counts': {'":': 2, '<': 7, '<?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': '471595', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1277, 'n_words': 498}