<commit_before>package anonlib

import (
	"encoding/binary"
	"log"
	"net"
	"sync"

	"../common"
)

type anonConnection struct {
	sync.Mutex
}

const (
	TYPE_ACK byte = iota
	TYPE_DATA
	TYPE_BEAT
)

//Some Global variables
var (
	BindAddr string
	BindPort string
)

// GetTheClientData builds the frame-id:bool value byte string
func BuildClientFrameData(framid string, enable bool) []byte {

	enableStr := "0"
	if enable == true {
		enableStr = "1"
	}
	tempStr := framid + ":" + enableStr
	return []byte(tempStr)

}

// BuildTheClientDataFromMap locks and read the map and build calls the BuildClientFrameData
func BuildTheClientDataFromMap(M map[string]bool) []byte {

	common.ToAnon.Lck.Lock()
	defer common.ToAnon.Lck.Unlock()

	result := []byte{}
	for key, value := range M {
		result = append(result, BuildClientFrameData(key, value)...)
	}

	return result
}

func GenerateDataMsg(msgType byte, data []byte, totalMsgCount int) []byte {

	//calculate the total byte lenght
	totalLength := int(len(data) + 4 + 4 + 1)
	//create a dummy byte array
	resultByteArray := make([]byte, totalLength)

	//encode msg type in the first byte
	resultByteArray[0] = msgType
	//encode the total msg length
	binary.BigEndian.PutUint32(resultByteArray[1:5], uint32(len(data)+(totalMsgCount*2)))
	//encode the total frames id sent
	binary.BigEndian.PutUint32(resultByteArray[5:9], uint32(totalMsgCount))
	//next start from 9th index
	copy(resultByteArray[9:], data)
	return resultByteArray
}

func NewaAnonConnection() *anonConnection {
	return &anonConnection{}
}

func BindAndStartListener() {
	//Bind to all the network inteface int the system on the same port
	tcpListener, err := net.Listen("tcp", ":"+BindPort)
	if err != nil {
		log.Println(" BindAndStartListener", err)
	}
	HandleClientConnection(tcpListener)
}

//listen for incommng connection
func HandleClientConnection(tcpListener net.Listener) {

	for {
		conn, err := tcpListener.Accept()
		if err != nil {
			log.Println("Unable to accept connection",err)
		}
		newAnonClientConn := NewaAnonConnection()
		go newAnonClientConn.SendMsg(conn)
		go newAnonClientConn.RecvMsg(conn)
	}
}

// SendMsg will wait on the Common.ToAnon
func (annonClient *anonConnection) SendMsg(conn net.Conn) {
	for {
		<-common.ToAnon.Ch
		result := BuildTheClientDataFromMap(common.ToAnon.M)
		result = GenerateDataMsg(TYPE_DATA, result, len(common.ToAnon.M))
		annonClient.Lock()
    log.Println("SendMsg: trying to send data to client",string(result))
		n, err := conn.Write(result)
		if err != nil {
			log.Println("SednMsg: Unable to send data to client", err)
			annonClient.Unlock()
			return
		}
		log.Printf("SendMsg: Total bytes sent %d bytes", n)
		annonClient.Unlock()
	}

}

func (annonClient *anonConnection) RecvMsg(conn net.Conn) {

	for {
		localClientBuf := make([]byte, 4096)
		//defer annonClient.Unlock()
		_, err := conn.Read(localClientBuf)
		if err != nil {
			log.Println("RecvMsg: Unable to read from the client", err, conn.RemoteAddr().String())
			return
		}
		msgType := localClientBuf[0:1]
		if msgType[0] == TYPE_BEAT {
			annonClient.Lock()
			annonClient.SendAck(conn)
			annonClient.Unlock()
		} else {
			log.Println("RecvMsg: Unknown msg from client", msgType,conn.RemoteAddr().String())
		}
	}

}

func (annonClient *anonConnection) SendAck(conn net.Conn) {
	_, err := conn.Write([]byte{TYPE_ACK})
	if err != nil {
		log.Println("SednAck: Failed to send ack", err, conn.RemoteAddr().String())
	}

}

func Run(inBindAddress string, inBindPort string) {
	BindAddr = inBindAddress
	BindPort = inBindPort

  log.Println("Starting the anonlib ")

	go BindAndStartListener()
}
<commit_msg>Appending seperator between framework id msg to client<commit_after>package anonlib

import (
	"encoding/binary"
	"log"
	"net"
	"sync"

	"../common"
)

type anonConnection struct {
	sync.Mutex
}

const (
	TYPE_ACK byte = iota
	TYPE_DATA
	TYPE_BEAT
)

//Some Global variables
var (
	BindAddr string
	BindPort string
)

// GetTheClientData builds the frame-id:bool value byte string
func BuildClientFrameData(framid string, enable bool) []byte {

	enableStr := "0"
	if enable == true {
		enableStr = "1"
	}
	tempStr := framid + ":" + enableStr
	return []byte(tempStr)

}

// BuildTheClientDataFromMap locks and read the map and build calls the BuildClientFrameData
func BuildTheClientDataFromMap(M map[string]bool) []byte {

  flag:=false

	common.ToAnon.Lck.Lock()
	defer common.ToAnon.Lck.Unlock()

	result := []byte{}
	for key, value := range M {
    if flag==true{
      result = append(result, []byte(" ")...)
    }
		result = append(result, BuildClientFrameData(key, value)...)
    flag=true
	}

	return result
}

func GenerateDataMsg(msgType byte, data []byte, totalMsgCount int) []byte {

	//calculate the total byte lenght
	totalLength := int(len(data) + 4 + 4 + 1)
	//create a dummy byte array
	resultByteArray := make([]byte, totalLength)

	//encode msg type in the first byte
	resultByteArray[0] = msgType
	//encode the total msg length
	binary.BigEndian.PutUint32(resultByteArray[1:5], uint32(len(data)+(totalMsgCount*2)))
	//encode the total frames id sent
	binary.BigEndian.PutUint32(resultByteArray[5:9], uint32(totalMsgCount))
	//next start from 9th index
	copy(resultByteArray[9:], data)
	return resultByteArray
}

func NewaAnonConnection() *anonConnection {
	return &anonConnection{}
}

func BindAndStartListener() {
	//Bind to all the network inteface int the system on the same port
	tcpListener, err := net.Listen("tcp", ":"+BindPort)
	if err != nil {
		log.Println(" BindAndStartListener", err)
	}
	HandleClientConnection(tcpListener)
}

//listen for incommng connection
func HandleClientConnection(tcpListener net.Listener) {

	for {
		conn, err := tcpListener.Accept()
		if err != nil {
			log.Println("Unable to accept connection",err)
		}
		newAnonClientConn := NewaAnonConnection()
		go newAnonClientConn.SendMsg(conn)
		go newAnonClientConn.RecvMsg(conn)
	}
}

// SendMsg will wait on the Common.ToAnon
func (annonClient *anonConnection) SendMsg(conn net.Conn) {
	for {
		<-common.ToAnon.Ch
		result := BuildTheClientDataFromMap(common.ToAnon.M)
		result = GenerateDataMsg(TYPE_DATA, result, len(common.ToAnon.M))
		annonClient.Lock()
    log.Println("SendMsg: trying to send data to client",string(result))
		n, err := conn.Write(result)
		if err != nil {
			log.Println("SednMsg: Unable to send data to client", err)
			annonClient.Unlock()
			return
		}
		log.Printf("SendMsg: Total bytes sent %d bytes", n)
		annonClient.Unlock()
	}

}

func (annonClient *anonConnection) RecvMsg(conn net.Conn) {

	for {
		localClientBuf := make([]byte, 4096)
		//defer annonClient.Unlock()
		_, err := conn.Read(localClientBuf)
		if err != nil {
			log.Println("RecvMsg: Unable to read from the client", err, conn.RemoteAddr().String())
			return
		}
		msgType := localClientBuf[0:1]
		if msgType[0] == TYPE_BEAT {
			annonClient.Lock()
			annonClient.SendAck(conn)
			annonClient.Unlock()
		} else {
			log.Println("RecvMsg: Unknown msg from client", msgType,conn.RemoteAddr().String())
		}
	}

}

func (annonClient *anonConnection) SendAck(conn net.Conn) {
	_, err := conn.Write([]byte{TYPE_ACK})
	if err != nil {
		log.Println("SednAck: Failed to send ack", err, conn.RemoteAddr().String())
	}

}

func Run(inBindAddress string, inBindPort string) {
	BindAddr = inBindAddress
	BindPort = inBindPort

  log.Println("Starting the anonlib ")

	go BindAndStartListener()
}

{'fraction_non_alphanumeric': 0.12562205783456623, 'fraction_numerical': 0.007531943510423672, 'mean_word_length': 4.365079365079365, 'pattern_counts': {'":': 4, '<': 5, '<?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': '1309961', 'n_tokens_mistral': 2692, 'n_tokens_neox': 2536, 'n_words': 652}