<commit_before>require 'magent/web_socket_channel'
module Magent
  class WebSocketServer
    def initialize(options = {})
      options = {:host => "0.0.0.0", :port => 34567}.merge(options)

      $stdout.puts ">> Server running and up! #{options}}" if options[:debug]

      EventMachine.run do
        @channel = EM::Channel.new
        @channels = {}
        @channel_ids = {}
        @sids = {}

        EM.run do
        EventMachine.add_periodic_timer(options.delete(:interval)||10) do
          while message = Magent::WebSocketChannel.dequeue
            if (channel = @channels[message["channel_id"]])
              channel.push(message.to_json)
            end
          end
        end
        end

        EventMachine::WebSocket.start(options) do |ws|
          ws.onopen do
            ws.onmessage do |msg|
              data = JSON.parse(msg) rescue {}

              case data["id"]
              when 'start'
                if data["channel_id"].present? && (channel_id = validate_channel_id(data["channel_id"]))
                  key = generate_unique_key(data["key"])
                  @channels[channel_id] ||= EM::Channel.new
                  @channel_ids[key] = channel_id

                  sid = @channels[channel_id].subscribe { |msg| ws.send(msg) }

                  @sids[key] = sid
                  ws.onclose do
                    @channel_ids.delete(key)
                    @channels[channel_id].unsubscribe(sid)
                    @sids.delete(key)
                  end

                  ws.send({:id => "ack", :key => key}.to_json)
                else
                  ws.close_connection
                end
              when 'chatmessage'
                key = data["key"]
                return invalid_key if key.blank? || @sids[key].blank?

                channel_id = @channel_ids[key]

                if channel_id
                  @channels[channel_id].push({:id => 'chatmessage', :from => user_name(key, @sids[key]), :message => data["message"]}.to_json)
                else
                  ws.send({:id => 'announcement', :type => "error", :message => "cannot find the channel"}.to_json)
                end<commit_msg>typo<commit_after>require 'magent/web_socket_channel'
module Magent
  class WebSocketServer
    def initialize(options = {})
      options = {:host => "0.0.0.0", :port => 34567}.merge(options)

      $stdout.puts ">> Server running and up! #{options}}" if options[:debug]

      EventMachine.run do
        @channel = EM::Channel.new
        @channels = {}
        @channel_ids = {}
        @sids = {}

        EM.run do
        EventMachine.add_periodic_timer(options.delete(:interval)||10) do
          while message = Magent::WebSocketChannel.dequeue
            if (channel = @channels[message["channel_id"]])
              channel.push(message.to_json)
            end
          end
        end
        end

        EventMachine::WebSocket.start(options) do |ws|
          ws.onopen do
            ws.onmessage do |msg|
              data = JSON.parse(msg) rescue {}

              case data["id"]
              when 'start'
                if data["channel_id"].present? && (channel_id = validate_channel_id(data["channel_id"]))
                  key = generate_unique_key(data["key"])
                  @channels[channel_id] ||= EM::Channel.new
                  @channel_ids[key] = channel_id

                  sid = @channels[channel_id].subscribe { |msg| ws.send(msg) }

                  @sids[key] = sid
                  ws.onclose do
                    @channel_ids.delete(key)
                    @channels[channel_id].unsubscribe(sid)
                    @sids.delete(key)
                  end

                  ws.send({:id => "ack", :key => key}.to_json)
                else
                  ws.close_connection
                end
              when 'chatmessage'
                key = data["key"]
                return invalid_key(ws) if key.blank? || @sids[key].blank?

                channel_id = @channel_ids[key]

                if channel_id
                  @channels[channel_id].push({:id => 'chatmessage', :from => user_name(key, @sids[key]), :message => data["message"]}.to_json)
                else
                  ws.send({:id => 'announcement', :type => "error", :message => "cannot find the channel"}.to_json)
                end
{'fraction_non_alphanumeric': 0.12491389207807119, 'fraction_numerical': 0.005051664753157291, 'mean_word_length': 1.6130773845230955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '948224', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1178, 'n_words': 237}