src/main/java/com/io7m/aeron_guide/take3/AeronMessagingClient.java
/**
     * Parse the initial response from the server.
     */
Parse the initial response from the server.
Parse the initial response from the server .
private void onInitialResponse(
            final String session_name,
            final DirectBuffer buffer,
            final int offset,
            final int length) {
        LOG.trace("debug: got response with length: " + length + ", offset: " + offset);
        if (length < 21 || length > 60) {
            return;
        }
        if (buffer.getByte(offset + 0) != 'C'
                || buffer.getByte(offset + 1) != 'O'
                || buffer.getByte(offset + 2) != 'N'
                || buffer.getByte(offset + 3) != 'T'
                || buffer.getByte(offset + 4) != 'R'
                || buffer.getByte(offset + 5) != 'O'
                || buffer.getByte(offset + 6) != 'L'
                || buffer.getByte(offset + 7) != ' ') {
            return;
        }
        final String response = MessagesHelper.parseMessageUTF8(buffer, offset, length);
        LOG.trace("[{}] response: {}", session_name, response);
        /**
         * Try to extract the session identifier to determine whether the
         * message was intended for this client or not.
         */
        final int space = response.indexOf(" ", 8);  
        if (space == -1) {
            LOG.error(
                    "[{}] server returned unrecognized message (can not find space): {}",
                    session_name,
                    response);
            return;
        }
        final String message_session = response.substring(8, space);  
        if (!Objects.equals(message_session, session_name)) {
            LOG.trace(
                    "[{}] ignored message intended for another client (expected session=" + session_name + ", but received session=" + message_session + ")",
                    session_name);
            return;
        }
        /**
         * The message was intended for this client. Try to parse it as one of
         * the available message types.
         */
        final String text = response.substring(space).trim();   
        final Matcher error_matcher = PATTERN_ERROR.matcher(text);
        if (error_matcher.matches()) {
            final String message = error_matcher.group(1);
            LOG.error("[{}] server returned an error: {}", session_name, message);
            this.failed = true;
            return;
        }
        final Matcher connect_matcher = PATTERN_CONNECT.matcher(text);
        if (connect_matcher.matches()) {
            final int port_data
                    = Integer.parseUnsignedInt(connect_matcher.group(1));
            final int port_control
                    = Integer.parseUnsignedInt(connect_matcher.group(2));
            final int session_crypted
                    = Integer.parseUnsignedInt(connect_matcher.group(3), 16);
            LOG.debug(
                    "[{}] connect {} {} (encrypted {})",
                    session_name,
                    Integer.valueOf(port_data),
                    Integer.valueOf(port_control),
                    Integer.valueOf(session_crypted));
            this.remote_control_port = port_control;
            this.remote_data_port = port_data;
            this.remote_session = this.duologue_key ^ session_crypted;
            this.remote_ports_received = true;
            return;
        }
        LOG.error(
                "[{}] server returned unrecognized message: {}",
                session_name,
                text);
    }
private void onInitialResponse ( final String session_name , final DirectBuffer buffer , final int offset , final int length ) { LOG . trace ( "debug: got response with length: " + length + ", offset: " + offset ) ; if ( length < 21 || length > 60 ) { return ; } if ( buffer . getByte ( offset + 0 ) != 'C' || buffer . getByte ( offset + 1 ) != 'O' || buffer . getByte ( offset + 2 ) != 'N' || buffer . getByte ( offset + 3 ) != 'T' || buffer . getByte ( offset + 4 ) != 'R' || buffer . getByte ( offset + 5 ) != 'O' || buffer . getByte ( offset + 6 ) != 'L' || buffer . getByte ( offset + 7 ) != ' ' ) { return ; } final String response = MessagesHelper . parseMessageUTF8 ( buffer , offset , length ) ; LOG . trace ( "[{}] response: {}" , session_name , response ) ; /**
         * Try to extract the session identifier to determine whether the
         * message was intended for this client or not.
         */ final int space = response . indexOf ( " " , 8 ) ; if ( space == - 1 ) { LOG . error ( "[{}] server returned unrecognized message (can not find space): {}" , session_name , response ) ; return ; } final String message_session = response . substring ( 8 , space ) ; if ( ! Objects . equals ( message_session , session_name ) ) { LOG . trace ( "[{}] ignored message intended for another client (expected session=" + session_name + ", but received session=" + message_session + ")" , session_name ) ; return ; } /**
         * The message was intended for this client. Try to parse it as one of
         * the available message types.
         */ final String text = response . substring ( space ) . trim ( ) ; final Matcher error_matcher = PATTERN_ERROR . matcher ( text ) ; if ( error_matcher . matches ( ) ) { final String message = error_matcher . group ( 1 ) ; LOG . error ( "[{}] server returned an error: {}" , session_name , message ) ; this . failed = true ; return ; } final Matcher connect_matcher = PATTERN_CONNECT . matcher ( text ) ; if ( connect_matcher . matches ( ) ) { final int port_data = Integer . parseUnsignedInt ( connect_matcher . group ( 1 ) ) ; final int port_control = Integer . parseUnsignedInt ( connect_matcher . group ( 2 ) ) ; final int session_crypted = Integer . parseUnsignedInt ( connect_matcher . group ( 3 ) , 16 ) ; LOG . debug ( "[{}] connect {} {} (encrypted {})" , session_name , Integer . valueOf ( port_data ) , Integer . valueOf ( port_control ) , Integer . valueOf ( session_crypted ) ) ; this . remote_control_port = port_control ; this . remote_data_port = port_data ; this . remote_session = this . duologue_key ^ session_crypted ; this . remote_ports_received = true ; return ; } LOG . error ( "[{}] server returned unrecognized message: {}" , session_name , text ) ; }
Parse the initial response from the server.
Parse the initial response from the server .
// The initial "CONNECT" response can't be shorter than 21 byte and can't be longer than ~60 bytes // The initial "CONNECT" response from the server is now supposed to start with "CONTROL " prefix // +8 is for "CONTROL " prefix // +8 is for "CONTROL " prefix // Trim leading session and space, so now response text should start with CONNECT