int32_t registerService(const std::string& host, const int32_t port) {
   // Your implementation goes here
   printf("registerService\n");
   pair<string,int> host_tuple(host,port);
   boost::shared_ptr<TTransport> socket(new TSocket(host,port));
   boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
   boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
   boost::shared_ptr<RemoteInfixSearchClient> client(new RemoteInfixSearchClient(protocol));
   thrift_container_t container;
   container.host = host;
   container.port = port;
   container.socket = socket;
   container.transport = transport;
   container.protocol = protocol;
   container.client = client;
   bool found = false;
   for(host_list_t::iterator it = host_list.begin();it!=host_list.end();++it){
     thrift_container_t container = *it;
     if(container.host.compare(host)==0 && container.port==port){
       printf("Found a matching entry\n");
       found = true;
       break;
     }
   }
   if(!found) host_list.push_back(container);
   printf("There are now %u workers\n",(uint)host_list.size());
 }
 int32_t deregisterService(const std::string& host, const int32_t port) {
   // Your implementation goes here
   printf("deregisterService\n");
   pair<string,int> host_tuple(host,port);
   host_list_t::iterator it = host_list.begin();
   while(it!=host_list.end()){
     thrift_container_t container = *it;
     if(container.host.compare(host)==0 && container.port==port){
       printf("Found a matching entry\n");
       it = host_list.erase(it);
     }else{
       ++it;
     }
   }
   printf("There are now %u workers\n",(uint)host_list.size());
 }
    error server_control_executor::validate_host_list(
        const host_list_t&  _irods_hosts,
        const host_list_t&  _cmd_hosts,
        host_list_t&        _valid_hosts ) {

        host_list_t::const_iterator itr;
        for ( itr  = _cmd_hosts.begin();
                itr != _cmd_hosts.end();
                ++itr ) {
            // check host value against list from the icat
            if ( !is_host_in_list(
                        *itr,
                        _irods_hosts ) ) {
                std::string msg( "invalid server hostname [" );
                msg += *itr;
                msg += "]";
                return ERROR(
                           SYS_INVALID_INPUT_PARAM,
                           msg );

            }

            // skip the IES since it is a special case
            // and handled elsewhere
            if ( compare_host_names( icat_host_name_, *itr ) ) {
                continue;
            }

            // skip the local server since it is also a
            // special case and handled elsewhere
            if ( compare_host_names( my_host_name_, *itr ) ) {
                continue;

            }

            // add the host to our newly ordered list
            _valid_hosts.push_back( *itr );

        } // for itr

        return SUCCESS();

    } // validate_host_list
  void findMatches(std::vector<std::string> & _return, const std::string& key) {
    // Your implementation goes here
    printf("findMatches\n");
    if(service_type ==  TYPE_SLAVE){
      search.find_path(key,_return);
      for(int i=0;i<_return.size();++i){
        _return[i] = search.extract_row(_return[i]);
      }
    }else{
      if(host_list.size()>0){
        _return.clear();
        list<delegator_t *> delegator_list;
        for(host_list_t::iterator it = host_list.begin();it!=host_list.end();++it){
          thrift_container_t container = *it;
          delegator_t * delegator = new delegator_t(container,key);
          delegator_list.push_back(delegator);
        }
        findMatchesThreaded(_return,key,delegator_list);
        for(list<delegator_t *>::iterator it = delegator_list.begin();
        it!=delegator_list.end();++it){
          delegator_t * delegator = *it;
          for(vector<string>::iterator it = delegator->matches.begin();
          it!=delegator->matches.end();++it){
             _return.push_back(*it);
          }
          delete(delegator);
//          try {
//            printf("Trying host %s port %d\n",container.host.data(),container.port);
//            container.transport->open();
//            vector<string> matches;
//            container.client->findMatches(matches,key);
//            container.transport->close();
//            for(vector<string>::iterator it = matches.begin();it!=matches.end();++it){
//              _return.push_back(*it);
//            }
//          }catch(TException & tx){
//            cout<<"ERROR: "<<tx.what()<<endl;
//          }
        }
      }
    }
  }
    bool server_control_executor::is_host_in_list(
        const std::string& _hn,
        const host_list_t& _hosts ) {
        for ( size_t i = 0;
                i < _hosts.size();
                ++i ) {
            if ( compare_host_names(
                        _hn,
                        _hosts[ i ] ) ) {
                return true;
            }

        } // for i

        return false;

    } // is_host_in_list
    error server_control_executor::get_resource_host_names(
        host_list_t& _host_names ) {
        rodsEnv my_env;
        _reloadRodsEnv( my_env );
        rcComm_t* comm = rcConnect(
                             my_env.rodsHost,
                             my_env.rodsPort,
                             my_env.rodsUserName,
                             my_env.rodsZone,
                             NO_RECONN, 0 );
        if ( !comm ) {
            return ERROR(
                       NULL_VALUE_ERR,
                       "rcConnect failed" );
        }

        int status = clientLogin(
                         comm,
                         0,
                         my_env.rodsAuthScheme );
        if ( status != 0 ) {
            rcDisconnect( comm );
            return ERROR(
                       status,
                       "client login failed" );
        }

        genQueryInp_t  gen_inp;
        genQueryOut_t* gen_out = NULL;
        memset( &gen_inp, 0, sizeof( gen_inp ) );

        addInxIval( &gen_inp.selectInp, COL_R_LOC, 1 );
        gen_inp.maxRows = MAX_SQL_ROWS;

        int cont_idx = 1;
        while ( cont_idx ) {
            int status = rcGenQuery(
                             comm,
                             &gen_inp,
                             &gen_out );
            if ( status < 0 ) {
                rcDisconnect( comm );
                freeGenQueryOut( &gen_out );
                clearGenQueryInp( &gen_inp );
                return ERROR(
                           status,
                           "genQuery failed." );

            } // if

            sqlResult_t* resc_loc = getSqlResultByInx(
                                        gen_out,
                                        COL_R_LOC );
            if ( !resc_loc ) {
                rcDisconnect( comm );
                freeGenQueryOut( &gen_out );
                clearGenQueryInp( &gen_inp );
                return ERROR(
                           UNMATCHED_KEY_OR_INDEX,
                           "getSqlResultByInx for COL_R_LOC failed" );
            }

            for ( int i = 0;
                    i < gen_out->rowCnt;
                    ++i ) {
                const std::string hn( &resc_loc->value[ resc_loc->len * i ] );
                if ( "localhost"    != hn ) {
                    _host_names.push_back( hn );

                }

            } // for i

            cont_idx = gen_out->continueInx;

        } // while

        freeGenQueryOut( &gen_out );
        clearGenQueryInp( &gen_inp );

        status = rcDisconnect( comm );
        if ( status < 0 ) {
            return ERROR(
                       status,
                       "failed in rcDisconnect" );
        }

        return SUCCESS();

    } // get_resource_host_names
    error server_control_executor::process_host_list(
        const std::string& _cmd_name,
        const std::string& _wait_option,
        const size_t&      _wait_seconds,
        const host_list_t& _hosts,
        std::string&       _output ) {
        if ( _hosts.empty() ) {
            return SUCCESS();

        }

        error fwd_err = SUCCESS();
        host_list_t::const_iterator itr;
        for ( itr  = _hosts.begin();
                itr != _hosts.end();
                ++itr ) {
            if ( "localhost" == *itr ) {
                continue;

            }

            std::string output;
            if ( compare_host_names(
                        *itr,
                        my_host_name_ ) ) {
                error ret = op_map_[ _cmd_name ](
                                _wait_option,
                                _wait_seconds,
                                output );
                if ( !ret.ok() ) {
                    fwd_err = PASS( ret );

                }

                _output += output;

            }
            else {
                error ret = forward_command(
                                _cmd_name,
                                *itr,
                                port_prop_,
                                _wait_option,
                                _wait_seconds,
                                output );
                if ( !ret.ok() ) {
                    log( PASS( ret ) );
                    fwd_err = PASS( ret );

                }
                else {
                    _output += output;

                }

            }

        } // for itr

        return fwd_err;

    } // process_host_list
    error server_control_executor::extract_command_parameters(
        const control_plane_command& _cmd,
        std::string&                 _name,
        std::string&                 _option,
        std::string&                 _wait_option,
        size_t&                      _wait_seconds,
        host_list_t&                 _hosts ) {
        // capture and validate the command parameter
        _name = _cmd.command;
        if ( SERVER_CONTROL_SHUTDOWN != _name &&
                SERVER_CONTROL_PAUSE    != _name &&
                SERVER_CONTROL_RESUME   != _name &&
                SERVER_CONTROL_STATUS   != _name ) {
            std::string msg( "invalid command [" );
            msg += _name;
            msg += "]";
            return ERROR(
                       SYS_INVALID_INPUT_PARAM,
                       msg );
        }

        // capture and validate the option parameter
        std::map<std::string, std::string>::const_iterator itr =
            _cmd.options.find( SERVER_CONTROL_OPTION_KW );
        if ( _cmd.options.end() == itr ) {
            return ERROR(
                       SYS_INVALID_INPUT_PARAM,
                       "option parameter is empty" );

        }

        _option = itr->second;
        if ( SERVER_CONTROL_ALL_OPT   != _option &&
                SERVER_CONTROL_HOSTS_OPT != _option ) {
            std::string msg( "invalid command option [" );
            msg += _option;
            msg += "]";
            return ERROR(
                       SYS_INVALID_INPUT_PARAM,
                       msg );
        }

        // capture and validate the server hosts, skip the option key
        for ( itr  = _cmd.options.begin();
                itr != _cmd.options.end();
                ++itr ) {
            if ( itr->first == SERVER_CONTROL_OPTION_KW ) {
                continue;

            }
            else if ( itr->first == SERVER_CONTROL_FORCE_AFTER_KW ) {
                _wait_option = SERVER_CONTROL_FORCE_AFTER_KW;
                _wait_seconds = boost::lexical_cast< size_t >( itr->second );

            }
            else if ( itr->first == SERVER_CONTROL_WAIT_FOREVER_KW ) {
                _wait_option = SERVER_CONTROL_WAIT_FOREVER_KW;
                _wait_seconds = 0;

            }
            else if ( itr->first.find(
                          SERVER_CONTROL_HOST_KW )
                      != std::string::npos ) {
                _hosts.push_back( itr->second );

            }
            else {
                std::string msg( "invalid option key [" );
                msg += itr->first;
                msg += "]";
                return ERROR(
                           SYS_INVALID_INPUT_PARAM,
                           msg );

            }

        } // for itr

        return SUCCESS();

    } // extract_command_parameters