<commit_before>require_relative 'web_socket.rb'

if ARGV.size < 1 then
	abort "Usage: #{__FILE__} <port>"
end

PORT = ARGV[0].to_i

server = WebSocketServer.new(:accepted_domains => ['*'], :port => PORT)

puts "Hosting Whiteboard server on localhost:#{PORT}"

clients = []

server.run do |ws|
	begin
		puts 'Connection accepted'

		if ws.path == '/whiteboard' then
			ws.handshake

			queue = Queue.new
			clients << queue

			n_clients = clients.size
			clients.each do |client|
				client.push("u_#{n_clients.to_s}")
			end
			puts "Active clients: #{n_clients}"

			thread = Thread.new do
				while true do
					message = queue.pop
					ws.send message
				end
			end

			while true do
				begin
					data = ws.receive
					clients.each do |client|
						client.push data
					end
				rescue Websocket::Error => e
					puts 'Something went wrong:'
					puts ">> #{e.message}"
				rescue
					break
				end
			end
		else
			ws.handshake '404 not found'
		end
	ensure
		puts 'Connection closed by client'
		clients.delete queue
		thread.terminate if thread

		n_clients = clients.size
		clients.each do |client|
			client.push("u_#{n_clients.to_s}")
		end
		puts "Active clients: #{n_clients}"
	end
end
<commit_msg>Server v2 is done and works - fix #2<commit_after>require_relative 'web_socket.rb'

if ARGV.size < 1 then
	abort "Usage: #{__FILE__} <port>"
end

PORT = ARGV[0].to_i

# Create server
server = WebSocketServer.new(:accepted_domains => ['*'], :port => PORT)

puts "Hosting Whiteboard server on localhost:#{PORT}"

clients = []

# Run the server
# The block will be run for every connection
# ws represents the WebSocket
server.run do |ws|
	begin
		puts 'Connection accepted'

		# Check requested path
		if ws.path == '/whiteboard' then
			# Perform handshake
			ws.handshake

			# Create queue
			# Used to synchronise between the main Thread
			# and client threads
			queue = Queue.new
			clients << queue

			# Send user count to all clients' queues
			n_clients = clients.size
			clients.each do |client|
				client.push("u_#{n_clients.to_s}")
			end
			puts "Active clients: #{n_clients}"

			# Create a Thread that will send messages
			# to the client
			thread = Thread.new do
				while true do
					message = queue.pop
					ws.send message
				end
			end

			# Main receive loop
			while true do
				begin
					# Receive data and push it to all clients' queues
					data = ws.receive
					clients.each do |client|
						client.push data
					end
				rescue Websocket::Error => e
					puts 'Something went wrong:'
					puts ">> #{e.message}"
				rescue
					break
				end
			end
		else
			ws.handshake '404 not found'
		end
	ensure
		puts 'Connection closed by client'
		clients.delete queue
		thread.terminate if thread

		n_clients = clients.size
		clients.each do |client|
			client.push("u_#{n_clients.to_s}")
		end
		puts "Active clients: #{n_clients}"
	end
end

{'fraction_non_alphanumeric': 0.09903047091412742, 'fraction_numerical': 0.004155124653739612, 'mean_word_length': 2.8726541554959786, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '2052051', 'n_tokens_mistral': 1213, 'n_tokens_neox': 978, 'n_words': 306}