def add_handlers #:nodoc:
      @server.set_default_handler do |method, *args|
        puts "unhandled call with #{method}, #{args}"
        [0, "I DON'T KNOW", 0]
      end

      @server.add_handler('getBusStats') do |caller_id|
        pubstats = @publishers.map do |pub|
          [pub.topic_name, pub.topic_type.type, pub.get_connection_data]
        end
        substats = @subscribers.map do |sub|
          [sub.topic_name, sub.get_connection_data]
        end
        servstats = @service_servers.map do |service|
          [service.get_connection_data]
        end
        [1, "stats", [pubstats, substats, servstats]]
      end

      @server.add_handler('getMasterUri') do |caller_id|
        [1, "master", @master_uri]
      end

      @server.add_handler('getSubscriptions') do |caller_id|
        topic_list = @subscribers.map do |sub|
          [sub.topic_name, sub.topic_type.type]
        end
        [1, "ok", topic_list]
      end

      @server.add_handler('getPublications') do |caller_id|
        topic_list = @publishers.map do |pub|
          [pub.topic_name, pub.topic_type.type]
        end
        [1, "ok", topic_list]
      end

      @server.add_handler('paramUpdate') do |caller_id, parameter_key, parameter_value|
        @parameter_subscribers.each do |param|
          # parameter_key has / in the end
          if param.key == canonicalize_name(parameter_key)
            param.call(parameter_value)
          end
        end
        [1, "ok", 0]
      end

      @server.add_handler('requestTopic') do |caller_id, topic, protocols|
        message = [0, "I DON'T KNOW", 0]
        protocols.select {|x| x[0] == 'TCPROS'}.each do |protocol|
          @publishers.select {|pub| pub.topic_name == topic}.each do |publisher|
            connection = publisher.add_connection(caller_id)
            message = [1, "OK! WAIT!!!", ['TCPROS',
                                          connection.host,
                                          connection.port]]
          end
        end
        message
      end

      @server.add_handler('shutdown') do |caller_id, msg|
        # puts "shutting down by master request: #{msg}"
        shutdown
        [1, 'shutdown ok', 0]
      end

      @server.add_handler('getPid') do |caller_id|
        [1, "pid ok", Process.pid]
      end

      @server.add_handler('getBusInfo') do |caller_id|
        info = []
        @publishers.each do |publisher|
          info.concat(publisher.get_connection_info)
        end
        @subscribers.each do |subscriber|
          info.concat(subscriber.get_connection_info)
        end
        [1, "getBusInfo ok", info]
      end

      @server.add_handler('publisherUpdate') do |caller_id, topic, publishers|
        @subscribers.select {|sub| sub.topic_name == topic}.each do |sub|
          publishers.select {|uri| not sub.has_connection_with?(uri)}.each do |uri|
            sub.add_connection(uri)
          end
          sub.get_connected_uri.select {|uri| not publishers.include?(uri)}.each do |uri|
            sub.drop_connection(uri)
          end
        end
        [1, "OK! Updated!!", 0]
      end
    end
def add_handlers #:nodoc:
      @server.set_default_handler do |method, *args|
        puts "unhandled call with #{method}, #{args}"
        [0, "I DON'T KNOW", 0]
      end

      @server.add_handler('getBusStats') do |caller_id|
        pubstats = @publishers.map do |pub|
          [pub.topic_name, pub.topic_type.type, pub.get_connection_data]
        end
        substats = @subscribers.map do |sub|
          [sub.topic_name, sub.get_connection_data]
        end
        servstats = @service_servers.map do |service|
          [service.get_connection_data]
        end
        [1, "stats", [pubstats, substats, servstats]]
      end

      @server.add_handler('getMasterUri') do |caller_id|
        [1, "master", @master_uri]
      end

      @server.add_handler('getSubscriptions') do |caller_id|
        topic_list = @subscribers.map do |sub|
          [sub.topic_name, sub.topic_type.type]
        end
        [1, "ok", topic_list]
      end

      @server.add_handler('getPublications') do |caller_id|
        topic_list = @publishers.map do |pub|
          [pub.topic_name, pub.topic_type.type]
        end
        [1, "ok", topic_list]
      end

      @server.add_handler('paramUpdate') do |caller_id, parameter_key, parameter_value|
        @parameter_subscribers.each do |param|
          # parameter_key has / in the end
          if param.key == canonicalize_name(parameter_key)
            param.call(parameter_value)
          end
        end
        [1, "ok", 0]
      end

      @server.add_handler('requestTopic') do |caller_id, topic, protocols|
        message = [0, "I DON'T KNOW", 0]
        protocols.select {|x| x[0] == 'TCPROS'}.each do |protocol|
          @publishers.select {|pub| pub.topic_name == topic}.each do |publisher|
            connection = publisher.add_connection(caller_id)
            message = [1, "OK! WAIT!!!", ['TCPROS',
                                          connection.host,
                                          connection.port]]
          end
        end
        message
      end

      @server.add_handler('shutdown') do |caller_id, msg|
        # puts "shutting down by master request: #{msg}"
        shutdown
        [1, 'shutdown ok', 0]
      end

      @server.add_handler('getPid') do |caller_id|
        [1, "pid ok", Process.pid]
      end

      @server.add_handler('getBusInfo') do |caller_id|
        info = []
        @publishers.each do |publisher|
          info.concat(publisher.get_connection_info)
        end
        @subscribers.each do |subscriber|
          info.concat(subscriber.get_connection_info)
        end
        [1, "getBusInfo ok", info]
      end

      @server.add_handler('publisherUpdate') do |caller_id, topic, publishers|
        @subscribers.select {|sub| sub.topic_name == topic}.each do |sub|
          publishers.select {|uri| not sub.has_connection_with?(uri)}.each do |uri|
            sub.add_connection(uri)
          end
          sub.get_connected_uri.select {|uri| not publishers.include?(uri)}.each do |uri|
            sub.drop_connection(uri)
          end
        end
        [1, "OK! Updated!!", 0]
      end
    end
add all handers