<commit_before>require 'xcodeproj'
require 'pathname'
require 'fileutils'
require 'listen'

require 'webrick'
include WEBrick

module Pbind
  class Command
    class Serv < Command
      self.abstract_command = false
      self.summary = 'Start a mock server for device.'
      self.description = <<-DESC
        This will start a HTTP server provides the APIs in PBLocalhost and
        also a Socket server to send file changes to the device. 
      DESC

      def validate!
        verify_project_exists
      end

      def run
        @src_name = 'PBLiveLoader'
        @api_name = 'PBLocalhost'
        @src_key = 'PBResourcesPath'
        @project_root = File.dirname(@project_path)
        @src_install_dir = File.absolute_path(File.join(@project_root, @src_name))
        @api_install_dir = File.absolute_path(File.join(@project_root, @api_name))
        @project = Xcodeproj::Project.open(@project_path)
        @changed = false

        listen_file_changes
        open_tcp_server

        trap("SIGINT") {
          @server.close
          @listener.stop
          exit
        }

        super
        # sleep
      end

      private

      #----------------------------------------#

      # !@group Private helpers

      # Create a HTTP server and open it
      #
      # @return [void]
      #
      def open_tcp_server
        require 'socket'

        server = TCPServer.new 8082 # Server bind to port
        @server = server
        @clients = []

        addr = server.addr
        addr.shift
        puts "server is on #{addr.join(':')}"

        loop do
          Thread.start(server.accept) do |client|
            @clients.push client
            loop do
              line = client.readpartial(1024)
              if line != nil and line.end_with?('.json')
                send_json(@clients, line)
              end
            end
            client.close
          end
        end

        # loop do
        #   client = server.accept    # Wait for a client to connect
        #   @client = client

        #   line = client.readpartial(1024)
        #   puts "get #{line}"
        #   if line != nil and line.end_with?('.json')
        #     puts "send #{line}"
        #     send_json(client, line)
        #   end

        #   client.close
        # end
      end

      def listen_file_changes
        @listener = Listen.to(@project_root) do |modified, added, removed|
          # puts "modified absolute path: #{modified}"
          # puts "added absolute path: #{added}"
          # puts "removed absolute path: #{removed}"

          modified.each { |m|
            if m.end_with?('.plist')
              if @clients != nil
                send_plist @clients, m
              end
            elsif m.end_with?('.json')<commit_msg>Support print server ip and client(iphone) name<commit_after>require 'xcodeproj'
require 'pathname'
require 'fileutils'
require 'listen'

require 'webrick'
include WEBrick

module Pbind
  class Command
    class Serv < Command
      self.abstract_command = false
      self.summary = 'Start a mock server for device.'
      self.description = <<-DESC
        This will start a HTTP server provides the APIs in PBLocalhost and
        also a Socket server to send file changes to the device. 
      DESC

      def validate!
        verify_project_exists
      end

      def run
        @src_name = 'PBLiveLoader'
        @api_name = 'PBLocalhost'
        @src_key = 'PBResourcesPath'
        @project_root = File.dirname(@project_path)
        @src_install_dir = File.absolute_path(File.join(@project_root, @src_name))
        @api_install_dir = File.absolute_path(File.join(@project_root, @api_name))
        @project = Xcodeproj::Project.open(@project_path)
        @changed = false

        listen_file_changes
        open_tcp_server

        trap("SIGINT") {
          @server.close
          @listener.stop
          exit
        }

        super
        # sleep
      end

      private

      #----------------------------------------#

      # !@group Private helpers

      # Create a HTTP server and open it
      #
      # @return [void]
      #
      def open_tcp_server
        require 'socket'

        server = TCPServer.new 8082 # Server bind to port
        @server = server
        @clients = []

        addr = server.addr
        addr.shift
        puts "#{'Pbind server is on'.bold} #{local_ip.underline}"

        loop do
          Thread.start(server.accept) do |client|
            @clients.push client
            loop do
              req = client.readpartial(1024)
              if req != nil
                handle_request(client, req)
              end
            end
            client.close
          end
        end
      end

      def handle_request(client, req)
        if req.start_with?('[C]')
          puts "client #{req[3..-1].green} connected"
        elsif req.end_with?('.json')
          send_json([client], req)
        end
      end

      def local_ip
        addr_infos = Socket.ip_address_list
        local_addr_info = addr_infos.select { |info|
          info.ipv4? and info.ip_address != '127.0.0.1'
        }
        local_addr_info[0].ip_address
      end

      def listen_file_changes
        @listener = Listen.to(@project_root) do |modified, added, removed|
          # puts "modified absolute path: #{modified}"
          # puts "added absolute path: #{added}"
          # puts "removed absolute path: #{removed}"

          modified.each { |m|
            if m.end_with?('.plist')
              if @clients != nil
                send_plist @clients, m
              end
            elsif m.end_with?('.json')
{'fraction_non_alphanumeric': 0.10368663594470046, 'fraction_numerical': 0.005317263381779511, 'mean_word_length': 1.6517857142857142, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '5906184', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1575, 'n_words': 426}