private RequestHostIdResponse requestHostId ( MessagingChannel messagingChannel , Set < String > activeVersions ) throws Exception { VersionChecker versionChecker = m_acceptor . getVersionChecker ( ) ; activeVersions . add ( versionChecker . getVersionString ( ) ) ; JSONObject jsObj = new JSONObject ( ) ; jsObj . put ( TYPE , ConnectionType . REQUEST_HOSTID . name ( ) ) ; // put the version compatibility status in the json jsObj . put ( VERSION_STRING , versionChecker . getVersionString ( ) ) ; // Advertise the port we are going to listen on based on config jsObj . put ( PORT , m_internalPort ) ; // If config specified an internal interface use that. // Otherwise the leader will echo back what we connected on if ( ! m_internalInterface . isEmpty ( ) ) { jsObj . put ( ADDRESS , m_internalInterface ) ; } // communicate configuration and node state jsObj = m_acceptor . decorate ( jsObj , Optional . empty ( ) ) ; jsObj . put ( MAY_EXCHANGE_TS , true ) ; byte jsBytes [ ] = jsObj . toString ( 4 ) . getBytes ( StandardCharsets . UTF_8 ) ; ByteBuffer requestHostIdBuffer = ByteBuffer . allocate ( 4 + jsBytes . length ) ; requestHostIdBuffer . putInt ( jsBytes . length ) ; requestHostIdBuffer . put ( jsBytes ) . flip ( ) ; messagingChannel . writeMessage ( requestHostIdBuffer ) ; // read the json response from socketjoiner with version info and validate it JSONObject leaderInfo = processJSONResponse ( messagingChannel , activeVersions , true ) ; // read the json response sent by HostMessenger with HostID JSONObject jsonObj = readJSONObjFromWire ( messagingChannel ) ; return new RequestHostIdResponse ( leaderInfo , jsonObj ) ; }
Connection handshake to the leader ask the leader to assign a host Id for current node .