<commit_before>import threading
import socket
import select

from ..util import network as sharednet
from ..util import timer 

class SyncedMusicClient(threading.Thread):
	def __init__(self, logger):
		threading.Thread.__init__(self)
		self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.socket.connect(('localhost', sharednet.PORT))

		self.logger = logger
		self.quitFlag = threading.Event()
		self.timer = timer.HighPrecisionTimer()
		self.packetBuffer = "" # a buffer, because it's not gauaranteed that every single send corresponds to a single recv

	def quit(self):
		self.quitFlag.set()

	def run(self):
		while not self.quitFlag.isSet():
			readableSockets = select.select([self.socket], [], [], 0)[0]
			for sock in readableSockets:
				self.packetBuffer += sock.recv(4096)

			idSize = struct.calcsize("B")
			if len(self.packetBuffer) >= idSize:
				type = struct.unpack("B", self.packetBuffer[0])[0]
				if type == sharednet.TIMESTAMP_PACKET_ID:
					packetSize = struct.calcsize("Bf")
					if len(self.packetBuffer) >= packetSize:
						type, timestamp = struct.unpack("Bf", self.packetBuffer[0:packetSize])
						self.packetBuffer = self.packetBuffer[packetSize:]
						self.timer.update(timestamp)
				elif type == sharednet.CHUNK_PACKET_ID:
					pass
				else:
					self.logger.warning("Received unknown packet id: " + str(type))<commit_msg>added connect() and made quit close the socket in client.<commit_after>import threading
import socket
import select

from ..util import network as sharednet
from ..util import timer 

class SyncedMusicClient(threading.Thread):
	def __init__(self, logger):
		threading.Thread.__init__(self)
		self.logger = logger
		self.quitFlag = threading.Event()
		self.timer = timer.HighPrecisionTimer()
		self.packetBuffer = "" # a buffer, because it's not gauaranteed that every single send corresponds to a single recv

	def connect(host):
		self.packetBuffer = ""
		self.timer.reset()
		if self.socket is not None:
			self.socket.close()
		self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.socket.connect((host, sharednet.PORT))

	def quit(self):
		self.socket.close()
		self.quitFlag.set()

	def run(self):
		while not self.quitFlag.isSet():
			readableSockets = select.select([self.socket], [], [], 0)[0]
			for sock in readableSockets:
				self.packetBuffer += sock.recv(4096)

			idSize = struct.calcsize("B")
			if len(self.packetBuffer) >= idSize:
				type = struct.unpack("B", self.packetBuffer[0])[0]
				if type == sharednet.TIMESTAMP_PACKET_ID:
					packetSize = struct.calcsize("Bf")
					if len(self.packetBuffer) >= packetSize:
						type, timestamp = struct.unpack("Bf", self.packetBuffer[0:packetSize])
						self.packetBuffer = self.packetBuffer[packetSize:]
						self.timer.update(timestamp)
				elif type == sharednet.CHUNK_PACKET_ID:
					pass
				else:
					self.logger.warning("Received unknown packet id: " + str(type))
{'fraction_non_alphanumeric': 0.1242320819112628, 'fraction_numerical': 0.0061433447098976105, 'mean_word_length': 4.160211267605634, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2746546', 'n_tokens_mistral': 1079, 'n_tokens_neox': 916, 'n_words': 217}