<commit_before>package de.haw.rnp.chat.server.businesslogiclayer;

import de.haw.rnp.chat.server.accesslayer.ITransportServices;
import de.haw.rnp.chat.server.accesslayer.ITransportServicesForIncomingPeerAdapter;
import de.haw.rnp.chat.server.accesslayer.IUserServices;
import de.haw.rnp.chat.server.dataaccesslayer.entities.Frame;

import java.util.Arrays;

public class TransportBusinessLogic implements ITransportServices, ITransportServicesForIncomingPeerAdapter {

    private IUserServices userServices;

    public TransportBusinessLogic(IUserServices userServices){
        this.userServices = userServices;
    }

    @Override
    public void sendMessage(Frame frame) {

    }

    @Override
    public void sendLogin(Frame frame) {

    }

    @Override
    public void sendLogout(Frame frame) {

    }

    @Override
    public void SendUsername(Frame frame) {

    }

    @Override
    public void recieveFrameAsBytes(byte[] bytes) {
        Frame frame = new Frame(Arrays.copyOf(bytes, 12));

    }

}<commit_msg>added parser to transport business logic<commit_after>package de.haw.rnp.chat.server.businesslogiclayer;

import de.haw.rnp.chat.server.accesslayer.ITransportServices;
import de.haw.rnp.chat.server.accesslayer.ITransportServicesForIncomingPeerAdapter;
import de.haw.rnp.chat.server.accesslayer.IUserServices;
import de.haw.rnp.chat.server.dataaccesslayer.entities.Field;
import de.haw.rnp.chat.server.dataaccesslayer.entities.Frame;
import de.haw.rnp.chat.server.dataaccesslayer.enumerations.FieldType;
import de.haw.rnp.chat.util.AddressType;
import de.haw.rnp.chat.util.ChatUtil;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class TransportBusinessLogic implements ITransportServices, ITransportServicesForIncomingPeerAdapter {

    private IUserServices userServices;

    public TransportBusinessLogic(IUserServices userServices){
        this.userServices = userServices;
    }

    @Override
    public void sendMessage(Frame frame) {

    }

    @Override
    public void sendLogin(Frame frame) {

    }

    @Override
    public void sendLogout(Frame frame) {

    }

    @Override
    public void SendUsername(Frame frame) {

    }

    @Override
    public Frame recieveFrameAsBytes(byte[] bytes) {
        Frame frame = new Frame(Arrays.copyOf(bytes, 12));
        frame.setFields(parseFields(Arrays.copyOfRange(bytes, 12, bytes.length)));
        return frame;
    }

    private Collection<Field> parseFields(byte[] bytes){
        Collection<Field> fields = new ArrayList<>();

        while(bytes.length > 0){
            FieldType fieldType = FieldType.fromBytes(Arrays.copyOf(bytes, 2));
            int length = ChatUtil.byteArrayToInt(Arrays.copyOfRange(bytes, 2, 4));
            bytes = ChatUtil.cut(bytes, 4);
            switch(fieldType){
                case IP: fields.add(parseIP(Arrays.copyOf(bytes, length), fieldType, length)); break;
                case Port: fields.add(parsePort(Arrays.copyOf(bytes, length), fieldType, length)); break;
                case Name: fields.add(parseName(Arrays.copyOf(bytes, length), fieldType, length)); break;
                case Text: fields.add(parseText(Arrays.copyOf(bytes, length), fieldType, length)); break;
                case UserList: fields.add(parseUserlist(Arrays.copyOf(bytes, length), fieldType, length)); break;
            }
            bytes = ChatUtil.cut(bytes, length);
        }

        return fields;
    }

    private Field parseIP(byte[] bytes, FieldType fieldType, int length){
        try {
            InetAddress address = InetAddress.getByAddress(bytes);
            return new Field<>(fieldType, length, address);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        }
    }

    private Field parsePort(byte[] bytes, FieldType fieldType, int length){
        return new Field<>(fieldType, length, ChatUtil.byteArrayToInt(bytes));
    }

    private Field parseName(byte[] bytes, FieldType fieldType, int length){
        return  new Field<>(fieldType, length, new String(bytes, StandardCharsets.US_ASCII));
    }

    private Field parseText(byte[] bytes, FieldType fieldType, int length){
        return new Field<>(fieldType, length, new String(bytes, StandardCharsets.US_ASCII));
    }

    private Field parseUserlist(byte[] bytes, FieldType fieldType, int length){
        Collection<AddressType> addresses = new ArrayList<>();
        for(int x = 0; x < (length / 8); x++){
            try {

                InetAddress ip = InetAddress.getByAddress(Arrays.copyOf(bytes, 4));
                int port = ChatUtil.byteArrayToInt(Arrays.copyOfRange(bytes, 6, 8));
                addresses.add(new AddressType(ip, port));

            } catch (UnknownHostException e) {
                e.printStackTrace();
            }

            bytes = ChatUtil.cut(bytes, 8);
        }
        return new Field<>(fieldType, length, addresses);
    }

}
{'fraction_non_alphanumeric': 0.10300766660114016, 'fraction_numerical': 0.0033418517790446234, 'mean_word_length': 3.1842105263157894, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3639290', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1430, 'n_words': 313}